function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
} 





$(document).ready(function(){
	if ( $.browser.msie  && $.browser.version <= 6 ) { var IE6 = true; } else { var IE6 = false; }
	$("ul.subnav").parent().append("<span class='ui-icon ui-icon-triangle-1-s dropdown' style='margin-top: 16;'></span>"); 

	var page = getQueryVariable("page");
	if ( page ) {
		$('#content').load(page+".php",{},tailorPage(IE6));
		$("#pageUrl").html("<a href='/?page="+page+"#'><img src='images/external_link.png' alt='Link for this page!'/></a>");		
	} else {
		$('#content').load('welcome.php',{},tailorPage(IE6));
		$("#pageUrl").html("<a class='link' href='/?page=welcome#'><img src='images/external_link.png' alt='Link for this page!'/></a>");
	}

	$("ul.topnav li span, ul.topnav li a.subnav").click(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		//When the mouse hovers out of the subnav, move it back up
		$(this).parent().hover(function() {}, function(){$(this).parent().find("ul.subnav").slideUp('slow'); });

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { $(this).addClass("subhover"); }, //On hover over, add class "subhover"
			 function() { $(this).removeClass("subhover"); }); //On hover out, remove class "subhover"

    
         $('#menu a:not([class])').click(function() { //start function when any link is clicked
		$("#content").hide("fast");
		var pageUrl = $(this).attr("title"); //retrieve title of link so we can compare with php file
                $.ajax({
			method: "get",url: pageUrl + ".php",
			dataType: "html",
			beforeSend: function(){$("#loading").show("fast");}, //show loading just when link is clicked
			complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
					$("#content").html(html); //show the html inside .content div
					$("#content").show("slow"); //animation
					$("#pageUrl").html("<a class='link' href='/?page="+pageUrl+"#'><img src='images/external_link.png' alt='Link for this page!' onmouseover='<a doTooltip(event,\'The Link for this page\')' /></a>");
					tailorPage(IE6); 
					}
		}); //close $.ajax(
		
	}); //close click(


	
	
	$("span.dropdown").vAlign();
     }); //close $(

function tailorPage(IE6) {
	if ( $('.slideshow').length > 0 ) {$('.slideshow').cycle({ fx: 'fade'}); }
}
   
(function ($) {
	$.fn.vAlign = function(container) {
	        return this.each(function(i){
		   if(container == null) {
		      container = 'div';
		   }
		   var paddingPx = 10; //change this value as you need (It is the extra height for the parent element)
		   $(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
		   var el = $(this).children(container + ":first");
		   var elh = $(el).height(); //new element height
		   var ph = $(this).height(); //parent height
		   if(elh > ph) { //if new element height is larger apply this to parent
		       $(this).height(elh + paddingPx);
		       ph = elh + paddingPx;
		   }
		   var nh = (ph - elh) / 2; //new margin to apply
		   $(el).css('margin-top', nh);
	        });
	     };
})(jQuery);
