/*
  
  script.js  
  
*/  
  
  (function($){
	
	$.fn.ajaxRequest = function (a,evts) {
	  el = $(this);
	   el.addClass('loading');
      $.ajax({
        type: "GET",
        url: "index.php",
		dataType : "html",
		//async : true,
	    data: "ajax_request=" + a,
	    success: function(response){
          el.html(response);
		  el.ajaxComplete( function(){
            el.removeClass('loading');
			if(evts) {
              new evts;
		    }
		  });
        }
      }); // ajax preload
    }
	

	$.fn.ajaxRequestThumb = function (file,size,evts2) {
	  el2 = $(this);
	  el2.addClass('loading');
      $.ajax({
        type: "GET",
        url: "thumb.php",
		dataType : "html",
		//async : true,
	    data: "file="+file+"&size="+size,
	    success: function(response){
			el2.hide();
		  var response = new Image();
		  response.src = "thumb.php?file="+file+"&size="+size;
		  el2.ajaxComplete( function(){

            el2.removeClass('loading');
			el2.html(response);
			//el2.html('<img src="thumb.php?file='+file+'&size='+size+'">');
			el2.fadeIn();
			if(evts2) {
              new evts2;
		    }
		  });
        }
      }); // ajax preload
    }
	
  })(jQuery)
  
  
  function changeCarousel (colorDefault) {
	  
	 
   
	if(colorDefault)   { var c = colorDefault; } else { var c = $('.styledSelect ul li.closed').attr('rel');}

	$(".carousel").empty();
    $(".carousel").addClass('loading');
    
	$.ajax({
      type: "GET",
      url: "index.php",
	  data: "ajax_request=carousel&colour="+c,
	  dataType: "html",
	  success: function(response){
	
        $(".carousel").hide();
		$(".carousel").html(response);
		$(".carousel").fadeIn("slow");
		  
		$(".carousel").jCarouselLite({
              btnNext: ".next-carousel",
              btnPrev: ".prev-carousel"
        });
		  
		$('.carousel li').each(function() {
          var rel = $(this).attr("rel");
	      var el = $(this);
	      el.html('<img src="thumb.php?file='+rel+'&size=99">');
		});
		  
		$('.carousel li').bind('click',function() {
		  var rel = $(this).attr("rel");
		  $('#main').html('<div id="main-image" style="height:200px;overflow:hidden;"></div><div id="main-text" style="height:250px;overflow:hidden;"></div>');
		  $('#main-text').ajaxRequest('ajax_content&page=bespoke&lang='+lang);
		  $('#main-image').ajaxRequestThumb(rel,'352');
		});  
		  
		$(".carousel").removeClass('loading');
		//$(".carousel").ajaxComplete( function(){
		//}); // 
	  }
    }); // ajax*/
  }
  
/**********  Events functions JqZoom **************/

  function loadZoom(l) {
    $.ajax({
      type: "GET",
      url: "index.php",
	  data: "ajax_request=zoom&lang="+l,
	  success: function(response){   
        $("#zoom").ajaxComplete(function(event, request, settings){
		  $("#zoom").html(response);
		  $(".zoom-text").hide();
		  $(".jqzoom").hide();
        });
	  }
    }); // ajax loadZoom
  }
  
 function eventsJqZoom()  {
	  
    $(".pause").click();
	$(".jqzoom").fadeIn("fast");
	$(".zoom-text").hide();
    var current_img = $(".diaporama .active img").attr("src");
	// substring param 1 value 15 = thumbs.php?file= | param 2 current_img.length-9 = &size=475
	var img_original = current_img.substring(15,(current_img.length-9));
	$(".jqzoom img").attr("jqimg",img_original);
	$(".jqzoom img").attr("src",current_img);

    $(".jqzoom").jqueryzoom({xzoom:367,yzoom:473,preload:0}); // jqueryzoom
	
	$('body').prepend('<div id="overlay" style="display:none;"></div>');
	$("#overlay").show();
	$('#overlay').click(function() {
	  $('#overlay').remove();
      $(".jqzoom").hide();
	});
	
	var eventEnd = function() {
	  $('#overlay').remove();
	  $(".jqzoom").hide();
	  $(".play").click();
	};
	
	$('.jqzoom').mouseleave(eventEnd);
	$('.jqzoom').click(eventEnd);


  } // eventsJqZoom
  
 /**********  Events Ready **************/ 

  $(document).ready(function(){
	
	$('.colours li img').bind("click", function() {
	  var groupColour = $(this).attr('rel');
	  var position = $(this).attr('pos');
	  $('.colours li.default').css({backgroundPosition: "-"+position+"px 0"});
	  $('.colours li img').removeClass('colours-activate');
	  $(this).addClass('colours-activate');
	  changeCarousel(groupColour);
    });	
	
	$('.colours li img').bind("mouseover", function() {
	  var position =$(this).attr('pos');
	  $('.colours li.default').css({backgroundPosition: "-"+position+"px 0"});
	  $('.colours li img').fadeTo(0,0.5);
	  $(this).fadeTo("fast",1);
    });
		
	$('.colours li.default').bind("click mouseover", function() {
	  $('.colours li.default').css({backgroundPosition: "0 0"});
	  $('.colours li img').fadeTo("fast",1);
    });
	
	$('.colours').bind("mouseleave", function() {
	  $('.colours li.default').css({backgroundPosition: "0 0"});
	  $('.colours li img').fadeTo("fast",1);
    });
	
	$('#main ul').addClass('diaporama_description');
	
    $(".menu-boucle-top").click(function() { 
	  $(".man-woman-title").toggle("slow");								 
	  $(".carousel-container").toggle("slow");
      $(".menu-footer").slideToggle("slow");
      $(".menu-left").slideToggle("slow");
	  
    });
  
    $(".menu-boucle-footer").click(function() { 
	  $(".man-woman-title").toggle("slow");	
	  $(".carousel-container").toggle("slow");
      $(".menu-footer").slideToggle("slow");
      $(".menu-left").slideToggle("slow");
    
	});

	/* contact */ 
	
    $("#ajax-contact-form").submit(function(){
											
      var redirect = function () {
         setTimeout("location.href = 'index.php';",3000);
      }
		
	  $("#note").empty();
      $("#note").addClass('loading');
      var str = $(this).serialize();
	  var lang = $(this).attr("lang");
      $.ajax({
        type: "POST",
        url: "index.php?ajax_request=contact&lang=" + lang,
        data: str,
        success: function(response){   
          $("#note").removeClass('loading');
          $("#note").ajaxComplete(function(event, request, settings){
		    $("#note").html(response);
		    if($("#success").length==1) {
              $("#contact-container").hide();
		      new redirect;
            }		  
          });
	    }
      }); // ajax

      return false;
  
    }); // submit
 
    
	
  }); // Events Ready