var carousel = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery(".rack1 .rack-design1").cycle({
			fx: "scrollLeft",
			pager: ".pagination",
			speed: 600,
			timeout: 13500
		});
	}
};

var ticker = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery("#ticker ul").liScroll(); 
	}
};


var modal = {
	init: function() {
		this.bind();		
	},
	bind: function() {
		jQuery("#videos .media-video a").click(function() {
			jQuery("#video_modal iframe").eq(0).attr("src", "http://www.youtube.com/embed/" + jQuery(this).attr("rel"));
			jQuery.colorbox({height:"545px", href:"#video_modal", inline:true, width:"640px"})
		});

		jQuery("#stage div.media-video a").click(function() {			
			jQuery("#video_modal iframe").eq(0).attr("src", "http://www.youtube.com/embed/" + jQuery(this).attr("rel"));
			jQuery.colorbox({height:"545px", href:"#video_modal", inline:true, width:"640px"})
		});
		
		jQuery("#news_item_media div.media-video a").click(function() {			
			jQuery("#video_modal iframe").eq(0).attr("src", "http://www.youtube.com/embed/" + jQuery(this).attr("rel"));
			jQuery.colorbox({height:"545px", href:"#video_modal", inline:true, width:"640px"})
		});
		
		/*jQuery("#screenshots a img").click(function() {
			currentImageId = jQuery(this).parent().attr("id");
			var overlayId = "#" + currentImageId + "_overlay";
			jQuery("#screenshot_modal h3").html(jQuery(this).parent().parent().find("h5").text());
			jQuery("#screenshot_modal img").attr("src", jQuery(overlayId).html());
			jQuery.colorbox({height:"315px", href:"#screenshot_modal", inline:true, width:"540px"})
		});
		
		jQuery("#wallpapers a img").click(function() {
			currentImageId = jQuery(this).parent().attr("id");
			var overlayId = "#" + currentImageId + "_overlay";
			jQuery("#screenshot_modal h3").html(jQuery(this).parent().parent().find("h5").text());
			jQuery("#screenshot_modal img").attr("src", jQuery(overlayId).html());
			jQuery.colorbox({height:"315px", href:"#screenshot_modal", inline:true, width:"540px"})
		});*/
		
		jQuery("a[rel='wp_slideshow'], a[rel='sc_slideshow']").colorbox({
			height:"400px",
			href:"#wallpaper_modal", 
			inline:true,
			width:"600px",
			onLoad: function(){
				var overlayImgId =  "#" + jQuery(this).attr("id") + "_overlay";
				currentImageId = jQuery(this).attr("id");
				jQuery("#wallpaper_modal img").attr("src", jQuery(overlayImgId).html());				
			}
		});
		
		jQuery("#community_bottom_last_news > ul li a").click(function() {
			var section = jQuery(this).attr("href");
			section = section.replace(/#/g, "");
			jQuery("#community_bottom_last_news > ul li").removeClass("active");
			jQuery("#community_bottom_last_news > div > div").hide();
			jQuery(this).parent().addClass("active");
			jQuery("#community_" + section).show();
		});
		
	}
};

var fixes = {
	init: function() {
		
	},
	bind: function() {
		
	},
	nav: function() {
		jQuery("#nav").clone().appendTo("#nav").removeClass("png");
	}
}


jQuery(document).ready(function(){
	if(jQuery("table.mgnlControlBar").length) {
		jQuery("#nav").css("position", "static");
		jQuery("#logo").css("display", "none");
		jQuery(".jspContainer").css("overflow", "auto");
	}
});

var about_carousel = {
	init: function() {
		this.bind();
	},
	bind: function() {
			jQuery("#about_carousel ul").cycle({
			fx: "scrollHorz",
			timeout: 0
		});
	jQuery("#nav_community > li:nth-child(3)").append("<ul id='subnav'><li class='about'><a href=''>About</a></li><li class='about-char'><a href=''>Characters</a></li><li class='about-powers'><a href=''>Powers</a></li><li class='about-new-marias'><a href=''>New Marias</a></li><li class='about-morality'><a href=''>Morality</a></li></ul>");
		
		    var homeLink = jQuery(".home-link").attr("href");
			if(homeLink!=null && homeLink.indexOf(".html")>0)
				homeLink = homeLink.replace(".html","/");
			jQuery("#subnav li.about a").attr('href', homeLink+'the-game.html#' );
			jQuery("#subnav li.about-char a").attr('href', homeLink+'the-game.html#characters' );
			jQuery("#subnav li.about-powers a").attr('href', homeLink+'the-game.html#powers' );
			jQuery("#subnav li.about-new-marias a").attr('href', homeLink+'the-game.html#newmarias' );
			jQuery("#subnav li.about-morality a").attr('href', homeLink+'the-game.html#morality' );
		
			jQuery("#subnav li.about a").attr('target', '_top');
			jQuery("#subnav li.about-char a").attr('target', '_top');
			jQuery("#subnav li.about-powers a").attr('target', '_top');
			jQuery("#subnav li.about-new-marias a").attr('target', '_top');
			jQuery("#subnav li.about-morality a").attr('target', '_top');
			
		jQuery("li").hover(
			  function () {
				jQuery(this).children("#subnav").show();
			  }, 
			  function () {
				jQuery(this).children("#subnav").hide();
			  }
			);
		jQuery("#about_carousel ul .arrow").click(function() {
			jQuery("#about_carousel ul").cycle(parseInt(jQuery(this).attr("rel")) - 1);
			return false;
		});

		var hash = document.location.hash;
		hash = hash.replace(/#/ig, "");

		switch(hash){
			case "characters":
				jQuery("#about_carousel ul").cycle(1);
				break;
			case "powers":
				jQuery("#about_carousel ul").cycle(2);
				break;
			case "newmarias":
				jQuery("#about_carousel ul").cycle(3);
				break;
			case "morality":
				jQuery("#about_carousel ul").cycle(4);
				break;
			default:
				jQuery("#about_carousel ul").cycle(0);
				break;
		}

		jQuery("#subnav a").click(function() {
			var loc = jQuery(this).text().toLowerCase().replace(/ /ig,"");

			switch(loc){
				case "characters":
					jQuery("#about_carousel ul").cycle(1);
					break;
				case "powers":
					jQuery("#about_carousel ul").cycle(2);
					break;
				case "newmarias":
					jQuery("#about_carousel ul").cycle(3);
					break;
				case "morality":
					jQuery("#about_carousel ul").cycle(4);
					break;
				default:
					jQuery("#about_carousel ul").cycle(0);
					break;
			}
		});

		jQuery("a.hotspot").click(function() {
			var divContent = jQuery(this).children('.hidden_modal').html();
			jQuery("#hotspot_modal").html(divContent);
			jQuery.colorbox({height:"490px", href:"#hotspot_modal", inline:true, width:"720px"})			
		});
	}
};

jQuery().ready(function(){

jQuery(".pagination a").each(function(){

jQuery(this).addClass(jQuery(this).text());

});
	jQuery("#stage .next-button a").click(function() {
		var slide = jQuery(this).attr("rel");
		jQuery(".pagination a."+slide).trigger("click");		
	});

//Deeplinking for hero pre-order page
	var pageURL = window.location.href.toLowerCase();
		if(pageURL.indexOf("#hero-edition") != -1){
			jQuery("#pre_order").hide(); 
			jQuery("#pre_order_hero").show();
	}
});

function enterAgeGate(){
	var territory = jQuery.cookie('territory');
	if(territory != null){
		var page = "/infamous2/age-gate.html";
		if(territory == "en_US"){
			page = "/infamous2.html";
		}
		var url = if2ContextPath + territory + page;
		window.location = url;
	}
}

function enterSite(){
	var territory = jQuery.cookie('territory');
	var sourceurl = jQuery.cookie('sourceurl');
	if(territory != null){
		var url = if2ContextPath + territory + "/infamous2.html";
		if(sourceurl!=null){
			if(sourceurl.indexOf(territory) > 0 ){
				url = sourceurl;
			}
			jQuery.cookie('sourceurl',null,{ path: '/'  });
		}
		window.location = url;
	}
}

var country_selector = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery("#age_gate_country_select .country_list a").click(function() {
			if(jQuery(this).attr('href') != "#"){
				return;
			}
			var territory = jQuery(this).attr("rel");
			jQuery.cookie('territory',territory,{ path: '/'  });
			var url = jQuery.cookie('sourceurl');
			if(territory=="en_US" && url!=null){
				window.location = url;
				jQuery.cookie('sourceurl',null,{ path: '/'  });
			}else{
				enterAgeGate();
			}
		});

		jQuery("#locale_list a").click(function() {
			var territory = jQuery(this).attr("rel");
			jQuery.cookie('territory',territory,{ path: '/'  });
			enterAgeGate();
		});
	}
};

