$(document).ready(
	function () {
		$('a.hndle').bind('click', toggleContent);

                $('#phototheque a[rel*="lightbox"]').lightBox();


                jQuery('#mycarousel').jcarousel();

                $('#attrib1').livequery('change', updateIdAttribute);
                $('#attrib2').livequery('change', updateIdAttribute);

		$('h2.devis').click(function(){
			$(this).next('div').slideToggle("slow");
		});

		var reponse_devis = $("#reponse_devis");
		if (reponse_devis.size() > 0){
			$("h2.devis").html(reponse_devis.html());
		}

/*
	$('.option_zone').hover( ChargerPhotoOption2, 
	  function () {
		$("#pic_option").remove()
	  });
*/	  
	  	/* ajouter en 22/05/2011 */
		var menu = $('.menu');
		
		$('ul li a', menu).each(function() {
			$(this).append('<span />');
		});
		
		$('.photo_pic').click(function(e){
			e.preventDefault();
			var div_form = $('#listing_position_input');
			if (!$(this).hasClass('active')) {
				fermerAutresMenu();
				$(this).next('.menu').css({display: 'block'});
				$('.ar', this).html('&#9650;').css({top: '3px'});
				$(this).addClass('active');
				$(this).parent('.settings').nextAll('.settings').css({display: 'none'});

			} else {
				$(this).next('.menu').css({display: 'none'});
				$('.ar', this).html('&#9660;').css({top: '5px'});
				$(this).removeClass('active');
				$(this).parent('.settings').nextAll('.settings').css({display: 'block'});
			}
		});
		
		$('a.attribut').hover( chargeImage,
			function()
			{
				$("#pic_option").remove();
			}
		);
		
		$('a.attribut').click(selectionOption);
		/*****************************************************/

	}

);

var updateIdAttribute = function(e)
{

	var id_produit = $('#id').val();
	var id_option_attribute1 = $('#attrib1').val();
	var id_option_attribute2 = ($('#attrib2').length) ? $('#attrib2').val() : 1;



	$.post("ajax/produits.php",
      { action: "getIdAttribute", id_product:id_produit, id_option_attribute1:id_option_attribute1, id_option_attribute2:id_option_attribute2 },

      function (data){
    	  //alert(data.id_prod_attrib +' '+ data.ref +' '+data.prix);

    	  $('#id_prod_attribut').val(data.id_prod_attrib);
    	  $('#ref').html(data.ref);
    	  $('#price_area').html(data.prix);
    	  $('#attrib2_area').html(data.attrib2);
    	  $('#form_panier').html(data.order);
    	  $('#form_caddie').attr("action", data.url);

      },"json"

    );

}



var toggleContent = function(e)
{
	var targetContent = $("#dicr_area");

        //if (targetContent.hasClass("postbox")){
          if (targetContent.css('display') == 'none') {
                  targetContent.slideDown(800);
                  //$(this).children("span").html(" [-]");
          } else {
                  targetContent.slideUp(800);
                  //$(this).children("span").html(" [+]");
          }
        //}

	return false;
};

/* labbouz en 10/05/2011 
*	pour les photos d'option
*/
function ChargerPhotoOption(id_product_attribut){
    if(id_product_attribut != ""){
		$.post("ajax/produits.php",
	      { action: "ChargerPhotoOption", id_product_attribut:id_product_attribut },
	      function(data){

	        $("#option_pic").html(data);

	      }
	    );
    } else {
    	$("#option_pic").html("");
    }
}


function ChargerPhotoOption2(e)
{

//alert(123);

	var theSelect =  $(this).parent();

//alert(theSelect.html());
//theSelect.before('<p>test</p>');
//alert($(this).html());

var id_product_attribut = $(this).val();


if (id_product_attribut != ''){

	$.post("ajax/produits.php",
	      { action: "ChargerPhotoOption", id_product_attribut:id_product_attribut },
	      function(data){

		theSelect.before('<p id="pic_option">'+data+'</p>');

//	alert($(this).html());

//	        $("#option_pic").html(data);

	      }
	    );
    } else {
    	$("#pic_option").remove();
    }


}

/* en 22/05/2011 par labbouz */

var fermerAutresMenu = function(){
	// closed les autre bouton
	$('.photo_pic').each(function(i) {
		if ($(this).hasClass('active')) {
			$(this).next('.menu').css({display: 'none'});
			$('.ar', this).html('&#9660;').css({top: '5px'});
			$(this).removeClass('active');
		}
	});
	//
}

var chargeImage = function(e){
 	e.preventDefault(); //empeche le navigateur de suivre le lien

	var theSelect =  $(this).parent().parent().parent().parent();

	var attribut = $(this).attr("href");
	attribut = attribut.substr(1, 11);
	
	var reg=new RegExp("[ ,]+", "g");
	var tableau_attribut=attribut.split(reg);

//alert(tableau_attribut[1])

	if(tableau_attribut[1] != "" && tableau_attribut[2] != "" && typeof(tableau_attribut[1]) != 'undefined'){
//alert(tableau_attribut[1])

		id_attribut = tableau_attribut[1] + "," + tableau_attribut[2];
			$.post("ajax/produits.php",
			  { action: "ChargerPhotoOption", id_product_attribut:id_attribut },
			  function(data){
			//$("#zoneAffiche").html(data);
			//$("#zoneAffiche").show();
			theSelect.before('<p id="pic_option">'+data+'</p>');
		  }
		);
  	} else {
		$("#pic_option").remove();
	}
};

var selectionOption = function(e){
 	e.preventDefault(); //empeche le navigateur de suivre le lien
	
	var nom_option = $(this).attr("title");

	var attribut = $(this).attr("href");
	attribut = attribut.substr(1, 20); //retire le 1er caractère, à améliorer
	
	var reg=new RegExp("[ ,]+", "g");
	var tableau_attribut=attribut.split(reg);
	
	if(tableau_attribut[0] != "" && tableau_attribut[1] != "" && tableau_attribut[2] != "" ){
		$('#text_zone_'+tableau_attribut[0]).text(nom_option);
		$('#zone_'+tableau_attribut[0]).val(tableau_attribut[1]+','+tableau_attribut[2]);
  	} else {
		var nom_zone = $('#nom_zone_'+tableau_attribut[0]).val();
		$('#text_zone_'+tableau_attribut[0]).text(nom_zone);
		$('#zone_'+tableau_attribut[0]).val('');
	}
	fermerAutresMenu();

	$('.settings').css({display: 'block'});

	$("#pic_option").remove();


};

/* fin modification  22/05/2011 */

