/* settings for Disney chrome */
var chromeSettings = {
	styleBackground: "4d4d4d"
}

//console fallback
if (!window.console) {
  window.console = {};
  window.console.log = function(){};
}


//global variables
var currentSubpage = 1;
var slideInterval = null; // global slideshow interval variable
var storyInterval = null; // global slideshow interval variable
//show first images in slideshow
var currentSlide = 1;
var currentFeatured = 1;
var currentFact = 1;
var heroInterval = 5000;
var sInterval = 8000;
var leftSticky;
// global pageload functionality
$(document).ready(function() {
	
	// IE fading png's hack
	/*
	var i;
    for (i in document.images) {
        if (document.images[i].src) {
            var imgSrc = document.images[i].src;
            if ((imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') && document.images[i].className != 'nohack') {
                document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
            }
        }
    }
    */
	
	// initialize the menu accordion
	$("ul.menu").initMenu();
	
	//check too see if print version is needed
	if(document.location.search != ""){
	    if(document.location.search.substr(1).split('=')[0] == 'print' && document.location.search.substr(1).split('=')[1] == 'true'){
	        $('link[media=print]').attr('media', 'all');
	        $('body').css('overflow-x', 'hidden');
			setTimeout(function(){
				window.print();
			}, 2000);
	        
	    }
	}
	
	//make blurry caption background appear last
	// this is only for non-webkit and non-msie browsers, which don't seem to handle .load() very well on images. For these 2 the caption is shown at the end of the footer include.
	if(!$.browser.webkit && !$.browser.msie){
	   
	    if($('#intro-image img').length > 0){
		    var i = $('#intro-image img')[1];
		    $(i).load(function(){
			    if($("#slideshow-info").length > 0)
				    $("#slideshow-info").css('display', 'block');
		    });
	    }

	    $('#slide1 img').load(function(){
	        if($("#slideshow-info-home").length > 0)
			    $("#slideshow-info-home").css('display', 'block');
	    });
	}
	
	// keep left navigation in the viewport when user scrolls
	leftSticky = $("div#left-nav").stickyfloat({duration:200, offsetY:20});
	
	// keep table head in the viewport when user scrolls
	if($("div#tableHead").length > 0)
		$("div#tableHead").stickyfloat({duration:0, offsetY:10});
	
	// utility navigation - text size adjustment
	$("ul#textsize-options li a").live('click',function() {
		changeSize($(this).attr("class"));
		$(this).parent().siblings().removeClass("selected");
		$(this).parent().addClass("selected");
		return false;
	});
	
	// utility navigation - set state for text size adjustment
	changeSize($.cookie("yirFontSize") || "normal");
	
	// utility navigation - print page functionality
	$("li#util-nav-print a").live('click',function() {
		//window.print();
		openPrintPreview();
		return false;
	});
	
	// utility navigation - load bookmarks from cookie
	loadBookmarks();
	
	// utility navigation - add pages to the bookmarks list
	$("a#bookmarks-add").live('click',function() {
		addBookmark();
		return false;
	});
	
	// utility navigation - remove pages from the bookmarks list
	$("div#bookmarks-items-list table td.remove a").live("click", function() {
		removeBookmark(this);
		return false;
	});
	
	// utility navigation - open/close the bookmarks list
	$("a#bookmarks-view").live('click',function() {
		$("div#bookmarks-items").toggle();
		return false;
	});
	
	$("div#bookmarks-items-close a").live('click',function() {
		$("div#bookmarks-items").toggle();
		return false;
	});
	
	//slideshow
	$('#slide1').css('z-index', '100').fadeIn('fast');
	$('#main-slideshow .slideshow-right').click(function(){
	    var slideNumber = $('#main-slideshow .slide').length;
	    if(slideInterval != null) clearInterval(slideInterval);
	    $('#slide' + currentSlide).css('z-index', '99');
	    var oldSlide = currentSlide;
	    currentSlide++;
	    if(currentSlide > slideNumber) currentSlide = 1;
	    // change caption
	    $('#slideshow-info-home .main').html($('#slide' + currentSlide + ' img').attr('title'));
	    $('#slide' + currentSlide).css('z-index', '100').stop(true, true).fadeIn('slow', function(){
	        $('#slide' + oldSlide).stop(true, true).fadeOut('fast', function(){
	            slideInterval = setInterval("autoPlay()", heroInterval);
	        });
	    });
	    return false;    
	});
	
	$('#main-slideshow .slideshow-left').click(function(){
	    var slideNumber = $('#main-slideshow .slide').length;
	    
	    if(slideInterval != null) clearInterval(slideInterval);
	    $('#slide' + currentSlide).css('z-index', '99');
	    var oldSlide = currentSlide;
	    currentSlide--;
	    if(currentSlide < 1) currentSlide = slideNumber;
	    // change caption
	    $('#slideshow-info-home .main').html($('#slide' + currentSlide + ' img').attr('title'));
	    $('#slide' + currentSlide).css('z-index', '100').stop(true, true).fadeIn('slow', function(){
	        
	        $('#slide' + oldSlide).stop(true, true).fadeOut('fast', function(){
	            slideInterval = setInterval("autoPlay()", heroInterval);
	        });
	    });
	    return false;    
	});
	
	//featured story slideshow
	$('#featured-item1').fadeIn('fast');
	$('#box-featured .slideshow-right').click(function(){
	    var featuredNumber = $('#box-featured .box-center div').length;
		var oldSlide = currentFeatured;
		
		if(storyInterval != null) clearInterval(storyInterval);
		
	    currentFeatured++;
	    if(currentFeatured > featuredNumber) currentFeatured = 1;
	    $('#featured-item' + oldSlide).css('display', 'none');
	    $('#featured-item' + currentFeatured).css('display', 'block');
	    storyInterval = setInterval("autoPlayStory()", sInterval);
	    return false;    
	}
	);
	$('#box-featured .slideshow-left').click(function(){
	    var featuredNumber = $('#box-featured .box-center div').length;
		var oldSlide = currentFeatured;
		
		if(storyInterval != null) clearInterval(storyInterval);
		
	    currentFeatured--;
	    if(currentFeatured < 1) currentFeatured = featuredNumber;
	    $('#featured-item' + oldSlide).css('display', 'none');
	    $('#featured-item' + currentFeatured).css('display', 'block');
	    storyInterval = setInterval("autoPlayStory()", sInterval);
	    return false;    
	}
	);
	
	//fast fact slideshow
	$('#box-facts #fact1').fadeIn('fast');
	var factNumber = $('#box-facts .box-center img').length;
	$('#box-facts .slideshow-right').click(function(){
	    if($(this).attr('class').split(' ')[1] != 'noclick'){
    		var oldSlide = currentFact;
    	    currentFact++;
    	    if(currentFact > factNumber) currentFact = 1;
    	    $('#box-facts #fact' + oldSlide).fadeOut('slow', function(){
    	        $('#box-facts #fact' + currentFact).fadeIn('fast');
    	    });
    	    return false;  
    	}  
	}
	);
	$('#box-facts .slideshow-left').click(function(){
	    if($(this).attr('class').split(' ')[1] != 'noclick'){
    		var oldSlide = currentFact;
    	    currentFact--;
    	    if(currentFact < 1) currentFact = factNumber;
    	    $('#box-facts #fact' + oldSlide).fadeOut('slow', function(){
    	        $('#box-facts #fact' + currentFact).fadeIn('fast');
    	    });
    	    return false;    
    	}
	}
	);
    /* chrome */
    $('#gde_chromeContents').css('width', '993px');	
    $('#gde_footerContents').css('width', '993px');	
    
    /* external links */
    /*$('a').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target','_blank');
    */
    
    
});

