$(document).ready(function(){
	$('a.printLink').click(function(){
		window.print();
	});
	
	$('#desc_from_div_to_move').html($('#div_to_move').html());
	$('#div_to_move').remove();
	
	//used to swap main image with extra images 
	$('.extra_img').click(function(){
		//quick check to see if img is present
		//if you dont assign object to variable and image is not there you'd get javascript error!
		var el = document.getElementById('mainImg_img');
		
		var src = $(this).attr('href');
		var img = $(this).find("img").attr('src');
		
		if (el)
		{					
			$('#mainImg_a').attr( 'href', src );
			var number = $(this).find("img").attr('id');
			var prod_id = $('#product_id').val();		
			var string = prod_id + ',' + number;
			
			var url = "/json/index/get_image_src/"+string;
			$.getJSON(url,function(data){
				if(data.src)
				{
					$('#mainImg_img').attr( 'src', data.src );
					$('#zoom_glass').attr( 'href', src );
				}
				});
		}
		else
		{
			$('#mainImg').find("div").remove();
			a = document.createElement('a');
			a.id = "mainImg_a";
			a.className = "thickbox";
			img = document.createElement('img');
			img.id = "mainImg_img";
			a.appendChild(img);
			$('#mainImg').append(a);
			
			tb_init('#mainImg_a');
			
			$('#mainImg_a').attr( 'href', src );
			var number = $(this).find("img").attr('id');
			var prod_id = $('#product_id').val();		
			var string = prod_id + ',' + number;
			
			var url = "/json/index/get_image_src/"+string;
			$.getJSON(url,function(data){
				if(data.src)
				{
					$('#mainImg_img').attr( 'src', data.src );
				//	$('#zoom_glass').attr( 'href', src );
				}
				});
		}	
	      return false;
	});
	
});

//Initialize dynamic media panel tabs
$(document).ready(function()
{
	var initialSize = 9;
	
	//Mark all tabs as selected to start
	$('ul.productMedia li a').addClass('selected');
	
	//Hide video panel if image panel exists
	if($('ul#imageContent').length>0)
	{
		$('ul#videoContent').hide();
		$('li.videosTab a').removeClass('selected');
	}
	else
	{
		initialSize = 20;
	}
	
	//Hide sound panel if video panel or image panel exists
	if($('ul#imageContent').length>0 || $('ul#videoContent').length>0)
	{
		$('ul#soundContent').hide();
		$('li.soundsTab a').removeClass('selected');
	}
	
	//Size the media panel
	//$('.mediaPanel').css('height',initialSize+"em");
	
	//Remove tabs which have no content
	if($('ul#imageContent').length==0) $('li.imagesTab').hide();
	if($('ul#videoContent').length==0) $('li.videosTab').hide();
	if($('ul#soundContent').length==0) $('li.soundsTab').hide();
	
	//Set position: absolute so the panels don't jump around when clicking the media tabs
	//  This is here instead of in the css file so that all panels will show if javascript is disabled
	//$('.mediaFragment').css({position:"absolute"});
	
	$('ul.productMedia li').click(function(){	
		
		//Determine what we clicked on
		elem_class = $(this).attr('class');
		panel = elem_class.substr(0,5);
		
		//Clear selected attribute from tabs and set currently selected tab
		$('ul.productMedia a').removeClass('selected');
		$('ul.productMedia li').removeClass('selected');
		$(this).addClass('selected');
		
		//Hide all panels and show selected panel
		$('ul.mediaFragment').fadeOut('slow');
		$('ul#'+panel+'Content').fadeIn('slow');
		
		//Alter panel height (video panel needs to be larger
		//var size = 9;
		//if(panel=='video') size = 20;
		//$('.mediaPanel').animate({height: size+"em"});
		
		
		
		
		//have to put it here, beacuse jCarousel freak out when tab has display: none
		//because it cant calculate width/height, so we have to instantiate video
		//carousel only when user click video tab
		//global js variables is being set in template to make sure we are creating
		//carousel only when we need to and only once
		var c;		
		if(panel=='video' && !c == 1 && window.show_video == true && window.car_size > 2)
		{
			c = 1;
			$(".carousel1").jcarousel({							    						    
							        vertical: false,
							        start: 0,
							        visible: 3,
							        scroll: 1,
							        animation: "slow",
							        wrap: "null",
							        size: window.car_size
							    });
		}
		
		//Stop event bubbling
		return false;
	});
});

