/*
* Custom jQuery functions
**/


/* Sitemap */
function toggleSitemap(id, aId) {
	if($('#'+id).css('display') == 'none') {
		$('#'+id).css('display','block');
		$('a#'+aId+' img.toggleButton').attr('src',imgPath+'sitemap/min.png');
	}else{
		$('#'+id).css('display','none');
		$('a#'+aId+' img.toggleButton').attr('src',imgPath+'sitemap/plus.png');
	}
}

var activeProductInfoNr = 1;

function toggleProductInfo(n){
	// toggle tabs
	$('#productTabs a').removeClass('active');
	$('#tab'+n).addClass('active');
	// define height of tabs
	productTabHeight = $('#productTabs').height();

	if(n > activeProductInfoNr){
		// Fade out current visible div
		$('#info'+activeProductInfoNr).fadeOut(900);
		$('#info'+activeProductInfoNr).css('display','none');
		
		// Place div to be displayed just below the currently displayed div
		currentHeight = $('#info'+activeProductInfoNr).innerHeight()+productTabHeight;
		$('#info'+n).css('position','absolute');
		$('#info'+n).css('top',currentHeight+'px');
		$('#info'+n).css('opacity',0);
		// Fade in div to be displayed
		// Move div up
		$('#info'+n).show();
		$('#info'+n).animate({top: productTabHeight, opacity: 1});
		// Adjust container height
		$('#productInfo').animate({height: productTabHeight+$('#info'+n).innerHeight()});
		//$('#info'+activeProductInfoNr).hide();
	}else if(n < activeProductInfoNr){
		currentHeight = $('#info'+activeProductInfoNr).innerHeight()+productTabHeight;
		
		// Move currently displayed div to height of div to be displayed
		$('#info'+activeProductInfoNr).animate({top: $('#info'+n).innerHeight(), opacity: 0});
		$('#info'+activeProductInfoNr).css('display','none');
		//$('#info'+activeProductInfoNr).hide();
		
		// Position div to be displayed to top 0px
		$('#info'+n).css('top',productTabHeight);
		// Fade div to be displayed in
		$('#info'+n).css('display','none');
		$('#info'+n).css('opacity', 1);
		$('#info'+n).fadeIn(600);
		// Adjust container height
		$('#productInfo').animate({height: productTabHeight+$('#info'+n).innerHeight()});
	}
	// set active number
	activeProductInfoNr = n;
}

// toggle submenu up/down
function toggleSubmenu(){
	// toggle slide up / down
	$('#contentSubMenuItems').slideToggle('slow');

	// if submenuArrow de class naar beneden heeft switchen naar up 
	if($('#submenuArrow').hasClass('submenuLink')){
		$('#submenuArrow').removeClass('submenuLink').addClass('submenuLinkUp');
	}else{
		$('#submenuArrow').removeClass('submenuLinkUp').addClass('submenuLink');
	}

}

$(document).ready(function() {
	// eventlistener for product tabs
	$('#productTabs a').bind('click', function(e) {
	  //alert($(this).attr('id'));
	  e.preventDefault();	
	  n = $(this).attr('id').substr(3);
	  toggleProductInfo(n);
	});
	// eventlisteren for click on link in submenu
	$('#contentSubMenuLinkContent a').bind('click', function(e) {	
		//alert(e);
		e.preventDefault();	
		toggleSubmenu();
	});
	
	// Voegt tooltip toe aan body
	$("body").append("<div id='ToolTipDiv'></div>");
	productMouseOver();
		
	$("input:radio[name=productGroup]").bind('click', function(e) {	
		calculateProducts();	
	});
		
	// bind calculate products function to allergic checkboxes
	$('#vermelding').bind('change', function(e) {	
		display(this);
	});
	// bind calculate products function to suitable for checkboxes
	$('input:checkbox[name=suitable]').bind('click', function(e) {	
		calculateProducts();
	});
	
	$("a[rel=youtube]").click(function() {
		$.fancybox({
				'autoScale'		 : false,
				'transitionIn'	 : 'none',
				'transitionOut'	 : 'none',
				'titlePosition'  : 'inside',
				'title'			 : this.title,
				'width'			 : 680,
				'height'		 : 495,
				'overlayColor'	 : '#000',
				'overlayOpacity' : '0.3',
				'href'			 : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')+'?fs=1',
				'type'			 : 'swf',
				'swf'			 : {
				'wmode'			 : 'transparent',
				'allowfullscreen': 'true'
				}
			});
	
		return false;
	});
	$('#radio1, #radio2').bind('click', function(e) {
		checkErrorBGN();
	});
});