// utility navigation - change font size
function changeSize(size) {
	
	$("ul#textsize-options li").removeClass("selected");
	$("ul#textsize-options li a."+size).parent().addClass("selected");
	
	// store the user's preference
	$.cookie("yirFontSize", size, { expires: 14, path: '/' });
	
	switch(size) {
		case "small":
			size = "0.8em";
		break;
		
		case "normal":
			size = "1.0em";
		break;
		
		case "large":
			size = "1.2em";
		break;
	}
	
	$("body").css("font-size", size);
}

// utility navigation - open print preview
function openPrintPreview(){
    window.open(document.location.protocol + "//" + document.location.host + document.location.pathname + '?print=true' + document.location.hash,'pp', "width=765,scrollbars=1");
}

// utility navigation - toggle site animations
function toggleAnimation() {
	
	switch($.cookie("yirShowAnimations")) {
		case "true":
			$("li#util-nav-animation a img").attr({
				src: siteUrl + "/global/img/util-nav-animation-start.png",
				attr: "Start Animation"
			});
			$.cookie("yirShowAnimations", "false", { expires: 14, path: "/" });
		break;
		
		case "false":
			$("li#util-nav-animation a img").attr({
				src: siteUrl + "/global/img/util-nav-animation-stop.png",
				attr: "Stop Animation"
			});
			$.cookie("yirShowAnimations", "true", { expires: 14, path: "/" });
		break;
		
		default:
			// if no cookie exists, or the yirShowAnimations cookie contains a non-boolean value, animations are enabled.
			// Therefore, disable animations if the two previous switch cases don't run.
			$("li#util-nav-animation a img").attr({
				src: siteUrl + "/global/img/util-nav-animation-start.png",
				attr: "Start Animation"
			});
			$.cookie("yirShowAnimations", "false", { expires: 14, path: "/" });
		break;
	}
}

