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

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

function navigation(){
	
	var btn = $ps("#control-nav li.level1");
	
	btn.each(function(){
		var a = $ps(this).children('.text-line').children('a.level1').width();
		$ps(this).children('.text-line').children('.x-line').width(a);
	});
	
	btn.hover(
		function () {
        	$ps(this).addClass("active");
			$ps(this).children("ul").show()
			$ps(this).children('.text-line').children('.x-line').css("visibility","visible");
			$ps(this).children('.left-cap').css("visibility","visible");
			$ps(this).children('.right-cap').css("visibility","visible");
        }, 
        function () {	
			if(!$ps(this).hasClass("selected")){
				$ps(this).removeClass("active");
				$ps(this).children("ul").hide()
				$ps(this).children('.text-line').children('.x-line').css("visibility","hidden");
				$ps(this).children('.left-cap').css("visibility","hidden");
				$ps(this).children('.right-cap').css("visibility","hidden");
			}else{
				$ps(this).removeClass("active");
				$ps(this).children("ul").hide()
				return(false);
			}
        }
	);
}


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: '',
			lastClass: 'ln-last',
			selectedClass: 'ln-selected',
			disabledClass: 'ln-disabled',
			showCounts: false
		};
	
	$ps('#myList').listnav(opts);
}

// ------------------------------------------------------------------------------------------------ TABBING
function tabbingSetup(includeFlash, t){
	$ps(".tabs").each(function(){
		var ul = $ps(this).find("ul");
		ul.children("li").removeClass("selected");
		ul.tabs({ 
			// fx: { 
			// 		 	opacity: "toggle",
			// 		 	duration: 400
			// 		},
			selected:null,
			spinner: '',
			cache: false,
			cookie: { 
				expires: 30,
				secure: true,
				domain: "playsafe"
			},
			select: function(e, ui){
				var t = $ps(e.target);
				
				playsafe.changeURL(ul, ui.index);
				
			},
			load: function(e, ui){
				addCarousel();
				tooltip();
				var t = $ps(e.target);
				//$ps(".ui-tabs-panel").css({opacity:0})
				if (includeFlash == true){
					//console.log(includeFlash);
					playsafe.comFlashTabs(ui.index);
				}
			}
		});	
		ul.tabs("select", 0);
	})
}

playsafe.changeURL = function(e, i){
	var cTag = e.attr("id"); //container ID
	var sp = sitePath+"/" + cultureCode + "/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+'/' + cultureCode + "/scripts/TRList",
			data: "id="+i,
			success: function(html){
					$ps(".tr-list .content").empty();
					$ps(".tr-list .content").append	(html);
			}
		})
	
}

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, c){
	var flashvars = {
		LANG: c
		};
	
	
	var params = {
		wmode: "transparent",
		menu: "false",
		allowScriptAccess: "sameDomain",
		name: id
	};
	var attributes = {};

	swfobject.embedSWF(sitePath+"assets/swf/"+swf, id, w, h, "8.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, #country-selector").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').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+ '/' + cultureCode + "/scripts/topTips",
			//url:sitePath+'/' + cultureCode + "/scripts/TRList",
			//url:"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 });
	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 });
	getMovieName("flash-sound").SetVariable("soundCookie", snd);
	getMovieName("flash-main-panel").SetVariable("soundControl", snd);
}

// ------------------------------------------------------------------------------------------------ DOCUMENT.READY
$ps(document).ready(function(){
	navigation();
	addSliders();
	addCarousel();
	alphabettiSpaghetti();
	//changeTab("#console-tab", 5)
	//-------------------------------------------------------------------------------------------- REPLACE SUBMIT
	replaceSubmit();
	
	if (jQuery('#footer-nav').children()[0] != undefined) {
		modalOn($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();}
	}
