$(document).ready(function(){
		

	/* Homepage picture */
	
	$(".pic_wrapper").hover(function(){
		$(".nav_box, .pic_caption").fadeTo("slow", .9); // This sets the opacity to 90% on hover
	},function(){
		$(".nav_box, .pic_caption").fadeTo("slow", 0.0); // This sets the opacity back to 0% on mouseout
	});
	
	$("#nav_thumbs img").hover(function(){
		$(this).fadeTo("fast", 1.0); // This sets the opacity to 100% on hover
		$(this).css("borderColor", "#ff9e33");
	},function(){
		$(this).fadeTo("fast", 0.5); // This sets the opacity back to 50% on mouseout
		$(this).css("borderColor", "#ffffff");
	});
	
	$("#nav_thumbs img").click(function(event) {
		event.preventDefault();
		var image = $(this).attr("href");
		var caption = $(this).attr("title");
		
		$("#pic_main, .caption").fadeOut("500", function() {
			$("#pic_main, .caption").fadeIn("500").attr("src", image);
			$(".caption").empty();
			$(".caption").append(caption);
		});

	});	
	
	/* FAMILY ALBUM */
	
	$("#fam_album img").click(function(event) {
		event.preventDefault();		
		/*clear last picture */
		$("#fam_album_overlay_pic").empty();
		active_pic = 0;
		var id = $(this).attr("rel");
		$("#blackout").css('filter', 'alpha(opacity=80)'); //fix for IE7
		$("#blackout").fadeIn("fast");
		$("#fam_album_overlay").fadeIn("slow");
		fam_album_ajax(id);
		
	});
	
	$("#fam_album img").hover(function(){
		$(this).fadeTo("fast", 1.0); // This sets the opacity to 100% on hover
		$(this).css("borderColor", "#ff9e33");
	},function(){
		$(this).fadeTo("fast", 0.9); // This sets the opacity back to 90% on mouseout
		$(this).css("borderColor", "#575757");
	});
	
	var active_pic = 0;
	var num_pics = 9;
	var viewable_pics = 1;
	
	$(".fam_album_overlay_close").click(function(event) {
		event.preventDefault();
		$("#fam_album_overlay").fadeOut("fast");
		$("#blackout").fadeOut("slow");
		/*reset fam album nav arrows on close */
		$("#left_arrow img").attr("src", "http://www.shorelineschools.org/design_elements/grey_arrow_left.png");
		$("#left_arrow img").removeClass("orange_arrow_left");
		$("#right_arrow img").attr("src", "http://www.shorelineschools.org/design_elements/orange_arrow_right.png");
		$("#right_arrow img").addClass("orange_arrow_right");
		
		
		
	});
	
	

	$("#right_arrow").click(function(event) {
		event.preventDefault();	
		if(active_pic > -1) {
			$("#left_arrow img").attr("src", "http://www.shorelineschools.org/design_elements/orange_arrow_left.png");
			$("#left_arrow img").addClass("orange_arrow_left");
		}
		
		if(active_pic == num_pics - viewable_pics -1) {
			$("#right_arrow img").attr("src", "http://www.shorelineschools.org/design_elements/grey_arrow_right.png");
			$("#right_arrow img").removeClass("orange_arrow_right");
		}
		
		if(active_pic < num_pics - viewable_pics) {
			$("#fam_album_overlay_pic ul").animate({ marginLeft : "-=322px" }, 300);
		
			active_pic++;
		}

	});
	
	$("#left_arrow").click(function(event) {
		event.preventDefault();	
		if(active_pic == num_pics - viewable_pics) { //we are at the end and heading back left; set the right arrow back to white
			$("#right_arrow img").attr("src", "http://www.shorelineschools.org/design_elements/orange_arrow_right.png");
			$("#right_arrow img").addClass("orange_arrow_right");
		}
		
		if(active_pic == 1) {
			$("#left_arrow img").attr("src", "http://www.shorelineschools.org/design_elements/grey_arrow_left.png");
			$("#left_arrow img").removeClass("orange_arrow_left");
		}
			
		if(active_pic > 0) {
			$("#fam_album_overlay_pic ul").animate({ marginLeft : "+=322px" }, 300);
		
			active_pic--;
		}
	});
	
	
	
	/* MINI CALENDAR */
	
	$(".mini_cal_numbers a").live("click", function(event) {
		event.preventDefault(); /* block the default action of the link a tag */
		var date = $(this).attr("alt"); /* retrieve date from the alt attribute of the link */
		
		cal_day_view_ajax(date);
	});	
	
	$(".back_button a").live("click", function(event) {
		event.preventDefault(); /* block the default action of the link a tag */
		var date = $(this).attr("alt"); /* retrieve eventsid from the alt attribute of the link */
		
		cal_day_view_ajax(date);					
	});

	$(".cal_day_view a").live("click", function(event) {  /*live allows this to apply to injected DOM objects such as those returned from our AJAX php scripts. */
		event.preventDefault(); /* block the default action of the link a tag */
		var the_event = $(this).attr("alt"); /* retrieve eventsid from the alt attribute of the link */
		
		cal_event_view_ajax(the_event);
	});	
	
	$(".cal_desc a").click(function(event) {
		event.preventDefault(); /* block the default action of the link a tag */
		var the_event = $(this).attr("alt"); /* retrieve eventsid from the alt attribute of the link */
		
		cal_event_view_ajax(the_event);
	});						
	
	$(".cal_overlay_close").click(function(event) {
		event.preventDefault();	
		
		cal_overlay_close();					
	});	
		
	//this is to fix the Safari "jumping" issue with jQnR on the cal overlay element. We are manually setting the top and left as a pixel value rather than the initial percent value set in the style sheet.
	//var pos = $("#cal_overlay").position();
	//$("#cal_overlay").css({ top: pos.top, left: pos.left });
	
	
	//$('#cal_overlay').jqDrag(); /* Using jqDnR pluging to allow user to move the cal_overlay box around the browser window */
	
	$("#cal_overlay").draggable({ opacity: 0.8 });
	$("#cal_overlay").css('filter', 'alpha(opacity=90)'); //opacity fix for IE
	
	//forward and back buttons for mini-calendar

	$(".mini_cal_nav_back").live("click", function(event) {
		event.preventDefault();
		
		current_month = current_month - 1;
		if(current_month < 1)
		{
			current_month = 12;
			current_year = current_year -1;
		}
		
		mini_cal_ajax(current_year, current_month);
	});
	
	 $(".mini_cal_nav_forward").live("click", function(event) {
		event.preventDefault();
		
		current_month = current_month + 1;
		
		if(current_month > 12)
		{
			current_month = 1;
			current_year = current_year +1;
		}
		
		mini_cal_ajax(current_year, current_month);
	});
	

	/* ORANGE ARROW AND SEARCH BUTTON MOUSEOVERS */
	
	$(".orange_arrow_left").live("mouseover", function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/left_arrow_over.png");
		}).live("mouseout", function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/orange_arrow_left.png");
	});
	
	$(".orange_arrow_right").live("mouseover", function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/right_arrow_over.png");
		}).live("mouseout", function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/orange_arrow_right.png");
	});	
	
	$(".orange_close_box").hover(function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/orange_close_box_over.png");
	},function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/orange_close_box.png");
	});
	
	$(".search_button").hover(function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/search_button_over.png");
	},function(){
		$(this).attr("src", "http://www.shorelineschools.org/design_elements/search_button.png");
	});
	
	$(".search_input").focus(function(){ 
		$(this).val("");
	});
});