function checkTerritoryAndAge(){
	
	var territory = jQuery.cookie('territory');
	var ageCookie = jQuery.cookie('age_group');
	
	if(jQuery("body.country-selector").length == 0 && jQuery("body.age-gate").length == 0 ){
		if(territory != null && ageCookie != null ){
			return;
		}
	}
	
	if(jQuery("body.country-selector").length == 0){
		if(territory == null){
			window.location = if2ContextPath + "country-selector.html";
			return;
		}
	}
	
	if(jQuery("body.age-gate").length == 0 ){
		
		if( territory!="en_US" && (ageCookie == null || ageCookie == "true")) {
			enterAgeGate();
		}else{
			enterSite();
		}
	}else{
		var ageCookie = jQuery.cookie('age_group');
		if( territory=="en_US" || (ageCookie != null && ageCookie == "false")) {
			enterSite();
		}
	}
}

function checkAgeOnSitePages(){
	
	var territory = jQuery.cookie('territory');
	var ageCookie = jQuery.cookie('age_group');
		
	if(territory == null){
		//test if we can extract the language from the url
		var matches = window.location.href.match('/[a-z]{2}_[A-Z]{2}/');
		if(matches[0]){
		    var territory = matches[0].replace(/\//g,'');
		    jQuery.cookie('territory',territory,{path: '/'});
		    if(territory !="en_US" && (ageCookie == null || ageCookie == "true")){
		    	enterAgeGate();
		    }
		    return;
		}
		window.location = if2ContextPath + "country-selector.html";
		return;
	}else if(territory !="en_US" && (ageCookie == null || ageCookie == "true")){
		enterAgeGate();
	}
}

function checkAgeOnCountrySelector(){
	
	var territory = jQuery.cookie('territory');
	var ageCookie = jQuery.cookie('age_group');
	
	if(territory != null){
		if(territory=="en_US"){
			enterSite();
		}else if(ageCookie != null && ageCookie == "false"){
			enterSite();
		}else{
			enterAgeGate();
		}
	}

}

var locale = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery("#locale_switch a:first").click(function() {
			jQuery("#locale_list").slideToggle();
			
			return false;
		});
	}
};

