/* Copyright (c) 2009 Jurriaan Topper (iksi.tv) */


(function($) {

 function pageload(hash) {
	
	$("#debug").html(hash);
		
  if (!hash || hash == "/" || hash == "/twindex.php") {
   hash = "/news";
  }

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

  $(".submenu").css("display","none");	
  $("#menu").css("height",$("#menu").css("lineHeight"));

  // main items
  $(".ajax").removeClass("active"); // reset
  $("#menu_"+_href).addClass("active"); // activate

  // open submenus on deeplink
  var mainPage = getMainPage(_href,subPages);
  if (mainPage != false) {
   if (mainPage != "projects") {
   $("#"+mainPage+"_submenu").css("display","block");
   //$("#"+mainPage+"_submenu").slideDown("normal");
   $("#menu").css("height",$("#"+mainPage+"_submenu").height()+20+"px");
   $(".ajax").removeClass("active"); // reset
   // set active mainPage and subPage
   $("#menu_"+mainPage).addClass("active"); // activate
   $("#menu_"+_href).addClass("active"); // activate
   } else { // humpf
 	   $("#"+"discography"+"_submenu").css("display","block");
	   //$("#"+mainPage+"_submenu").slideDown("normal");
	   $("#menu").css("height",$("#"+"discography"+"_submenu").height()+20+"px");
	   $(".ajax").removeClass("active"); // reset
	   // set active mainPage and subPage
	   $("#menu_"+"discography").addClass("active"); // activate
	   $("#menu_"+_href).addClass("active"); // activate   	
   }
   if (mainPage == "projects" || mainPage == "discography") {
    vars[3] = vars[2];
    vars[2] = "cat_"+_href;	
    _href = mainPage;
   }
  }
  //

	   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?page="+_page;
	    } else {
	     var _check = vars[2].substr(0,4);	
		 if (_check == "cat_") {
		  // category
		  var _url = "files/ajax/"+_href+".php?cat="+vars[2].substr(4);
		 } else {
		  // id
		  var _url = "files/ajax/"+_href+".php?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+"&id="+vars[3];
	     }		
	    }

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

	   // set loading
	   $("#content > .content").css("display","none");
	   $("#content > .content").html("loading...");
	   $('#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);  
		 // $("#content > .content").fadeIn(300);
	   	 $("#content > .content").css("display","block");
	
		// post-submit callback 
		function showResponse(responseText, statusText)  { 
		    if (responseText.length > 5) {
		     $('#shout_feedback').html(responseText);
		    } else {
		     $('#shoutform').resetForm();	
		     pageload("/guestbook");
		    }
		}
	
		if (_href == "guestbook") {
		  $('#shoutform').ajaxForm( {
			url: 'files/ajax/post_guestbook.php',
			target: '#shout_feedback',
            success: showResponse // function() { 
			// 		     $('#shoutform').resetForm();
			//      		 pageload("/guestbook");
			// 			}
		  });	
		}	
		
		 // for new ajax & mp3 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;
			});

		// var queryString; 
		// function showRequest(formData, jqForm, options) { 
		// 	     queryString = $.param(formData); 
		// 	     return true; 
		// }

	    $('#pagerform').ajaxForm( {
	     //beforeSubmit:  showRequest,
	     success: function() {
	      var page = queryString.split("=");	
		  $.history.load("/news/page"+page[1]);
	      //$('#pagerform').resetForm();                           
	     }
	    });

	    }
	   });

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

	   // set body id
	   $('#wrapper').parent().attr("id",_href);
	   
	}
 $(document).ready(function() {
	
  // init player
  createPlayer();
	
	$("a[class='mp3']").click(function(){
     var file = this.rel;
     var title = this.title;
     //alert(file+" "+title);
     loadList(file,title);
     return false;
    });

	$("a[class='toggle-mailinglist']").click(function(){
	 // 
     if ($("#mailinglist").css("display") == "block") {
      $("#mailinglist").slideUp("normal");	
      //$("#mailinglist").css("display","none");
     } else if ($("#mailinglist").css("display") == "none") {
      $("#mailinglist").slideDown("normal");
      //$("#mailinglist").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;
	});

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

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

 });

})(jQuery);

function showResponse(responseText, statusText)  { 
  if (responseText == "check your email and confirm your subscription") {
   $('#mailinglistform').resetForm();
  }
}

/* in_array function */
function getMainPage (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;
}


