var $ps = jQuery.noConflict();
var sitePath = 'http://' + location.hostname + '/';
var snd;

if(!playsafe){
	var playsafe = {};
}



function navigation(){
	var btn = $ps("#control-nav li.level1");
	var dp = $ps("#control-nav");
	var timer = '';
	btn.mouseover(function(){
		$ps(this).addClass("active");
		$ps(this).children("ul").show();
		clearTimeout(timer);
	})
	btn.not(".baby-blue").mouseout(function(){
		//setTimeout(function(){remClass(this)},1000);

	})
	
	dp.mouseout(function(){
		timer = setTimeout(function(){remClass(this)},1000);
	})

	function remClass(thisobj) { 
		btn.removeClass("active");
		btn.children("ul").hide();
		btn.parents(".baby-blue").removeClass("active");
	}
}

function addSliders(){
	$ps('.scroll-area').each(function(){
		var sa = $ps(this);
		var ul = sa.children("ul");
		var li = ul.children("li");
		var items = Number(sa.attr("title"));
		if(li.size() >= items){
			var ih;
			var p =0;
			for(var i=0;i<Number(items);i++){
				lii = 
				p += (li.eq(i).outerHeight({margin:true}));
			}
			sa.height(p);
		}
		if(li.size() > items){
			sa.height(sa.height()-8)
		}
		
		$ps(this).css({overflow: "hidden", width: ($ps(this).width()-10)});
		$ps(this).parent().append("<div class=\"slider right\"></div>");
		$ps(this).parent().append('<br class="clear" />');
		var slider = $ps(this).parent().children(".slider");
		slider.height($ps(this).height());
		slider.css({})
		
		var h = ul.height() - sa.height()  
		
		if(h>0){
			slider.slider({
				min : 0,
				max : h,
				animate : false,
				slide : function(e, ui){
					//console.log($ps(this).slider("value"));
					ul.css({top:-$ps(this).slider("value")})
				}
			});
		}
	})

}

function addCarousel(){
	$ps(".carousel").each(function(){
		$ps(this).jslider();		
	})
}

// ------------------------------------------------------------------------------------------------ GAMING GLOSSARY listNav Plugin
function alphabettiSpaghetti(){
	var opts = {
			initLetter: '',
			includeAll: true,
			flagDisabled: true,
			noMatchText: 'No matching entries',
			lastClass: 'ln-last',
			selectedClass: 'ln-selected',
			disabledClass: 'ln-disabled',
			showCounts: false
		};
	
	$ps('#myList').listnav(opts);
}

// ------------------------------------------------------------------------------------------------ TABBING
function tabbingSetup(e, t){
		
	var tab = $ps("#console-tab li");
	tab.removeClass("ui-tabs-selected");
	tab.eq(t).addClass("ui-tabs-selected");
	
	playsafe.changeURL($ps("#console-tab"), t);
	
}

playsafe.changeURL = function(e, i){
	//$ps(".ui-tabs-panel").animate({opacity:0}, 200)
	var cTag = e.attr("id"); //container ID
	var sp = sitePath+"/scripts/tabContent";
	var aTag = e.find("a").eq(i); //a tag reference
	var ref = new Array(); 
	ref = aTag.attr("id").split('x'); //create the array from the <a> "id" 
	var script = ref[0]+"?id=";
	var pageID = ref[1];
	playsafe.changeTRListPanel(pageID)
	$ps("#"+cTag).tabs("url", i, sp + script + pageID);
}

playsafe.changeTRListPanel = function(i){
	
		$ps.ajax({
			type:"GET",
			url:sitePath+"/scripts/TRList",
			data: "id="+i,
			success: function(html){
			//	$ps(".tr-list .content div").fadeOut(400, function(){
					$ps(".tr-list .content").empty();
					$ps(".tr-list .content").append	(html);
			//		$ps(".tr-list .content div").hide();
			//		$ps(".tr-list .content div").fadeIn(400);
			//	});
			}
		})
	
}

function changeTab(elm, i){
	$ps(elm).tabs("select", i);
}

playsafe.comFlashTabs = function(i){	
	
	// if(window["flash-main-panel"]) window.document["flash-main-panel"].SetVariable("CurrentPosition", i);
	// if(document["flash-main-panel"]) document["flash-main-panel"].SetVariable("CurrentPosition", i);
	
	
	getMovieName("flash-main-panel").SetVariable("CurrentPosition", i);

}

function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
    	return window[movieName]
 	}else {
    	return document[movieName]
	}
}

function flashReplace(swf, id, w, h, t){
	setCookie()
	var flashvars ={soundOn: snd}
	if (t != "null"){
	var flashvars = {
			ps3Tab: t,
			soundOn: snd
		};
	}else{
		
	}
	var params = {
		wmode: "transparent",
		menu: "false",
		allowScriptAccess: "sameDomain",
		name: id
	};
	var attributes = {};

	swfobject.embedSWF(sitePath+"assets/swf/"+swf, id, w, h, "9.0.0","expressInstall.swf", flashvars, params, attributes);
	
	//setTimeout(setCookie,160)
}

