/* Global Variables */
var sliderOn = true; //if true, Slider is on
var ticOn = false; //if true, Today in Chatterbean slider is on
var timedSlides;
var slide_fade = 100;
var slide_interval = 6000;

//this executes after the page is loaded
$(window).bind('load', function() {
	//begin timer for sliders and Today in Chatterbean
	timedSlides = setInterval("swapTimedSlides()", slide_interval);
});

//this executes when page is ready to be loaded
$(document).ready(function() {
	$(".today_element").click(function () {
		//stop interval for tic
		ticOn = false;
		//if slider interval is also off, turn off interval completely
		if(!sliderOn)
			clearInterval(timedSlides);
	});
	$(".today_buttons").click(function () { 
		//stop interval for tic
		ticOn = false;
		//if slider interval is also off, turn off interval completely
		if(!sliderOn)
			clearInterval(timedSlides);

		elmnt_id = $(this).attr("id");

		next_tic(elmnt_id);
		return false;
	});
	$(".next,.previous").click(function () { 
		//stop interval for slider
		sliderOn = false;

		if(this.className == "next")
			next_slide(1);
		else
			next_slide(0);
		
		return false;
	});
	$("#today_weather_submit").click(function () { 
		get_weather($("#weather_zip").val());
	});
});

function swapTimedSlides() {
	if(sliderOn)
		next_slide(1);
}
function get_weather(zip) { 
	$.getJSON("/get_weather/"+zip, function(json){
		if(json.notfound == "true" || json.temp == "") {
			$("h4#wrong_zip").html("zip code not found<br>please try again.");
			$("h3#temp").html("Enter Zip Code Below");
			$("h3#location").html("");
			$("h3#date").html("");
			$("#condition_icon").hide();
		} else {
			$("h4#wrong_zip").html("");
			$("h3#temp").html(json.temp);
			if(json.location != "")
				$("h3#location").html(json.location);
			if(json.date != "")
				$("h3#date").html(json.date);
			if(json.condition_icon != "") {
				$("#condition_icon").html("<img src='"+json.condition_icon+"' width='50'><br>"+json.condition);
				$("#condition_icon").show("slow");
			}
		}
	});
}
/*******************************************************************************
	Begin slider function
*******************************************************************************/
function next_slide(isForward) {
	$current_active_obj = $("#slider_images").children(":visible");
	$current_active_hd_obj = $("#slider_head").children(":visible");

	//if moving slide forward
	if(isForward) {
		//if last slide active, set next active to first slide
		if($current_active_obj.is(":last-child")) {
			$next_active_obj = $current_active_obj.prevAll(":last");
			$next_active_hd_obj = $current_active_hd_obj.prevAll(":last");
		}
		//set next active to next slide
		else {
			$next_active_obj = $current_active_obj.next();
			$next_active_hd_obj = $current_active_hd_obj.next();
		}
	}
	//else moving slide backwards
	else {
		//if first slide active, set next active to last slide
		if($current_active_obj.is(":first-child")) {
			$next_active_obj = $current_active_obj.nextAll(":last");
			$next_active_hd_obj = $current_active_hd_obj.nextAll(":last");
		}
		//set next active to previous slide
		else {
			$next_active_obj = $current_active_obj.prev();
			$next_active_hd_obj = $current_active_hd_obj.prev();
		}
	}

	//if new current slide is last slide, set next control text to first slide's text
	if($next_active_obj.is(":last-child")) {
		$next_control = $next_active_obj.prevAll(":last").attr("title");
	}
	//set next control text to next slide's text
	else {
		$next_control = $next_active_obj.next().attr("title");
	}
	//if new current slide is first slide, set prev control text to last slide's text
	if($next_active_obj.is(":first-child")) {
		$prev_control = $next_active_obj.nextAll(":last").attr("title");
	}
	//set prev control text to prev slide's text
	else {
		$prev_control = $next_active_obj.prev().attr("title");
	}

	//fade current slide out and next slide in
	$current_active_obj.fadeOut(slide_fade, function() {
		$next_active_obj.fadeIn(slide_fade);
	});
	//fade current slide's head out and next slide's head in
	$current_active_hd_obj.fadeOut(slide_fade, function() {
		$next_active_hd_obj.fadeIn(slide_fade);
	});
	//update 'next' controller text image
	$(".next_text img").attr("src","../images_home/slider_control_"+$next_control+".gif");
	$(".next_text a").attr("href",$hostname+$next_control+"-quizzes/");
	//update 'previous' controller text image
	$(".prev_text img").attr("src","../images_home/slider_control_"+$prev_control+".gif");
	$(".prev_text a").attr("href",$hostname+$prev_control+"-quizzes/");

	return true;
}
/******************* End slider functions ************************************/
/*******************************************************************************
Begin Today in Chatterbean functions
*******************************************************************************/
function next_tic($new_active_btn) {
var $new_active, $old_active;

//loop through all alements
$(".today_element").each(function(i){
	//find current active/displayed element
	if(this.style.display == "block") {
		//if no button was pressed (timer called it)
		if(!$new_active_btn) {
			new_index = (i+1 == $(".today_element").length)?0:i+1; //set new index to next (or first if current was last)
//			if(new_index == 0 || new_index == 5 || new_index == 6)
			$new_active = $(".today_element")[new_index].id.replace(/today_/,""); //generate new element name from id
		}
		else
			$new_active = $new_active_btn.replace(/today_buttons_/,""); //set new element name from pressed button
		
		$old_active = this.id.replace(/today_/,""); //set old element name from current active

		return true;
	}
});

swapTic($new_active, $old_active); //function that does the actual swapping
}

function swapTic(element_id, old_element_id) {
//swap old with new slide
$("#today_"+old_element_id).fadeOut(slide_fade, function() {
								$("#today_"+element_id).fadeIn(slide_fade);
								});
//deactivate all buttons
$(".today_buttons").css("background-image","url('"+$hostname+"images_home/tb_bg.gif')");
//Activate clicked button
$("#today_buttons_"+element_id).css("background-image","url('"+$hostname+"images_home/tb_bg-"+element_id+".gif')");
}
/******************* End Today in Chatterbean functions ************************/
