var curHashVal = location.hash; //global reference naar de hash value

$(function(){
	Cufon.replace('#footer p, div.right h1, div.right h2, div.right h3, #favorites h3, a.case, #content h1');
	
	$("#favorites span.opener").click(function(){
		$("#favorites span.opener").hide();
		$("#favoriteBox").show();
		return false;
	});
	
	$("#favoriteBox h3").click(function(){
		$("#favorites span.opener").show();
		$("#favoriteBox").hide();
		return false;
	});
    
    $("#diensten").change(function(){
        goTo();
    });
	
	// Cases:
	$("#caseTop a.close").click(function(){
		$("#caseOverlay, #box").hide();
		location.hash = '';
		return false;
	});
	
	$("a.case").click(function(){
		src = $(this).attr("href");
		showCase(src);
		return false;
	});
	
	// Kijk of er een case geladen moet worden (voor bookmarking, url met hash-tag):
	if(location.hash != '' && location.hash != '#') {
		src = location.hash.replace('#', '');
		showCase(src);
	}
	
	// Verberg de case-overlay als de hash-tag veranderd: (door bijvoorbeeld de backbutton)
	// Dit is een interval-functie die iedere 500 ms uitgevoerd word:
	setInterval("checkHash()", 500);
});

function showCase(src)
{
	$("#box").css({opacity: 0.5}).show();
	$("#ajaxloader").show();
	
	hash = src;
	
	// In dit voorbeeld is src de directe bron van de HTML (dus: case.html).
	// Je kunt dit ook dynamisch maken met bijvoorbeeld een ID en dan deze javascript aan te passen hoe deze geladen moet worden. Voorbeelden:
	//
	// Volledig pad (huidige voorbeeld):
	// content.html:	<a href="case.html">...
	// global.js:		$("#caseContent").load(src, function(){...
	//
	// Óf een ID (met GET):
	// content.html:	<a href="23">...
	// global.js:		$("#caseContent").load("case.php?id=" + src, function(){...
	// óf een ID met POST:
	// global.js:		$("#caseContent").load("case.php", {id: src}, function(){...
	//
	// Of bijvoorbeeld een alias:
	// content.html:	<a href="mijn-te-gekke-case">...
	// global.js:		$("#caseContent").load("case.php?alias=" + src, function(){...
	// of POST:
	// global.js:		$("#caseContent").load("case.php", {alias: src}, function(){...
	//
	// Voor zoekmachine optimalisatie moet het volgende nog even gedaan worden:
	// Uncomment de volgende regels:
	//
     a = src.split("?");
     if(a.length > 1) {
         temp = a[1];

       a = temp.split("&");
       
       if(a.length > 1) {
           hash = a[0];
           b = hash.split("=");
           if(b.length > 1) {
            hash = b[1];
           }
           hash2 = a[1];
           c = hash2.split("=");
           if(c.length > 1) {
            hash2 = c[1];
           }
       }

     } 

//     a = src.split("?");
//     if(a.length > 1) {
//         temp = a[1];
//       a = temp.split("=");
//       if(a.length > 1) {
//           hash = a[1];
//       }
//     } 
     

	//
	// Als je nu de volgende link hebt:
	// content.html:	<a href="case.php?id=23">   <-- Google volgt deze link, dus ziet de content van de case
	// global.js: 		$("#caseContent").load("ajaxCase.php?id" + hash, function(){...
	// De hash-tag van de pagina wordt in dit geval "#23", en het bestand "ajaxCase.php" wordt geladen met GET id=23
	//
	// De case.php-pagina moet een stukje javascript hebben die meteen naar de juiste pagina gaat met de juiste hashtag. (voor als je vanuit Google op de link klikt)
	// De code hiervoor zou er bijvoorbeeld als volgt uit kunnen zien:
	//
	// a = window.location.split("?");
	// if(a.length > 1) {
	//	 temp = a[1];
	//   a = temp.split("=");
	//   if(a.length > 1) {
	//	   hash = a[1];
	//     window.location = "/content.php?id=" + hash;  // (bijvoorbeeld, afhankelijk van het bestand)
	//   }
	// }
	
	location.hash = hash;	
    
	$("#caseContent").load("/cms/modules/cases/case.php", {id: hash}, function(){

		// Verberg de ajaxloader:
		$("#ajaxloader").hide();
		// Plaats de overlay in het midden van het scherm:
		var ScrollTop = document.body.scrollTop;
		if (ScrollTop == 0) {
			if (window.pageYOffset) {
				ScrollTop = window.pageYOffset;
			} else {
				ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
		}
		$("#caseOverlay").css({top: ScrollTop + 50}).show();
		// Cufon de header en de list-items:
		Cufon.replace('#caseContent h1, #caseContent ul.actions a');
		// Tell a friend:
		$("a.tellAFriend").click(function(){
            ChangeVisibilityForClass('movie', 'hidden')
			// $("#box").css({zIndex: 11});
			$("#tellAFriend").show();
			return false;
		});
		$("#tellAFriend a.close").click(function(){
            ChangeVisibilityForClass('movie', 'visible')
			$("#tellAFriend").hide();
			// $("#box").css({zIndex: 10});
			return false;
		});
        // Send PDF:
        $("a.sendPDF").click(function(){
            ChangeVisibilityForClass('movie', 'hidden')
            // $("#box").css({zIndex: 11});
            $("#sendPDF").show();
            return false;
        });
        $("#sendPDF a.close").click(function(){
            ChangeVisibilityForClass('movie', 'visible')
            $("#sendPDF").hide();
            // $("#box").css({zIndex: 10});
            return false;
        });
		// Hier kan nog extra javascript uitgevoerd worden voor de nieuw geladen data, mocht dat nodig zijn:
		
		
		
	});
	
}


function checkHash()
{
	if(curHashVal!=location.hash){ //changed
		if(location.hash != '' && location.hash != '#') {
			src = location.hash.replace('#', '');
			showCase(src);
		} else {
			$("#caseOverlay, #box").hide();
			location.hash = '';
		}
		//do some work then save the current hash value
		curHashVal = location.hash;
	}
}

function goTo()
{
    var dienst = document.selector.dienst.value;
    window.location = '/diensten/'+dienst;
    return false;
}

function setCookie(c_name,value,expiredays)
{
savedValues = getCookie('cases');
savedValuesArray = savedValues.split(',');

    if (savedValuesArray.length > 10) 
    {
    alert("Maximum bereikt");
    } else {    
    
        if (in_array(value,savedValuesArray) == true)
        {    
        alert("Favoriet bestaat al"); 
        } else {   
        alert("Favoriet toegevoegd");  
        savedValues = savedValues + "," + value     
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(savedValues)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
        ';path=/';
        location.reload(true); 
        }
    }    
}



function deleteInCookie(c_name,value,expiredays)
{
if (confirm('Weet u zeker dat u deze favoriet wilt verwijderen?') == true) {
savedValues = getCookie('cases');
savedValues = replaceChars(savedValues, "," + value, "");

    alert("Favoriet is verwijderd");
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(savedValues)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    location.reload(true);
}
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function in_array( what, where ){
    var a=false;
    for(var i=0;i<where.length;i++){
      if(what == where[i]){
        a=true;
        break;
      }
    }
    return a;
}

function replaceChars(entry,out,add) {
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
return temp;
}

function checkformTAF() {
 var error = "";
 if (document.TAF.naam.value == "")                { error = error + "- Naam\n"; }
 if (document.TAF.email.value == "")                   { error = error + "- E-mailadres\n"; }
 if ((emailCheck(document.TAF.email.value) == false) 
    & (document.TAF.email.value != ""))              { error = error + "- E-mail ongeldig\n"; }
 if (document.TAF.naamVriend.value == "")                { error = error + "- Naam vriend\n"; }
 if (document.TAF.emailVriend.value == "")                   { error = error + "- E-mailadres vriend\n"; }
 if ((emailCheck(document.TAF.emailVriend.value) == false) 
    & (document.TAF.emailVriend.value != ""))              { error = error + "- E-mail vriend ongeldig\n"; }    
 
        


 if (error != "") {
  error = "U bent de volgende verplichte velden vergeten:\n" + error;
  alert(error);
  document.returnValue = false;
 }
 else {
  thanks = "Beste " + document.TAF.naam.value + ", de e-mail met een link naar deze case is verzonden.\n";
 alert(thanks);
  document.returnValue = true;
 }
}

function checkformPDF() {
 var error = "";
 var thanks = "";   
 if (document.PDF.naam.value == "")                { error = error + "- Naam\n"; }
 if (document.PDF.email.value == "")                   { error = error + "- E-mailadres\n"; }
 if ((emailCheck(document.PDF.email.value) == false) 
    & (document.PDF.email.value != ""))              { error = error + "- E-mail ongeldig\n"; }

 if (error != "") {
  error = "U bent de volgende verplichte velden vergeten:\n" + error;
  alert(error);
  document.returnValue = false;
 }
 else {
 thanks = "Beste " + document.PDF.naam.value + ", R&R zal z.s.m. contact met je opnemen.\n";
 alert(thanks);
  document.returnValue = true;
 }
}

function emailCheck(str) {

        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1){
            return false
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
            return false
         }
        
         if (str.indexOf(" ")!=-1){
            return false
         }

          return true                    
    }
    


function ChangeVisibilityForClass(ClassToHide, Visibility) {
  var ToHide = document.getElementsByTagName('DIV');
    for(var i=0; i < ToHide.length; i++){
              if (ToHide[i].className && ToHide[i].className == ClassToHide) ToHide[i].style.visibility = Visibility;
    }
}
