// JavaScript Document
function toggleSubmenu(submenuId, show){
	var theSub=document.getElementById('submenu-'+submenuId);
	var theMenu=document.getElementById(submenuId);
	if(show==1){
		theSub.className='submenu';
		theMenu.className='menu-over';
	}
	else{
		theSub.className='submenu-hide';
		theMenu.className='';
	}
}

function toggleSubmenuH(submenuId, show){
	var theSub=document.getElementById('submenu-'+submenuId);
	var theMenu=document.getElementById(submenuId);
	if(show==1){
		theSub.className='submenu-h';
	}
	else{
		theSub.className='submenu-hide';
	}
}


$(function(){
		//Controll the main menu drop down list
		$('ul li',$('#mainMenu')).hover(
			function(){$(this).addClass('over');},
			function(){$(this).removeClass('over');}
			);
		
		//Make the shipping details same as billing details
		$('#shippingController').click(function(){
			var theForm=document.forms['cart'];
			if(theForm.elements['controller'].checked){
				theForm.elements['dFirstName'].value=theForm.elements['bFirstName'].value;
				theForm.elements['dLastName'].value=theForm.elements['bLastName'].value;
				theForm.elements['dCompany'].value=theForm.elements['bCompany'].value;
				theForm.elements['dAddress'].value=theForm.elements['bAddress'].value;
				theForm.elements['dAddress1'].value=theForm.elements['bAddress1'].value;				
				theForm.elements['dCity'].value=theForm.elements['bCity'].value;		
				theForm.elements['dState'].value=theForm.elements['bState'].value;		
				theForm.elements['dPostcode'].value=theForm.elements['bPostcode'].value;				
				theForm.elements['dCountry'].value=theForm.elements['bCountry'].value;		
				theForm.elements['dPhone'].value=theForm.elements['bPhone'].value;
			}else{
				theForm.elements['dFirstName'].value='';
				theForm.elements['dLastName'].value='';
				theForm.elements['dCompany'].value='';
				theForm.elements['dAddress'].value='';
				theForm.elements['dAddress1'].value='';
				theForm.elements['dCity'].value='';		
				theForm.elements['dState'].value='';		
				theForm.elements['dPostcode'].value='';				
				theForm.elements['dCountry'].value='';		
				theForm.elements['dPhone'].value='';
			}
		});
		
		//User registration form verification
		$('#loginForm').submit(function(){
			if($('#member_pass').attr('value')!=$('#member_pass1').attr('value')){
				alert('Passwords entered are not the same!');
				return false;
			}
		});
		
		//product images lightbox
		$('#allImages a').lightBox();
		
		//setup default selection for product browswer and focus area.
		$('img:first',$('#product-swatch-area')).addClass('over');
		$('img',$('#product-swatch-area')).click(function(){
														  	if($('#product-main-photo').attr('src')!=$(this).attr('id')){
																$('img',$('#product-swatch-area')).removeClass('over');
																$(this).addClass('over');
																$('#product-main-photo').attr('src',$(this).attr('id'));
																$('#swatch-caption').text($(this).attr('alt'));
																$('#color-code').text($(this).attr('title'));
															}
														   });
		
		var currentCategoryId=$('#currentCategoryId').attr('value');
		var currentProductId=$('#currentProductId').attr('value');
		$('#'+currentCategoryId,$('#sub-category')).addClass('over');
		$('#category-'+currentCategoryId).css('display','block');	
		$('#row-'+currentProductId).addClass('over');
		
		$('#sub-category a').click(function(){
											   var categoryId=$(this).attr('id');
											   var currentCategoryId=$('#currentCategoryId').attr('value');
											   var currentProductId=$('#currentProductId').attr('value');
											   if(currentCategoryId!=categoryId){
												   $('#sub-category a').removeClass('over');
												   $(this).addClass('over');
												   
												   $('#category-'+currentCategoryId).slideUp('slow');
												   $('#category-'+categoryId).slideDown('slow');
												   
												   $('tr',$('#category-'+categoryId)).removeClass('over');
												   $('#row-'+currentProductId).addClass('over');
												   
												   $('#currentCategoryId').attr('value',categoryId);
											   }
											});
		
		
		$('.product-row').click(function(){
										 	var productId=parseInt($(this).attr('id').substr(4));
											var currentProductId=$('#currentProductId').attr('value');
											if(productId!=currentProductId){
												$('#row-'+currentProductId).removeClass('over');
												$('#row-'+productId).addClass('over');
												$.ajax({
														   type: "GET",
														   url: "product.php",
														   data: "id="+productId,
														   dataType: 'json',
														   success: function(json){
																		var data=json;
																		if(data.result==1){
																			$('#product-main-photo').attr('src',data.productMainPhoto);
																			$('#product-main-photo-text').text(data.productName);
																			$('#product-desc-area').html(data.productDesc);
																			$('#product-swatch-area').html(data.productSwatchList);
																			$('#product-code').html(data.productCode);
																			$('#color-code').html(data.mainPhotoColorCode);
																			
																			$('img:first',$('#product-swatch-area')).addClass('over');
																			
																			$('img',$('#product-swatch-area')).click(function(){
																				if($('#product-main-photo').attr('src')!=$(this).attr('id')){
																					$('img',$('#product-swatch-area')).removeClass('over');
																					$(this).addClass('over');
																					$('#product-main-photo').attr('src',$(this).attr('id'));
																					$('#swatch-caption').text($(this).attr('alt'));
																					$('#color-code').text($(this).attr('title'));
																				}
																			   });
																			
																			$('#currentProductId').attr('value',productId);
																		}else{
																			alert(data.msg);	
																		}
														   			}
														 });
											}
										 });
		
});


