/*
 * Copyright (c) 2009 Jurriaan Topper (iksi.tv)
 *
 * Version: 0.0.3
 */
	
(function($) {
	
	function pageload(hash) {
	  if (!hash || hash == "/") {
	    hash = "/home";
	  }

	  var vars = hash.split("/");
	  var _href = vars[1];

	  if (vars[2]) {
	   // second variable
	   var _check = vars[2].substr(0,4);
	   if (_check == "page") {
	    // page
	    var _page = vars[2].substr(4,9);		
	    var _url = "files/ajax/"+_href+".php?p="+_href+"&page="+_page;
	   } else {
		 // id
		 var _url = "files/ajax/"+_href+".php?p="+_href+"&id="+vars[2];		
	   }

	   if (vars[3]) {
	    //_url = _url+"&item=100";
	    var _check = vars[3].substr(0,4);
	    if (_check == "page") {
	     // page
	     var _page = vars[3].substr(4,9);		
	     var _url = _url+"&page="+_page;
	    } else {
	      var _url = _url+"&i="+vars[3];
	    }
	   }

	  } else {
	   // just the page
	   var _url = "files/ajax/"+_href+".php?p="+_href;	
	  }	

	   // set loading
	   $("#content > .content").css("display","none");
	   $("#content > .content").html("<div class=\"loading\">loading...</div>");
	   $('#content > .content').attr("id","content_loading");
	   $("#content > .content").css("display","block");

	   $.ajax({
	    url: _url,
	    cache: false,
	    success: function(html){
		 // set content
	   	 $("#content > .content").css("display","none");	
	     $("#content > .content").html(html);
	   	 $('#content > .content').attr("id","content_"+_href);  

		// for new ajax links:
		 $("a[class='mp3']").unbind("click");	
			$("a[class='mp3']").click(function(){
		     var file = this.rel;
		     var title = this.title;
             // make ajax request to set database and after succes loadlist
	         $.ajax({
	          url: "files/ajax/listen.php?f="+file,
	          cache: false,
	          success: function(html){
    		   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;
	     });
	
	     if (_href == "contact") {
          $('#contactform').ajaxForm( {
		   target: '#contact_feedback',
		   url: 'files/ajax/contactform.php',
		   success: function() {
			if ($("#contact_feedback").children().attr("class") != "error") {
		     $('#contactform').resetForm();
	        }
		   }
		  });
	     }
         // make content visible
	   	 $("#content > .content").css("display","block");		

	    }
	   });

	   var parts = document.title.split(" - ");
	   var siteTitle = parts[0];
	   // set title
	   var pageTitle = _href.substr(0,1).toUpperCase()+_href.substr(1);
	   var documentTitle = document.title.toString().split(" / ");
	   if (pageTitle != "Home") {
	    document.title = documentTitle[0]+" / "+pageTitle;	
	   } else {
        document.title = documentTitle[0];
	   }

	   // set body id
	   $('#wrapper').parent().attr("id",_href);
	 }
	
$(document).ready(function() {
  
  // init player
  createPlayer();
  // set twitter update  
  function updateTweet(){
   // $('#aside').load('/files/ajax/twitter.php');
  }
  window.setInterval(updateTweet, 3000 );

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


 $('#searchform').ajaxForm(
  { beforeSubmit: showRequest }
 );

 // pre-submit callback 
 function showRequest(formData, jqForm, options) { 
  var form = jqForm[0];	
  var reg = /\s+/;
  var query = form.search_query.value
  if (form.search_query.value) {	
   $('#searchform').resetForm();	
   $.historyLoad("/search/"+query.replace(reg,'+'));
  }
  return false; 
 }

 $('#mailinglistform').ajaxForm( {
  target: '#mailinglist_feedback',
  success: function() { 
   $('.hidden').css('display','block');
   if ($("#mailinglist_feedback").children().attr("class") != "error") {
    $('#mailinglistform').resetForm();                           
   }
  }
 });

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

});

})(jQuery);

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

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