var site = {
	isIE: ( jQuery.browser.msie ) ? true : false,
	isFF: ( jQuery.browser.mozilla ) ? true : false,
	isMac: ( navigator.userAgent.indexOf("Mac") > 0 ) ? true : false,

	init: function() {
		this.txtSlideInit($('body'));
		$("a.pageListBtn").livequery("click",function(e){
			ajax.launch( $(this).attr('href') ); //this.toString()
			site.stopAction(e);
		});

        $('select.selectShow').livequery("change",function(e){			
			var cut = window.location.href.split('/')
			var url = "http://"+cut[2]+"/"+cut[3]+"/"+cut[4]+"/"+cut[5]+"/"+$(this).attr('id')+"?show="+$(this).attr('value');
			ajax.launch( url );
			site.stopAction(e);
        });
		
		if($('form.check').length>0) {
			site.loadCSS( _PATH_CSS_+'bubble.css' );
			site.loadJS( _PATH_JAVASCRIPT_+'checkClass.js', function(){
				$('form.check').each(function() { //Pour chaque formulaire de la liste on va binder les champs suivant l'event
					checkClass.init( $(this) );
				});
			});
		}

		if($("select.selectDragNDrop").length>0) {
			$("select.selectDragNDrop").each(function(){
				var i = 1;
				if($(this).attr('multiple')) {
					var currentSelect = $(this);
					var currentSelectName = $(this).attr('name').slice(0,-2);
					alert(currentSelectName);
					$(this).hide();
					var divFront = $('<ul id="'+currentSelectName+'Front" class="connectedDragNDrop" ></ul>');
					var divBack = $('<ul id="'+currentSelectName+'Back" class="connectedDragNDrop" ></ul>');
					$("option",currentSelect).each(function(){
						$(this).attr('id',currentSelectName+'Option'+i);
						var divOption = $('<li id="'+'Option'+i+'" >'+$(this).html()+'</li>');
						if( $(this).attr('selected')==true ) divBack.append( divOption );
						else divFront.append( divOption );
						i++;
					});
					divFront.insertAfter( currentSelect );
					divBack.insertAfter( divFront );

					if( divFront.height()>divBack.height() ) divBack.height( (divFront.height()-divBack.height())/2 );
					else divFront.height( (divBack.height()-divFront.height())/2 );

					$('#'+currentSelectName+'Front , #'+currentSelectName+'Back').sortable({
						connectWith: '.connectedDragNDrop'
					}).disableSelection();
					divBack.droppable({drop: function (event,ui){$("option#"+currentSelectName+ui.draggable.attr("id"),currentSelect).attr("selected","selected");}});
					divFront.droppable({drop: function (event,ui){$("option#"+currentSelectName+ui.draggable.attr("id"),currentSelect).removeAttr("selected");}});
				}
			});
		}
		
		if($('.tinymce').length>0){
			//site.loadJS( _PATH_JAVASCRIPT_+'tiny_mce/jquery.tinymce.js' );
			site.loadJS( _PATH_JAVASCRIPT_+'tinymce.js' );
		}
	},

    stopAction: function( event ) {
		( event && event.preventDefault ) ? event.preventDefault() : window.event.returnValue = false;
	},

	txtSlideInit: function (jobj) { // Txt Auto Slide
		$('.txtSlideContener',jobj).hover(
		function() {
			var slideLeft = $(this).width()-$('.txtSlide',this).width();
			if (slideLeft < 0) {
				$('.txtSlide',this).stop().animate( {left:slideLeft} , parseInt(-slideLeft)*25, 'linear' );
			}
		},
		function() {
			$('.txtSlide',this).stop().animate( {left:0} , 500, 'linear'  );
		});
	},

	loadJS: function(jsFile, callBack, cache) {
		$.ajax({type: "GET", url: jsFile, success: callBack, dataType: "script", cache: true});
	},

	loadCSS: function(cssFile) {
		if ( $('head link[href*="' + cssFile + '"]').length > 0 ) return this;
		if (site.isIE) {
			var headID = document.getElementsByTagName("head")[0];
			var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = cssFile;
			cssNode.media = 'all';
			headID.appendChild(cssNode);
		} else {
			h = '<link href="' + cssFile + '" media="all" rel="stylesheet" type="text/css" />';
			$('head link[type="text/css"]:last').after(h);
		}
		return this;
	}
};

jQuery.fn.extend({
   findPos : function() {
	   obj = $(this).get(0);
	   var curleft = obj.offsetLeft || 0;
	   var curtop = obj.offsetTop || 0;
	   while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
	   }
	   return {x:curleft,y:curtop};
   }
});

$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		var y = 0;
		if(hashes[0] != window.location.href){
			for(var i = 0; i < hashes.length; i++){
				hash = hashes[i].split('=');
				if( !hash[1] || hash[1].length==0 ){
					vars[y] = hash[0];
					y++;
				}
				else {
					vars.push(hash[0]);
					vars[hash[0]] = hash[1];
				}
			}
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

jQuery.fn.maxlength = function(){
	$("textarea[maxlength]").keypress(function(event){
		var key = event.which;
		if(key >= 33 || key == 13) {
			var maxLength = $(this).attr("maxlength");
			var length = this.value.length;
			if(length >= maxLength) {
				event.preventDefault();
			}
		}
	});
}

$(document).ready( function(){site.init();} );