$.fn.delay = function( time, name ) {

    return this.queue( ( name || "fx" ), function() {
        var self = this;
        setTimeout(function() { $.dequeue(self); } , time );
    } );

};

$(document).ready(function(){
	var appel = true ;
	$('#datedebut').datepicker({showOtherMonths: true, selectOtherMonths: true,changeMonth: true,showOn: 'button',buttonImage: 'css/images/calendar.gif',showAnim: 'fadeIn'});
	$('#datefin').datepicker({showOtherMonths: true, selectOtherMonths: true,changeMonth: true,showOn: 'button',buttonImage: 'css/images/calendar.gif',showAnim: 'fadeIn'});

	$('#rechercher').click(function(){

		var rel = $('#rechercher').attr('rel');
		if (rel=='') {
			$('#rechercher').css({backgroundPosition : 'bottom'});
			$('#moteur-cache').css({display: 'block'});
			/*$('#rechercher').animate({top: '390px'},1000);*/
			$('#moteur-cache').animate({height: '380px'},1000,function(){
				if (appel) {
					/*$('#casino-select').sSelect();
					$('#forme').sSelect();
					$('#prizepool').sSelect();
					$('#chips').sSelect();*/


					$('#carte .carte a[class*=cas]').cluetip({
			    		splitTitle:			'|',
			    		width:            	100,
						dropShadowSteps:  	0,
						sticky : 			false,
						clickThrough:     	true,
					    showTitle: 			false,
					    positionBy:       'mouse',
					    tracking: true

					  });
					appel = false;
				}
			});
			$('#rechercher').attr('rel','open');
		} else {
			$('#rechercher').css({backgroundPosition : 'top'});
			$('#moteur-cache').css({display: 'block'});
			/*$('#rechercher').animate({top: '0px'},1000);*/
			$('#moteur-cache').animate({height: '0px'},1000,function(){
				$(this).css({display : 'none'});
			});
			$('#rechercher').attr('rel','');

		}
		/*if (rel=='') {
			$('#rechercher').css({backgroundPosition : 'bottom'});
			$('#moteur-cache').css({display: 'block'});
			$('#moteur-cache').animate({width: '760px'},1000,function(){
				if (appel) {
					$('#casino').sSelect();
					$('#forme').sSelect();
					$('#prizepool').sSelect();
					$('#chips').sSelect();
					$('#datedebut').datepicker({showOtherMonths: true, selectOtherMonths: true,changeMonth: true,showOn: 'button',buttonImage: 'css/images/calendar.gif',showAnim: 'fadeIn'});
					$('#datefin').datepicker({showOtherMonths: true, selectOtherMonths: true,changeMonth: true,showOn: 'button',buttonImage: 'css/images/calendar.gif',showAnim: 'fadeIn'});
					appel = false;
				}
			});
			$('#rechercher').attr('rel','open');
		} else {
			$('#rechercher').css({backgroundPosition : 'top'});
			$('#moteur-cache').css({display: 'block'});
			$('#moteur-cache').animate({width: '0px'},1000);
			$('#rechercher').attr('rel','');

		}*/
		/*if (rel=='') {
			$('#rechercher').css({backgroundPosition : 'bottom'});
			//$('#moteur-cache').css({display: 'block'});
			$('#moteur-cache').fadeIn(600,function(){
				if (appel) {
					$('#casino').sSelect();
					$('#forme').sSelect();
					$('#prizepool').sSelect();
					$('#chips').sSelect();
					$('#datedebut').datepicker({showOtherMonths: true, selectOtherMonths: true,changeMonth: true,showOn: 'button',buttonImage: 'css/images/calendar.gif',showAnim: 'fadeIn'});
					$('#datefin').datepicker({showOtherMonths: true, selectOtherMonths: true,changeMonth: true,showOn: 'button',buttonImage: 'css/images/calendar.gif',showAnim: 'fadeIn'});
					appel = false;
				}
			});
			$('#rechercher').attr('rel','open');
		} else {
			$('#rechercher').css({backgroundPosition : 'top'});
			//$('#moteur-cache').css({display: 'block'});
			$('#moteur-cache').fadeOut(600);
			$('#rechercher').attr('rel','');
		}*/


	});
	$('#fermer').click(function (){
		$('#rechercher').css({backgroundPosition : 'top'});
		$('#moteur-cache').css({display: 'block'});
		/*$('#rechercher').animate({top: '0px'},1000);*/
		$('#moteur-cache').animate({height: '0px'},1000,function(){$(this).css({display : 'none'});});
		$('#rechercher').attr('rel','');

		/*$('#rechercher').css({backgroundPosition : 'top'});
		$('#moteur-cache').css({display: 'block'});
		$('#moteur-cache').animate({width: '0px'},1000);
		$('#rechercher').attr('rel','');*/
	});
	var close = false;
	$('#error-moteur').dialog({autoOpen: false,
				resizable: false,
				height:135,
				width:240,
				position: 'center',
				show: 'blind',
				hide: 'blind',
				modal:true,
				buttons: {
					'Fermer': function() {
						$(this).dialog('close');
					}
				},
				close: function(event, ui) { close = true; }
	});
	var firstappel = true;
	$('#form-moteur').submit(function (){

		if (firstappel) {
			firstappel=false;
			var url = 'index.php?module=tournoi&ajax=1';
			$.post(url,	$("#form-moteur").serialize(),function (data){
				if(parseInt(data) > 0) {
						$('#form-moteur').submit();
				} else {
					$('#error-moteur').html('<p>Aucun tournoi ne correspond &agrave; votre recherche. Nous vous invitons &agrave; la renouveler selon d\'autres crit&egrave;res.</p>');
					$('#error-moteur').dialog('open');
					setTimeout(function() {
						if (!close) {
							$('#error-moteur').dialog('close');
						}

					}, 3000);
					//$('#error-moteur').delay(4800).dialog('close');
					//$('#error-moteur').delay(4800).dialog('close');
					firstappel=true;
				}

			});
			return false;
		} else return true;

	});
});
