/* 
	COMMON S1 JAVASCRIPT
	www.bubiblomer.com
	by bubi blomer
*/

$(document).ready(function() {

	$('#mainLogo').click(function() {
		window.location.href = "/";
	});
	
	//cufon
	Cufon.replace('#mainSplash a span');
	
	//fancybox
	$('a.fancybox').each(function(){
		$(this).fancybox();	
	});
	
	var $myAccount = $('#myAccount');
	var $myAccountLiMain = $myAccount.find('li.main');
	var $childUl = $myAccountLiMain.find('ul');
	$myAccountLiMain.hover(function(e) {
		if(e.type == "mouseenter") { $childUl.toggle(); } else if(e.type == "mouseleave") { $childUl.toggle(); }
	});
	
	//my Sanikoop
	var $mySanikoop = $('#mijnSanikoop').parent();
	$mySanikoop.hover(function(e) {
		$mySanikoop.find('ul').toggle();		
	});
	
	$('#quickthumbs ul li').each(function(i) {
		var $this = $(this);
		if(i === 0){
			$this.addClass('first');
		} else if (i%6 === 0){
			$this.addClass('first');
		}
	});
	
	$('.related .block', '#mainContent').each(function(i) {
		var $this = $(this);
		if(i === 0){
			$this.addClass('first');
		} else if (i%5 === 0){
			$this.addClass('first');
		}
	});
	
	$('.related .block h3', '#mainContent').equalizeHeights();
	
	//mainSplash
	$mainSplashCont3ImgCount = $('#mainSplashCont3 img').length;
	splashLoop = 0;
	if($('#mainSplashCont3')){ var mainSpashInterval = setInterval("doMainSplashScroll()",5000); }
	
	//Second nav function
	$('#secondNav.minimized').each(function() {
		
		var $childrenLi = $(this).children('li.subCategory');
		$childrenLi.not('.mainCategoryName, .active').hide();
		$childrenLi.find('ul:visible li').not('.active').hide();
		var $subCategoryActive = $(this).find('.subCategory.active > a');
		var $parentSiblings = $subCategoryActive.siblings('ul').find('li').not('.active');
		$subCategoryActive.click(function() {
			$parentSiblings.slideToggle('fast');	
			return false;
		});
		
		$('#meerSubCategory a').live('click',function() {
			$childrenLi.not(':visible').slideToggle('fast');
			$(this).hide();	
			return false;
		});
		
		$(this).append('<li id="meerSubCategory" class="subCategory"><a href="#">Meer</a></li>').show();
		
	});
	
	//Filter Functions
	$('#productFilters .filter h6').each(function() {
		$(this).click(function() {
			var $filterContent = $(this).parent().next('.filterContent');
			var $anchor = $(this).find('a').toggleClass('opened');
			//$filterContent.slideToggle('fast');
			$filterContent.toggle();
		});
	});
		
	//shoppingcart
		
		//delete product //move it to shoppingcart.js ??
		var productDeleteId;
		$('table td.productDelete').each(function(){
			var $this = $(this);
			var $table = $this.parents('table');
			var $parent = $this.parents('tr');
			var $productId = $parent.attr('id');	
			
			$this.click(function() {
							
				$.ajax({
					type: "POST",
					url: "/scripts/ajax.php?shoppingcart=delete",
					dataTypeString: "json",
					data: "product=" + $productId,
					success: function(response){
						if(!response.error){
							$parent.fadeOut(500, function() {
								//$(this).remove();
								productDeleteId = $productId;	
								$table.find('.undoProductDeleteButton').fadeIn(500);	
							});
						}
					}
				});			
				return false;
			});
		}); //end delete product
	
		$('.undoProductDeleteButton').click(function(){
			var $this = $(this);
			var $table = $this.parents('table');
			var $undoProductDeleteButton = $table.find('.undoProductDeleteButton');
			$('#'+productDeleteId).fadeIn(500, function(){
				$this.fadeOut(500);
				$undoProductDeleteButton.fadeOut(500);
			});
			
		});
	
	// end shoppingcart
	
	//make cookie for #news-teasers
	var newsTeaserState = $.cookieJar('newsTeaserState', {cookie: {path: '/', expires: 30}});
	//saveBasket.destroy();
	
	function isNewsTeaserStateOpen() {
		if(newsTeaserState.get('open') === "no"){
			return true;
		} else {
			return false;
		}
	}
	
	//#news-teaser toggler
	$('#news-teasers h3').click(function() {
		if(isNewsTeaserStateOpen()){
			newsTeaserState.set('open', 'yes');
		} else {
			newsTeaserState.set('open', 'no');
		}
		
		$(this).parent().find('ul').slideToggle('fast');
		$(this).toggleClass('closed');
		
		return false;
	});
	
	if(isNewsTeaserStateOpen()){
		newsTeaserState.set('open', 'yes');	
		$('#news-teasers h3').trigger('click');
	}
	
	//search functions
	function eraseAllbutFirstOption(id){
		$('#' + id + ' option').not(':first').remove();
	}
	
	function addJsonDatatoSelectElement(id,selectedValue){
		$(selectedValue[id]).each(function(i){
			$('#' + id).append('<option value="'+selectedValue[id][i]+'">'+selectedValue[id][i]+'</option>');
		});
	}
	
	function changeData(selectedValue){
		selectedValue ? typeof(selectedValue) === "undifined" : sMerk[""]; // if selectedValue == undifined redifine to empty string
		//change model
		eraseAllbutFirstOption('z');
		addJsonDatatoSelectElement('z',selectedValue);								
	}
	
	$('#searchForm.inline input[type="reset"]').click(function() {	
		selectedValue = sMerk[""];
		changeData(selectedValue);
		$('#searchForm').find('option:selected').removeAttr('selected');
		$('#searchForm').find('option').eq(0).attr('selected', 'selected');
		
		//return false;
	});
	
	$('#m').change(function() {
		var val = $(this).find(':selected').val();
		selectedValue = sMerk[val];						
		
		//CHANGE DATA
		changeData(selectedValue);
	});
	// end search functions
	
	//$('#form-basket .productRow:last').removeClass();
		
});	//end jQuery Ready

function doMainSplashScroll(){	
	var $scrollTop = $('#mainSplashCont3').scrollTop();
	if(splashLoop <= ($mainSplashCont3ImgCount-1)){
		$('#mainSplashCont3').animate({scrollTop: $scrollTop + 400 }, 2000);
		splashLoop++;
	} else {
		$('#mainSplashCont3').animate({scrollTop: 0 }, 2000);
		splashLoop = 0;
	}	
}
