/*----------------------------------------------------------------------------------------------------------------------------*/

$(document).ready(function(){
	if ($('#toelichtingDiv').length > 0) {
		var myBorder = RUZEE.ShadedBorder.create({ corner:8, shadow:16, border:1 });
    	myBorder.render('toelichtingDiv');
	}

	checkIsContact();
});

function checkIsContact()
{
	if ($("div.contactBrands").length != 0)	{
		handleShowContactForm();
		//look for the get
		var docLoc = document.location.href;
		if (strpos(docLoc, 'productID') !== false) {
			var pId = getGETVar('productID');
			//request
			$.ajax({
			   type: "POST",
			   url: "/javascript/ajaxRequest/handleContactForm.php",
			   data: "productID="+pId,
			   success: function(msg){
				 $("div.contactBrands").html(msg);
			   }
			 });
		}
	}
}

function hideToelichting()
{
	$('#toelichtingDiv').slideUp(500);
}

function showToelichting()
{
    $('#toelichtingDiv').css('position','absolute');
	$('#toelichtingDiv').slideDown();

//	if ($('#toelichtingDiv_shadow').length == 0) {
//		 $('#toelichtingDiv').shadow({
//		       width:6,
//		       startOpacity:60,
//		       endOpacity:10,
//		       cornerHeight:10,
//		       color:"#000000"
//		});
//		$('#toelichtingDiv_shadow').corner();
//		$('#toelichtingDiv').corner();
//	}
//	$('#toelichtingDiv_shadow').hide();
//	$('#toelichtingDiv').show();
//	$('#toelichtingDiv_shadow').slideDown(500);
}

function handleShowContactForm()
{
	var docLoc = document.location.href;
	if (strpos(docLoc, 'productID') === false) {
		$('#labelMerk').hide();
		$('#merk').hide();
		$('#labelType').hide();
		$('#type').hide();
	} else {
		//$('#labelMerk').show();
		//$('#merkID').show();
		//$('#labelType').show();
		//$('#type').show();
		$('#labelAdres').hide();
		$('#adres').hide();
		$('#labelPlaats').hide();
		$('#plaats').hide();
	}
}

function hideSearchDivs()
{
	$('#searchBoxContainer').css('opacity','0.4');
	$('#overallSearchResults').css('opacity','0.4');
	$('.searchform_knop').attr('disabled',true);
}
function showSearchDivs()
{
	$('#searchBoxContainer').css('opacity','1.0');
	$('#overallSearchResults').css('opacity','1.0');
	$('.searchform_knop').removeAttr('disabled');
}

function handleVpSearch(merkID,typeVal,vp)
{
	hideSearchDivs();

	$.ajax({
	   type: "POST",
	   url: "/javascript/ajaxRequest/handleVpSearch.php",
	   data: "merkId="+merkID+"&type="+typeVal+"&vp="+vp,
	   success: function(msg){
		 $("div#overallSearchResults").html(msg);
	   },
	   complete: function(XMLHttpRequest, textStatus) {
	   		$('#overallSearchResults').show();
			showSearchDivs();
	   }
	 });
}



