﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	 if( $.browser.safari || $.browser.chrome){
		var sTop = document.body.scrollTop;
		var sLeft = document.body.scrollLeft;
	 } else if($.browser.mozilla){
		var sTop = document.documentElement.scrollTop;
		var sLeft = document.documentElement.scrollLeft;
	 }
	 else{
		var sTop = document.documentElement.scrollTop;
		var sLeft = document.documentElement.scrollLeft;
	 }
	var popupHeight = $("#search_pop").height();
	var popupWidth = $("#search_pop").width();
	//centering
	$("#search_pop").css({
		"position": "absolute",
		"top": (windowHeight/2-popupHeight/2)+sTop,
		"left": (windowWidth/2-popupWidth/2)+sLeft
	});
	
}
//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupAge").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupAge").fadeOut("slow");
		popupStatus = 0;
	}
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#children").change(function(){
		$val = $('#children').val();
		childrenAgeSelected($val);
		
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupAgeClose").click(function(){
		disablePopup();
		$('#childrenAgeDiv').hide(400);
	});
	
	
});

/*    popup inside search results          */

var popup_status_search = 0;

//loading popup with jQuery magic!
function load_search_Popup(){
	//loads popup only if it is disabled
	var popstat = getCookie("Spopup");
	if(popstat=="yes"){
		if(popup_status_search==0){
			
			$("#backgroundPopup_search").css({
				"opacity": "0.7"
			});
			$("#backgroundPopup_search").fadeIn("slow");
			$("#search_pop").fadeIn("slow");
			centerPopup();
			popup_status_search = 1;
		}
	}
}

//disabling popup with jQuery magic!
function disable_search_Popup(){
	//disables popup only if it is enabled
	if ($('#chHide').is(':checked')) {
    	setCookie('Spopup',"no",365);
	}
	else{
		setCookie('Spopup',"yes",365);
	}
	if(popup_status_search==1){
		$("#backgroundPopup_search").fadeOut("slow");
		$(".popBox").fadeOut("slow");
		popup_status_search = 0;
	}
}


function childrenAgeSelected(val)
{
	
	if (val == 0)
	{
		$('#childrenAgeDiv').hide(400);
		
	}
	else
	{
		
		$('#childrenAgeDiv').show(400);
		$('#childrenAgeDiv .selectWrap').slice(0,val).fadeIn();
		$('#childrenAgeDiv .selectWrap').slice(val).css('display', 'none');
		
		$('#childrenAgeDiv .selectWrap select #select').slice(0,val).fadeIn();
		$('#childrenAgeDiv .selectWrap select #select').slice(val).fadeOut();
		loadPopup();
	}
}

function getAgeValues()
{
    var children = document.getElementById('children').value;
	var numChildren = parseInt(children.substring(0,1));
	
	var childrenAges = [];
	var fail = 0;
	$(".childrenAgeSelects").slice(0,numChildren).each(function (index, element)
	{
		var age = $(element).val();
		
		if (age == "")
		{
			++fail;
			return;
		}
		
		childrenAges.push(age);
	});
	
	if (fail > 0)
	{
		$('#ageError').html("Välj ålder!");
		return false;
	}
	else
	{
	  childrenAges = childrenAges.join(',');
	  $('#childrenAgeVal').val(childrenAges);
	  disablePopup();
	}
	
}





