var _p;
var prevhash;
(function($) {

	$(document).ready(function() {
		
	 function pageload(hash) {	
	  if (!hash) {
		hash = "/nu";
	  }
	  // var vars = hash.split("/");
	  //if (prevhash != hash) {
	   prevhash = hash;
	   give(hash);
	  //}
	 }

		$("#title").css("display","none");	
		$.ajax({
			url: "files/pages/title.php",
			cache: false,
			success: function(html){
				$("#title").html(html);
				$("#title").fadeIn();

			}
		});
	
     // Initialize history plugin.
     $.history.init(pageload);

     $("a").live("click",function(){
      var hash = $(this).attr("rel");
   	  if (hash) {
   	   $.history.load(hash);
   	   return false;
      }
     });	
		
		// // page load check hash
		// if (document.location.hash) {
		// 	var hash = document.location.hash.split('#'); 
		// 	var _href = hash[1];
		// 	give(_href);
		// } else {
		// 	var _href = "/nu";
		// 	give(_href);			
		// }
		
		setBoxHeight();			
		//setAjaxLinks();		
	});

	
	function setAjaxLinks() {
		
		// $('a.ajax').unbind('click');
		// 
		// $("a.ajax").click(function(event){
		// 	event.preventDefault();
		// 	var _href = $(this).attr('rel');
		// 	document.location.hash = _href;
		// //	give(_href);
		// });

	}	

	function give(_href) {
		$("#debug > p").html("loading "+_href);		
		var hashstring = _href.split("/");
		_p = hashstring[1];
		// alert(hashstring[1]+" - "+hashstring[2]);
		// get page and set body attribute
		$("body").attr("id",_p);
		if (hashstring[2]) {
			if (_p == "films") {
				var _id = hashstring[2];
				var _url = "files/pages/page.php?p="+_p+"&id="+_id;
			} else if (_p == "nieuws") {
				var _id = hashstring[2];
				var _url = "files/pages/page.php?p="+_p+"&id="+_id;				
			} else if (_p == "archief") {
				var _q = hashstring[2];				
			} else if (_p == "verwacht" || _p == "nu") {
			   var _check = hashstring[2].substr(0,4);
			   if (_check == "page") {
				var _page = hashstring[2].substr(4,9);
				var _url = "files/pages/overview.php?p="+_p+"&page="+_page;							
			   } else {
				var _url = "files/pages/overview.php?p="+_p;							
			   }				
			}
		} else {
			// normal page
			if (_p == "nieuws") {
				var _url = "files/pages/carousel.php?p="+_p;
		    } else if (_p == "nu" || _p == "verwacht") {
				var _url = "files/pages/overview.php?p="+_p;			
			} else if (_p == "archief") {
				var _url = "files/pages/archief.php?q="+_q;
			} else {
				var _url = "files/pages/page.php?p="+_p;			
			}
		}
		
		// make ajax request
		$.ajax({
			url: _url,
			cache: false,
			success: function(html){
				$("#strip").css("visibility","hidden");
 				$("#strip").html(html);
				$("#strip").css("visibility","visible");
				// reset all ajax links
				if (_p == "nu" || _p == "verwacht" || _p == "nieuws") {
	
					
				} else if (_p == "archief") {
					
					// prepare the form when the DOM is ready  
					    // bind form using ajaxForm 
					    $('#searchform').ajaxForm({ 
					        // target identifies the element(s) to update with the server response 
					        target: '#archive-view', 

					        // success identifies the function to invoke when the server response 
					        // has been received; here we apply a fade-in effect to the new content 
					        success: function() {
					            $('#archief-view').fadeIn('slow'); 
								setBoxHeight();
					        } 
					    }); 
					
				} else if (_p == "films") {
					// title laden
				//	$("#title").css("display","none");	
				
					$.ajax({
						url: "files/pages/title.php?id="+_id,
						cache: false,
						success: function(html){
			 				$("#title").html(html);
							$("#title").fadeIn();
							// trigger lightbox
							$(".lightbox").lightbox();							
						}
					});
					// video laden
					$.ajax({
						url: "files/pages/video.php?film_id="+_id,
						cache: false,
						success: function(html){
						   // make embed code
		                   var flashvars = { video: "../videos/"+html+".flv",frame: "files/videos/"+html+".jpg", autoPlay: "false", loop: "false" };
		                   var params = { menu: "false", allowfullscreen: "true", wmode: "transparent" };
		                   var attributes = { id: "flash", name: "flash" };
		                   swfobject.embedSWF("files/swf/video.swf", "flash", "512", "288", "9.0.0","files/swf/expressInstall.swf", flashvars, params, attributes);
						}
					});
				} else if (_p == "overview") {

				}
				
				setBoxHeight();
				//setAjaxLinks();
				
			}
		});		
		
	}
	
	
	function carousel_itemLoadCallback(carousel, state)
	 {
	     // Check if the requested items already exist
	     if (carousel.has(carousel.first, carousel.last)) {
	         return;
	     }

	     jQuery.get(
	         'ajax.php',
	         {
	             first: carousel.first,
	             last: carousel.last,
				 p: _p
	         },
	         function(xml) {
	             carousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
	         },
	         'xml'
	     );
	 };

	 function carousel_itemAddCallback(carousel, first, last, xml)
	 {
	     // Set the size of the carousel
	     carousel.size(parseInt(jQuery('total', xml).text()));
	     jQuery('item', xml).each(function(i) {
	         carousel.add(first + i, carousel_getItemHTML(jQuery(this).text()));
			 //setAjaxLinks();			
	     });
	 };

	 /**
	  * Item html creation helper.
	  */
	 function carousel_getItemHTML(url)
	 {
	     return url;
	 };
	
	
		
	function setBoxHeight() {
		var width = $(window).width();
		var height = $(window).height();
		var centerHeight = $(".box-center").height();
		$(".box-left").css("height",centerHeight+"px");
		$(".box-right").css("height",centerHeight+"px");  
	}
	
	function obfuscate(coded,title) {
		cipher = "aZbYcXdWeVfUgThSiRjQkPlOmNnMoLpKqJrIsHtGuFvEwDxCyBzA1234567890"
		shift=coded.length
	 	link=""
	 	for (i=0; i<coded.length; i++) {
	  		if (cipher.indexOf(coded.charAt(i))==-1) {
	   			ltr=coded.charAt(i)
	   			link+=(ltr)
	  		} else {    
	   			ltr = (cipher.indexOf(coded.charAt(i))-shift+cipher.length) % cipher.length
	   			link+=(cipher.charAt(ltr))
	  		}				
	 	}
	 	if (coded == title) {
			title = link;
	 	}
	 	document.write("<a href='mailto:"+link+"'>"+title+"</a>");
	}

})(jQuery);