$(document).ready(function() {	
	jqslides_init();
	
	loadSpotlight(2);
	loadSuperMsg();
	
	addTabListeners();
	addSpotlightListeners();
	$(".osx").click();
});

// Add all listeners for any tabs on the frontpage
function addTabListeners() {
	$(".tabbed").children().not("ul").not(".current").hide();
	
	$(".tabbed>ul>li>a").live("click", function() {
		var mainParent = $(this).parents(".tabbed");
		var toShow = $("." + $(this).attr("id"));
		
		$(".current", mainParent).removeClass("current");
		$(this).parent().addClass("current"); 
		
		if(toShow.length > 0) {
			mainParent.children().not("ul").hide();
			toShow.show();
		}
	});
}


jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
}; 



function reveal(xlink){

         $("#reveal").fadeToggle("2") 

};





/*------- Banner Slide Handlers -------*/
	var controls_median = 405;
	var dot_size = 9;
	var slide_count = $('#slides li').size();
	var centered_control = controls_median - (dot_size * slide_count);
	
drawerMode =0;
var slideAnimate = true;
var interval;



function jqslides_init(){
	
	$('#slides').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 8000,
        pager:  '#c1-nav',
		prev:	'.panel .prev',
		next:	'.panel .next',
        before: function() { if (window.console) console.log(this.src); }
    });
	
	var controls_median = 405;
	var dot_size = 9;
	var slide_count = $('#slides li').size();
	var centered_control = controls_median - (dot_size * slide_count);
	
	$("#c1-nav").css("left",centered_control +"px");
	

	$('.panel').hover(
	   function() { 
	   $('#controls').fadeIn(function(){$('#slides').cycle('pause');}); 
	   $('.next').animate({width: '40px'}, 400);
	   $('.prev').animate({width: '40px'}, 400);
	   },
       function() { 
	   $('#controls').fadeOut(function(){$('#slides').cycle('resume');}); 
	   $('.next').animate({width: '0px'}, 100);
	   $('.prev').animate({width: '0px'}, 100);
	   }
    );	
	
	
	


	
	
	
}
	

	

	
	


function drawer_open(dr){
	if(dr == 0){
		drawerMode = dr;
		$(".tab").hide();
		$(".drawer").show();
		$(".drawerContent").show();
		$(".drawerContent2").hide();
		$(".drawer").css({height:"35px"});
	    $(".drawer").animate( { height:"300px" }, { queue:false, duration:700 } );
	}else{
		drawerMode = dr;
		$(".tab").hide();
		$(".drawer").show();
		$(".drawerContent2").show();
		$(".drawerContent").hide();
		$(".drawer").css({height:"35px"});
	    $(".drawer").animate( { height:"170px" }, { queue:false, duration:700 } );
	}
}

function drawer_close(mode){
	if(drawerMode == 0) {
		oppMode = 1;
	}else{
		oppMode = 0;
	}
	
	if(drawerMode == mode) {
		$(".drawer").animate({ height:"35px"}, 500, "linear", function() {
			$(".tab").show();
			$(".drawer").hide();
		});
	} else {
		$(".drawer").animate({ height:"35px"}, 500, "linear", function() {
			$(".tab").show();
			$(".drawer").hide();
			drawer_open(oppMode);
		});
	}
}

var slidemouse = 0;
/*------- End Banner Slide Handlers -------*/


/*------- Superintendent's Msg Handlers -------*/

// loads the superintendent's message through JSON
function loadSuperMsg() {
	$.getJSON("/trustees/superintendent/message/?do=getJSON", function(msg) { $(".superintendent").html("<img src=\"/trustees/superintendent/assets/img/super_big.jpg\" /><h2>" + msg["date"] + "</h2><p>" + msg["textPreview"] + "</p><a class=\"more\" href=\"/trustees/superintendent/message/\">More</a>"); });
}

/*------- End Superintendent's Msg Handlers -------*/


/*------- Spotlight Handlers -------*/

// Adds listeners to Spotlight Prev and Next buttons.
function addSpotlightListeners() {
	$(".prevSpotlightBtn").addClass("disabled").click(function() { if($(this).hasClass("disabled") == false) { prevSpotlight(); $(".nextSpotlightBtn").removeClass("disabled"); } });
	$(".nextSpotlightBtn").click(function() { if($(this).hasClass("disabled") == false) { nextSpotlight(); $(".prevSpotlightBtn").removeClass("disabled"); } });
}

// Loads the specified number of spotlight stories.
// calls drawSpotlight method for each story.
function loadSpotlight(numToLoad) {
	if(!numToLoad) {
		var numToLoad = 1;
	}
	
	var offset = $(".spotlight .story").length;
	$.getJSON("/newsroom/spotlight/?do=getNewStoriesJSON&o=" + offset + "&c=" + numToLoad, function(msg) { $.each(msg, function(k, v) { drawSpotlight(v); }); if(offset == 0) { $(".spotlight .story:first").show(); } }).error(function() { $(".nextSpotlightBtn").addClass("disabled"); });
}

