var ModalConfirmButton = null;

$(document).ready(function(){


	$("#header li img").navRollovers();
	$('.rollOver').imgflip();
	$('#header').topNavDropDowns();
	$('#music_catalog').downloadableInfoBox();
	
	ModalConfirmButton = $("#bumperBtn a")[0];


});

jQuery.fn.navRollovers = function() {

    $("#header li img").hover(
		function () {

			var imgSrc = $(this).attr("src");
			if (imgSrc.indexOf("_off") != -1) {
				var img_on = imgSrc.replace("_off", "_on");
				$(this).attr("src", img_on);

			} 


		}, 
		function () {
			var imgSrc = $(this).attr("src");

			if ( $(this).hasClass("nav_locked") ) { 
				//used to ensure menu dropdown button states are not changed on mouse out.	  
			} else {
			
				if (imgSrc.indexOf("_on") != -1) {
					var img_off = imgSrc.replace("_on", "_off");
					$(this).attr("src", img_off);				
				
				}
			
			}

		}
    );

}

var ro = {
	delay: 100,
	games: {
		isOver: false,
		isactive: false,		
		intervalID: null
	},
	music: {
		isOver: false,
		isactive: false,
		intervalID: null
	},
	media: {
		isOver: false,
		isactive: false,
		intervalID: null
	},
	buzz: {
		isOver: false,
		isactive: false,
		intervalID: null
	},
	stage: {
		isOver: false,
		isactive: false,
		intervalID: null
	},
	closeMenuDropDown: function(isactive, isOver, intervalID, selector, menuID)
	{
		if(!isOver)
		{
			// nav image to off state	
			var img_src_raw = $(selector).attr("src");
			if (isactive) {
				var img_on = img_src_raw.replace("_don", "_active");
			} else {
				var img_on = img_src_raw.replace("_don", "_off");			
			}
			$(selector).attr("src", img_on);		
			
			clearInterval(intervalID);
			$("#" + menuID).addClass("hide");
			intervalID = null;
			isOver = false;
			return;
		}
	}
};

