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

(function($) {

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

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

  if (vars[2] != "more" && _href != "news") {
   $(".block").css("width","300px");
   resizeWindow();
   $("#holder").animate({ opacity: 1.0 }, 300 );	
   $("#overlay").css("display","none");
   $("#overlay-holder").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) {
	// page
	// position = 0;
    // getPrevNext(position,positions,_href);	
    if (_href == "news") {
     $("#holder").animate({ opacity: 0.2 }, 300 );
   	 $("#overlay").css("display","block");	

	 $.ajax({
	  url: "files/ajax/news.php",
	  cache: false,
	  success: function(html){
	   $("#overlay-holder").html(html);
	
       // for new ajax links:
		 $("a[class='mp3']").unbind("click");	
			$("a[class='mp3']").click(function(){
		     var file = this.rel;
		     var title = this.title;
		     //alert("load: "+file+", with title: "+title);
		     loadList(file,title);
		     return false;
		    });
       $("a[class='ajax']").unbind("click");
		$("a[class='ajax']").click(function(){
			var hash = this.rel;
			hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
			return false;
		});	
	 
	   $("a[class='close']").click(function() {
	    $("#overlay").css("display","none");
        $("#overlay-holder").html("");	
	    $("#holder").animate({ opacity: 1.0 }, 300 );
	    return false;
	   });
	 
	  }
	 });

    }
   } else {
	// artist
    getPrevNext(position,positions,_href);
    $.scrollTo(position,300, { axis:'x', easing: 'easeOutQuint', onAfter: function() {
      if (vars[2] == "more") {
       // $(".block").css("width","300px");	
       // $("#holder").css("width",(total*300)+300+"px");
       // // $("#"+_href).css("width","600px");
       // $("#"+_href).animate({ width: "600px" }, 300 );





	           $("#holder").animate({ opacity: 0.2 }, 300 );
	    	   $("#overlay").css("display","block");
	    	          
	    	    $.ajax({
	    	     url: "files/ajax/artist.php?a="+_href,
	    	     cache: false,
	    	     success: function(html){
	    	      $("#overlay-holder").html(html);
	
		        // for new ajax links:
				 $("a[class='mp3']").unbind("click");	
					$("a[class='mp3']").click(function(){
				     var file = this.rel;
				     var title = this.title;
				     //alert("load: "+file+", with title: "+title);
				     loadList(file,title);
				     return false;
				    });
		        $("a[class='ajax']").unbind("click");
				$("a[class='ajax']").click(function(){
					var hash = this.rel;
					hash = hash.replace(/^.*#/, '');
					$.historyLoad(hash);
					return false;
				});
	    	    	  
	    	      $("a[class='close']").click(function() {
	    	       $("#overlay").css("display","none");
	    	       $("#overlay-holder").html("");	
	    	       $("#holder").animate({ opacity: 1.0 }, 300 );
	    	       return false;
	    	      });
	    	          
	    	     }
	    	    });
      
      }
   	 }
    });
   }
  }
  $('#'+_href).parent().parent().parent().attr("id","body_"+_href);
 }	
	
 $(document).ready(function() {
  
  // init player
  createPlayer();	


  // tweets

	var url = "/files/twitter/pipes.php";
   $.ajax({
	 url: url,
	 dataType: "html",
	 success: function(html){
	  // do stuff...
 	  $(".twitter").html(html);

	  $('.twitter').vTicker({
	   speed: 500,
	   pause: 3000,
	   showItems: 1,
	   animation: 'fade',
	   mousePause: true,
	   height: 0,
	   direction: 'up'
	  });

	 }
	});


  $(".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").css("display","none");
   } else if ($(".form").css("display") == "none") {
    $(".form").css("display","block");	
   }
   return false;
  });
	
	
  // 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
  resizeWindow();
  // function for window resize
  $(window).bind("resize", resizeWindow);

  // rollover
  $(".block").bind('mouseenter', function() {
   $(this).children().children().next().css("display","block");
  }).bind('mouseleave', function() {
   $(this).children().children().next().css("display","none");		
  });

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

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

 });

})(jQuery);

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

/* 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;
}