// utility navigation - add bookmark
function addBookmark() {
	
	// get cookie data for existing bookmarks
	var bookmarks = $.cookie("yirBookmarks");
	var count = 0;
	
	// if the cookie already has data, load it into an object, otherwise create an empty object
	if (bookmarks) {
		bookmarks = $.evalJSON(bookmarks);
		count = bookmarks["count"];
	} else {
		bookmarks = new Object();
	}
	
	// append the new bookmark to the end of the existing object
	bookmarks[count] = {
		"title": $("div#container").attr("data-yirTitle"),
		"url": window.location.href
	};
	
	// load the bookmark into the list, handle the candystriping of the bookmarks list
	if ($("div#bookmarks-items-list table tr:last").hasClass("dark")) {
		$("div#bookmarks-items-list table").append('<tr><td><a href="'+ window.location.href +'">'+ $("div#container").attr("data-yirTitle") +'</a></td><td class="remove"><a href="#" id="bookmarks-remove-'+ count +'" class="bookmarks-remove"><img src="'+siteUrl+'/global/img/util-nav-bookmarks-remove.png" alt="Remove" width="49" height="13" /></a></td></tr>');
	} else {
		$("div#bookmarks-items-list table").append('<tr class="dark"><td><a href="'+ window.location.href +'">'+ $("div#container").attr("data-yirTitle") +'</a></td><td class="remove"><a href="#" id="bookmarks-remove-'+ count +'" class="bookmarks-remove"><img src="'+siteUrl+'/global/img/util-nav-bookmarks-remove.png" alt="Remove" width="49" height="13" /></a></td></tr>');
	}
	
	// show the bookmarks list
	$("div#bookmarks-items").show();
	
	// convert the object back to JSON and store it back in the cookie
	bookmarks["count"] = ++count;
	
	$.cookie("yirBookmarks", $.toJSON(bookmarks), {path: "/"});
}

// utility navigation - remove bookmark
function removeBookmark(el) {
	// get the numerical id of the bookmark being removed from the list
	var id = $(el).attr('id').substring(17);
	
	// remove the link item from the UI
	$(el).parent().parent().remove();
	
	// load the cookie data for the bookmarks
	var bookmarks = $.cookie("yirBookmarks");
	var count = 0;
	
	// if the cookie doesn't exist, exit
	if (!bookmarks) {
		return;
	}
	
	// convert cookie data from JSON
	if (bookmarks) {
		bookmarks = $.evalJSON(bookmarks);
		count = bookmarks["count"];
	}
		
	// remove the bookmark from the data loaded from the cookie
	delete bookmarks[id];
	delete bookmarks["count"];
	
	// re-build the bookmarks object with sequential index
	var cnt = 0;
	var newBookmarks = new Object();
	
	$.each(bookmarks, function(key, value) {
		newBookmarks[cnt] = {
			"title": value.title,
			"url": value.url
		};
		cnt++;
	});
	
	// save the new count of bookmarks
	newBookmarks["count"] = cnt;
	
	// store the modified data back in the cookie
	$.cookie("yirBookmarks", $.toJSON(newBookmarks), {path: "/"});
	
	// load the new data back into the UI
	loadBookmarks();
}

// utility navigation - load bookmarks into UI
function loadBookmarks() {
	
	// load data from cookie
	var bookmarks = $.cookie("yirBookmarks");
	var count = 0;
	
	// exit if there's no bookmarks
	if(!bookmarks) {
		return;
	}
	
	// if data is pulled from the cookie, convert it to JSON
	if (bookmarks) {
		bookmarks = $.evalJSON(bookmarks);
		count = bookmarks["count"];
	}
	
	// clear the bookmark list and reload fresh data from the cookie
	$("div#bookmarks-items-list table").html("");
	
	for (var i = 0; i < count; i++) {
		// load the bookmark into the list, handle the candystriping of the bookmarks list
		if ($("div#bookmarks-items-list table tr:last").hasClass("dark")) {
			$("div#bookmarks-items-list table").append('<tr><td><a href="'+ bookmarks[i].url +'">'+ bookmarks[i].title +'</a></td><td class="remove"><a href="#" id="bookmarks-remove-'+ i +'" class="bookmarks-remove"><img src="'+siteUrl+'/global/img/util-nav-bookmarks-remove.png" alt="Remove" width="49" height="13" /></a></td></tr>');
		} else {
			$("div#bookmarks-items-list table").append('<tr class="dark"><td><a href="'+ bookmarks[i].url +'">'+ bookmarks[i].title +'</a></td><td class="remove"><a href="#" id="bookmarks-remove-'+ i +'" class="bookmarks-remove"><img src="'+siteUrl+'/global/img/util-nav-bookmarks-remove.png" alt="Remove" width="49" height="13" /></a></td></tr>');
		}
	}
}

//include function
function loadInclude(path, f){
        
    //path is used as the name of the html file to be included and also as the name of the container where the data gets added to
    $.ajax({
        type: "GET",
        url: siteUrl + '/global/includes/' + path + '.html',
        dataType: "html",
        success: function(data){
            $('#' + path).html(data);
            if(f != undefined) f();
        }
        });
}

