/* Copyright (c) 2009 Iksi.tv */

(function($) {

 function pageload(hash) {
  $(".dropdown-items").css("display","none");	

  if (!hash || hash == "/") {
	hash = "/"+first;
  }
  var vars = hash.split("/");
  var _href = vars[1];

  if (vars[2] != "more") {
   $("#overlay").css("display","none");	
  }
  // if (vars[2]) {
  // 	 var _check = vars[2].substr(0,4);
  // 	 if (_check == "page") {
  // 		// page
  // 		var _page = vars[2].substr(4,9);		
  // 		var _url = "files/load/"+_href+".php?page="+_page;
  // 	 } else {
  // 		// id
  // 		var _page = vars[3].substr(4,9);
  // 		var _url = "files/load/"+_href+".php?serie="+vars[2]+"&page="+_page		
  // 	 }
  // 
  //   $.ajax({
  //    url: _url,
  //    cache: false,
  //    success: function(html){
  // 	  $("#"+_href+" > .holder > .body > .content").html(html);
  //    }
  //   });
  // }

  // scroll to the right position on the page
  //if (!vars[3]) {
   // not while browsing photos

   // array with names and positions look up name to get position and scroll, only when in array, else show page

   var position = getPosition(_href,positions);
   if (!position) {
	position = 0;
   }
   getPrevNext(position,positions,_href);
//   $.scrollTo(position+"px",400);
   $.scrollTo(position,600, { axis:'x', easing: 'easeOutQuint'});

   // $.scrollTo(position,600, { axis:'x', easing: 'easeOutQuint', onAfter: function() {
   // 	  if (vars[2] == "more") {
   // 	   $("#overlay").css("display","block");
   // 	  }
   // } });


  //}
  // $("#content").css("display","none");
  //   $.ajax({
  //    url: "files/pages/"+_href+".php",
  //    cache: false,
  //    success: function(html){
  //     $("#content").html(html);
  //     $("#content").css("display","block");                   			  
  //    }
  //   });  				    
  // document.title = " _ " + hash;
	$('#'+_href).parent().parent().parent().attr("id","body_"+_href);
	
	// $('a.back').unbind('click');	
	// $("a.back").click(function(event){
	//  	event.preventDefault();		
	// 	alert("back!");
	// });
	
 }	
	
 $(document).ready(function() {
	
	$("a[class='close']").click(function() {
     $("#overlay").css("display","none");
     return false;
	});
	
    // init player
    // createPlayer();	

	$(".dropdown").click(function(){
     // 
     if ($(".dropdown-items").css("display") == "none") {
	     $(".dropdown-items").css("display","block");
     } else if ($(".dropdown-items").css("display") == "block") {
	     $(".dropdown-items").css("display","none");
     }
     return false;
	});
	
	$("a[class='toggle']").click(function(){
		 // 
	     if ($(".form").css("display") == "block") {
	      //$(".form").slideUp("normal");	
	      $(".form").css("display","none");
	     } else if ($(".form").css("display") == "none") {
	      //$(".form").slideDown("normal");
	      $(".form").css("display","block");	
	     }
	     return false;
		});
	
	
  // Initialize history plugin.
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "jquery_history.html");
	
	// set onlick event for buttons
	$("a[class='ajax']").click(function(){
		// 
		var hash = this.rel;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});	
	
  // on load set widths for page
  if (checkApp()) {
   // for IE
   var width = document.body.offsetWidth;
   var width = document.documentElement.offsetWidth;
  } else {
   // for other browsers
   var width = parseInt(window.innerWidth);
  }

//  $("#logo").css("marginLeft",((width/2)-($("#logo").width() /2))+"px");
  $("#holder").css("width",(total*300)+"px");
  $("#content").css("width",((total*300)+width-300)+"px");

  $(window).bind("resize", function(){
   if (checkApp()) {
    // for IE
    var width = document.body.offsetWidth;
    var width = document.documentElement.offsetWidth;
   } else {
    // for other browsers
    var width = parseInt(window.innerWidth);
   }
   $("#content").css("width",((total*300)+width-300)+"px");	
   $("#debug").html(width);
  });

  // rollover
  $(".block").bind('mouseenter', function() {
   //$(this).addClass('over');
		//    $(this).children().children().children().animate({
		// 	width: '300px', /* Set new width */
		// 	height: '400px', /* Set new height */
		// }, 0);
		$(this).children().children().next().css("display","block");
  }).bind('mouseleave', function() {
   //$(this).removeClass('over');
		//    $(this).children().children().children().animate({
		// 	width: '270px', /* Set new width */
		// 	height: '360px', /* Set new height */
		// }, 0);
		$(this).children().children().next().css("display","none");		
  });


  $('#mailinglistform').ajaxForm( {
   target: '#ml_feedback',
   success: function() {
    $('#mailinglistform').resetForm();                           
   }
  }); 

  // init
  // default beginpage or google link
  var _hash = window.location.toString().split("thrilleragency.nl");
  _hash = _hash[1].replace(/^.*#/, '');
  pageload(_hash);


 });

})(jQuery);

/* in_array function */
function getPrevNext(needle,haystack,current) {
 var nextPos = parseInt(needle)+300;
 var prevPos = parseInt(needle)-300;
 var next;
 var prev;
 //
 var key = "";
 for (key in haystack) {
  if (haystack[key] == nextPos) {
   next = key;
  }
  if (haystack[key] == prevPos) {
   prev = key;
  }
 }
 // check undefined links
 if (!next) { next = current; }
 if (!prev) { prev = current; }
 // set link attributes
 $("#menu_next").attr("rel","/"+next);
 $("#menu_next").attr("href",next);
 $("#menu_prev").attr("rel","/"+prev);
 $("#menu_prev").attr("href",prev);
}

function getPosition (needle,haystack) {
 var key = "";
 for (key in haystack) {
  if (key == needle) {
   return haystack[key];
  }
 }
 return false;
}

// clear / restore
function clearDefault(el) {
 if (el.defaultValue==el.value) el.value = "";
}

function putDefault(el) {
 if (el.value=="") el.value = el.defaultValue;
}