// Hides current spotlight story and shows the previous one.
function prevSpotlight() {
	var curStory = $(".spotlight .story:visible");
	var prevStory = curStory.prev(".story");
	
	if(prevStory.length > 0) {
		curStory.hide();
		prevStory.show();
	}
	
	if(prevStory.prev(".story").length == 0) {
		$(".prevSpotlightBtn").addClass("disabled");
	}
}

// Hides the current spotlight story and shows the next one.
// Keeps 1 story loaded in front of the current one at all times.
function nextSpotlight(numToLoad) {
	var curStory = $(".spotlight .story:visible");
	var nextStory = curStory.next(".story");
	
	if(curStory.next(".story").length > 0) {
		curStory.hide();
		nextStory.show();
	}

	if(nextStory.next(".story").length == 0) {
		loadSpotlight();
	}
}

// accepts a JSON object and uses it to append the story to the spotlight element.
// All stories drawn this way are hidden.
function drawSpotlight(story) {
	var s = $("<div class='story'></div>");
	if(story["mainImage"]["path"]) {
		s.append("<img src=\"" + story["mainImage"]["path"] + "\" />");
	}
	s.append("<h2><a href='" + story["url"] + "'>" + story["title"] + "</a></h2>");
	s.append("<p>" + story["textPreview"] + "</p>");
	s.append("<a href='" + story["url"] + "' class=\"more\">More</a>");
	s.hide();
	$(".spotlight").append(s);
}
/*------- End Spotlight Handlers -------*/


/*------- Gallery Handlers -------*/ 
var bGalleryAnimating = false;

// move the gallery one slideshow to the left or right
function scrollGallery(galleryEle, direction) {
	if(bGalleryAnimating == true)
		return;
		
	var scrollAmount = 206;
	var stacks = $(".stack", galleryEle);
	bGalleryAnimating = true;
	
	if(direction == "left") {
		var lastStack = $(stacks[stacks.length-1]);
		var parent = stacks.parent();
		var newStack = lastStack.clone();
		
		parent.css("margin-left", "-206px");
		parent.prepend(newStack);
		addGalleryListeners($(".stackover", newStack));
		$(".stack").animate({"left": "+=" + scrollAmount + "px"}, 500, function() { lastStack.remove(); $(".stack").attr("style", ""); parent.css("margin-left", ""); bGalleryAnimating = false; });
	} else {
		var firstStack = $(stacks[0]);
		var newStack = firstStack.clone();
		
		stacks.parent().append(newStack);
		addGalleryListeners($(".stackover", newStack));
		$(".stack").animate({"left": "-=" + scrollAmount + "px"}, 500, function() { firstStack.remove(); $(".stack").attr("style", ""); bGalleryAnimating = false; });
	}
}

function drawGallery(galleryEle) {
	var ele = galleryEle;
	
	var gallery = $("<div class='gallery'><h1>Media</h1></div>");
	var stacks = $("<div class='stacks'></div>");
	var stackHolder = $("<div></div>");
	gallery.append("<a class='left' href='Javascript: void(0);'>left</a>");
	gallery.append(stacks);
	stacks.append(stackHolder);
	gallery.append("<a class='right' href='Javascript: void(0);'>right</a>");
	
	ele.before(gallery);
	ele.remove();
	
	$(".left", gallery).click(function() { scrollGallery($(this).parent(), "left"); });
	$(".right", gallery).click(function() { scrollGallery($(this).parent(), "right"); });
	
	return stackHolder;
}

function drawSlideshow(gallery, ss) {
	var slideshow = $("<div class='stack'></div>");
	var stackover = $("<a class=\"stackover\" href=\"" + ss["url"] + "\"></a>");
	slideshow.append(stackover);
	
	$.each(ss["images"], function(key, val){
		if(key == 0 && ss["desc"].length > 0) {
			return true; // continue
		}
		
		stackover.append("<img src=\"" + ss["path"] + "thumbs/" + val + "\" />");
	});
	
	gallery.append(slideshow);
	
	gallery.width(gallery.width() + 206);
	$("img:first", slideshow).addClass("top");
	addGalleryListeners(stackover);
}

// Adds listeners to the gallery stacks for image changes on mouseover.
function addGalleryListeners(ss) {
	$(ss).mousemove(function(e) {
		var parnt = $(this).offset().left;
		var x = e.pageX - $(this).offset().left;
		var myImages = $("img", $(this));
		var step = 111 / myImages.length;
		var curstep = x / step;
		$('.debug').html("X: " + x + " , step =" + Math.round(curstep) ); 
		
		
		$(".top", $(this)).removeClass("top");
		myImages.eq(curstep).addClass("top");
	});
	/* Removed due to IE's incompatability with live().
	$(".stack .stackover").live("mousemove", function(e) {
		var parnt = $(this).parent().offset().left
		var x = e.pageX - this.offsetLeft - parnt;
		var myImages = $("img", $(this).parent());
		var step = 111 / myImages.length;
		var curstep = x / step;
		$('.debug').html("X: " + x + " , step =" + Math.round(curstep) ); 
		
		
		$(".top", $(this).parent()).removeClass("top");
		myImages.eq(curstep).addClass("top");
	}); */
}
/*------- End Gallery Handlers -------*/
