$(document).ready(function () {
    $('#tooltip').css("opacity", 0);

    checkNav();

    if (document.getElementById("subNav") != null) {
        checkSubNav();
    }

    if (document.getElementById("slider") != null) {
        initSlider();
    }

    if (document.getElementById("accordion") != null) {
        initAccordion();
        var hash = window.location.hash;
        var thash = hash.substring(hash.lastIndexOf('#'), hash.length);
        if (thash != "") {
            $('#accordion').find('a[href*=' + thash + ']').closest('h3').trigger('click');
        }
    }

    if (document.getElementById("recentNews") != null) {
        initTicker();

        initTickerNav();
    }
	
	$("header .headerRight .search a").bind("click", submitSearch);
	
	$("input[type=text]").keydown(function(e) {
        if (e.keyCode == 13){
            submitSearch(null);
		}
    });
});

var timer;
var tickerMoving = false; ;
var thumbArr = new Array();
var position = 0;
var maxShown = 3;
var curImage = "";

function checkNav(){
	var windowURL = document.location.href;
	
    $('#header .nav .navItem').children('a').each(function (ind, obj) {
		if(windowURL.indexOf(obj.href) != -1){
			var image = $(this).children("img");

			var tempArr = $(image).attr("src").split("_off");
			$(image).attr("src", tempArr[0] + "_on.png");
			
			curImage = $(image).attr("src");
		}
    });
}

function checkSubNav(){
	var windowURL = document.location.href;
	
    $('#subNav').children('a').each(function (ind, obj) {
		if(windowURL.indexOf(obj.href) != -1){
			$(this).children(".subNavItem").css("backgroundColor", "#0091d0");
			$(this).children(".subNavItem").css("color", "#ffffff");
		}
    });
}

function navRollOver(image){
	if(image.src.indexOf("_off") != -1){
		var tempArr = image.src.split("_off");
		image.src = tempArr[0] + "_on.png";
	}
}

function navRollOut(image){
	if(image.src.indexOf("_on") != -1){
		var tempArr = image.src.split("_on");
		
		if (curImage != $(image).attr("src")){
			image.src = tempArr[0] + "_off.png";
		}
	}
}

function initSlider(){
	$(".sliderThumb").each(function(){var thumb = $(this); thumbArr.push(thumb);});
	
	var sliderWidth = thumbArr.length * (thumbArr[0].width() + 9);
	$("#sliderThumbs").css("width", sliderWidth);
}

function moveSlider(direction){
	if (direction == "left"){
		if (position < 0){
			position += 1;
		}
	}else if (direction == "right"){
		if (position > -(thumbArr.length - maxShown)){
			position -= 1;
		}
	}
	
	var newMargin = (thumbArr[0].width()+9)*position;

	$("#sliderThumbs").animate({marginLeft:newMargin}, 250, 'easeOutCubic');
}

function showSliderTooltip(image){
	$('#tooltip').stop(true, false);

	var pos = $(image).offset();
	var tooltipY = pos.left + 10;
	
	$("#tooltip").css("display", "block");
	$("#tooltip").css("left", tooltipY + "px");
	$("#tooltip .tooltipCopy").html(image.name);

	$("#tooltip").animate({opacity: 1}, 250);
}

function hideSliderTooltip(){
	$("#tooltip").animate({opacity: 0}, 250);
}

function overSubNav(link){
	$(link).children(".subNavItem").css("backgroundColor", "#0091d0");
	$(link).children(".subNavItem").css("color", "#ffffff");
}

function outSubNav(link){
	var windowURL = document.location.href;
	
	if(link.href != windowURL){
		$(link).children(".subNavItem").css("backgroundColor", "#e7e5e8");
		$(link).children(".subNavItem").css("color", "#20527b");
	}
}

function initAccordion(){
	$("#accordion").accordion({active:false, collapsible:true, autoHeight:false});
	$("#accordion").bind("accordionchange", function(event, ui) {changeAccordionArrow()});
}

function changeAccordionArrow(){
	$("#accordion .ui-state-active .accordionHeader .arrow").each(function (ind, obj) {
		var image = $(this).children("img");
		if ($(image).attr("src").indexOf("_off") != -1){
			tempArr = $(image).attr("src").split("_off");
			$(image).attr("src", tempArr[0] + "_on.jpg");
		}
	});
	
	$("#accordion .ui-state-default .accordionHeader .arrow").each(function (ind, obj) {
		var image2 = $(this).children("img");
		if ($(image2).attr("src").indexOf("_on") != -1){
			tempArr = $(image2).attr("src").split("_on");
			$(image2).attr("src", tempArr[0] + "_off.jpg");
		}
	});
}

