/* fix IE background image flicker */
/*try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}*/

/*
	ESPN Navigation jQuery plugin
	takes a DIV id and enhances the CSS navigation elements with JavaScript
*/

Image1= new Image(253,142)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/bg-main-sport-content.png"

Image2= new Image(156,38)
Image2.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-main-sport-over.png"

Image2= new Image(156,38)
Image2.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-main-sport.png"

Image1= new Image(156,38)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-main-state-over.png"

Image1= new Image(156,38)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-main-state.png"

Image1= new Image(156,38)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-main-home.png"

Image1= new Image(156,38)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-main-home-over.png"

Image2= new Image(351,181)
Image2.src = "http://assets.espn.go.com/i/highschool/espnrise/bg-main-state-content.png"

Image1= new Image(150,186)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/bg-signin.jpg"

Image1= new Image(1,185)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/bg-choose.gif"

Image1= new Image(116,27)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/btn-choose-hover.gif"

Image1= new Image(50,20)
Image1.src = "http://assets.espn.go.com/i/highschool/espnrise/bg-signin-hover.gif"

;(function($) {

	var active = null, timer = null;

	$.fn.espnNav = function(options) {
		var pos = 'auto';
		options.position = options.position || 'auto';
		var id = $(this).attr('id') || 'nav'; // should be the id of the item we were passed
		$(this).find('ul:first > li').each(function(i) {
			$(this).attr('id',id+'-menu-items-'+i);
			var d = $(this).find('div:first');
			var tabWidth = d.parents('li').width();
			var w = d.width();
			var tabContentWidth = tabWidth > w ? tabWidth : w;

			// determine where to position the item
			var pos = 'auto';
			switch(options.position.toLowerCase()) {
				case 'auto':
					pos = 'auto';
					break;
				case 'center':
					var centerPos = Math.floor(($(this).offset().left));
					//console.log('center to ',centerPos);
					pos = Math.floor((centerPos - d.width() / 2 ) / 2) ;
					//console.log(pos);
					break;
			}

			d.css(
				{
					'left':pos,
					'display':'none',
					'width':tabContentWidth,
					'height':d.height()
				}
			);
			if(tabWidth > w) {
				d.find('ul').width(tabContentWidth);
			}

			// we need to have different on/off triggers for these elements


			$(this).bind(options.trigger,function() {

				// we need to make sure we clear the timeout for the onlick version as well
				if(options.trigger == 'click') {
					$(this).bind('mouseover',function() {
						clearTimeout(timer);
					});
				}
				$(this).addClass('hover');
				clearTimeout(timer); // clear the timer so we don't hide the active menu if it's the current item
				if( active && (active.attr('id') != $(this).attr('id')) ) {
					active.find('div').hide();
					active.removeClass('hover');
					active = null;
				}
				// find link lengths and adjust accordingly
				var d_w = d.width();
				var ul_w;
				var ul_widths = 0;
				var ul_count = 0;
				var processed = {};
				$(this).find('div').show().find('ul').each(function() {

					ul_count++;
					var ul = $(this);
					var div = ul.parent();
					if(ul.attr('id') === '') {
						var date = new Date(); var id = 'espn-div'+date.getTime();
						div.attr('id',id);
						processed[id]=true;
						if(ul.attr('id') === '') {
							var date = new Date();
							var id = 'espn-ul'+date.getTime();
							ul.attr('id',id);
							processed[id]=true;

							ul_w = ul.outerWidth();
							ul.children('li').each(function() {
								var li = $(this);
								var li_w = li.outerWidth();
								//console.log(li_w);
								if(li_w > ul_w) {
									ul_w = li_w;
								}
								li.width('100%');
							});
							//console.log(ul, 'setting ul width to ',ul_w)
							ul.width(ul_w);
							ul_widths += ul.outerWidth();
							//console.log('ul.outerWidth()',ul.outerWidth());
						}
						if(ul_count > 1) {
							d.width(ul_widths);
						} else {
							d.width(ul_w);
						}
					}
				});

				//console.log('ul_widths',ul_widths);
				//console.log(d_w,ul_widths);
				active = $(this);
				$(this).addClass('hover');
			});
			$(this).bind('mouseout',function() {
				timer = setTimeout('$.fn.espnNav.hideActive()',options.timeout);
			});
		});

		// return the main object for chainability;
		return this;
	};

	$.fn.espnNav.hideActive = function()
	{
		if(active) {
			active.find('div').hide();//fadeOut(50);
			active.removeClass('hover');
			active = null;
		}
	};

})(jQuery);