jQuery.fn.topNavDropDowns = function() {

/*
	$("#top_nav_language").toggle(function(evt) {
		showLanguageDropDown();
	}, function() {
		hideLanguageDropDown();
	});
*/
	function showLanguageDropDown() 
	{
		/*
		if($('#language_drop_down').length) {
			$('#language_drop_down').removeClass('hide');
			var imgSrc = $("#top_nav_language img").attr("src");
			var img_off = imgSrc.replace("_off", "_on");
			$("#top_nav_language img").attr("src", img_off);
		}
		*/
	}

	function hideLanguageDropDown() 
	{
		/*
		if($('#language_drop_down').length && !$('#language_drop_down').hasClass('hide')) {
			$('#language_drop_down').addClass('hide');
			var imgSrc = $("#top_nav_language img").attr("src");
			var img_off = imgSrc.replace("_on", "_off");
			$("#top_nav_language img").attr("src", img_off);
		}
		*/
	}


	$("#log_in_button_link").toggle(function(evt) {
		showLoginDropDown();
	}, function() {
		hideLoginDropDown();
	});
		
	function showLoginDropDown() 
	{
		if($('#log_in_drop_down').length) {
			$('#log_in_drop_down').removeClass('hide');
			var imgSrc = $("#log_in_button_link img").attr("src");
			var img_off = imgSrc.replace("_off", "_on");
			$("#log_in_button_link img").attr("src", img_off);	
		}			
	}
	
	function hideLoginDropDown()
	{	
		if($('#log_in_drop_down').length && $('#log_in_button_link').length && !$('#log_in_drop_down').hasClass('hide')) {
			$('#log_in_drop_down').addClass('hide');
			var imgSrc = $("#log_in_button_link img").attr("src");
			var img_off = imgSrc.replace("_on", "_off");
			$("#log_in_button_link img").attr("src", img_off);	
		}			
	}
	
	$("#main_nav_games img").mouseover(
		function () {
			if(!$("#games_drop_down:visible").length)
			{				
				var imgSrc = $(this).attr("src");
				if (imgSrc.indexOf("_active") != -1) {
					var img_off = imgSrc.replace("_active", "_don");
					ro.games.isactive = 1;
				} else {
					var img_off = imgSrc.replace("_on", "_don");
			
				}
				$(this).attr("src", img_off);
				$("#games_drop_down").removeClass("hide");	
				hideLanguageDropDown();
				hideLoginDropDown();
				ro.games.isOver = true;
			}
	}).mouseout(	
		function () {
			ro.games.isOver = false;
			clearInterval(ro.games.intervalID);
			ro.games.intervalID = setInterval("ro.closeMenuDropDown( ro.games.isactive, ro.games.isOver,ro.games.intervalID,'#main_nav_games img','games_drop_down')", ro.delay);
		}		
	);
	
	$("#games_drop_down").mouseover(function() {
		ro.games.isOver = true;
	}).mouseout(function() {
		ro.games.isOver = false;
	});
	
/*
	$("#main_nav_buy_now img").toggle(
		function () {
	
			var log_in_img_src_raw = $(this).attr("src");
			var log_in_img_src_length = log_in_img_src_raw.length;
			var img_src_cut = log_in_img_src_raw.substring(0, (log_in_img_src_length - 7) );
			var img_off = img_src_cut + '_active.png'; 

			$(this).attr("src", img_off)
			$(this).addClass("nav_locked");
			$("#buy_now_drop_down").removeClass("hide");			
			
		},
		function () {
			var log_in_img_src_raw = $(this).attr("src");
			var log_in_img_src_length = log_in_img_src_raw.length;
			var img_src_cut = log_in_img_src_raw.substring(0, (log_in_img_src_length - 11) );
			var img_on = img_src_cut + '_on.png'; 

			$(this).attr("src", img_on)
			$(this).removeClass("nav_locked");		
			$("#buy_now_drop_down").addClass("hide");			
		
		}
	);
*/

	$("#main_nav_music img").mouseover(
		function () {
			if(!$("#music_drop_down:visible").length)
			{				
				var imgSrc = $(this).attr("src");

				if (imgSrc.indexOf("_active") != -1) {
					var img_off = imgSrc.replace("_active", "_don");
					ro.music.isactive = 1;
				} else {
					var img_off = imgSrc.replace("_on", "_don");
			
				}
			
				$(this).attr("src", img_off);
				$("#music_drop_down").removeClass("hide");	
				hideLanguageDropDown();
				hideLoginDropDown();
				ro.music.isOver = true;	
			}						
	}).mouseout(
		function () {
			ro.music.isOver = false;
			clearInterval(ro.music.intervalID);
			ro.music.intervalID = setInterval("ro.closeMenuDropDown( ro.music.isactive, ro.music.isOver,ro.music.intervalID,'#main_nav_music img','music_drop_down')", ro.delay);
		}
	);
	
	$("#music_drop_down").mouseover(function() {
		ro.music.isOver = true;
	}).mouseout(function() {
		ro.music.isOver = false;
	});

	$("#main_nav_media img").mouseover(
		function () {
			if(!$("#media_drop_down:visible").length)
			{				
				var imgSrc = $(this).attr("src");

				if (imgSrc.indexOf("_active") != -1) {
					var img_off = imgSrc.replace("_active", "_don");
					ro.media.isactive = 1;
				} else {
					var img_off = imgSrc.replace("_on", "_don");
			
				}							
				
				$(this).attr("src", img_off);
				$("#media_drop_down").removeClass("hide");
				hideLanguageDropDown();
				hideLoginDropDown();
				ro.media.isOver = true;					
			}			
	}).mouseout(
		function () {
			ro.media.isOver = false;
			clearInterval(ro.media.intervalID);
			ro.media.intervalID = setInterval("ro.closeMenuDropDown( ro.media.isactive, ro.media.isOver,ro.media.intervalID,'#main_nav_media img','media_drop_down')", ro.delay);	
		}
	);

	$("#media_drop_down").mouseover(function() {
		ro.media.isOver = true;
	}).mouseout(function() {
		ro.media.isOver = false;
	});
	
	$("#main_nav_buzz img").mouseover(
		function () {	
			if(!$("#buzz_drop_down:visible").length)
			{				
				var imgSrc = $(this).attr("src");
				if (imgSrc.indexOf("_active") != -1) {
					var img_off = imgSrc.replace("_active", "_don");
					ro.buzz.isactive = 1;
				} else {
					var img_off = imgSrc.replace("_on", "_don");
			
				}				
				
				$(this).attr("src", img_off);
				$("#buzz_drop_down").removeClass("hide");
				hideLanguageDropDown();
				hideLoginDropDown();				
				ro.buzz.isOver = true;
			}				
	}).mouseout(
		function () {
			ro.buzz.isOver = false;
			clearInterval(ro.buzz.intervalID);
			ro.buzz.intervalID = setInterval("ro.closeMenuDropDown( ro.buzz.isactive, ro.buzz.isOver,ro.buzz.intervalID,'#main_nav_buzz img','buzz_drop_down')", ro.delay);			
		}
	);

	$("#buzz_drop_down").mouseover(function() {
		ro.buzz.isOver = true;
	}).mouseout(function() {
		ro.buzz.isOver = false;
	});
	
	$("#main_nav_stage img").mouseover(
		function () {
			if(!$("#stage_drop_down:visible").length)
			{				
				var imgSrc = $(this).attr("src");
				if (imgSrc.indexOf("_active") != -1) {
					var img_off = imgSrc.replace("_active", "_don");
					ro.stage.isactive = 1;
				} else {
					var img_off = imgSrc.replace("_on", "_don");
			
				}					
				
				$(this).attr("src", img_off);
				$("#stage_drop_down").removeClass("hide");
				hideLanguageDropDown();
				hideLoginDropDown();
				ro.stage.isOver = true;
			}			
	}).mouseout(
		function () {
			ro.stage.isOver = false;
			clearInterval(ro.stage.intervalID);
			ro.stage.intervalID = setInterval("ro.closeMenuDropDown( ro.stage.isactive, ro.stage.isOver,ro.stage.intervalID,'#main_nav_stage img','stage_drop_down')", ro.delay);
		}
	);

	$("#stage_drop_down").mouseover(function() {
		ro.stage.isOver = true;
	}).mouseout(function() {
		ro.stage.isOver = false;
	});
	
    $("#games_column_2 li").hover(
		function () {
			$("#games_column_2 li").removeClass('current');	
			$(this).addClass('current');
			var link_id = this.id;
			var link_id_length = link_id.length;
			var section_id = link_id.substring(0, (link_id_length - 5) );
			var section_id_element = '#' + section_id;
			$(".games_column_3").addClass('hide');
			$(section_id_element).removeClass('hide');
	
		}, 
		function () {

		}
    );


    $("#buy_now_column_2 li").hover(
		function () {
			$("#buy_now_column_2 li").removeClass('current');	
			$(this).addClass('current');
			var link_id = this.id;
			var link_id_length = link_id.length;
			var section_id = link_id.substring(0, (link_id_length - 5) );
			var section_id_element = '#' + section_id;
			$(".buy_now_column_3").addClass('hide');
			$(section_id_element).removeClass('hide');
	
		}, 
		function () {

		}
    );


	$("#all_games_drop_down_btn").toggle(
		 function () {
			$("#all_games_drop_down").removeClass("hide");
		  },
		 function () {
			$("#all_games_drop_down").addClass("hide");
		
	  }
	);	

	$("#all_platforms_drop_down_btn").toggle(
		function () {
			$("#all_platforms_drop_down").removeClass("hide");
		},
		function () {
			$("#all_platforms_drop_down").addClass("hide");
		
		}
	);
	
	// if it's and external link... send it through the now leaving dialog
	/*
	$("a").click(function(){
		
		action = true;
		
		// no need to make this perform the Lookup everytime.
		//if($(this)[0] != $("#bumperBtn a")[0])
		if($(this)[0] != ModalConfirmButton)
		{
			url = ($(this).attr('href'));
		
			if(url.indexOf('http://') == -1)
			{
				action =  true;
			}
			else
			{
				if(url.indexOf('ghhub.atvi.2advanced.com') > 0 || 
				   url.indexOf('guitarhero.com') > 0)
				{
					action = true;
				}
				else
				{
					openNowLeaving(url);
					action = false;
				}
			}
		}
		
		return action;
	})
	*/

}



