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

(function($) {
	function pageload(hash) {

	  if (!hash || hash == "/") {
	    hash = "/news";
	  }

	  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?page="+_page;
	   } 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";	
	  }	
	  _url = "/"+_url;
	
	   if (checkApp()) {
	    // for IE
	    var height = document.documentElement.offsetHeight;
	   } else {
	    // for other browsers
	    var height = parseInt(window.innerHeight);
	   }

	   // set loading
	   $("#content > .content").css("display","none");
	   $("#content > .content").html("<div class=\"loading\"><h5>loading...</h5></div>");
	   $('#content > .content').attr("id","content_loading");
	
	   //setPosition();
	
	   $("#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);
		 //setPosition();
		 //
	   	 $("#content > .content").css("display","block");		
        

        // for new ajax links:
        $("a[class='ajax']").unbind("click");
		$("a[class='ajax']").click(function(){
			var hash = this.rel;
			hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
			return false;
		});

	    }
	   });

	   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() {
	// 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: function() {
    $('#mailinglistform').resetForm();                           
   }
  });

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

  //$(window).bind("resize", setPosition);

 });

})(jQuery);

function setPosition() {
	  if (checkApp()) {
	   // for IE
	   var height = document.documentElement.offsetHeight;
	  } else {
	   // for other browsers
	   var height = parseInt(window.innerHeight);
	  }
     if (height-370 > $("#content > .content").height()) {
	  var resetMargin = (height/2)-($("#content > .content").height()/2);
	  $("#content > .content").css("margin-top",resetMargin+"px");	
	 } else {
      $("#content > .content").css("margin-top","185px");
	  $("#content > .content").css("margin-bottom","65px");	
	 }
  }

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

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

function checkApp() {
 if (navigator.appName == 'Microsoft Internet Explorer') {
  return true;
 }
 return false;
}