/* INITIALIZE THE PAGE */
$(function() {
	var originalBodyClass = $('body').attr('class');
	var b = $('body');
	$('#nav-main').espnNav({'trigger':'mouseover','timeout':500}).click(function(e) {
		$('li.active').removeClass('active');
		$(this).espnNav.hideActive();
	});

	$('.secondary-nav').espnNav({'trigger':'click','timeout':500,'position':'center'}).click(function(e) {
		if(e.target.tagName.toLowerCase() === 'a') {
			return false;
		}
	});

	$('li#myespn').hover(

		function(){
			$(this).addClass('hover');
			//var originalImageSrc = $(this).find('img').attr('src');
			$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow_active.gif');
			$('#myespn-content').show().hover(
				function(){$(this).show();$('li#myespn').addClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow_active.gif');},
				function(){$(this).hide();$('li#myespn').removeClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow.gif');}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#myespn-content').hide();
			$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow.gif');
		}

	);

	$('li#signin-dropdown').hover(
			function(){
				if($(this).find('img').attr('src') != undefined){

					$(this).addClass('hover');
					$('#signin-content').show().hover(
						function(){
							$(this).show();$('li#signin-dropdown').addClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow_active.gif');

						},
						function(){
							$('#signin-content').bind("mouseleave", function(e){
								// hide if we are out of our bounds
								if(e.pageX > 150 || e.pageX > 186 ){
								  $(this).hide();$('li#signin-dropdown').removeClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow.gif');
								}
							});

					});
				}
			},
			function(){
				$(this).removeClass('hover');
				$('#signin-content').hide();
				$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/myespn_down_arrow.gif');
			}

	);

	$('li#choose-state').hover(
		function(){
			//var originalImageSrc = $(this).find('img').attr('src');
			$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow.gif');
			$(this).addClass('hover');
			$('#choose-state-content').show().hover(
				function(){$(this).show();$('li#choose-state').addClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow.gif');},
				function(){$(this).hide();$('li#choose-state').removeClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow.gif');}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#choose-state-content').hide();
			$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow.gif');
		}

	);

	$('li#choose-sport').hover(
		function(){
			//var originalImageSrc = $(this).find('img').attr('src');
			$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow-active.gif');
			$(this).addClass('hover');
			$('#choose-sport-content').show().hover(
				function(){$(this).show();$('li#choose-sport').addClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow-active.gif');},
				function(){$(this).hide();$('li#choose-sport').removeClass('hover').find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow.gif');}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#choose-sport-content').hide();
			$(this).find('img').attr('src','http://assets.espn.go.com/i/highschool/espnrise/btn-choose-arrow.gif');
		}

	);

	$('#main-home').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }

	);

	$('#main-home-active').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }

	);

	$('#main-sport').hover(
		function(){
			$(this).addClass('hover');
			$('#main-sport-content').show().hover(
				function(){$(this).show();$('#main-sport').addClass('hover');},
				function(){$(this).hide();$('#main-sport').removeClass('hover');
				}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#main-sport-content').hide();
		}

	);

	$('#main-sport-active').hover(
		function(){
			$(this).addClass('hover');
			$('#main-sport-content').show().hover(
				function(){$(this).show();$('#main-sport').addClass('hover');},
				function(){$(this).hide();$('#main-sport').removeClass('hover');
				}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#main-sport-content').hide();
		}

	);

	$('#main-state').hover(
		function(){
			$(this).addClass('hover');
			$('#main-state-content').show().hover(
				function(){$(this).show();$('#main-state').addClass('hover');},
				function(){$(this).hide();$('#main-state').removeClass('hover');
				}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#main-state-content').hide();
		}

	);

	$('#main-state-active').hover(
		function(){
			$(this).addClass('hover');
			$('#main-state-content').show().hover(
				function(){$(this).show();$('#main-state').addClass('hover');},
				function(){$(this).hide();$('#main-state').removeClass('hover');
				}
			);
		},
		function(){
			$(this).removeClass('hover');
			$('#main-state-content').hide();
		}

	);

	$('body').click(function(e) {
		if(e.target.tagName.toLowerCase() == 'a') {
			b.attr('class',originalBodyClass); // reset the body class
			var content = '';
			if( $(e.target).parents('#nav-wrapper').length > 0 ) {
				b.addClass('secondary');
			} else {
				$('li.active').removeClass('active');
				$(this).espnNav.hideActive();
			}

			$(e.target).parents('li').each(function(i) {
				$(this).addClass('active');
				var navItem = $(this).find('a').html();
				if(content.length > 0) { content = ' &gt; '+content; }
				content = navItem + content;
				b.addClass( $(this).find('a').attr('href').replace('#','') );
			});
			b.addClass( $(e.target).attr('href').replace('#','') );

			if($('li.active').length > 0) {
				var val = $('li.active').find('a').html();
				if(content.indexOf(val) < 0) {
					content = val + ' &gt; ' +content;
				}
				b.addClass($('li.active').find('a').attr('href').replace('#',''));
			}
			$('#bodyTarget').html(content);
			//return false;
		}
	});

	$('li.teams > a').click(function() {
		var teamItem = $(this);
		teamItem.css('background-color','#fff');
		var teamDiv = $('<div/>').css({
			'position':'absolute',
			'width':'300px',
			'height':'150px',
			'top':teamItem.offset().top + teamItem.height() +'px'	,
			'left':teamItem.offset().left - 150 +'px',
			'background-color':'#fff'
		}).hover(
			function() {teamItem.css('background-color','#fff');$(this).show();},
			function() {teamItem.css('background-color','transparent');$(this).remove();}
		);
		var teamUL = $('<ul class="teams" />');
		for(var i=0; i<15; i++) {
			var li = $('<li><a href="#team">Team '+(i+1)+'</a></li>');
			li.css({
				'float':'left',
				'width':'30%',
				'padding':'1%'
			});
			teamUL.append(li);
		}
		teamDiv.append(teamUL);
		$('body').append(teamDiv);
	});


});


/*function toggleScores(gametext, linktext) {
	var el = document.getElementById(gametext);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
	link = document.getElementById(linktext);
	link.innerHTML = (link.innerHTML != 'Hide' ? 'Hide' : 'Show');
}

function open(gametext, linktext) {
	var x = document.getElementById(gametext);
	x.style.display = (x.style.display == 'none' ? ' ':' ');
	var y = document.getElementById(linktext);
	y.innerHTML = (y.innerHTML == 'Show' ? 'Hide': 'Show');
}

 $(function() {
 	$('#scoreboard').tabs({ fxSlide: true, fxFade: true, fxSpeed: 'fast' });
 });*/