$j.elementReady('frame-border', function(){
	/* game info box nav selection */

	
	if (typeof SPY == 'undefined') {
		var SPY = {}
	}
	SPY.LOCALE = getLocale().toLowerCase();

	
	
	/* Navigation */
	$j('.nav-btn').hover(function(e){
		if(!$j(this).hasClass('selected')){ $j(this).addClass('hover');}
	},function(e){
		$j(this).removeClass('hover')
	});

	/*Click events */	
	$j('#stf_spy').click(function(){
		stf_spy()
	})

	/* Login Box */
	//Get the login cookie if it exists (using the cookie plugin). If there is no cookie, the user is not logged in.
	$j.getScript('http://scripts.ign.com/scripts/ign/plugins/jquery.cookie.js',function(){
		if($j.cookie('ignlogin')){
			logIn($j.cookie('ignlogin')) //Pass the cookie to the logIn function			
		}else{	
			//Not logged in. Show default layout.		
			$j('#login-link1, #login-link2').attr('href','https://login.gamespyid.com/aggrpassthrough.aspx?o=login&d=gamespy&r='+ location.href) //set log in and registration links (same location)
			$j('#login-box').show() 
		}
	})		
	//Get the unread PMs and insert it into #newPmCountLayer
	$j.getScript('http://forums.gamespy.com/PrivateMessages/NewPMCount.aspx?zero=true">',function(){
		if($j('#newPmCountLayer').text()==''){ //In case the webservice doesn't return a 0.
			$j('#newPmCountLayer').text('0')
		}
	})	

	function logIn(data){
		var loginName = data.split('\\')[3].slice(0,20) //Extract the username from the cookie and slice it to the first 20 chars.
		if (loginName) {  //One last sanity check
			$j('#login-welcome').html('Welcome <b><a href="http://forums.gamespy.com/UserPages/Profile.aspx?user=' + loginName+'">'+loginName+'</a></b>')
			$j('#login-link1').attr('href','https://login.gamespy.com/').text('My Account') //NEED CORRECT HREF
			$j('#login-link2').attr('href','https://login.gamespyid.com/aggrpassthrough.aspx?o=logout&d=gamespy&r=' + location.href).text('Log Off')
			$j('#login-pm').show() //Logged in so show PM info
			$j('#login-box').show() 
		}
		var loginNameReal = data.split('\\')[3] // pull full username to check for icon
		if (loginNameReal) {  //One last sanity check
			var url = 'http://forums.gamespy.com/servicesv3/userservices.asmx/JSON_GetUserDetails?userName='+loginNameReal+'&viewingUserName='+loginNameReal; 
            jQuery.getJSON(url + '&callback=?', function(json) {
				if (typeof(json) != 'undefined') {
					var iconURL = json.Icon.ImageURL;
					if (iconURL != null && typeof(iconURL) != 'undefined') { 
						$j('#login-icon').html('<a href="http://forums.gamespy.com/UserPages/Icons.aspx"><img width="50" height="50" style="border:none;" src="'+iconURL+'" alt="'+loginNameReal+'" title="'+loginNameReal+'" /></a>')
						$j('#login-icon').show()
					}
					else {
						$j('#login-icon').html('<a href="http://forums.gamespy.com/UserPages/Icons.aspx"><img width="50" height="50" style="border:none;" src="http://media.gamespy.com/spy/imgs/chooseicon.jpg" /></a>')
						$j('#login-icon').show()
					}
				}
			});
		}
	}	


	
	
	/* Video Gallery */
	$j('.watchButton img').hover(function(){
		$j(this).attr('src',$j(this).attr('src').slice(0,-5) + '1.gif')
	},function(){
		$j(this).attr('src',$j(this).attr('src').slice(0,-5) + '0.gif')
	});
	
	/* Locale based changes */
	switch(SPY.LOCALE){
		case 'uk':
		  //game module details
			$j('.us_details').hide();
			$j('.au_details').hide();
			$j('.uk_details').show();
			break;
		case 'au':
		  //game module details
			$j('.us_details').hide();
			$j('.uk_details').hide();
			$j('.au_details').show();
			break;
		default:
		 //put non-uk/au stuff here if necessary, however the default styles should be set to show via the CSS	
		   //game module details
			$j('.us_details').show();
			$j('.uk_details').hide();
			$j('.au_details').hide();	
	}

	/* nav for object article pages */
	$j('.allArticles').find('div').hide();
	$j('.all-header').find('div').show();
	$j('.all-header').show();
	
})