function ieReady(){
	$ps(document).ready(function(){
		$ps(".btm, .top, .cloud, .panel-wide, .technicolor, .ps3Icon, .pspIcon, .gameIcon, .homeIcon, .onlineIcon").pngfix();
	});
}

// ------------------------------------------------------------------------------------------------ MODAL OFF
function modalOff(close,removeMe){
	$ps(close).click(function(){
		$ps('.overlay').remove();
		$ps(removeMe).remove();
		$ps('.dropdown').show();
		$ps('body').css({overflow: 'auto'});
		return(false);
	});
}

// ------------------------------------------------------------------------------------------------ MODAL ON
function modalOn(btn,request,modalClass,className){
	$ps(btn).click(function(){
		var docBody = $ps('body');
		docBody.append('<div class="overlay"></div>');
		docBody.append('<div class="' + className + '"></div>');
		$ps(modalClass).hide();
		var windowHeight = document.documentElement.clientHeight;
		var windowWidth = document.documentElement.clientWidth;
		docBody.css({width: windowWidth});
		var yOffSet = document.body.scrollTop;
		var yHeight = windowHeight + yOffSet;
		$ps('.overlay').css({'top':yOffSet, height: $ps('body').outerHeight() });
		$ps('.dropdown').hide();
		$ps(modalClass).load(request,{},
			function(response,text,xmlrequest) {
				height = $ps(modalClass).outerHeight();
				width = $ps(modalClass).outerWidth();
				replaceSubmit();
				$ps(modalClass).css({top:(windowHeight / 2)-(height / 2), left:(windowWidth / 2)-(width / 2)}).fadeIn(350);
				modalOff('.close-box a',modalClass);
				if( $ps(".msg-optional").length ){
					modalOff('.overlay');
				}
			}
		);
		var modalTop = 200 + yOffSet;
		$ps(modalClass).css({'top':modalTop});
		return(false);
	});
}

function changeLocale(){
	
}

function replaceSubmit(){
	var submit = $ps('input[type=submit]');
	submit.each(function(){
		var thisSubmit = $ps(this);
		var submitClass = thisSubmit.attr('class');
		var submitValue = thisSubmit.attr('value');
		thisSubmit.replaceWith('<a class="' + submitClass + '" href="#"><span>' + submitValue + '</span></a>');
	});
}

// ------------------------------------------------------------------------------------------------ TOOLTIP

function tooltip(){
	$ps('.pegi').each(function(){
		$ps('.pegi').tooltip({
			track: true,
			delay: 0,
			showURL: false,
			showBody: " - ",
			extraClass: "pretty",
			top: 20,
			left: -90
		});
	});
}

// ---------------------------------------------------- TOPTIP

playsafe.changeTopTip = function(i){

		$ps.ajax({
			type:"GET",
			url:sitePath+"/scripts/topTips",
			data: "id="+117,
			success: function(html){
			//	$ps(".tr-list .content div").fadeOut(400, function(){
					$ps(".toptipPanel .content p.top-tip").empty();
					$ps(".toptipPanel .content p.top-tip").append	(html);
			//		$ps(".tr-list .content div").hide();
			//		$ps(".tr-list .content div").fadeIn(400);
			//	});
			}
		})
	
}

// ------------------------------------------------------- FLASH MUTE

function soundUp(){
	//console.log("up");
	snd = "1";
	$ps.cookie('ps-sound', snd, { expires: 7 });
	//window.location = window.location;
	//getMovieName("flash-sound").SetVariable("soundCookie", snd);
	//getMovieName("flash-main-panel").SetVariable("soundControl", snd);

}

function soundDown(){
//	console.log("down");
	snd = "0";
	$ps.cookie('ps-sound', snd, { expires: 7 });
	//window.location = window.location;
//	getMovieName("flash-sound").SetVariable("soundCookie", snd);
	//getMovieName("flash-main-panel").SetVariable("soundControl", snd);
}

function soundClick(status){
	if(status){
		soundUp();
	}else{
		soundDown();
	}

	document.location = location.href;

}

// ------------------------------------------------------------------------------------------------ DOCUMENT.READY
$ps(document).ready(function(){
	
	navigation();
	addSliders();
	addCarousel();
	alphabettiSpaghetti();
	
	//changeTab("#console-tab", 5)
	//-------------------------------------------------------------------------------------------- REPLACE SUBMIT
	replaceSubmit();
	
	modalOn($ps($ps('#footer-nav').children()[0]),sitePath+'en_GB/change_locale.html','.language-selector-modal','language-selector-modal');

});

//------------------------------------------------------------------------------------------- COOKIE
function setCookie() {
	if ($ps.cookie('ps-sound')) { 
		snd=$ps.cookie('ps-sound');
	}
	else {
		snd = "1";
		$ps.cookie('ps-sound', "1", { expires: 7 });
	}
	
	
	if (snd == "1") {soundUp();}
	else{soundDown();}
}