/*MINI-CALENDAR FUNCTIONS */
	
function mini_cal_ajax(the_year, the_month) {
	$.get("http://www.shorelineschools.org/mini_cal.php", { year: the_year, month: the_month },
	function(returned_data) {
		$("#mini_cal_body").empty();
		$("#mini_cal_body").append(returned_data);
	});
}

function cal_day_view_ajax(date) {
	$.get("http://www.shorelineschools.org/cal_day_view.php", {selected_date:date},
	function(returned_data)
	{
		$("#cal_overlay_content").empty();
		$("#cal_overlay_content").append(returned_data);
	});
	$("#cal_overlay").css("visibility", "visible");
}

function cal_event_view_ajax(the_event) {
	$.get("http://www.shorelineschools.org/cal_event_view.php", {eventsid:the_event},
	function(returned_data)
	{
		$("#cal_overlay_content").empty();
		$("#cal_overlay_content").append(returned_data);
	});
	$("#cal_overlay").css("visibility", "visible");
}

/*function cal_event_view_ajax(the_event) {
	$("#cal_overlay_content").empty();
	
	language = $.cookie('lang_pref');
	$.get("cal_event_view.php", {eventsid:the_event},
	function(returned_data)
	{		
		$.translate(returned_data, language, {
		  complete: function(translation){ 
		 	 $("#cal_overlay_content").append(translation);
		  }
		});
	});
	$("#cal_overlay").css("visibility", "visible");
}*/

function cal_overlay_close() {
	$("#cal_overlay_content").empty();
	$("#cal_overlay").css("visibility", "hidden");
}

function fam_album_ajax(id) {
	$.get("http://www.shorelineschools.org/fam_album.php", {album_id:id},
	function(returned_data)
	{
		$("#fam_album_overlay_pic").empty();
		$("#fam_album_overlay_pic").append(returned_data);
		
	});
}