$j(document).ready(function(e){
function fixMyPNG(){
		if ($j.browser.msie && $j.browser.version < 7) {
			$j.getScript('http://scripts.ign.com/scripts/ign/plugins/jquery.pngFix.js', function(){
				$j(document).pngFix();
			})
		}
	}
	fixMyPNG()
/* Top Stories  */
	var ts_clicked = false;
	$j.getScript('http://scripts.ign.com/scripts/ign/plugins/jquery.cycle.pack.js', function(){	
		$j('#topstories-image-container').cycle({
			timeout: 5000,
			speed:800,
			fastOnEvent:true,
			pager:'#ts-nav',
			pagerClick: function(idx, slide){
			  if(!ts_clicked){
				this.timeout = 0;
				ts_clicked = true;
			  }
			},
			pagerAnchorBuilder: function(idx,slide){
				var i = parseInt(idx + 1);
				return '<a>'+ i +'</a>';
			}
		});		  
	    fixMyPNG()  // fix PNG's after the pager has been drawn
	})
	$j('.allArticles').find('div').hide();
	$j('.all-header').find('div').show();
	$j('.all-header').show();
	
		/* -- Generic Tab Function --
	   when an item with the class tab is clicked
	   it gets the id from the clicked tab and the tab container
	   adds the tab-hide class to all tab layers
	   removes the selected class from all tabs within the container
	   removes the tab-hide class and adds the tab-show class to the tab layer that was clicked
	   and adds the selected class to the clicked tab
	*/
	$j('div.tab').click(function(e){
		tab_id = $j(this).attr('id');
		tab_layer = tab_id + '-layer';
		tab_parent = $j(this).parent().attr("id");
		$j("#" + tab_parent).find("div").each(function(i) {
			$j("#" + this.id + "-layer").removeClass("tab-show");
			$j("#" + this.id).removeClass("selected");
    	});
		$j("#" + tab_layer).addClass("tab-show");
		$j(this).addClass('selected');
	});
	
	$j('.article-nav-btn').click(function(e) {
	if(!$j(this).hasClass('disabled')){ 
		tab_id = $j(this).attr('id');
		tab_parent = $j(this).parent().attr("id");
		$j(".article-nav-bar").find("div").removeClass('selected');
			var divider=$j(this).next();
			$j(".article-nav-divider").removeClass('div-selected');
			$j('.allArticles').find('span').hide();
			$j('.all-header').hide();
		$j("." + tab_id).show();
		$j(".row-date").show();
		$j("." + tab_id).find('div').show();
		$j("#" + tab_id).addClass("selected");
		var divider=$j(this).next();
		divider.addClass('div-selected');
		if(tab_id == 'allArticles'){$j(".allArticles").find("span").show();
		$j('.allArticles').find('div').hide();
		$j('.all-header').find('div').show();
		$j('.all-header').show();
		}
	}
	})
	;
	
	
})

	
	/*Utilities */
	function checkParam(param,value) {
		p = new String(document.location);
		p = p.substring( p.indexOf('?')+1 , p.length );
		var compareField = new String(param);
		if (p.indexOf(compareField) > -1) { 
			var compareValue = new String(value);
			pArray = p.split("&");
			k = pArray.length;
			for (i= 0 ; i < k; i++) {
				keyVal = pArray[i].split('=');
				if (keyVal.length != 2) { 
					continue; 
				}
				if (keyVal[0] == compareField && keyVal[1] == compareValue) { 
					return true; 
				}
			}
		 }
		return false;
	}

	function getLocale(){
	  p = new String(document.location);
	  regx = /\/\/(\w.*?)\./;
	  result = p.match(regx);
		if ( result != null ) { 
			return result[1];
		}
	}
	function  checkLocale(LOCALE){
		urlLocale = getLocale();
		paramLocale = LOCALE;
		
	if (urlLocale == paramLocale){
		return true;
	}
	if (checkParam('setLocale',paramLocale)){
		return true;
	}
 return false;

}
  /* send to friend function */
	function stf_spy( url ) { 
		var u = top.document.location;
    	if ( url ) {
    	    u = url;
    	}
		var stfurl = "http://www.gamespy.com/stf/index.html?pst=1&url=" + u;
		var newWindow = window.open(stfurl,"","status,height=500,width=450");
	}