function checkErrorBGN(){
	if($('input[@name=aangesloten]:checked').val()=='Y'){
		$('#errorBGN').hide();
	}else{
		$('#errorBGN').show();
	}
}

function productMouseOver(){
	// div waarbinnen de a title een Tooltip moeten worden
	$("#contentBlock a[title]").each(function() {
		// Voer functies uit op de Hover van dit element
   		$(this).hover(function(e) {
   			// bij een muisbeweging bereken Tooltip lokatie en wijzig CSS voor de positie
			$(this).mousemove(function(e) {
				var tipY = e.pageY + 16;
				var tipX = e.pageX + 16;
				$("#ToolTipDiv").css({'top': tipY, 'left': tipX});
			});
			// Voeg html toe met de waarde die in TITLE staat, en fade de tooltip in
			$("#ToolTipDiv").html('<div class="left"></div><div class="middle" id="ToolTipDivMiddle">'+$(this).attr('title')+'</div><div class="right"></div>').stop(true,true).fadeIn("fast");
			// Maak title leeg zodat de standaard functionaliteit niet gebruikt wordt
			$(this).attr("title", "");				
		}, function() {// Wanneer er niet meer gehoverd word
			// fade out Tooltip
			$("#ToolTipDiv").stop(true,true).fadeOut("fast");
		   // en haal de waarde uit de toolTipDivMiddle om de "oude" title weer terug te zetten voor de volgende keer
			$(this).attr('title', $("#ToolTipDivMiddle").html());
		});
	});
}

//set vars for product ajax
var viewType = 'images';
function calculateProducts(){
	// group
	var selectedGroup = new Array;
	$("input:radio[name=productGroup]:checked").each(function() {
		selectedGroup.push($(this).val());		
	});
	// allergic
	var selectedAllergic = new Array;
	$("input:checkbox[name=allergic]:checked").each(function() {
		selectedAllergic.push($(this).val());		
	});
	// suitable for
	var selectedSuitable = new Array;
	$("input:checkbox[name=suitable]:checked").each(function() {
		selectedSuitable.push($(this).val());		
	});
	//alert(selectedGroup);
	// make data feed for ajax
	var dataFeed = {
		action: 'getProducts', // action
		websiteLanguageId : websiteLanguageId, // website id for getting products
		website : website, // website for making links
		selectedGroup : selectedGroup, // product group
		selectedAllergic : selectedAllergic, // allergic data
		viewType : viewType, // view type
		selectedSuitable : selectedSuitable // suitable for data
	}
	
	// call ajax
	$.ajax({
		type: 'GET',
		url: rootPath+'page/products.ajax.php',
		data: dataFeed,
		success: function(msg) {
			$('.dynamicProductlist').html(msg);
			// create new mouse overs
			productMouseOver();
		}
	});
}

// switch view of products
function switchView(newViewType){
	// set new view
	viewType = newViewType;
	// calculate new products
	calculateProducts();
	// switch class for selected button
	if(viewType == 'text'){
		$('#btnProductImages').removeClass('in');
		$('#btnProductText').addClass('in');
	}else{
		$('#btnProductImages').addClass('in');
		$('#btnProductText').removeClass('in');
	}
}

/* product complaint */
function getEanNumbers(productId, website, selectedOption){
	$.ajax({
		type: "GET",
		url: rootPath+"page/contact_products.ajax.php",
		data: "productId="+productId+'&website='+website+'&selectedOption='+selectedOption,
		success: function(msg){
			$('#productEanCode').html(msg);
		}
	});	
}


