$(document).ready(function(){
   			
	//Start sitemap toggle
	$('#map').hide();
	$('#sitemap, .close').click(function () {
		$('#map').slideToggle('slow');
	});
	
	//Turn on top menu options
	$('ul.topmenu').superfish({
		animation:			{height:'show'},
		autoArrows:		false
	}); 
	
	//Listen for logout click
	if($('#logout')){
		$('a[id="logout"]').click(function(){
			document.cookie = "USERNAME=;path=/;expires=; domain=.pspt.ca";
			document.cookie = "PASSWORD=;path=/;expires=; domain=.pspt.ca";
			location.replace("http://www.pspt.ca/login?m=logout");
		});
	}
	
	//Tabular data row highlights (and update on ajax complete)
	$(".data tr:even:not(.headers)").css("background-color", "#EFEFEF");
	$(".data-entry td:even").css("fontWeight", "bold");
	
	$("body").ajaxComplete(function(){
		$(".data tr:even:not(.headers)").css("background-color", "#EFEFEF");
		$(".data-entry td:even").css("fontWeight", "bold");
	});
	
	//Update Checkbox Hidden Elements for parent #checkbox (usually tr)
	$('#checkbox :checkbox').click(function(){
		if( $(this).attr('checked') == true ) set = 1;
		if( $(this).attr('checked') == false ) set = 0;	
		$(this).next().val(set);
	});
		
});

//Message
showMessage = function(message, status){
	
	var bgcolor;
	if(status==0){
		bgcolor = '#990000';
	} else {
		bgcolor = '#88A649';
	}
	
	$('#message').show().animate({ 
		opacity: 1, 
		'backgroundColor' : bgcolor 
	}, 1000 ).html(message);

}

//Media Functions
$.media = function(galleryId, table){
	$(".thumb").click(function(){
		if(confirm("Are you sure you want to remove photo " + $(this).find('div.img').attr("id") + " permantently?")==true){
			
			$(this).load('../../uploadify/deletePhotos.php', { 'galleryId': galleryId, 'id': $(this).attr("id"), 'file': $(this).find('div.img').attr("id") }, function(responseText, textStatus, XMLHttpRequest){
				if(responseText==1){
					$(this).remove();
					if( $(".thumb").size()==0 && $(".preview").size()==0 ) $("#media").remove();
				} else {
					$(this).remove();
					alert("There were one or more errors in removing this photo. The file could still be in the datebase or stored on the server. Errors are below:\n\n" + responseText.replace(/(<([^>]+)>)/ig,""));
				}
			});
			
		}
			
	});
	
	$(".videodelete").click(function(){
		if(confirm("Are you sure you want to remove this video permantently?")==true){
			$(this).load($(this).attr('href'), { 'galleryId': galleryId, 'table': table }, function(responseText, textStatus, XMLHttpRequest){
				if(responseText==1){
					$(".preview").remove();
					$(this).parents("tr").remove();
					if( $(".thumb").size()==0 && $(".preview").size()==0 ) $("#media").remove();
					$("#filename").attr("value", "");
				} else {
					alert("There were one or more errors in removing this video:\n\n" + responseText.replace(/(<([^>]+)>)/ig,""));
				}
			});
		}
		return false;
	});
	
	$(".media").click(function(){
		$("#ajax").load( $(this).attr("href"), {'galleryId': galleryId}, '' );
		return false;
	});
	
	$("a.preview").click(function(){
		$(".wrapper").before('<div id="overlay" style="width:' + $(this).attr("id") +'px"><span></span></div>');
	}).overlay({ 
		onBeforeLoad: function() { 
			$("#overlay span").addClass("ajax-loader");
		},
		onLoad: function(content, link) { 
			content.find("span").load(link.attr("href"));
			this.ajaxComplete(function(){ 
				$("#overlay span").removeClass("ajax-loader"); 
			});
		},
		onClose: function(){
			$("#overlay").remove();
		}
	});
}

//Preload the menu images
jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}