var t;

$(document).ready(function() {
    positionSwoosh(); 
    initializeMenu();  
    $(".topics_link").click(onTopicClick);  
    $(".subtopics_link").click(onSubTopicClick);  
    hideAllTopics();  
    hideAllSubTopics();  
    $("#default").show();
    $('#slides').cycle({ 
	         fx:     'fade',
	         next:   '#next', 
	         prev:   '#prev' });

	 $('#slides').cycle('pause');
	 $('#play').text('Start');
	 $("#play").click(slidePause);
	 $("#slideshow").cycle();
     $("#pageSlideShow").cycle();
     $("#pageSlideShowCaption").cycle(); 

  
    $("#container a:not(.hover)").hover(
        function(){
            $(this).addClass("hover");
        }
    ,
        function(){
            $(this).removeClass("hover");
        }
    );
    
    $("#container").height($("#content").outerHeight(true) + $("#masthead").outerHeight(true));
});

function positionSwoosh(){
    var selector = "#headline h1";
    var width = $(selector).innerWidth();
   /*$(selector).after('<img src="images/swoosh.gif" alt="swoosh" id="swoosh_img" />');*/
    var swoosh_width = 343;
    var h_pos = 0;
    if (width < swoosh_width - 50){
        h_pos = swoosh_width - width + 50;
    var h_pos_style = h_pos + "px";
    $("#headline h1").css("padding-right", h_pos_style);
    }
    
}

function initializeMenu(){
    $("#menu ul li ul").hide();
    $("#menu ul li").hover(
        function(){
            clearTimeout(t);
            hideSubmenus();
            $(this).addClass("hover");
            showSubmenu(this);
        }
    ,
        function(){
            t = setTimeout("hideSubmenus()", 1700);
            $(this).removeClass("hover");
        }
    );
}

function showSubmenu(obj){
    var total_width = $("#menu").outerWidth(true);
    var child = $(obj).children("ul");
    var left = $(obj).offset().left + 1;
    var width = child.outerWidth(true);
    if ((left + width) >= total_width){
        var margin_left = left - 5;
        child.css("margin-left", "-" + margin_left + "px");
    }
    child.show();
}

function hideSubmenus(){
    $("#menu ul li:not(.hover) ul:visible").hide();
}

function hideAllTopics(){
	$(".topic").hide();
}

function hideAllSubTopics(){
	$(".subtopic").hide();
}

function onSubTopicClick(event){
	$(".default_topic").hide();
	hideAllSubTopics();
	var id = "#" + $(this).attr("name");
    $(".subtopics_link").removeClass("current");
    $(this).addClass("current");
	$(id).show();
	event.preventDefault();
}

function onTopicClick(event){
	$(".default_topic").hide();
	hideAllTopics();
	var id = "#" + $(this).attr("name");
    $(".topics_link").removeClass("current");
	$(this).addClass("current");
	$(id).show();
	event.preventDefault();
}

function slidePause(event){
    if ($('#play').text() == 'Pause'){
        $('#slides').cycle('pause');
        $('#play').text('Start');
    }else{
        $('#slides').cycle('resume');
        $('#play').text('Pause');
    }
    event.preventDefault();
}