function changeSubNavigationImage(){
	var territory = jQuery.cookie('territory');
	if(territory!=null){
		var subnavURL = jQuery("#subnav").css("background-image");
		subnavURL = subnavURL.replace("/dms/infamous2/img","/dms/infamous2/"+territory);
		jQuery('#subnav').css("background-image",subnavURL);
	} 
}

function selectFlag(){
	if(document.getElementById("selectedLocale") !=null ){
		var defaultLocale = document.getElementById("selectedLocale").src;
		defaultLocale = defaultLocale.substring(0, defaultLocale.length - 9);
		var defaultTerritory = jQuery.cookie('territory');
		if(defaultTerritory == null){
			var matches = window.location.href.match('/[a-z]{2}_[A-Z]{2}/');
			if(matches[0]){
			    defaultTerritory = matches[0].replace(/\//g,'');
			}
		}		
		defaultLocale = defaultLocale + defaultTerritory.substring(3).toLowerCase() + '/' +  defaultTerritory.substring(3).toLowerCase() + '.png';
		document.getElementById("selectedLocale").src = defaultLocale;
	}
}

var preorder = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery("#pre_order_nav a, #pre_order_nav_off a").click(function() {
			jQuery("#pre_order, #pre_order_hero").toggle();
			return false;
		});
	}
};

function setSourceURL(url){
	var sourceurl = jQuery.cookie('sourceurl');
	if(sourceurl==null)
		jQuery.cookie('sourceurl',window.location.href,{ path: '/'  });		
}

var subnav = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery("#subnav_community").parent().mouseenter(function(){
			jQuery("#subnav_community").show();
		}).mouseleave(function(){
			jQuery("#subnav_community").hide();
		});
	}
};

var news_item = {
	init: function() {
		this.bind();
	},
	bind: function() {
		jQuery("a.video").click(function() {
			jQuery("#video_modal iframe").eq(0).attr("src", "http://www.youtube.com/embed/" + jQuery(this).attr("rel"));
			jQuery.colorbox({height:"545px", href:"#video_modal", inline:true, width:"640px"})
		});

		jQuery("a.image").click(function() {
			jQuery("#image_modal img").attr("src", "img/" + jQuery(this).attr("rel"));
			jQuery.colorbox({height:"510px", href:"#image_modal", inline:true, width:"720px"})
		});
	}
};