//Initialize variation selection actions
$(document).ready(function(){
	var cartButtonEnabled = false;
	
	if(!$('dl.productNotes input#on_sale').val())
	{
		$('dl.productNotes dd span.oldPrice').hide();
	}
	
	$('dl.productNotes dd.available').hide();
	

	$('dl.productNotes dd.preOrder').hide();
	$('dl.productNotes dd.notAvailable').hide();
	$('dl.productNotes div#inStockEmailInput').hide();
	$('dl.productNotes button#addCartBtn').hide();
	$('dl.productNotes div.variationOptions').hide();
	$('dl.productNotes .giftWrap').hide();
	
	//Remove -email address- from email input when clicking
	$('dl.productNotes input#in_stock_email').click(function(){
		if($(this).val()=='-email address-') $(this).val('');
	});
	
	$('dl.productNotes button#emailMeSubmit').click(function(){
		$('form#productVariations').attr('action',location.href+'?notification_sent=1');
	});
	
	//Show an availability option if only one variation is chosen
	var oneOption = $('input#only_one_option').val();
	if(oneOption)
	{
		cartButtonEnabled = true;
		
		var available = $('input#available').val();
		if(available==0)
		{
			$('dl.productNotes dd.placeHolder').hide();
			$('dl.productNotes dd.notAvailable').show();
			$('dl.productNotes dt.available').show();
			$('dl.productNotes div#inStockEmailInput').show();
		}
		else
		{
			$('dl.productNotes dd.placeHolder').hide();
			$('dl.productNotes button#addCartBtn').show();
			$('dl.productNotes div.variationOptions').show();
		}
		if(available==1) {$('dl.productNotes dd.preOrder').show(); $('dl.productNotes dt.available').show();}
		if(available==2) { $('dl.productNotes dd.available').show(); $('dl.productNotes dt.available').show();}
		
		if($('#allow_gift_wrap').val() && $('#allow_gift_wrap').val()!='0')
		{
			$('dl.productNotes .giftWrap').show();
		}
	}
	else
	{
		$('dl.productNotes button#addCartBtn').show();
	}
	
	$('select#productOptions').bind('change',function(){
		var variation_id = $('select#productOptions').val();
		var url = "/json/index/get_variation_info/"+variation_id;
		$.getJSON(url,function(data){
			if(data.found)
			{
				cartButtonEnabled = true;
				
				//Sale price
				if(data.on_sale)
				{
					$('dl.productNotes dd span.oldPrice').show();
					$('dl.productNotes dd span.oldPrice')[0].innerHTML = data.old_price;
					$('dl.productNotes dd span.savings')[0].innerHTML = "you save "+data.you_save;
				}
				else
				{
					$('dl.productNotes dd span.oldPrice').hide();
					$('dl.productNotes dd span.savings')[0].innerHTML = "&nbsp;";
				}
				
				//Regular price
				$('dl.productNotes dd span.price')[0].innerHTML = data.price;
				
				//Availability
				$('dl.productNotes dd.preOrder')[0].innerHTML = data.pre_order_msg;
				if(data.available==2)
				{
					$('dl.productNotes dd.shipping').show();
					
					$('dl.productNotes dd.placeHolder').hide();
					$('dl.productNotes dd.available').show();
					$('dl.productNotes dt.available').show();
					$('dl.productNotes dd.preOrder').hide();
					$('dl.productNotes dd.notAvailable').hide();
					$('dl.productNotes div#inStockEmailInput').hide();
					$('dl.productNotes button#addCartBtn').show();
					$('dl.productNotes div.variationOptions').show();
				}
				else if(data.available==1)
				{
					$('dl.productNotes dd.shipping').show();
									
					$('dl.productNotes dd.placeHolder').hide();
					$('dl.productNotes dd.available').hide();
					$('dl.productNotes dt.available').show();
					$('dl.productNotes dd.preOrder').show();
					$('dl.productNotes dd.notAvailable').hide();
					$('dl.productNotes div#inStockEmailInput').hide();
					$('dl.productNotes button#addCartBtn').show();
					$('dl.productNotes div.variationOptions').show();
				}
				else
				{
					$('dl.productNotes dd.shipping').show();
									
					$('dl.productNotes dd.placeHolder').hide();
					$('dl.productNotes dt.available').show();
					$('dl.productNotes dd.available').hide();
					$('dl.productNotes dd.preOrder').hide();
					$('dl.productNotes dd.notAvailable').show();
					$('dl.productNotes div#inStockEmailInput').show();
					$('dl.productNotes button#addCartBtn').hide();
					$('dl.productNotes div.variationOptions').hide();
				}
				
				//Shipping
				$('dl.productNotes dd.shipping')[0].innerHTML = data.direct_ship
					? "within 3 to 7 business days from the toy's manufacturer (note: this item may be shipped in packaging which reveals the contents)"
					: 'within 24 hours';
					
				//Sku
				$('p.item_num')[0].innerHTML = "Item # " + data.sku;
				//setting up Omniture variables
				window.s.products = data.sku;
				window.s.events = 'prodView,event3';
				
				//Gift wrapping
				data.allow_gift_wrap ? $('dl.productNotes .giftWrap').show() : $('dl.productNotes .giftWrap').hide();
				
				//Batteries (this must be the last statement because it will crash for products without batteries
				$('dd.batteryTypes')[0].innerHTML = data.battery_types;				
			}
			else
			{
				cartButtonEnabled = false;
				
				$('dl.productNotes dd.placeHolder').show();
				$('dl.productNotes dd.shipping').hide();
				
				$('dl.productNotes dd span.oldPrice').hide();
				$('dl.productNotes dd.available').hide();
				$('dl.productNotes dd.notAvailable').hide();
				$('dl.productNotes dd span.savings')[0].innerHTML = "&nbsp;";
				$('dl.productNotes dd span.price')[0].innerHTML = "Select an option below";
				$('dl.productNotes div#inStockEmailInput').hide();
				$('dl.productNotes button#addCartBtn').hide();
			}
		});
				
	});
	
	$('button#addCartBtn').click(function(){
		if(!cartButtonEnabled)
		{
			alert('Please choose an option');
			return false;
		}
	});	
	
	$('button#addWishlistBtn').click(function(){
		if(!cartButtonEnabled)
		{
			alert('Please choose an option');
			return false;
		}
		
		$('form#productVariations')[0].action = $('input#wishlistUrl').val();
	});	
});

