/*javascript for all pages*/

var win = null;

function newWindow(mypage,myname,w,h,features) {
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

$(document).ready(function(){
	
	//jCarousel init
	jCarousel.init();
	
	//enlarge font size
	fontSizeButton.init();
	
	//adding delay to menu
	//Menu.init();

	//video embed
	video.init();
	
	//games: overlay	
    $("a#button_find[rel]").overlay();  
    
    //faq:overlay
     $("a.retailers[rel]").overlay(); 
    
    //linking small boxes
    linkBoxes.init();
	
	$("#t-and-c").click(function() {
		newWindow('/terms','','650','550','toolbar=0,status=0,scrollbars=0,address=0');
		return false;
	})
});



var linkBoxes = {
	init: function () {
			
			$("#index .box-small").each(function(){
				$(this).attr("title", $(this).find("a").attr("title"));
				$(this).click(function() {
					location.href = $(this).find("a").attr("href");
					return false;
				});
				
			});
 
		$("html").addClass("has-js");
	}
}




//embedding videos
var video = {
	
	init: function() {			

		//flash for games, ds + character pages	
		if ( $("div#flash").length > 0 ) {
			var flashvars = {};
		   	var params = { wmode: "opaque", allowFullScreen:"true"};
		   	var attributes = {};
			swfobject.embedSWF('/public/swf/player.swf?file=' + $('div#flash').attr('rel'), "flash", "529", "296", "9.0.0", '' , flashvars, params, attributes);
		}

		//flash for homepage
		if ( $("div#flash-home").length > 0 ) {
			var flashvars = {};
			var params = { wmode: "opaque", allowFullScreen:"true"};
			var attributes = {};
			swfobject.embedSWF($('div#flash-home').attr('rel'), "flash-home", "990", "370", "9.0.0", '' , flashvars, params, attributes);			
		}	
		
		//flash for your-questions-answered page
		if ( $("div#flash-faq").length > 0 ) {
			var flashvars = {};
		   	var params = { wmode: "opaque", allowFullScreen:"true"};
		   	var attributes = {};
		   	
			swfobject.embedSWF('/public/swf/player.swf?file=' + $('div#flash-faq').attr('rel'), "flash-faq", "529", "296", "9.0.0", '', flashvars, params, attributes);
		}		
		
		
	}
}



var jCarousel = {
	init: function () {
		
		jQuery('#screenshots').jcarousel({
	        scroll: 1,
	        visible: 1
    	});	
		
		//for games: more games
		/*jQuery('#more-games').jcarousel({
	        scroll: 1,
	        visible: 8
    	});*/
  

	}
}


var fontSizeButton = {
	init: function () {		

		var fontSizeStages = new Array ();
		fontSizeStages[0] = '62.5%';
		fontSizeStages[1] = '80.0%';
		fontSizeStages[2] = '100%';
	
		
		if ($.cookie('nintendo_fontsize')) {
			currentFontSize = $.cookie('nintendo_fontsize');	
		} else {
			currentFontSize = 0;	
		}
		
		$('body').css('font-size', fontSizeStages[currentFontSize]);
			
		
		//applying event to the button
		$("#button_enlarge-site-text").click(function(){

			if (fontSizeStages[parseInt(currentFontSize) + 1]) {
				
				var newfontSize = parseInt(currentFontSize) + 1;		
				
				
			} else {
				
				var newfontSize = 0;
				
			}			
			
			currentFontSize = newfontSize;
			
			//saving it in cookie too so other pages can grab it...
			$.cookie('nintendo_fontsize', newfontSize, { expires: 7 });
			
			$('body').css('font-size', fontSizeStages[newfontSize]);	

			return false;
		})	
		
		
		
			
		
	}
}


var Menu = {};

	// config
	Menu.hideDataKey = "hideAt";
	Menu.hideDelay = 500;
	Menu.hoverClass = "hover";
	Menu.itemSelector = "#navi-main > li.games";
	Menu.itemLinkSelector = "#navi-main > li.games > a";
	Menu.rolloverClass = "rollover";
	Menu.subMenuSelector = "#navi-main ul.submenu";
	Menu.subMenuLinkSelector = "#navi-main ul.submenu a";
	
	Menu.hideUnusedMenus = function() {
	    $.each($(Menu.itemSelector), function() {
	            if (!$(this).hasClass(Menu.hoverClass) && $(this).data(Menu.hideDataKey) < new Date().getTime()) {
	                $(this).removeClass(Menu.rolloverClass);
	            }
	        }
	    );
	};
	Menu.init = function(callback) {
	    $(Menu.itemSelector).hover(Menu.item_Mouseover, Menu.item_Mouseout);
	    $(Menu.subMenuSelector).hover(Menu.subMenu_Mouseover, Menu.subMenu_Mouseout);
	    $(Menu.itemLinkSelector).blur(Menu.item_Blur).focus(Menu.item_Focus);
	    $(Menu.subMenuLinkSelector).blur(Menu.subMenu_Blur).focus(Menu.subMenu_Focus);
	    if (callback && typeof (callback) == "function") {
	        callback();
	    }
	};
	Menu.item_Blur = function() {
	    $(this).closest(Menu.itemSelector).removeClass(Menu.hoverClass);
	    Menu.setHideItemTime($(this).closest(Menu.itemSelector));
	};
	Menu.item_Focus = function() {
	    $(this).closest(Menu.itemSelector).siblings().removeClass(Menu.rolloverClass).removeClass(Menu.hoverClass);
	    $(this).closest(Menu.itemSelector).addClass(Menu.rolloverClass).addClass(Menu.hoverClass);
	};
	Menu.item_Mouseover = function() {
	    $(this).siblings().removeClass(Menu.rolloverClass).removeClass(Menu.hoverClass);
	    $(this).addClass(Menu.rolloverClass).addClass(Menu.hoverClass);
	};
	Menu.item_Mouseout = function() {
	    $(this).removeClass(Menu.hoverClass);
	    Menu.setHideItemTime($(this));
	};
	Menu.setHideItemTime = function(item) {
	    $(item).data(Menu.hideDataKey, new Date().getTime() + parseInt(Menu.hideDelay * .9));
	    setTimeout(Menu.hideUnusedMenus, Menu.hideDelay + 1);
	};
	Menu.subMenu_Blur = function() {
	    $(this).closest(Menu.itemSelector).removeClass(Menu.hoverClass);
	    Menu.setHideItemTime($(this).closest(Menu.itemSelector));
	};
	Menu.subMenu_Focus = function() {
	    $(this).closest(Menu.itemSelector).addClass(Menu.hoverClass);
	};
	Menu.subMenu_Mouseover = function() {
	    $(this).closest(Menu.itemSelector).addClass(Menu.hoverClass);
	};
	Menu.subMenu_Mouseout = function() {
	    $(this).closest(Menu.itemSelector).removeClass(Menu.hoverClass);
	    Menu.setHideItemTime($(this).closest(Menu.itemSelector));
	    
};













