// GLOBAL VARIABLES
var paused = false;

// FUNCTIONS FOR BUTTON ROLLOVERS

function submitRollover () {
	document.getElementById("submit").src = "http://www.eartheasy.com/blog/wp-content/themes/default/images/f_search_button_over.gif";
	}
	
function submitOff () {
	document.getElementById("submit").src = "http://www.eartheasy.com/blog/wp-content/themes/default/images/f_search_button.gif";
	}
	
function hSubmitRollover () {
	document.getElementById("h_submit").src = "http://www.eartheasy.com/blog/wp-content/themes/default/images/h_search_button_over.gif";
	}
	
function hSubmitOff () {
	document.getElementById("h_submit").src = "http://www.eartheasy.com/blog/wp-content/themes/default/images/h_search_button.gif";
	}
	
function rollEvent () {
	// Get form element
	var submit = document.getElementById("submit");
	var h_submit = document.getElementById("h_submit");
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		submit.attachEvent("onmouseover", submitRollover);
		submit.attachEvent("onmouseout", submitOff);
		
		h_submit.attachEvent("onmouseover", hSubmitRollover);
		h_submit.attachEvent("onmouseout", hSubmitOff);
		}
		
	else {
		submit.addEventListener("mouseover", submitRollover, true);
		submit.addEventListener("mouseout", submitOff, true);
		
		h_submit.addEventListener("mouseover", hSubmitRollover, true);
		h_submit.addEventListener("mouseout", hSubmitOff, true);
		}
	}
	
function swapSearchBg() {
	$("input#h_searchfield").focus(function() {
		$("input#h_searchfield").css('backgroundPosition','bottom left');
		});
	$("input#f_searchfield").focus(function() {
		$("input#f_searchfield").css('backgroundPosition','bottom left');
		});
	}
	

// FUNCTIONS FOR FEATURED IMAGE SWAPPER

// Set up event handlers
function changeFeatureButtons() {
	$("#featured_nav").find("a:not('#pause')").click(function (event) {
		// Remove all existing ID attributes
		$("#featured_nav").find("li").removeAttr("id");
		
		// Add "active" ID to clicked list item, load variable with name of item clicked
		$(this).parent().attr('id','active');
		var featureClicked = $(this).parent().find("strong").text();
		
		event.preventDefault();			// Prevent default click action from occuring - might need alternate for IE
		
		loadFeature(featureClicked);	// Call the function that loads the feature
		});
	}
	
// Swaps in new div when feature text is clicked
function loadFeature(featureClicked) {
	// Iterate through each of the feature boxes
	$(".featured").each(function() {
		
		$(this).removeClass("show");
		
		// Match the button clicked with the actual feature, show the correct feature box
		if ($(this).find("h3").text() == featureClicked) {
			// Hide current feature
			
			$(this).addClass("show");		// Show new feature
			var backgroundImg = $(this).attr("id");
			backgroundImg = "url(\"" + backgroundImg + "\")";
			$(this).css('backgroundImage', backgroundImg);
			}
		});
	}

// Set up rotation interval, call rotation function
function rotateFeatures() {
	setTimeout('doRotate();', 7000);				// Timeout between rotations
	}
	
// This function handles the actual rotation through images
function doRotate() {
	if (!paused) {
		var current = $(".show");
		var next = $(".show + .featured").slice(0,1);
		
		if (!next[0]) {
			next = $(".featured").slice(0,1);			// Set up the next feature box to be shown
			}
		
		next.fadeIn("slow",function() {					// Visually fade in, then apply the correct "show" class
			current.removeClass("show");
			next.addClass("show");
			next.css("display","");
			});
		
		var backgroundImg = next.attr("id");			// Set up background images
		backgroundImg = "url(\"" + backgroundImg + "\")";
		next.css('backgroundImage', backgroundImg);
		setActiveTitle(next.find("h3").text());
		rotateFeatures();
		}
	}
	
function setActiveTitle(title) {
	$("#featured_nav").find("li").removeAttr("id");				// Remove any existing active ID
	$("#featured_nav li").each(function() {						// Check each of the items for title match
		if ($(this).find("strong").text() == title) {
			$(this).attr('id','active');						// Apply active ID
			}
		});
	}
	
// Functionality for pause button
function pauseButton() {
	$("#pause").click(function (event){
		event.preventDefault();			// Prevent default click action from occuring - might need alternate for IE
		
		// If unpaused, clicking the pause button stops the rotation
		if (!paused) {
			paused = true;
			}
		
		// If already paused, clicking the pause button resumes the rotation
		else {
			paused = false;
			doRotate();			// Directly calls the rotation function without waiting for the timed interval first
			}
		});
		
	}
	
$(document).ready(function(){
	
	 jQuery('#storescroll').jcarousel({
        // Configuration goes here
    });
    
    jQuery('#articlescroll').jcarousel({
        // Configuration goes here
    });
	
	// Move featured to its correct position
	if ($("#feature_wrapper")) {
		$("#main_col").prepend($("#feature_wrapper"));
		}
	if ($("#popular")) {
		$("#main_col").prepend($("#popular"));
		}
	
	
	// Show initial feature
	$(".featured").slice(0,1).addClass("show");
	var bgImage = $(".featured.show").attr("id");
	bgImage = "url(\'" + bgImage + "')";
	$(".featured.show").css('backgroundImage', bgImage);
	
	// Make first nunber link active
	$("#featured_nav li").slice(0,1).attr('id','active');
	
	rollEvent();				// Rollovers for form buttons
	changeFeatureButtons();		// Set up functionality for feature switcher links
	rotateFeatures();			// Start rotating through features
	pauseButton();				// Pause/resume rotation
	
	swapSearchBg();

	});