function popupWindow(linkURL){
	var pathsDown = $('.logo a').attr('href');
	window.open (pathsDown + linkURL,"mywindow","menubar=0,resizable=0,width=500,height=400");
}

function initTickerNav() {
    var output = "";
    $("#recentNews .content").each(function (index, value) {
        $(this).attr("id", "newsItem" + index);
        output += "<div class=\"navItem\"><a href=\"javascript:changeTicker('" + index + "');\"><img src=\"assets/images/home/home_ticker_off.jpg\" width=\"12\" height=\"12\" border=\"0\" id=\"tickerNav" + index + "\" /></a></div>";
    });
    $("#content .homeFeature .nav").html(output);
    updateTickerNav(0);
}

function updateTickerNav(index) {
    $("#content .homeFeature .nav .navItem").each(function (index, value) {
        if ($(this).children("a").children("img").attr("src") == "assets/images/home/home_ticker_on.jpg") {
            $(this).children("a").children("img").attr("src", "assets/images/home/home_ticker_off.jpg");
        }
    });

    var id = "#tickerNav" + index
    $(id).attr("src", "assets/images/home/home_ticker_on.jpg");
}

function initTicker() {
    $("#recentNews .content:last").css("padding-bottom", 0);
    var ticker = $("#recentNews");

    //start the ticker
    animator(ticker.children(':first'));

    //set mouseenter
    ticker.mouseenter(function () {
        //stop current animation
        //ticker.children().stop();
    });

    //set mouseleave
    ticker.mouseleave(function () {
        //resume animation
        //animator_continue(ticker.children(":first"));
    });
}

function changeTicker(index) {
    if (!tickerMoving) {
        clearTimeout(timer);

        var thisContent;
        var exit = 0;
        var first = 0;
        $("#recentNews .content").each(function () {
            if (exit == 0) {
                var idArr = $(this).attr("id").split("newsItem");
                var curID = idArr[1];
                var curContent = $(this);

                if (curID == index) {
                    exit = 1;
                    animator(curContent);
                } else {
                    var distance = 100;
                    var duration = (distance + parseInt(curContent.css("marginTop"))) / 0.1;
                    tickerMoving = true;
                    curContent.animate({ marginTop: -distance }, duration, "easeOutCubic", function () {
                        curContent.appendTo(curContent.parent()).css("marginTop", 0);
                        $("#recentNews .content").css("display", "block");
                        tickerMoving = false;
                    });

                    if (first == 0) {
                        first = 1;
                    } else {
                        curContent.css("display", "none");
                    }
                }
            }
        });

        updateTickerNav(index);
    }
}

function animator(currentItem) {
    //work out new anim duration
    var distance = 100;
    var duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.05;
    //animate the first child of the ticker
    timer = setTimeout(function () {
        tickerMoving = true;
        currentItem.animate({ marginTop: -distance }, duration, "easeOutCubic", function () {
            //move current item to the bottom
            currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
            //recurse
            animator(currentItem.parent().children(":first"));

            var indexArr = currentItem.attr("id").split("newsItem");
            var index = indexArr[1];
            index++;

            if (index >= $("#recentNews .content").size()) {
                index = 0;
            }

            tickerMoving = false;
            updateTickerNav(index);
        });
    }, 5000);
}

function animator_continue(currentItem) {
    //work out new anim duration
    var distance = 100;
    duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.05;
    //animate the first child of the ticker
    currentItem.animate({ marginTop: -distance }, duration, "easeOutCubic", function () {
        tickerMoving = true;
        //move current item to the bottom
        currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
        //recurse
        animator(currentItem.parent().children(":first"));

        var indexArr = currentItem.attr("id").split("newsItem");
        var index = indexArr[1];
        index++;

        if (index >= $("#recentNews .content").size()) {
            index = 0;
        }

        tickerMoving = false;
        updateTickerNav(index);
    });
}

function clearInputText(input) {
	if(input.value == $(input).attr("alt")){
		$(input).attr("alt", input.value);
		input.value = "";
	}
}

function returnInputText(input) {
	if(input.value == ""){
		input.value = $(input).attr("alt");
	}
}

function submitSearch(e){
	if(e != null){
		e.preventDefault();
	}

	if(document.getElementById("searchBox").value != "SEARCH" && document.getElementById("searchBox").value != ""){
		window.location = $("#header .nav .navItem.search .searchBox a").attr("href") + "?q=" + document.getElementById("searchBox").value;
	}
}

function getQueryVariable(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1]; 
		} 
	} 
} 
