var timer = null;
var headerImages = [];
var disableImageLinks;

function showHeader (elm, elmIdx) {

	if (disableImageLinks == true) {
	
	} else {
	
		if (elm.hasClass('active') || elm.attr('rel') == '') {
		
			// DO NOTHING
		
		} else {
		
			disableImageLinks = true;

			var preloadElm = headerImages[elmIdx][0];
			var targetElm = $('#headerImage img');

			$(preloadElm).load(function(){
				headerImages[elmIdx][2] = true;
				elm.addClass('active').siblings().removeClass('active').blur();
				targetElm.replaceWith($(this)).animate({'opacity' : 1},{ duration: 500, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');
			});
			
			targetElm.animate(
				{'opacity' : 0},
				{ duration: 250, queue: false, easing: 'easeOutSine', complete: function(){
					if (headerImages[elmIdx][2] != true) {
						$(preloadElm).attr({'src' : headerImages[elmIdx][1]});
					} else {
						elm.addClass('active').siblings().removeClass('active').blur();
						$(this).replaceWith($(preloadElm)).animate({'opacity' : 1},{ duration: 500, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');					
					};
				}}
			);
		}
	}
};

function nextHeaderImage()
{
	var total = $('#imageNav a').size();
	var current = -1;
	$('#imageNav a').each(function(i) {
		if ($(this).hasClass("active")) current = i;
	});
	next_image = current + 1;
	if (next_image > total - 1) {
		next_image = 0;
	}
	showHeader($("#imageNav a:eq(" + next_image + ")"), next_image);
	//clearInterval(timer);
}

function prepHeader () {
	
	$('#imageNav a').each(function(i) {
		var imgObj = $('<img>');
		var imgSrc = $(this).attr('rel');
		headerImages[i] = [imgObj, imgSrc, false];
		$(this).click(function(){
			showHeader($(this), i);
			return false;
		});
	});
	
	$('#imageNav a:first').addClass('active');
	timer = setInterval("nextHeaderImage()", 10000);
}

function setNav(activeElm) {
    if( activeElm != '' ) {
        var targetElm = $('#HorzNav li.' + activeElm);
        if (targetElm.size() > 0) {
            targetElm.each( function(i) {
                var currentItem = $(targetElm[i]);
                currentItem.parents('li').addClass('active').end().addClass('active');
                });
                
            var topActiveLi = $('#HorzNav > ul > li').filter('.active');
            var activeOffset = topActiveLi.offset().left - topActiveLi.parent().offset().left;
            var activeWidth = topActiveLi.width();
            
            $('#navIndicator').stop().animate({left:activeOffset+"px",width:activeWidth+"px"},0);
        }
    }
    
	$('#HorzNav > ul > li').hover(
		function(){
			
			var overLi = $(this);
			var activeLi = $('#HorzNav > ul > li').not('.active');

			if (overLi.is('.active')){
				overLi.addClass("over").find('> ul').slideDown(); 				
			} else {
				var offset = overLi.offset().left - overLi.parent().offset().left;
				var thiswidth = $(this).width();
				$('#navIndicator')
					.stop()
					.animate({left:offset+"px",width:thiswidth+"px"}, 250, function(){ overLi.children('ul').stop().animate({height: 'show'}, 250, function(){}, '', false ).end().addClass("over"); }, '', '' );
			}
		},
		
		function(){
			var overLi = $(this);
			var activeLi = $('#HorzNav > ul > li').filter('.active');
			if (overLi.is('.active')) {
			} else {
				if (activeLi.length > 0 ) {
					var offset = activeLi.offset().left - overLi.parent().offset().left;					
					var activewidth = activeLi.width();
				} else {
					var offset = 0;
					var activewidth = 0
				}

				$('#navIndicator').stop().animate({left:offset+"px",width:activewidth+"px"}, 600, '', '', '' );
				
			}
			
			overLi.children('ul').stop().animate({height: 'hide'}, 250, function(){ $(this).css({height: 'auto' }) }, '', false ).end().removeClass("over");
		}
	);
	
	$('#HorzNav ul ul li')
		.hover(
			function(){ $(this).addClass("over"); },
			function(){ $(this).removeClass("over"); }
		);
}

$(document).ready(function(){
	$('a.lightbox').lightBox();
	$('#siteLogo','.sportImage').supersleight({shim: 'media/img/ui/transparent.gif'});
	$('#newsFeed li:first div.fold').show();
	$('#newsFeed div.title').click(function(){ $(this).next('div.fold').slideDown().end().parent().siblings().find('div.fold:visible').slideUp()});
	$('#newsFeed div.title:odd').addClass('alt');
	$('#newsList li:first div.fold').show();
	$('#newsList div.title').click(function(){ $(this).next('div.fold').slideDown().end().parent().siblings().find('div.fold:visible').slideUp()});
	$('#newsList div.title:odd').addClass('alt');
	prepHeader();
    $("#id_athlete_name").focus(function () {
        if ($(this).val() == "Name") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("Name");
    });
    $("#athleteSearch form").submit(function() {
        var i = $(this).find("input:eq(0)");
        if (i.val() == "Name") i.val("");
    });
    
    $("#athlete_name").focus(function () {
        if ($(this).val() == "Athlete Name") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("Athlete Name");
    });
    $("#athletesFilter form").submit(function() {
        var i = $(this).find("input:eq(0)");
        if (i.val() == "Athlete Name") i.val("");
    });
    
    $("#speaker_name").focus(function () {
        if ($(this).val() == "Speaker Name") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("Speaker Name");
    });
    $("#speakersFilter form").submit(function() {
        var i = $(this).find("input:eq(0)");
        if (i.val() == "Speaker Name") i.val("");
    });
    
    // autocompleter
    //$("#athleteSearch #id_athlete_name").autoComplete({
    //    ajax: '/quick_search/autocomplete/',
    //    delay: 1000,
    //    cache: false
    //});    
    // autocompleter
    $("#athleteSearch #id_athlete_name").autocomplete("/quick_search/autocomplete/");
});



