/**
jquery toolkit plugins for planetviandz
*/

(function($){


jQuery(function(){
	//- $.toolkit.storage.enable( 'cookies'); //--only persistant storages
	$.toolkit.storage.enable( 'localStorage|globalStorage|gears|cookies'); //--only persistant storages
});

var THEME_ROOT_URL = ROOT_URL+'elements/gui/themes/';
//-- gestion du theme du site --//
$.toolkit('tk.civtheme',{

	_classNameOptions: { 'theme':'|hardrock|punk|cosmic|default' },
	_storableOptions:{ pluginLevel:'theme' },
	_init:function(){
		this._applyOpts('theme');
	},
	_set_theme:function(name){
		if(! name){
			$('link[id=theme]').remove();
		}
		//check for existing link element
		if( ! $('link[id=theme]').length){
			$('head').append('<link rel="stylesheet" type="text/css" id="theme" />');
		}
		$('link[id=theme]').attr('href',THEME_ROOT_URL+name+'/'+name+'.css');
	}

});
$.tk.civtheme.defaults = {
	'theme':false
};

//-- gestion du menu dropdown principal --//
$.toolkit('tk.civDropDown',{
	items:{},
	pannels:{},
	activeLink:{},
	_timeout:null,
	_init:function(){
		var self = this;
		// get menu Entrys
		self.items = self.elmt.find('ul:eq(0) li');
		self.pannels = self.elmt.find('.pannel');
		self.activeLink = self.items.find('a.active');
		// link each items with its pannel
		self.items.each(function(){
			var item = $(this),
				cname = item.attr('class'),
				pannel = self.pannels.filter('.'+cname);
			item.mouseover(function(){self.hide();});
			if( pannel.length){
				item.mouseenter(function(){
					self.show(pannel);
					item.find('a').addClass('active');
				});
				item.mouseleave(function(){self._timeout = setTimeout(function(){self.hide();},250);});
				pannel.mouseenter(function(){self._clearTimeout();});
				pannel.mouseleave(function(){self.hide();});
			}

		});
	},
	_clearTimeout:function(){
		if( this._timeout ){
			clearTimeout(this._timeout);
		}
		this._timeout = null;
	},
	show:function(pannel){
		this._clearTimeout();
		this.hide();
		pannel.show();
	},
	hide:function(){
		this._clearTimeout();
		this.pannels.hide();
		this.items.find('a').not(this.activeLink).removeClass('active');
	}
});

//-- boite de login --//
$.toolkit('tk.loginbox',{
	_dropBox:null,
	_init:function(){
		//create the loginBox
		var self = this;
		self._createElements();
		self.elmt.click(function(){self.toggle(); return false;});

	},
	toggle:function(){
		this._dropBox.slideToggle(250);
	},
	_createElements:function(){
		this._dropBox = $('<form action="'+ROOT_URL+'pages/login" method="post" class="loginDropBox">'+
			'<label><input type="text" name="pseudo" />Pseudo </label>'+
			'<label><input type="password" name="pass" />Mot de passe </label>'+
			'<label><input type="submit" value="Ok" style="width:auto;"/></label>'+
			'<a href="'+ROOT_URL+'pages/oublie">Mot de passe oublié.</a></form>'
		).insertAfter(this.elmt).hide();
	}
});

})(jQuery);


//Ouverture de la popup jeux
function openGame(i,r){

	var fo = new SwfObject(ROOT_URL+"elements/swf/civ_quete.swf", "game", "800", "400", "9", "#000");
	fo.addParam("quality", "high");
	fo.addVariable("uid", i);
	fo.addVariable("connectorURL", r);
	fo.write("popup_game");

	$('#popup_game_bg').height($(document).height()).show();
	$("#popup_game").css({ top: ($(window).height()/2)-$("#popup_game").height()/2 });
	$('#popup_game').show();
	window.scroll(0,0);
}

function closeGame(){
	$('#popup_game_bg').hide();
	$('#popup_game').hide();
}