jQuery.fn.downloadableInfoBox = function() {

	$(".downloadable").live('click', function() {
		var infoBoxDataElt = $(".info_box_data", this);
		var info_box_dataClicked = infoBoxDataElt.text();
		var info_box_data;
		if($(this).hasClass("isInfoBoxHidden"))
		{			
			$(".downloadable").each(function() {
				infoBoxDataElt = $(".info_box_data", $(this));
				info_box_data = infoBoxDataElt.text();
				hideInfoBox($(this), info_box_data);
			});
			showInfoBox($(this), info_box_dataClicked);
		}
		else
		{
			hideInfoBox($(this), info_box_dataClicked);
		}		
	});	
	
	
	function showInfoBox(downloadableElt, info_box_data)
	{
		downloadableElt.removeClass("isInfoBoxHidden");
		downloadableElt.append('<div id="downloadable_info_box"><div id="info_box_top_bg"><div id="info_box_bottom_bg"><div id="info_box_top_area">	<p>Downloadable Song Compatibility</p></div><div id="info_box_bottom_area"><p>' + info_box_data + '</p></div></div></div></div>	');	
	}
	
	function hideInfoBox(downloadableElt, info_box_data)
	{
		downloadableElt.addClass("isInfoBoxHidden");
		downloadableElt.html('<a href="javascript:;"><span class="downloadableTxt">Downloadable </span><img src="themes/guitarhero/images/misc_icon_eyeglass.png" width="17" height="17" alt="" /></a><div class="info_box_data hide">' + info_box_data + '</div>');
	}
}