//Set rating value dynamically when selecting a star	
$(document).ready(function(){
	var selectedRating = 5;
	
	function setStarSrc(rating,src)
	{
		for(i=1;i<=rating;i++)
		{
			//This path will need to be altered if the star rating system needs to be used outside the product page.
			$('img#ratingStar'+i)[0].src = '../../../img/'+src+'.png';
		}
	}
	
	function activateStars(rating)
	{
		setStarSrc(5,'star_inactive');	//Inactivate all stars
		setStarSrc(rating,'star');		//Activate selected number of stars
	}
	
	$('.ratingStar').bind('mouseover',function(){
		activateStars(parseInt(this.id.substring(14,15)));	//id format = ratingStarSpan<X>
	});
	
	$('.ratingStar').bind('click',function(){
		selectedRating = parseInt(this.id.substring(14,15));	//id format = ratingStarSpan<X>
		$('input#ratingValueInput').val(selectedRating);
		$('p.ratingText')[0].innerHTML = "(" + selectedRating + " Stars)";
	});
	
	$('.ratingStar').bind('mouseout',function(){
		activateStars(selectedRating);
	});
});

//Show popups
$(document).ready(function(){
	$('a.playPopupLink').hover(function(){
		elem = this;
		$(elem).parent().find('div.productPlayPopup').show();
	},function(){
		elem = this;
		$(elem).parent().find('div.productPlayPopup').hide();
	});
});