// handy urlEncoder -- used by regprotect
function urlEncode(inStr) {
	inStr = new String(inStr);
	outStr=' '; //not '' for a NS bug!
	for (i=0; i < inStr.length; i++) {
		aChar=inStr.substring (i, i+1);
		switch(aChar){
case '%': outStr += "%25"; break; case ',': outStr += "%2C"; break;
case '/': outStr += "%2F"; break; case ':': outStr += "%3A"; break;
case '~': outStr += "%7E"; break; case '!': outStr += "%21"; break;
case '"': outStr += "%22"; break; case '#': outStr += "%23"; break;
case '$': outStr += "%24"; break; case "'": outStr += "%27"; break;
case '`': outStr += "%60"; break; case '^': outStr += "%5E"; break;
case '&': outStr += "%26"; break; case '(': outStr += "%28"; break;
case ')': outStr += "%29"; break; case '+': outStr += "%2B"; break;
case '{': outStr += "%7B"; break; case '|': outStr += "%7C"; break;
case '}': outStr += "%7D"; break; case ';': outStr += "%3B"; break;
case '<': outStr += "%3C"; break; case '=': outStr += "%3D"; break;
case '>': outStr += "%3E"; break; case '?': outStr += "%3F"; break;
case '[': outStr += "%5B"; break; case '\\': outStr += "%5C"; break;
case ']': outStr += "%5D"; break; case ' ': outStr += "+"; break;
default: outStr += aChar;
		}
}
return outStr.substring(1, outStr.length);
}

// allows a search with radio buttons. this function is GameSpy specific, other network includes have diff site values
function gspy_multiSiteSearchSubmit(form) {
	qry = urlEncode(form.query.value);
	if (qry=="") { alert('Please type in your search query.'); return; } // check for empty strings 
	redir = "";
	if (form.domain[0].checked) { // assumes global WebSearch is listed first
		redir = "http://search.gamespy.com/web?query= " + qry;
	} else if (form.domain[1].checked) { // assumes site search is second
		redir = "http://search.gamespy.com/products?query=" + qry;
	}
	gotoval = redir;
	if (gotoval != "") { document.location=gotoval; }
	return;
}

if (typeof page_type != 'undefined'){
$j.elementReady(page_type, function(){

	$j('#'+page_type).addClass('selected');

});
}
function getScore(id) {
	var url = 'http://websvc.ign.com/objectWebService?entityid='+id+'&format=json'; 
            jQuery.getJSON(url + '&callback=?', function(json) {
                if (typeof(json) != 'undefined') {
                    var game = json.object;
					var pressratings="press ratings";
					if(game.pressRatingCount <= 1) {pressratings="press rating";}
                    var pressRating = Math.round(game.pressRatingAvg*10)/10;
					$j("a#press-rating").attr('href', game.networkResources[0].objectPageURL).html(pressRating);
					$j("a#press-rating-count").attr('href', game.networkResources[0].objectPageURL).html('('+game.pressRatingCount+' ' +pressratings+')</span>');

                }
            });

}


