var YM = {
	layout: {
		init: function() { 
			// IE6 .png fix
			$('#header h1, #touts a, #sidebar .talking-point').supersleight({
				shim: '/images/blank.gif'
			});
			
			
			// font replacement
			Cufon.replace('h1, h3');
			Cufon.replace('#tabs a, .caption', {
				hover: true
			});
			// get IE to behave
			Cufon.now();  
			
			// hide subnav
			var $subnav_yantraway = $('#nav li.yantra ul');
			var $subnav_using = $('#nav li.using ul');
			var $yantraway_link = $('#nav a.yantra');
			var $using_link = $('#nav a.use');

			
			$('#nav li.yantra').hoverIntent(function() {
				$subnav_yantraway.show();
			}, function() {
				$subnav_yantraway.hide();
			}); 
			
			$('#nav li.using').hoverIntent(function() {
				$subnav_using.show();
			}, function() {
				$subnav_using.hide();
			}); 
			
			// open links in new windows
			$('a.target-blank').click(function() {
				window.open(this.href);
				return false;
			});
			
			// get image buttons to have hover effect in IE6 
			if(typeof document.body.style.maxHeight === "undefined") {
				$('input.img-button').hover(function() {
					$(this).css('background-position', 'center left');
				}, function() {
					$(this).css('background-position', 'top left');
				});
			}
			
		}
	},
	
	index: {
		init: function() {
            YM.Shared.Nav.make_selected('home');

			// basic positions slideshow (not in IE6) 		
			if(typeof document.body.style.maxHeight === "undefined") {
				var truthy = true;
			} else {  
				
				var $pos_back = $('<img src="/images/hp_tout_pos1.jpg" alt="Basic Positions - Back" />');
				var $pos_sit = $('<img src="/images/hp_tout_pos3.jpg" alt="Basic Positions - Sitting" />');			
				var $pos_cheek = $('<img src="/images/hp_tout_pos4.jpg" alt="Basic Positions - Cheek" />');
				
			    $('#slideshow').append($pos_back, $pos_sit, $pos_cheek).cycle({
					fit: 1,
					delay: 35000,
					timeout: 8000
				});
			}
		}
	},
	
	yantra_way: {
		init: function() {
			YM.Shared.Tabs.init();
		    YM.Shared.Nav.make_selected('yantra');
		
			$('#body-mind .enlarge a').lightBox(YM.Shared.Lightbox.options);
			$('#the-yantra .enlarge a').lightBox(YM.Shared.Lightbox.options);
		}
	}, 
	
	use: {
		init: function() {
			YM.Shared.Tabs.init(); 
		    YM.Shared.Nav.make_selected('use');
		    
			// hide buzz sidebar if on Buzz page
			var subpage = YM.Shared.Tabs.get_tab_path(location.pathname);
			if (subpage == 'buzz') {
				$('#sidebar .buzz').hide();
			};			
		}
	},
	
	contact: {
		init: function() {
		    YM.Shared.Nav.make_selected('contact');	  			
		}
	}, 
	
	buy: {
		init: function() { 
			
		    YM.Shared.Nav.make_selected('buy');	  			
			
			// disable autocomplete attr in form to workaround FF selected radio button bug
			$('form').attr('autocomplete', 'off');
			
			// insert price display HTML
			var $price_display = $('<h4 class="clear"></h4>');
			$('.select-size').after($price_display);  
			                                        
			// price callback function
			function get_price_display(size) {
				$.get('/mat-prices', {size: size}, function(data, response) {
					if (response == 'success') {
						$price_display.text(data);
					};
				});
			}
    	    
			// get initial price
			get_price_display('med'); 
			
			// enable price display change depending on size      		 
			$('input[name="size"]').click(function() {
				var size = $(this).val();  
				get_price_display(size);     				
			});
			
			// enable photo change depending on color
			var $mat_pic = $('#main-content.buy img');
			var $pic_link = $('#main-content.buy .enlarge a');
			$('input[name="color"]').click(function() {
				var color = $(this).val();
				var thumb_src, img_src;
				switch(color) {
					case 'green':
						thumb_src = "/images/buynow_mat_green.jpg";
						img_src = "/images/buynow_large_green.jpg";
						break;
					default:
						thumb_src = "/images/buynow_mat.jpg";
						img_src = "/images/buynow_large_purple.jpg";						
				}
				$mat_pic.attr('src', thumb_src);
				$pic_link.attr('href', img_src);
			});
			
			// lightbox
		   	$('#main-content.buy .enlarge a').lightBox(YM.Shared.Lightbox.options); 
		}
	},
	
	cart: {
		init: function() {
			// gift checkbox functionality
			var $gift_checkbox = $('#send-gift input[type="checkbox"]');
			$gift_checkbox.click(function() {  
				var gift = false;
				if ($(this).attr('checked')) {
					gift = true;
				};
				$.post('/gift-check', {gift: gift});
			});
		}
	},
	
	Shared: {
		
		Tabs: {
			init: function() {
				var $tabs = $('#tabs li a');
				var $container = $('.tabs');
				var $divs = $('.tabs > div');
				var subpage = YM.Shared.Tabs.get_tab_path(location.pathname);

				// logic for hiding/showing divs
				$divs.hide();
                if (subpage) {
	            	$('#' + subpage).show();
				} else {
					$divs.eq(0).show(); 
				}; 
				
				// logic for selecting/unselecting navs 
				make_selected = function(href) {      
					$tabs.removeClass('selected');
					if (href) {
						$tabs.filter('[href*=' + href + ']').addClass('selected');   	
					} else {
						$tabs.eq(0).addClass('selected');
					};
				};
				make_selected(subpage);
		  
			},
			
			get_tab_path: function(pathname) {
			   	var tab_regex = /^.+\/(.+)$/;
				var path_match = pathname.match(tab_regex);
				if (path_match) {
					return path_match[1];
				}
				return false; 
			}
		},
		
		Nav: {
			make_selected: function(page) {
				$('#nav li a').removeClass('selected');
				// ie6 workaround
				$('#nav li a').each(function(i) {
				   $(this).get(0).className == $(this).get(0).className.replace(/\b\w+_selected\b/g, '');
				});
				$('#nav li a.' + page).addClass('selected').addClass(page + '_selected');
			}
		},
		
		Lightbox: {
			options: {
				imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
				imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
				imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
				imageBtnNext: '/images/lightbox/lightbox-btn-next.gif'
			}
		}
	}
};


//random quote fade in out
$(document).ready(
		function(){
			
			$('#quotes').innerfade({
				animationtype: 'fade',
				speed: 750,
				timeout: 3000,
				type: 'sequence',
				containerheight: '210px'
			});
			
			$("span.descr").hide();
			
			$("table.webshoptbl tr").click(function(){			  
        $("span.descr").hide();
        $("span.more").show();
			  
			  $(this).find("span.descr").toggle();
        $(this).find("span.more").toggle();
			});
	}
);
