$(document).ready(function(){
  $("button, a.button").each(function(){
    $(this).addClass("fg-button ui-state-default ui-corner-all");
    
	  if ( $(this).is( '.active' ) ) {
		  $(this).addClass("ui-state-hover");
		}
  });
  
  $(".fg-button:not(.ui-state-disabled), a.button")
  .hover(function(){ 
  		$(this).addClass("ui-state-hover");
  	},function(){ 
  	  if ( !$(this).is( '.active' ) ) {
  		  $(this).removeClass("ui-state-hover");
  		}
  	}
  )
  .mousedown(function(){
		$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
		if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
		else { $(this).addClass("ui-state-active"); }	
  })
  .mouseup(function(){
  	if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
  		$(this).removeClass("ui-state-active");
  	}
  });
  
  $('div.movingbox').movingBoxes({
    startPanel   : 2,
    width        : 600,
    panelWidth   : 232/600,
    reducedSize  : 207/279,
    imageRatio   : 232/279,
    speed        : 500,
    fixedHeight  : true,
    hashTags     : false,
    wrap         : true,
    buildNav     : false,
    navFormatter : null,
    easing       : 'easeOutExpo',
    tooltipClass : 'tooltip',
    panelTitle   : 'h1',
    panelText    : 'div'
  });
  
  //selectmenu ui
  $('.selectmenujoli').selectmenu({
    width: '145px'
  });
  
  $('#sliderAge').slider({
    range: true,
    min: 18,
    max: 45,
    step: 1,
    values: [ $("#sliderAgeDefMin" ).text(), $("#sliderAgeDefMax" ).text() ],
    change: function(event, ui) {
      var texteMin = $("#sliderAge").slider("values", 0);
      var texteMax = $("#sliderAge").slider("values", 1);
      var maxVal = $( "#sliderAge" ).slider( "option", "max" );
      if ( texteMax == maxVal ) { texteMax += "+"; }
      
      $("#minA").text(texteMin);
      $("#maxA").text(texteMax);
    },
    slide: function(event, ui) {
      var texteMin = $("#sliderAge").slider("values", 0);
      var texteMax = $("#sliderAge").slider("values", 1);
      var maxVal = $( "#sliderAge" ).slider( "option", "max" );
      if ( texteMax == maxVal ) { texteMax += "+"; }
      
      $("#minA").text(texteMin);
      $("#maxA").text(texteMax);
    }
  });
  
  $('#sliderTaille').slider({
    range: true,
    min: 145,
    max: 190,
    step: 1,
    values: [ $("#sliderTailleDefMin" ).text(), $("#sliderTailleDefMax" ).text() ],
    change: function(event, ui) {
      var texteMin = $("#sliderTaille").slider("values", 0);
      var texteMax = $("#sliderTaille").slider("values", 1);
      
      var maxVal = $( "#sliderTaille" ).slider( "option", "max" );
      if ( texteMax == maxVal ) { texteMax += "+"; }
      $("#minT").text(texteMin);
      $("#maxT").text(texteMax);
    },
    slide: function(event, ui) {
      var texteMin = $("#sliderTaille").slider("values", 0);
      var texteMax = $("#sliderTaille").slider("values", 1);
      
      var maxVal = $( "#sliderTaille" ).slider( "option", "max" );
      if ( texteMax == maxVal ) { texteMax += "+"; }
      $("#minT").text(texteMin);
      $("#maxT").text(texteMax);
    }
  });
  
	$('#btnRecherche').click(function() {
		$('#contenurecherche').html('<img src="../images/loader.gif" alt="" />');
		
		var genre          = $('#genre').val();
		var nom            = $('#nom').val();
		var ageMin         = $("#sliderAge").slider("values", 0);
		var ageMax         = $("#sliderAge").slider("values", 1);
		var tailleMin      = $("#sliderTaille").slider("values", 0);
		var tailleMax      = $("#sliderTaille").slider("values", 1);
		var CheveuxCouleur = $('#CheveuxCouleur').val();
		var YeuxCouleur    = $('#YeuxCouleur').val();
		var Poitrine       = $('#Poitrine').val();
		var LanguesParlees = $('#LanguesParlees').val();
		var Interets       = $('#Interets').val();
		var Prestations    = $('#Prestations').val();
		var Tarif          = $('#Tarif').val();
		var Localisation   = $('#Localisation').val();

		$.ajax({
		  type: "GET",
		  url: "../ajax/escortes.asp?genre=" + genre + "&AgeDebut=" + ageMin + "&AgeFin=" + ageMax + "&TailleDebut=" + tailleMin + "&TailleFin=" + tailleMax + "&nom=" + nom + "&CheveuxCouleur=" + CheveuxCouleur + "&YeuxCouleur=" + YeuxCouleur + "&Poitrine=" + Poitrine + "&LanguesParlees=" + LanguesParlees + "&Interets=" + Interets + "&Prestations=" + Prestations + "&Tarif=" + Tarif + "&Localisation=" + Localisation + '&langue=' + current_language,
		  success: function(data) {
  			$('#contenurecherche').html(data);
  			ChargerDivs();
		  }
		});
	});
	
	$( 'div.search-results' ).delegate( 'a.search-results-next', 'click', function(){
	  var ajaxURL = $( this ).attr( 'href' ) + '&langue=' + current_language;
	  
		$.ajax({
		  type: "GET",
		  url: "../ajax/" + ajaxURL,
		  success: function(data) {
  			$('#contenurecherche').html(data);
  			ChargerDivs();
		  }
		});
		
		return false;
	});
	
	$( 'div.search-results' ).delegate( 'a.search-results-prev', 'click', function(){
	  var ajaxURL = $( this ).attr( 'href' ) + '&langue=' + current_language;
	  
		$.ajax({
		  type: "GET",
		  url: "../ajax/" + ajaxURL,
		  success: function(data) {
  			$('#contenurecherche').html(data);
  			ChargerDivs();
		  }
		});
		
		return false;
	});
	
	$( 'div.search-results' ).delegate( 'select.search-results-pages', 'change', function() {
	  var ajaxURL = 'escortes.asp' + $( 'div.search-results-url-no-page' ).text() + 'langue=' + current_language + '&page=' + $( this ).val();
	  
		$.ajax({
		  type: "GET",
		  url: "../ajax/" + ajaxURL + '&langue=' + current_language,
		  success: function(data) {
  			$('#contenurecherche').html(data);
  			ChargerDivs();
		  }
		});
	});
  
  function ChargerDivs() {
    $("button, a.button").each(function(){
      $(this).addClass("fg-button ui-state-default ui-corner-all");
      
  	  if ( $(this).is( '.active' ) ) {
  		  $(this).addClass("ui-state-hover");
  		}
    });
    
    $(".fg-button:not(.ui-state-disabled), a.button")
    .hover(function(){ 
    		$(this).addClass("ui-state-hover");
    	},function(){ 
    	  if ( !$(this).is( '.active' ) ) {
    		  $(this).removeClass("ui-state-hover");
    		}
    	}
    )
    .mousedown(function(){
  		$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
  		if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
  		else { $(this).addClass("ui-state-active"); }	
    })
    .mouseup(function(){
    	if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
    		$(this).removeClass("ui-state-active");
    	}
    });
    
  	var displayTimeout = 0;
    
  	$( 'div.search-results-group' ).each(function(){
  		$( this ).children( 'a.search-result' ).each(function(){      
  		  $( this ).css({ opacity: 0, visibility: 'visible' } );
  		  
  		  setTimeout( "$( 'a#" + $( this ).attr( 'id' ) + "' ).animate( { opacity: 1 }, 250 );", displayTimeout );
  		  
  		  displayTimeout += 100;
  		});
  		
  		$( this ).children( 'div.search-result-big' ).css({ opacity: 0, visibility: 'visible' });
  		
  		setTimeout( "$( 'div#" + $( this ).children( 'div.search-result-big' ).attr( 'id' ) + "' ).animate( { opacity: 1 }, 250 );", displayTimeout );
  		
  		displayTimeout += 100;
    });
    
    $( 'div.search-result-big-images' ).each(function(){
      $( this ).width( $( this ).children('img').length * 260 );
    });
    
  }
  
  /* result page */
  
  $( 'div.search-results' ).delegate( 'a.search-result', 'click', function(){
    var resultBigID = $( this ).attr( 'rel' );
    var resultHotesseID = $( this ).attr( 'id' ).replace( "search-result-", "" );
    
    $( 'div#' + resultBigID + ' div.search-result-big-inner' ).stop( true, true ).animate({ opacity: 0 }, { duration: 250, complete: function(){
      $.ajax({
        url: '../ajax/escortes-photos.asp?hotesse=' + resultHotesseID + '&langue=' + current_language + $( 'div.search-results-url-add' ).text(),
        dataType: 'xml',
        success : function( xml ) {
          $( 'div#' + resultBigID + ' div.search-result-big-images' ).css({ left: 0 }).html( $( xml ).find( 'photos' ).text() ).width( $( 'div#' + resultBigID + ' div.search-result-big-images' ).children( 'img' ).length * 260 );
          
          $( 'div#' + resultBigID + ' div.search-result-big-text-content' ).html( $( xml ).find( 'text' ).text() );
          
          $( 'div#' + resultBigID + ' a.button' ).attr( 'href', $( xml ).find( 'url' ).text() ); 
          
          $( 'div#' + resultBigID + ' div.search-result-big-inner' ).animate({ opacity: 1 }, { duration: 500 });
        }
      });
    }});
	
    return false;
  });
  
  $( 'div.search-result-big-images' ).each(function(){
    $( this ).width( $( this ).children('img').length * 260 );
  });
  
  $( 'div.search-results' ).delegate( 'a.search-result-image-next', 'click', function(){
    var ImagesElement = $( this ).parent().children( 'div.search-result-big-images' );
    
    ImagesElement.stop( true, true );
    
    if ( Math.abs( parseInt( ImagesElement.css( 'left' ) ) ) < ( ImagesElement.width() - 260 ) ) { 
      ImagesElement.animate( { left: '-=260' }, { duration: 500, easing: 'easeOutExpo' } );
    } else {
      ImagesElement.animate( { left: '0px' }, { duration: 500, easing: 'easeOutExpo' } );
    }
    
    return false;
  });
  
  $( 'div.search-results' ).delegate( 'a.search-result-image-previous', 'click', function(){
    var ImagesElement = $( this ).parent().children( 'div.search-result-big-images' );
    
    ImagesElement.stop( true, true );
    
    if ( Math.abs( parseInt( ImagesElement.css( 'left' ) ) ) > 0 ) { 
      ImagesElement.animate( { left: '+=260' }, { duration: 500, easing: 'easeOutExpo' } );
    } else {
      ImagesElement.animate( { left: ( ImagesElement.width() - 260 ) * ( -1 ) }, { duration: 500, easing: 'easeOutExpo' } );
    }
    
    return false;
  });
  
  var displayTimeout = 0;
  
  $( 'div.search-results-group' ).each(function(){
    $( this ).children( 'a.search-result' ).each(function(){      
      $( this ).css({ opacity: 0, visibility: 'visible' } );
      
      setTimeout( "$( 'a#" + $( this ).attr( 'id' ) + "' ).animate( { opacity: 1 }, 250 );", displayTimeout );
      
      displayTimeout += 100;
    });
    
    $( this ).children( 'div.search-result-big' ).css({ opacity: 0, visibility: 'visible' });
    
    setTimeout( "$( 'div#" + $( this ).children( 'div.search-result-big' ).attr( 'id' ) + "' ).animate( { opacity: 1 }, 250 );", displayTimeout );
    displayTimeout += 100;
  });
  
  /* detail page */
  
  $( 'div.escorte-big-images' ).each(function(){
    $( this ).width( $( this ).children('img').length * 260 );
  });
  
  $( 'a.escorte-image-next' ).click(function(){
    var ImagesElement = $( this ).parent().children( 'div.escorte-big-images' );
    
    ImagesElement.stop( true, true );
    
    if ( Math.abs( parseInt( ImagesElement.css( 'left' ) ) ) < ( ImagesElement.width() - 260 ) ) { 
      ImagesElement.animate( { left: '-=260' }, { duration: 500, easing: 'easeOutExpo' } );
    } else {
      ImagesElement.animate( { left: '0px' }, { duration: 500, easing: 'easeOutExpo' } );
    }
    
    return false;
  });
  
  $( 'a.escorte-image-previous' ).click(function(){
    var ImagesElement = $( this ).parent().children( 'div.escorte-big-images' );
    
    ImagesElement.stop( true, true );
    
    if ( Math.abs( parseInt( ImagesElement.css( 'left' ) ) ) > 0 ) { 
      ImagesElement.animate( { left: '+=260' }, { duration: 500, easing: 'easeOutExpo' } );
    } else {
      ImagesElement.animate( { left: ( ImagesElement.width() - 260 ) * ( -1 ) }, { duration: 500, easing: 'easeOutExpo' } );
    }
    
    return false;
  });
  
  $( 'a.escorte-image' ).click(function(){
    var CurrentImageIndex = $( this ).index();
    
    var ImagesElement = $( 'div.escorte-big-images' );
    
    var NewLeftImages = ( CurrentImageIndex * 260 ) * ( -1 );
    
    ImagesElement.stop( true, true );
    
    if ( Math.abs( NewLeftImages ) < ImagesElement.width() ) {
      ImagesElement.animate( { left: NewLeftImages }, { duration: 500, easing: 'easeOutExpo' } );
    }
    
    return false;
  });
  
});