function locationChange(variable, value)
{
	var variables = getQueryVariables();
	var queryString = '?';
	var found = false;
	for (i = 0; i < variables.length; i++) {
		if ( variables[i][0] == variable ) {
			variables[i][1] = value;
			found = true;
		}
		if (i == 0) {
			queryString += variables[i][0] + '=' + variables[i][1];
		} else {
			queryString += '&' + variables[i][0] + '=' + variables[i][1];
		}
	}
	if (found == false) {
		if (variables.length > 0) {
			queryString = queryString + '&' + variable + '=' + value;
		} else {
			queryString = queryString + variable + '=' + value;
		}
	}
	window.location = queryString;
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

/* Get something from GET query */

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

function getQueryVariables() {
  var query = window.location.search.substring(1);
 var pairs = new Array();
  if (query != '') {
	  var vars = query.split("&");
	  for (var i=0; i < vars.length; i++) {
	    var pair = vars[i].split("=");
	    pairs[pairs.length] = pair;
	  }
  }
  return pairs;
}

function initContactType() {
	var val = getGETVar('contact_over');
	if (val != '') {
		document.getElementById('contact_over').value = val;
	}
}


/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle mouseover for images in topmenu */

function topMenuOver(oImg)
{
	var newSrc = oImg.src.replace('.gif','_act.gif');
	return(newSrc);
}

/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle mouseout for images in topmenu */

function topMenuOut(oImg)
{
	var newSrc = oImg.src.replace('_act.gif','.gif');
	return(newSrc);

}


/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle AJAX requests for newsletters */

function handleNewsletter(vlag,nbid)
{
	if(nbid != 'undefined')
	{
		/* Specific newsletter */
		var url = '/modules/newsletter/newsletter.php?action=newsletter&vlag='+vlag+'&nbid='+nbid;
	}
	else
	{
		/* Overview */
		var url = '/modules/newsletter/newsletter.php?action=overview&vlag='+vlag;

	}

	new Ajax.Updater('newslettercontent', url);
}

/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle AJAX requests for searches */

function handleSearch(vlag,query,start,skipbackcheck)
{

	if(query != '')
	{



	if(!skipbackcheck)
	{

		/* Check if user has pressed back button on browser, to make sure the correct AJAX call is made */
		var currentUrl = document.location.href;
		var currentUrlElements = currentUrl.split('#');
		if ((!isNaN(currentUrlElements[1])) && (!currentUrlElements[1].length == 0))
		{
			var start = currentUrlElements[1];
		}

	}


	/* Create URL */
	//var url = '/modules/search/search.php?query='+encodeURIComponent(query)+'&vlag='+vlag+'&start='+start;

	/* Request AJAX Update */
	//new Ajax.Updater('searchresults', url);

	//$("div.searchresults").load(url);

	$.ajax({
	   type: "GET",
	   url: "/modules/search/search.php",
	   data: "query="+encodeURIComponent(query)+"&vlag="+vlag+"&start="+start,
	   success: function(msg){
		 $("div#searchresults").append(msg);
	   }
	 });


	}

}

function getCookie(Name)
{
	var search = Name + "=";
	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
			end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
}

function tjek()
{
	var t = new Date().getTime();

	if (document.inlogformulier.onthoud.checked == true)
	{
		var m = document.inlogformulier.email.value;
		var p = document.inlogformulier.password.value;

		var Verval = new Date();
		Verval.setMonth(Verval.getMonth()+6);

		document.cookie = "onthoudn="+m+"; expires=" + Verval.toGMTString();
		document.cookie = "onthoudp="+p+"; expires=" + Verval.toGMTString();
	}else{
		var Verval = new Date();
		Verval.setMonth(Verval.getMonth()-6);
		document.cookie = "onthoudn=; expires=" + Verval.toGMTString();
		document.cookie = "onthoudp=; expires=" + Verval.toGMTString();
	}

	return true;

}

function laden()
{
	var on = getCookie("onthoudn");
	var op = getCookie("onthoudp");

	if (op && on)
	{
		if(testIsValidObject(document.inlogformulier)){
			document.inlogformulier.emailLogin.value = on;
			document.inlogformulier.passwordLogin.value = op;
			document.inlogformulier.onthoud.checked = true;
		}
		if(testIsValidObject(document.inlogformulier_main)){
			document.inlogformulier_main.emailLogin.value = on;
			document.inlogformulier_main.passwordLogin.value = op;
		}

	}
}

function testIsValidObject(objToTest) {
	if (null == objToTest) {
		return false;
	}
	if ("undefined" == typeof(objToTest) ) {
		return false;
	}
	return true;

}

function getGETVar( name )
{
   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regexS = "[\\?&]"+name+"=([^&#]*)";
   var regex = new RegExp( regexS );
   var results = regex.exec( window.location.href );
   if( results == null )
                  return "";
   else    return results[1];
}

/*function toonActies(tonen)
{
	if (tonen) {

		$("#actiemenu_uitklap_header").fadeIn();
		$("#actiemenu_uitklap_content").fadeIn();
		$("#actiemenu_buttonImage").removeAttr("class");
		$("#actiemenu_buttonImage").addClass("hoovered");

		//display = 'block';
		//image = 'hoovered';
	} else {
		$("#actiemenu_uitklap_header").fadeOut();
		$("#actiemenu_uitklap_content").fadeOut();
		$("#actiemenu_buttonImage").removeAttr("class");
		$("#actiemenu_buttonImage").removeClass("hoovered");
		//display = 'none';
		//image = '';
	}

	/*if (document.getElementById('actiemenu_uitklap_header') != undefined) {
		document.getElementById('actiemenu_uitklap_header').style.display = display;
	}
	if (document.getElementById('actiemenu_uitklap_content') != undefined) {
		document.getElementById('actiemenu_uitklap_content').style.display = display;
	}
	if (document.getElementById('actiemenu_buttonImage') != undefined) {
		document.getElementById('actiemenu_buttonImage').className = image;
	}

}*/

jQuery(document).ready(function($)	{
	$("#actiemenu_buttonImage").mouseover(function()	{
		$("#actiemenu_uitklap_header").toggle("normal");
		$("#actiemenu_uitklap_content").toggle("normal");
		$("#actiemenu_buttonImage").removeAttr("class");
		$("#actiemenu_buttonImage").addClass("hoovered");

		$("#kruimelspoor").mouseenter(function()	{
			$("#actiemenu_uitklap_header").fadeOut("fast");
			$("#actiemenu_uitklap_content").fadeOut("fast");
			$("#actiemenu_buttonImage").removeAttr("class");
			$("#actiemenu_buttonImage").removeClass("hoovered");
		});
		$("#actiemenu_uitklap_content").mouseleave(function()	{
			$("#actiemenu_uitklap_header").fadeOut("fast");
			$("#actiemenu_uitklap_content").fadeOut("fast");
			$("#actiemenu_buttonImage").removeAttr("class");
			$("#actiemenu_buttonImage").removeClass("hoovered");
		});
	});

	$('.stepHeaderFirst, .stepHeader').click(function() {
		var id = $(this).attr('id');
		var splitted = id.split('_');
		$(this).children('img').attr('src','/grafix/dropdown_hover.gif');
		switch (splitted[1]) {
			case "1":
				$('#step_2').children('img').attr('src','/grafix/dropdown.gif');
				$('#step_3').children('img').attr('src','/grafix/dropdown.gif');
				break;
			case "2":
				$('#step_1').children('img').attr('src','/grafix/dropdown.gif');
				$('#step_3').children('img').attr('src','/grafix/dropdown.gif');
				break;
			case "3":
				$('#step_1').children('img').attr('src','/grafix/dropdown.gif');
				$('#step_2').children('img').attr('src','/grafix/dropdown.gif');
				break;
		}
	});

	$("input#datum").datepicker(
		{
			dateFormat: 'dd-mm-yy',
			dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
			monthNamesShort: ['Jan','Feb','Mrt','Apr','Mei','Jun', 'Jul','Aug','Sep','Okt','Nov','Dec'],
			monthNames: ['Januari','Februari','Maart', 'April','Mei','Juni','Juli','Augustus', 'September','Oktober','November','December'],
			changeMonth: true,
			changeYear: true,
			gotoCurrent: true
		}
	);
});

function showStep1()
{
	$('#step_2_content').slideUp(500, function () { $('#step_1_content').slideDown( 500, function () { $('#quantity').focus(); } ); });
	$('#step_3_content').slideUp(500, function () {  });
	$('#step_1').children('img').attr('src','/grafix/dropdown_hover.gif');
	$('#step_2').children('img').attr('src','/grafix/dropdown.gif');
	$('#step_3').children('img').attr('src','/grafix/dropdown.gif');
}

function showStep2()
{
	pageTracker._trackPageview('/trechter_1/stap3.html');
	$('#step_1_content').slideUp(500);
	$('#step_3_content').slideUp(500, function () { $('#step_2_content').slideDown(500, function () {  } ); });
	$('#step_1').children('img').attr('src','/grafix/dropdown.gif');
	$('#step_2').children('img').attr('src','/grafix/dropdown_hover.gif');
	$('#step_3').children('img').attr('src','/grafix/dropdown.gif');

}

function showStep3()
{
	pageTracker._trackPageview('/trechter_1/stap4.html');
	$('#step_1_content').slideUp(500, function () {  });
	$('#step_2_content').slideUp(500, function () { $('#step_3_content').slideDown( 500, function () { $('#bedrijfsnaam').focus(); } ); });
	$('#step_1').children('img').attr('src','/grafix/dropdown.gif');
	$('#step_2').children('img').attr('src','/grafix/dropdown.gif');
	$('#step_3').children('img').attr('src','/grafix/dropdown_hover.gif');
}

function checkStep1()
{
//	if ($("#quantity").val() == '') {
//		alert('U dient het aantal nog in te voeren');
//		return false;
//	} else {
		$('#step_1_content').slideUp(500, function () { $('#step_2_content').slideDown(); });
//	}
}

function reloadLocationNewTime()
{
	window.location.href = window.location.href + '?t=' + new Date().getTime();
}