	
$(document).ready( function() {
	///button clicks
	$("#nav-bar-home_")
		.hover( function() {
			$("#nav_bar_home").fadeIn("fast");
		}, function() {
			$("#nav_bar_home").fadeOut("fast");
	});
	$("#nav-bar-profile_")
		.hover( function() {
			$("#nav_bar_profile").fadeIn("fast");
		}, function() {
			$("#nav_bar_profile").fadeOut("fast");
	});
	$("#nav-bar-services_")
		.hover( function() {
			$("#nav_bar_services").fadeIn("fast");
		}, function() {
			$("#nav_bar_services").fadeOut("fast");
	});
	$("#nav-bar-facilities_")
		.hover( function() {
			$("#nav_bar_facilities").fadeIn("fast");
		}, function() {
			$("#nav_bar_facilities").fadeOut("fast");
	});
	$("#nav-bar-news_")
		.hover( function() {
			$("#nav_bar_news").fadeIn("fast");
		}, function() {
			$("#nav_bar_news").fadeOut("fast");
	});
	$("#nav-bar-gallery_")
		.hover( function() {
			$("#nav_bar_gallery").fadeIn("fast");
		}, function() {
			$("#nav_bar_gallery").fadeOut("fast");
	});
	$("#nav-bar-contact_")
		.hover( function() {
			$("#nav_bar_contact").fadeIn("fast");
		}, function() {
			$("#nav_bar_contact").fadeOut("fast");
	});
	//end button clicks
	
	//fancybox
	$("a.fancyBox").fancybox();
	
	
	//history
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "English.php");
	
	// set onlick event for buttons
	$("a[rel='history']").click(function(){
		// 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});
});



//show reflection
function showify(id) {	
	document.getElementById(id).style.display='none';
}

//hide reflection
function hidify(id) {	
	document.getElementById(id).style.display='block';
}


// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// alert("pageload: " + hash);
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like едц.
			hash = encodeURIComponent(hash);
		}
		
		//event to be fired
		//$("#load").load(hash + ".html");	
		firePage(hash);
	} else {
		// start page
		//$("#content").empty();
	}
}


function firePage(page) {  
	if(page == null || page == '' || page == 'home') {
		$('#content').fadeOut('slow')
		return false;
	}
	
	$('#myajaxcontent').html(""); //so that content from the old section doesn't momentarily show up when clicking on a new section's link
	//__***__
	$('#content').hide();	
	
	$("#loader").show();
	$("#content").fadeIn("slow", function() {
		
		
		//moved to here from __***__
		$('#content').css('opacity', '0.9');
	
		//url = page + "_content.html";
			
		//get content data to show
		$.get("actions/ContentManagerAction.php", {method_name : 'firePage', link_id : page},
			  function(data, textStatus) {
			$("#myajaxcontent").html(data);
			
			//edit link for admins
			$.get("actions/AppleFitnessAction.php", {method_name : "isLoggedIn"},
				  function(data) {
				  	if(data == "1") { //if logged in
					
						if(page.indexOf('newsReel') != -1) //if it's the news page
							$("#myajaxcontent").prepend("<div style='padding-top:5px;float:right'><a href='AddNews.php'><b>Add News</b></a>&nbsp;</div>");
						else
							$("#myajaxcontent").prepend("<div style='padding-top:5px;float:right'><a href='PageEditor.php?link_id=" + page + "' rel='history'><b>Edit Page</b></a>&nbsp;</div>");
					}
					
					//start ___***___
					if(page.indexOf('newsItem') != -1) { //if it's a newsItem
						$("#myajaxcontent").prepend("<div style='padding-top:5px;float:right'><a href='#newsReel' rel='history' onclick=\"$.historyLoad(this.href.replace(/^.*#/, ''))\"><b>Back to News</b></a></div>");
					}
			
					//add the 'X' to close the box
					$("#myajaxcontent").prepend("<div style='cursor:pointer;float:right;margin-top:5px;margin-left:9px' onclick='javascript:$(\"#content\").fadeOut(\"slow\")' onmouseover='$(this).css(\"text-decoration\", \"underline\")' onmouseout='$(this).css(\"text-decoration\", \"none\")'>x</div>");
					
					//for fancybox images
					$("a.fancyBox").fancybox();
					
					//regenerate scrollers
					var myDiv=document.getElementById("mycustomscroll");
					if(myDiv!=null&&myDiv.scrollUpdate) myDiv.scrollUpdate();
					
					$("#loader").fadeOut("slow");
					//end ___***___
				  }, "text");
			
			//___***___ was here
			
		},"html");
	});
	//end get content data to show
	

	return false;
}


function loadMainPageNews(num_posts_) {
	$.get("actions/ContentManagerAction.php", {method_name : 'loadMainPageNews', num_posts : num_posts_},
			function(data) {
				$("#latest_news").html(data);
	},"html");
}


var labels = new Array();
labels[1] = "APPLE FITNESS PLUS";
labels[2] = "RECEPTION AREA";
labels[3] = "CARDIO HALL";
labels[4] = "SWIMMING POOL";
labels[5] = "LOCKER ROOM";

function changeHiResPicture(nextOrPrev) {
	var flag = 0;
	var i = $("#main_image").attr("alt");
	
	if(nextOrPrev == "next")
		i++;
	else if(nextOrPrev == "prev")
		i--;
	
	//edge cases
	if(i > 5)   i = 1;
	else if(i < 1)   i = 5;
		
	$("#main_image").attr("alt", i);
	
	
	setTimeout(function() {
		if(flag != 1)		
			$("#loading").show();
	}, 5);
	
	url = "images/main_page/" + i  + ".jpg";

	
		
	var img = new Image();
	$('#loader').show();
	$('#hires_nav_bar_label').hide();
	
	//fade out the current image, then set our Image object and replace the main_image's url with the new one
	$('#main_image').fadeOut("slow", function() {
		$(img).attr('src', url);
		$(img).hide();
		$('#main_image').attr('src', url);		
	});
	
	//then wait until the image has finished loading before fading everything in
	$(img).load(function () {
		//alert("<img src='" + $(img).attr("src") + "' alt='"+ i +"' />");
		//$('#main_image').attr("src",$(img).attr("src"));
		setTimeout(function() {
			$('#main_image').fadeIn("slow");
			$('#loader').hide();
			$('#hires_nav_bar_label').hide();
			$('#hires_nav_bar_label').html(labels[i]);
			$('#hires_nav_bar_label').fadeIn("slow");
		}, 2000);
		//alert("3");
	});
	  	
	
	return false;
}


function expandNewsItem(newsItem) {
	/* $("#" + i).animate({ 
        height: "200px"
      }, 1500 );*/
	
	
	
	$("#loading").fadeIn("fast");
		
	$.get(newsItem + "content.html", '',function(data, textStatus) {
		$("#myajaxcontent").hide();
		$("#myajaxcontent").html(data);
		$("#myajaxcontent").prepend("<div style='padding-top:5px;float:right'><a href='#newsReel' rel='history'><b>Back to News</b></a> | <a href='#' onclick='alert(\'this function is currently under development\');return false;'><b>Print this Page</b></a></div>");
		$("#myajaxcontent").fadeIn("slow");
				
		//for fancybox images
		$("a.fancyBox").fancybox();
		
		var myDiv=document.getElementById("mycustomscroll");
		if(myDiv!=null&&myDiv.scrollUpdate) myDiv.scrollUpdate();
		
		$("#loading").fadeOut("slow");
		flag = 1;
	},"html");
		
}


function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

