/*Nazwa ciastka z cookie uzytkownika*/
var UID_COOKIE_V3 = 'rs_uid_cookie_v3';
var IMAGE_SRC = 'http://cachei.gamedesire.com';


/*Elementy wykonywanie po załadiowaniu się drzewa DOM*/
jQuery(document).ready(function()
{
		addFirstLastElement();
		jQuery(window).resize(function(){
			maxBody();
		})
		 
		/*dodanie hintow w inputach*/
		placeHolder();
		maxBody();
		resizeBox();
		buildJSLinks();
});




	/*Definicje funkcjii*/
	// ----------------------------------------------------------------
	
	function buildJSLinks()
	{
		jQuery(".addJSLink").each(function(){
			jQuery(this).attr("href",jQuery(this).attr("rel"));
		})
	}
	
	
	// ----------------------------------------------------------------
		function stretchModuls(container)
		{
			
			modulesHeight = 0;
			stretchCount = 0;
			marginsBottom = 0;
			jQuery(container).children(".module").each(function(){

				marginsBottom += parseInt(jQuery(this).css("margin-bottom"));

				if (!jQuery(this).hasClass("stretch")) {
					modulesHeight += jQuery(this).outerHeight();
				}
				else
				{
					stretchCount++;
				}	

			})
			
			newHeight = (jQuery(container).innerHeight() - modulesHeight - marginsBottom)   / stretchCount
			
			if (newHeight > 0) {
				jQuery(container).find(".stretch").height(newHeight);
			}
			
		}
	
	// ----------------------------------------------------------------

	/* Wyrównuje wielkość kolumn do jednakowego rozmiaru*/	
	function allignColumns()
	{
			
			columnHeight = 0;			
			jQuery(".column").each(function(){
			/*	jQuery(this).css("height","auto");*/
				columnHeight = Math.max(columnHeight,jQuery(this).height());
			})

			if (columnHeight > 0) {
				jQuery(".column").css("min-height",columnHeight);
			}
	
	}
	
	
	/* Wyrównuje wielkość kolumn do jednakowego rozmiaru*/	
	function allignColumnsOther(delta)
	{			
	
			columnHeight = 0;
			columnShorterHeight = 0;
			
			jQuery(".column").each(function(){
			/*	jQuery(this).css("height","auto");*/
				columnHeight = Math.max(columnHeight,jQuery(this).height());
			})
			jQuery(".columnShorter").each(function(){
			/*	jQuery(this).css("height","auto");*/
				columnShorterHeight = Math.max(columnShorterHeight,jQuery(this).height());
			})		

			if(columnHeight >= columnShorterHeight)
			{
				jQuery(".column").css("min-height",columnHeight);
				jQuery(".columnShorter").css("min-height",columnHeight - delta);
			}
			else
			{
				jQuery(".column").css("min-height",columnShorterHeight + delta);
				jQuery(".columnShorter").css("min-height",columnShorterHeight);
			}	
	}	
	
		
	// ----------------------------------------------------------------

	//** dodaje klase last first do list()
	function addFirstLastElement()
	{
	jQuery("ul").each(function(){
		
		jQuery(this).find("li:last").addClass("last");
		jQuery(this).find("li:first").addClass("first");
	})

	}

	// ----------------------------------------------------------------

	function getLoginName(element)
	{
		userData = ((_cookie = jQuery.cookie(UID_COOKIE_V3)) != null)
						? _cookie.split(":")
						: false;

		if (userData)
		{
			user = userData[0];

			jQuery(element).each(function(){
				jQuery(element).html(getVip() + '<a href="/player/'+user+'" title="'+ user +'" class="userName">'+ user +'<\/a>');
			})

		}


		return false;
	}

	// ----------------------------------------------------------------

	function getPureLoginName(element)
	{
		userData = ((_cookie = jQuery.cookie(UID_COOKIE_V3)) != null)
						? _cookie.split(":")
						: false;

		if (userData)
		{
			user = userData[0];
            if(user.length>20) user = user.substr(0, 20) + '...';
			jQuery(element).each(function(){
				jQuery(element).html(getVip() + user);
			})

		}


		return false;
	}

	// ----------------------------------------------------------------

	function getVip()
	{
		vipData = ((_cookie = jQuery.cookie("vip_level")) != null) 
 					? _cookie /*.split("|")*/
					: false;
	
		if (vipData)
		{
			
			switch (parseInt(vipData))			
			{
				case 1: return '<a href="/dd-15.html" title=""><img src="'+IMAGE_SRC+'/i/em.gif" width="60" height="23" class="vipIco vip_1" alt="" >\</a>'; break; 
				case 2: return '<a href="/dd-15.html" title=""><img src="'+IMAGE_SRC+'/i/em.gif" width="60" height="23" class="vipIco vip_2" alt=""><\/a>'; break; 
				case 3: return '<a href="/dd-15.html" title=""><img src="'+IMAGE_SRC+'/i/em.gif" width="60" height="23" class="vipIco vip_3" alt="" ><\/a>'; break; 
				case 4: return '<a href="/dd-15.html" title=""><img src="'+IMAGE_SRC+'/i/em.gif" width="60" height="23" class="vipIco vip_4" alt=""><\/a>'; break; 
				case 5: return '<a href="/dd-15.html" title=""><img src="'+IMAGE_SRC+'/i/em.gif" width="60" height="23" class="vipIco vip_5" alt=""><\/a>'; break; 
				
				default: return '<img src="'+IMAGE_SRC+'/i/em.gif" width="10" height="23" >'; break;
			}
		}

		
		return '';		
	}
	// ----------------------------------------------------------------

	//Funkcja pobiera ilość wiadomości w krzynce uzytkownika
	
	function mailoxCount()
	{
		msgCount = parseInt(getc('mbox'));

		if (msgCount > 0) {
			jQuery(".mailBox").append( ' (' + msgCount+')');
		}

	}
	
	/*Pobnieranie stanu gamechipsów*/
	function getMoneyStatus()
	{
		var moneyCookie = jQuery.cookie("money");
		var moneyCount = 0;
		if(moneyCookie)
		{
			var ind=moneyCookie.indexOf("|");
			if(ind==-1)
				moneyCount=moneyCookie;
			else
				moneyCount=moneyCookie.substring(0,ind);

			if (moneyCount)
			{
				moneyCount = moneyCount.replace(/\+/g, "&nbsp;");
				moneyCount = moneyCount.replace(/#/g, "&nbsp;");
			}
		}
		moneyObj = jQuery(".money");

		if (jQuery(moneyObj).find("img").length > 0)
		{
			jQuery(moneyObj).find("img").after(moneyCount+ ' ');
		}
		else
		{
			jQuery(".money").prepend(moneyCount+ ' ');
		}
	}



	function changeLangLayer(){

		jQuery("#selLang").hide();

		jQuery("#changeLang").click(function()
		{
			
			
			_position = jQuery("#changeLang").offset();
			_layerWidth = jQuery("#selLang").width();
			_buttonWidth = jQuery("#changeLang").width(); 
			
			_bodyWidth = jQuery("body").width();

			if ((_layerWidth + _position.left ) > _bodyWidth) {

				jQuery("#selLang").css({
					"left": _position.left - _layerWidth + _buttonWidth,
					"top": _position.top + 35
				})
			}
			else {
				jQuery("#selLang").css({
					"left": _position.left,
					"top": _position.top + 35
				})
			}	
			
			
			
			jQuery("#selLang").slideToggle();
			
			
			if (jQuery("#layer").length == 0)
			{
				jQuery("body").append('<div id="layer" style="width: 100%; height: 100%; position: absolute; top: 0px; left: 0px;"><\/div>')
			}
			else
			{
				jQuery("#layer").remove();
			}
			
		})
	
	
		jQuery("#layer").live("click",function(){
			jQuery("#layer").remove();
			jQuery("#selLang").slideToggle();
		})
	
	
	}
	
	
	
	
	/* place holder */
	function placeHolder()
	{
		jQuery(".placeHolder").each(function(){
			if (jQuery(this).val() == "") {
				jQuery(this).wrap('<span class="placeHolderBox"><\/span>').after('<span class="placeHolderHint">' + jQuery(this).attr("title") + '<\/span>');
			}	
		
			jQuery(this).bind("focus", function(){
				jQuery(this).parent().find(".placeHolderHint").hide();
			});
			
			jQuery(this).parent().find(".placeHolderHint").click(function(){
				jQuery(this).parent().find(".placeHolderHint").hide();
				jQuery(this).parent().find(".input").focus();
			})

			jQuery(this).blur(function(){
				if (jQuery(this).val() == ""){
					jQuery(this).parent().find(".placeHolderHint").show();
				}

			});
		})

	}


	function loading(show, obj)
	{
		
		if (show == 1 )
		{
			
			if (obj != undefined) 
			{
				jQuery(obj).append('<div id="loading"><\/div>');
				jQuery("#loading").css({
					"margin": "10px auto"
				});
			}
			else 
			{
				jQuery("body").append('<div id="loading"><\/div>');
				jQuery("#loading").css({
					"top":"50%",
					"left":"50%",
					"margin-top":"-25px",
					"margin-left":"-25px",
					"position":"fixed"					
				});
			}	
			
		}
		else
		{
			jQuery("#loading").remove();
		}
	}
	
	
	function maxBody()
	{
		
		windowHeight = (jQuery(window).height());
 		headerHeight = jQuery("#topWarp").outerHeight();
 		bottmHeight = jQuery("#bottomWarp").outerHeight();
		advHeight = jQuery("div.adv.top").outerHeight();

		headerHeight = headerHeight > 0 ? headerHeight : 0;
		advHeight = advHeight > 0 ? advHeight : 0;
		bottmHeight = bottmHeight > 0 ? bottmHeight : 0; 
		
		if (advHeight > 0)
		{
			advHeight = advHeight + 10;
		}
		computedHeight = parseInt(windowHeight) - parseInt(headerHeight) - parseInt(bottmHeight) - diffSize - advHeight;
		if(computedHeight < 0)
		{	
			computedHeight = 400;
		}	
		jQuery("#content").css("min-height", computedHeight);

	}


	
/**
 * COOKIE SERIALIZER 
 */
CookieSerializer = {
	getValue : function(serializedString, key, defValue){
		toReturn = defValue;
		if(typeof(serializedString) != 'string' || typeof(key) != 'string')
			return toReturn;
		arr = serializedString.split("#");
		for(var item in arr){
			keyValArr = arr[item].split(":");
			if(keyValArr.length == 2 && keyValArr[0] == key){
				toReturn = keyValArr[1];
			}
		}
		return toReturn;
	},
	setValue : function(serializedCookie, key, value){
		toReturn = "";
		if(typeof(key) != 'string' || value === undefined)
			return serializedCookie;	
		if(typeof(serializedCookie) != 'string' || serializedCookie.length == 0)
			return "" + key + ":" + value;
			
		arr = serializedCookie.split("#");
		inserted = false;
		for(var item in arr){
			keyValArr = arr[item].split(":");
			if(keyValArr.length == 2 && keyValArr[0] == key){
				toReturn += keyValArr[0] + ":" + value;
				inserted = true;
			}else if(keyValArr.length == 2){
				toReturn += arr[item];
			}
			if(item < arr.length - 1 ){
				toReturn += "#";
			}
		}
		if(!inserted){
			toReturn += ((arr.length > 0)?"#":"") + key + ":" + value;
		}
		return toReturn;
	},
	overwriteCookie : function(cookieName, key, value, params){
		oldOne = jQuery.cookie(cookieName);
		newOne = this.setValue(oldOne, key, value);
		jQuery.cookie(cookieName, newOne, params);
	}
};
	//Binds switch functionality
	function resizeBox()
	{
		resolution = CookieSerializer.getValue(jQuery.cookie("layout"), "sres", '1024');
		jQuery('.resizerBar a[rel="'+ resolution +'"]').addClass('active');
		
		jQuery(".resizerBar a").click(function()
		{	jQuery(".resizerBar a").removeClass("active");
			$sizeSize = jQuery(this).attr("rel");
			if(LayoutResizer.switch_size($sizeSize)){
				jQuery(this).addClass('active');
			}
			maxBody();
			
			return false;
		})
		

	
		
	}

var LayoutResizer = {
	current_size : "",
	supported_sizes : ["800", "1024", "1280"],
	callbacks : [],
	/**
	 * Register callback, that is fired while switching layout!
	 * callback : function(new_size : string, old_size : [string | false]);
	 */
	register_resize_callback : function(callback){
		if(typeof(callback) === "function"){
			this.callbacks.push(callback);
			return true;
		}
		return false;
	},
	//returns previous size or false
	remove_size_class : function(){
		for(var size in this.supported_sizes){
			var class_name = "sres_" + this.supported_sizes[size];
			if(jQuery("html").hasClass(class_name)){
				jQuery("html").removeClass(class_name);
				return this.supported_sizes[size];
			}
		}
		return false;
	},
	switch_size : function(new_size, skip_callback){
		if(this.in_array(new_size, this.supported_sizes)){
			CookieSerializer.overwriteCookie("layout", "sres", new_size, { path: "/", expires: 365 });
			var old_size = this.remove_size_class();
			jQuery("html").addClass("sres_" + new_size);
			this.current_size = new_size;
			if(typeof(skip_callback) == "undefined" || skip_callback == false){
				for(var cid in this.callbacks){
					this.callbacks[cid](new_size, old_size);
				}
			}
			return true;
		}
		return false;
	},
	
	in_array : function(value, arr){
		for( var id in arr){
			if(arr[id] == value){
				return true;
			}
		}
		return false;
	},
	get_current_size : function(){
		for(var id in this.supported_sizes){
			if(jQuery("html").hasClass("sres_" + this.supported_sizes[id])){
				return this.supported_sizes[id];
			}
		}
		return false;
	}
};
	
/** STUFF FOR SHOWING/HIDING overlay DIV  - connected with mod_settings_module.lib **/
ModuleSettings = function(params){
	this.contentId = params.contentId;
	this.buttonId = params.buttonId;
	this.show = function(){
			_position = jQuery("#" + this.buttonId).offset();
			_layerWidth = jQuery("#" + this.contentId).width();
			_buttonWidth = jQuery("#" + this.buttonId).width(); 
			
			_bodyWidth = jQuery("body").width();

			if ((_layerWidth + _position.left + 20) > _bodyWidth) {

				jQuery("#" + this.contentId).css({
					"left": _position.left - _layerWidth + _buttonWidth,
					"top": _position.top +15
				})
			}
			else {
				jQuery("#" + this.contentId).css({
					"left": _position.left,
					"top": _position.top + 15
				})
			}
			im = new Image();
			im.onload = function(){jQuery("#skinSelector li").removeClass("skin_loading_ajax");};
			im.src= 'http://cachei.gamedesire.com/i/gd3/skins.png';
			jQuery("#" + this.contentId).slideToggle();
			
			
			if (jQuery("#layer_" + this.contentId).length == 0)
			{
				jQuery("body").append('<div id="layer_' + this.contentId+ '" style="width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; z-index:999;"></div>')
			}
			else
			{
				jQuery("#layer_" + this.contentId).remove();
			}
	}
	this.hide = function(){
		jQuery("#layer_" + this.contentId ).remove();
		jQuery("#" + this.contentId).slideToggle();	
	}

	handler = this;
	jQuery("#"+this.buttonId).click(function(){
		handler.show();
	});
	jQuery("#layer_" + this.contentId).live("click",function(){
			handler.hide();
	});	
};

/** SKIN SELECTOR OBJECT **/
SkinSelector = {
	params : { src : window.IMAGE_SRC, site : "gd3", css_ver : 123, skins: ["pink", "red", "black", "lolipop", "default", "green", "blue", "grey", "classic"]},
	init : function(params){
		if(params.src)
			this.params.src = params.src;
		if(params.site)
			this.params.site = params.site;
		if(params.css_ver)
			this.params.css_ver = params.css_ver;
		if(params.skins)
			this.params.skins = params.skins;
	},
	removeSkinClasses : function(){
		for(var skinName in this.params.skins){
			jQuery("html").removeClass("html_" + this.params.skins[skinName]);
		}
	},	
	changeSkin : function(skinName){
		oldLink = document.getElementById("layoutSkin");
		newLink = document.createElement("link");
		newLink.setAttribute("type","text/css");
		newLink.setAttribute("href",this.params.src + "/ver/" + this.params.site + "/skins/skin_" + skinName + "_generated." + this.params.css_ver + ".css");
		newLink.setAttribute("rel","stylesheet");
		newLink.setAttribute("id","layoutSkin");			
					
		head = document.getElementsByTagName("head");
		head = head[0];
		if(!oldLink){
			jQuery("head link:last").after(newLink);
		}else{
			jQuery("head #layoutSkin").replaceWith(newLink);	
		}
		this.removeSkinClasses();
		jQuery("html").addClass("html_" + skinName);
	}
};
