document.write('<script src="/wgbh/masterpiece/js/jquery.hoverIntent_r5.min.js" type="text/javascript"></script>');

var menuJSON = '{ "season" : [ \
								{ "name":"classic", \
								  "program": \
									[ {"title":"Return to Cranford", "link":"/wgbh/masterpiece/cranford2/index.html", "date":"Dec 4 &amp; 11"}, \
									{"title":"Downton Abbey, Season 1", "link":"/wgbh/masterpiece/downtonabbey/season1.html", "date":"Dec 18, 25 &amp; Jan 1"}, \
									{"title":"Downton Abbey, Season 2", "link":"/wgbh/masterpiece/downtonabbey/season2.html", "date":"Jan 8-Feb 19"}, \
									{"title":"The Old Curiosity Shop", "link":"/wgbh/masterpiece/curiosityshop/index.html", "date":"Feb 26"}, \
									{"title":"Great Expectations", "link":"/wgbh/masterpiece/greatexpectations/index.html", "date":"April 1 &amp; 8"}, \
									{"title":"The Mystery of Edwin Drood", "link":"", "date":"April 15"}, \
									{"title":"Birdsong", "link":"", "date":"April 22 &amp; 29"} \
									] \
								}, \
								{ "name":"mystery", \
								  "program": \
									[ {"title":"2012 Mystery! titles forthcoming", "link":"", "date":""} \
									] \
								}, \
								{ "name":"contemporary", \
								  "program": \
									[ {"title":"2012 Contemporary titles forthcoming", "link":"", "date":""} \
									] \
								} \
							] \
				}';

//									[ {"title":"2011 Contemporary titles forthcoming", "link":"", "date":""} \

var currentSeason = "classic";  // reset this variable to change the site's default season [classic|mystery|contemporary]
var navSeason;
var navSection;

// parse the JSON driving the menus
var menu = jQuery.parseJSON(menuJSON);

function writeMenu(seasonObj){
	var seasonMenuID = "#s_" + seasonObj.name + "_sub";
	// iterate over the programs in the season and write out the appropriate list items
	$.each(seasonObj.program, function() {
			if (this.link) { // normal linked item
				var item = '<li><a href="' + this.link + '">' + this.title + '<\/a> <span class="date">' + this.date + '<\/span><\/li>\r'
			} else if (!this.link && !this.date) { // line of plain text
				var item = '<li class="nosite plaintext"><a href="#">' + this.title + '<\/a><\/li>\r'
			} else { // unlinked item -- no site available yet
				var item = '<li class="nosite"><a href="#">' + this.title + '<\/a> <span class="date">' + this.date + '<\/span><\/li>\r'
			}
			$(seasonMenuID).append(item);
		});
	// hijack linking for all the list items
	$(seasonMenuID + ' > li').click(function() {
			if ($(this).children('a').attr('href') != "#") {
				var destination = $(this).children('a').attr('href');
				window.location.assign(destination);
			} else {
				return false;
			}
		});
	// add rollovers for all the list items
	$(seasonMenuID + ' > li').mouseover(function() {
//			if ($(this).children('a').attr('href') != "#") { // this test is insufficient for IE's needs
			if (!($(this).children('a').attr('href').match(/#$/))) { // this test covers issues with IE thinking the value of the href attribute is the full URL of the page plus the hash, rather than just the hash as set in the code on the page
				$(this).addClass('hover');
			}
		});

	$(seasonMenuID + ' > li').mouseout(function() {
			if (!($(this).children('a').attr('href').match(/#$/))) {
				$(this).removeClass('hover');
			}
		});

	// resize the menus based on the width of their content

	var titleSize = 0;
	var dateSize = 0;
	
	// ask each title how big it is
	$(seasonMenuID + ' > li > a').each(function(index) {
			if ( $(this).outerWidth() > titleSize) {
				titleSize = $(this).outerWidth();
			}
		});
	
	// ask each date how big it is
	$(seasonMenuID + ' > li > span').each(function(index) {
			if ( $(this).outerWidth() > dateSize) {
				dateSize = $(this).outerWidth();
			}
		});
	
	// set all the titles to the width of the widest one plus one pixel (to deal with rounding issues)
	// in order to make the dates left-align correctly
	$(seasonMenuID + ' > li > a').css('width', titleSize+1 + "px"); 
	
	// set all the dates to the width of the widest one plus one pixel (to deal with rounding issues)
	// in order to keep IE6/7 happy
	$(seasonMenuID + ' > li > span').css('width', dateSize+1 + "px"); 
	
	// let's set the width of the menu
	var menuSize;

	// first, iterate over the items in the menu to see if any is more than just a plain text entry
	var hasAttrs = 0 ;
	$.each(seasonObj.program, function() {
			if ( this.link || this.date) {
				hasAttrs += 1;
			}
		});
	
	if (hasAttrs) { // if so, set menu size to include the gutter
		menuSize = (titleSize + 1) + 20 + (dateSize + 1) + 16;
	} else {  // if the menu only contains a line of text, remove the 20px gutter and the date space
		menuSize = (titleSize + 1) + 16;
	}

	$(seasonMenuID).css('width', menuSize);
}



$(document).ready(function(){
	// build the menus
	$.each(menu.season, function() {
		// pass each season in turn to the writeMenu function
		writeMenu(this);
	});

	// hide the menus
	$('#seasons > li > ul').css('display','none');


	// initialize the 3 season buttons
	// NB: Are you getting an error telling you that mouseenter isn't a function?
	// Make sure that no older versions of jQuery are overriding the site's default version.
	// A frequent culprit is the call to jQuery in PBS's social bookmarking widget.

	/*
	$('#seasons > li').mouseenter(function() {
			$(this).children('ul').slideDown(100);
			// make sure the season buttons stay lit when hovering over the submenu
			$(this).children('.seasonname').addClass('light');
		});

	$('#seasons > li').mouseleave(function() {
			$(this).children('ul').slideUp(100);
			$(this).children('.seasonname').removeClass('light');
		});
	*/
	
	// start Kal's code
	// try event reactions with hoverIntent
    function showMenu(){
		$(this).children('ul').slideDown(100);
		// make sure the season buttons stay lit when hovering over the submenu
		$(this).children('.seasonname').addClass('light');
    }

    function hideMenu(){
		$(this).children('ul').slideUp(100);
		$(this).children('.seasonname').removeClass('light');
    }

    var menuConfig = {
         interval: 100,
         sensitivity: 10,
         over: showMenu,
         timeout: 100,
         out: hideMenu
    };

    $('#seasons > li').hoverIntent(menuConfig);
	// end Kal's code

	// configure the menus to reflect the local settings of the page they're appearing on
	// season::
	// mpt index changes with the current season
	// mpt site level pages are seasonless -- that is, nothing should be selected in the menu
	// individual program site pages are always selecting the season they aired as
	if (navSeason == "current") {
		$("#s_" + currentSeason + " > a").addClass('selected'); // use current season's value
	} else if (navSeason == "classic" || navSeason == "mystery" || navSeason == "contemporary") {
		$("#s_" + navSeason + " > a").addClass('selected'); // use value set on individual pages
	} else {
		; // default to nothing selected
	}
	// section::
	// mpt index and site level pages are set to their appropriate section 
	// individual program site pages always have all options off
	if (navSection) {	
		$("#nav_" + navSection).addClass('selected'); // use value set on individual pages
	} else {
		; // default to nothing selected
	}

	window.name = "main";// name current window "main"

});