//Animate the product detail slideshow images.
$(function(){
	var imagesUrl=Array();
	var mainImage=$('img').filter('#mainImage');
	var current=0;
	var interval=null;
	var intervalTime=5000;
	var thumbnailImages=$('img').filter('.thumbnail');
	
	thumbnailImages.each(function(i){
		var theSrc=this.src;
		var otherImagesUrl=Array();
		
		otherImagesUrl[0]=theSrc.replace(/thumb_/,'medium_');
		otherImagesUrl[1]=theSrc.replace(/thumb_/,'');
		imagesUrl[i]=otherImagesUrl;
		
		$(this).click( function(){
				clearInterval(interval);
				mainImage.attr('src',imagesUrl[i][0]).fadeIn();
				$('#theLink').attr('href',imagesUrl[i][1]);
				
				current=i;
				if(imagesUrl.length>1)//start slide show for over 1 image.
					interval=setInterval(slideShow,intervalTime);
				//The thumbnail class switcher
				thumbnailImages.each(function(i){
									if(i!=current)
										$(this).removeClass('thumbnail-over');
									else
										$(this).addClass('thumbnail-over');
								 });

			});
		
		$(this).mouseover( function(){
			if(current!=i)
				$(this).addClass('thumbnail-over');
			}
		);	

		$(this).mouseout( function(){
			if(current!=i)
				$(this).removeClass('thumbnail-over');
			}
		);	
	});

	function slideShow(){
			if(current<(imagesUrl.length-1))
				current++;
			else
				current=0; 
			
			mainImage.attr('src',imagesUrl[current][0]).fadeIn();
			$('#theLink').attr('href',imagesUrl[current][1]);
			
			thumbnailImages.each(function(i){
									if(i!=current)
										$(this).removeClass('thumbnail-over');
									else
										$(this).addClass('thumbnail-over');
								 });
			
		}
	
	
	$(thumbnailImages.get(0)).addClass('thumbnail-over');
	
	if(imagesUrl.length>1) //start slide show for over 1 image.
		interval=setInterval(slideShow, intervalTime);
});