
/* ------------------ for change currency ------------------*/
function buildHiddenSelect(){
	//build a form select into a ul li list
	$('.selectLi').show();
	$('.selectLi').each(function(){
	var formId = $(this).attr('data-formId');
		$(this).find('a').click(function(){
			var theInput = $('#' + formId).find(":hidden").first();
			theInput.val($(this).attr('data-value'));
			$('#' + formId).submit();	
			return false;
		});
	});
}
	
	
	
	
/* --------------- shopping cart dropdowns ------------------ */	
function fillDropdownCart(answer){
		var data = answer.split('||');	
		$('#cartSummary .largeRight').html(data[0]);
		$('#cartSummary .largeRight .summary').show('500');													
		$('#cartSummary div').first().html(data[1]);
		$('#cartSummary .amountAndQuantity').html(data[2]);
							
		buildDropdownCartDelete();	
		buildDropdownCartUpdate();
}
	
function buildDropdownCartDelete(){
	$('.closeButton').each(function(){
			var productId = $(this).attr('data-value');
			var langCode  = $(this).attr('data-lang');
			$(this).click(function(){
				$(this).closest('tr').hide('500','linear',function(){
				$('#cartSummary .largeRight .summary').fadeOut('fast');
				$.ajax({
					  url: '/' + langCode +'/shop-dropdownCart?displayMode=content&deleteProductFromCart=' + productId,
					  success: function( answer ) {
							fillDropdownCart(answer) 
						}
					}); 
				});
			});
	});
}
	
function buildDropdownCartUpdate(){
	$('p').each(function(){
		var productId = $(this).attr('data-value');
		var langCode  = $(this).attr('data-lang');
		var action    = $(this).attr('data-action');
		$(this).click(function(){
			$('#cartSummary .largeRight .summary').fadeOut('fast');
			$.ajax({
				  url: '/' + langCode +'/shop-dropdownCart?displayMode=content&updateQuantity=' + productId + '&action=' + action,
				  success: function( answer ) {
					fillDropdownCart(answer) 
				  }
				}); 
			
		});
	});
}
/* --------------- category submenu ------------------ */	
//timeOut config
	var timeout     		= 300;
	var opentimer 			= 150;
//animation config
	var fadeInAnimation 	= true;
	var	fadeInTime			= 100;
	var	slideDownAnimation	= false;
	var	slideDownTime		= 200;
	
//variables for later usage
	var closetimer  		= 0;
	var open_timeout		= 0
	var dropDown 			= 0;
	var li 					= 0
	var liLoad      		= new Array();
	var openSub = null;

function categoryDropdown_open() {
		//close dropdowns that are already open
		categoryDropdown_canceltimer();
   		categoryDropdown_close();
		
		
		
		//save element in memory
		li 		 = $(this);
		dropDown = $(this).children('.categoryDropdown');
			
		//get dropdown content
		if (dropDown.html() == '') {
			categoryDropdown_load();
		}
		//show dropdown
		 if (dropDown.attr('data-categoryId') in liLoad) {		
				
			categoryDropdown_show();
		}
		//calculate position
			//categoryDropdown_calculatePosition();

	
}
function categoryDropdown_show(){
		dropDown.show();
		li.addClass('hover')
}
function categoryDropdown_animate (){
		 if(openSub != dropDown.attr('data-categoryId')){ 
		 	openSub = dropDown.attr('data-categoryId'); 
			dropDown.find('img').hide();
			dropDown.find('ul').hide();
			dropDown.hide();
			
			//open animation
			if(slideDownAnimation == true){
				dropDown.slideDown(slideDownTime,function(){ 
					if(fadeInAnimation == true){
						dropDown.find('img').fadeIn(fadeInTime);
						dropDown.find('ul').fadeIn(fadeInTime)
					}
					});
		 }else{
			
				if(fadeInAnimation == true){
					dropDown.show();
					dropDown.find('img').fadeIn(fadeInTime);
					dropDown.find('ul').fadeIn(fadeInTime)
					
				}else
					dropDown.show();
			}
			 
		}
		
	 }
function categoryDropdown_load(){
			var categoryId = dropDown.attr('data-categoryId');
			var langCode = dropDown.attr('data-langCode');
			url = '/'+langCode+'/shop-categoryProducts?categoryId='+categoryId+'&displayMode=content';
			$.ajax({
					  url: url,
					  success: function( answer ){
							  //place content
							  dropDown.html(answer);
							  //calculate position
							  categoryDropdown_calculatePosition();
							  //set in already load list
							  liLoad[dropDown.attr('data-categoryId')] = dropDown.attr('data-categoryId');
							  //finaly show the menu
							  categoryDropdown_show();
						}
				});	
}
function categoryDropdown_close(){
	if(dropDown){ dropDown.hide() }
	if(li){li.removeClass('hover')}
}
function categoryDropdown_timer(){
		closetimer = window.setTimeout(categoryDropdown_close, timeout); 	
}

function categoryDropdown_canceltimer() {  
	if (closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function categoryDropdown_calculatePosition() {
	dropDown.css('opacity',1);
	position = li.position();
	if(dropDown.find('.group').length > 4)
		dropDown.addClass('large');
		
	if(position.left + dropDown.width() > $('#categoryMenu').width() ){
		dropDown.css('right',0);
	}else{
		dropDown.css('left',li.position().left);	
	}
	dropDown.css('top', 43);	
}


function categoryDropdown_handler_ie7(){
	 	 	  //make the categoryDropdown and his css compitable with ie7
		  /* build tables om div with display: table proporties */
		  
		  
		  
		   $("#categoryMenu nav ul li").wrap("<td />"); 		/* display: table-cell */
		   $("#categoryMenu nav ul").wrapInner("<tr />"); 		/* display: table-row */
		   $("#categoryMenu nav ul").wrapInner("<table />");	/* display: table */
		   $("#categoryMenu").css('visibility','visible'); 		/* show after fox */
		 
			//for a equil width
			var width = 100 / $("#categoryMenu nav").find("td").length;
			$("#categoryMenu nav").find("td").css('width', width + '%');
  
			//for the category Dropdown
			$('#categoryMenu').find('td').find('li').bind('mouseover', categoryDropdown_open); 		//get the element, and content
			
			
			
			$('#categoryMenu').find('td').find('li').bind('mouseenter',categoryDropdown_animate)	//show animation (fade in and/or slide)			
			$('#categoryMenu').find('td').find('li').bind('mouseout', categoryDropdown_timer);		//start timer for close
}
function categoryDropdown_handler(){
	
	if ($.browser.msie && $.browser.version == 7){
	 	categoryDropdown_handler_ie7();
	}else
		{
			$('#categoryMenu > nav > ul > li .categoryDropdown').css('opacity',0) 			//for hide while the time nescecary for caclculate and contentload
			$('#categoryMenu > nav > ul > li').bind('mouseover', categoryDropdown_open); 	//get the element, and content
			$('#categoryMenu > nav > ul > li').bind('mouseenter',categoryDropdown_animate);	//show animation (fade in and/or slide)	
			$('#categoryMenu > nav > ul > li').bind('mouseout', categoryDropdown_timer);	//start timer for close
		}
	document.onclick = categoryDropdown_close; 
	
	
}


/* --------------- handlers ------------------ */	

$(document).ready(function(){
	buildHiddenSelect();
	buildDropdownCartDelete();
	buildDropdownCartUpdate();
});
$(window).load(function(){
	window.setTimeout("categoryDropdown_handler()",500);		
});

