/**
 * JavaScript behaviors for content elements.
 *
 * @copyright  Copyright (c) 2005-2009 Found Line, Inc. (http://www.foundline.com/)
 * @version    $Id: content.js 54 2009-06-03 00:18:18Z jason.pelletier $
 */

var vitOverviewSecond = 0;

$("document").ready(function() {
    
    $("map#locations-map area").mouseover(function () {
        var location = $(this).attr("class");
        $("#locations-list").find("a." + location).addClass("active");
    });
    $("map#locations-map area").mouseout(function () {
        var location = $(this).attr("class");
        $("#locations-list").find("a." + location).removeClass("active");
    });
    
    $(".slideshow").addClass("loop");
	
	$.timer(1000, function (timer) {
		if ($(".slideshow").hasClass("loop")) {
			vitOverviewSecond++;
			if (vitOverviewSecond >= 5) {
				vitOverviewSecond = 0;
				moveNext();
			}
		}
	});
	
	$("ul#controls li a#next").click(function (){
		pause();
		moveNext();
		$("ul#controls li a#pause").parent().hide();
		$("ul#controls li a#play").parent().show();
		return false;
	});
	$("ul#controls li a#previous").click(function (){
		pause();
		movePrevious();
		$("ul#controls li a#pause").parent().hide();
		$("ul#controls li a#play").parent().show();
		return false;
	});
	$("ul#controls li a#play").click(function (){
		moveNext();
		play();
		$("ul#controls li a#play").parent().hide();
		$("ul#controls li a#pause").parent().show();
		return false;
	});
	$("ul#controls li a#pause").click(function (){
		pause();
		$("ul#controls li a#pause").parent().hide();
		$("ul#controls li a#play").parent().show();
		return false;
	});
	
	$("dl.schedule dt, dl.schedule dd").addClass("active");
	$("dl.schedule dt input.selector").attr({
        checked: "checked"
    });

    setCheckBoxes("selectedLocations");

    $("dl.schedule dt input.selector").click(function () {
        setCookieData("selectedLocations");
        if ($(this).is(":checked")) {
            $(this).closest("dt").addClass("active");
            $(this).closest("dt").next().addClass("active");
        } else {
            $(this).closest("dt").removeClass("active");
            $(this).closest("dt").next().removeClass("active");
        }
    });
    
    $("dl.schedule ul.available li").mouseover(function () {
		$(this).addClass("sfHover");
	});
	$("dl.schedule ul.available li").mouseout(function () {
		$(this).removeClass("sfHover");
	});


});

function moveNext() {
	if ($(".slideshow li.current").next("li").size() == 0) {
			$(".slideshow li.current").fadeOut(500).removeClass("current");
			$(".slideshow li").fadeOut(500).filter(":first").addClass("current").fadeIn(500);
		} else {
			$(".slideshow li.current").fadeOut(500).removeClass("current").next("li").addClass("current").fadeIn(500);
		}
};
function movePrevious() {
	if ($(".slideshow li.current").prev("li").size() == 0) {
			$(".slideshow li.current").fadeOut(500).removeClass("current");
			$(".slideshow li").fadeOut(500).filter(":last").addClass("current").fadeIn(500);
		} else {
			$(".slideshow li.current").fadeOut(500).removeClass("current").prev("li").addClass("current").fadeIn(500);
		}
};
function play() {
	vitOverviewSecond = 0;
	$(".slideshow").addClass("loop");
};
function pause() {
	$(".slideshow").removeClass("loop");
};

function setCheckBoxes(cookieName) {
    var data = getCookieData(cookieName);
    if (0 == data.length) {
        return;
    }
    $("dl.schedule dt input.selector").each(function () {
        var idFound = false;
        for (var i = 0; i < data.length; i++) {
            if(data[i] == $(this).attr("id")) {
                idFound = true;
                break;
            }
        }
        if (idFound) {
            $(this).attr("checked", true);
            $(this).closest("dt").addClass("active");
            $(this).closest("dt").next().addClass("active");
        } else {
            $(this).attr("checked", false);
            $(this).closest("dt").removeClass("active");
            $(this).closest("dt").next().removeClass("active");
        }
    });
}

function getCookieData(cookieName) {
    var data;
    if (!$.cookie(cookieName)) {
        data = [];
    } else {
        data = $.cookie(cookieName).split(",");
    }
    return data;
}

function setCookieData(cookieName) {
    var data = [];
    var dataIndex = 0;
    $("dl.schedule dt input.selector").each(function () {
        if ($(this).attr("checked")) {
            data[dataIndex] = $(this).attr("id");
            dataIndex++;
        }
    });
    $.cookie(cookieName, data.join(","), { path: "/"});
}

