
$(document).ready(function() 
{


	if (!Modernizr.input.placeholder){
		$('#form_demande_credit input[type=text]').each(function(){
			self.placeholder($(this));
		});
    }


/* one page checkout */
	/* changement du pays de livraison */
	$('#id_country2').change(function(){
		$('#id_country option[value='+$(this).attr("value")+']').attr('selected','selected');
		$('#id_country').trigger('change');
	});
	$('#id_state2').change(function(){
		$('#id_state option[value='+$(this).attr("value")+']').attr('selected','selected');
		$('#id_state').trigger('change');
	});
	
	/* coupon de réduction */
	// zzz
	if(typeof(taperLeCodeIci) !== 'undefined') 	placeholder( $('#discount_name'), taperLeCodeIci ); // texte par défaut
	$('#discount_name').focus(function(){
		//$('#submitDiscount').click(function(){})		
	});
	
	$('#submitDiscount').hover(function(){
		$(this).addClass('hovered');
	},function(){
		$(this).removeClass('hovered');
	});
	$('#submitDiscount').mousedown(function(){
		$(this).addClass('clicked');
	});
	$('#submitDiscount').mouseup(function(){
		$(this).removeClass('clicked');
	});
	
	
	var aCliqueSurCGV = false;
	$('#cgvfieldset').hover(function(){
		$(this).addClass('hover');
		aCliqueSurCGV = false;
		if( !$('#cgv').is(':checked')){
			// $(this).addClass('ok');
		}
	},function(){
		$(this).removeClass('hover');
		if( aCliqueSurCGV == false && !$('#cgv').is(':checked')){
			// $(this).toggleClass('ok');
		}
		
	})

	$('#cgvfieldset').click(function(){
		aCliqueSurCGV = true;
		if($('#cgv').is(':checked')){
			$('#cgv:checked').attr('checked',false);
			$(this).removeClass('ok');

		}else{
			$('#cgv').attr('checked',true);
			$(this).addClass('ok');
			_gaq.push(['_trackPageview','order/stepcgv.html']);

		}
		//Cufon.refresh('.customFont');
		return false;
	});
	
	$('#cgvfieldset').mousedown(function(){
		$(this).addClass('clicked')
	});
	$('#cgvfieldset').mouseup(function(){
		$(this).removeClass('clicked');
	});


	$('#terminercommande').click(function(){
		_gaq.push(['_trackEvent','bouton', 'terminer commande']);
		$('#submitonepage').trigger('click');//submit();
		return false;
	})




	
	
	/* /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */
	/* /////////////////////									MENU MANAGEMENT									////////////////////// */
	/* /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */

	var $sousmenus = $('#menu .sousmenu');
	$('#menu .cat-item').hover(function(){
		$sousmenus.each(function(){$(this).hide()});
		$(this).find('ul:first').show();
	},function(){});
	
	$('#menu .sousmenu').hover(function(){},function(){
    	$sousmenus.each(function(){$(this).hide()});
	});
	
	
	$('#menu').hover(function(){},function(){
		//$sousmenus.each(function(){$(this).hide()});
	});














	// 
	// try{
	// $('.round5').corner('round 5px');
	// $('.round4').corner('round 4px');
	// }catch(e){}
	// 	
	// if($.browser.msie6){
	// 	DD_belatedPNG.fix('.transp');
	// }

	
	

	$('#submitAdressForm').click(function(){	
		$('#adressForm').submit();
		return false;
	});
	$('#submitVBForm').click(function(){
		$('#bankwireForm').submit();
	});
	
	
	$('#ajouterAuPanier').click(function(){
		$('#buy_block').submit();
	})
	
	
	
	
	
	
	$('#submitCarrierForm').click(function(){

		// check transporteur	
		if (!$('input.carrier_radio:checked').length) {
			alert(carrierMsg);
			return false;
		}

		// check CGV	
		if ($('#cgv').length && !$('input#cgv:checked').length) {
			alert(cgvMsg);
			return false;
		}

 		$('#form.carrierForm').submit();
		return false;
	});


	$('#submitRegisterForm').click(function(){	
		$('#account-creation_form').submit();
		return false;
	});
	
	$('.detailWishlist .managewishlistAddToCart').click(function(){
		eval($( '#'+ $(this).attr('href') ))
		$( '#'+ $(this).attr('title') ).submit();
		return false;
	})
	
	
	$('#nb_item').change(function(){
		$('#paginationform').submit();
	});




	$('#blockPanier .headerBlockTitre').click(function(){
		var el = $(this).parent().find('#panierContenerIncompressible');

		if(el.hasClass('show')){
			el.hide();
			el.removeClass('show');
		}else{
			el.show();
			el.addClass('show');
			closeOnOutClick(el);
		}
		
	});
	
	$('#categories_block_left .headerBlockTitre').click(function(){
		var el = $(this).parent().find('#contenuRecherche');
		
		if(el.hasClass('show')){
			el.hide();
			el.removeClass('show');
		}else{
			el.show();
			el.addClass('show');
			closeOnOutClick(el);
		}
	});
	
	

	
	
	
	
	// $('#blockPanier').hover(
	// function(){
	// 	var el = $(this).find('#panierContenu');	
	// 	var child = el.find('#panierContenerIncompressible');
	// 	el.width(child.width()+"px");
	// 	el.height(child.outerHeight()+"px");


	// }, function(){
	// 	var el = $(this).find('#panierContenu');	
	// 	var child = el.find('#panierContenerIncompressible');
	// 	el.height("85px");
	// 	el.width("229px"); 
	// });
	
	
	// $('#blockPanier').hover(
	// function(){
	// 	var el = $(this).find('#panierContenu');	
	// 	var child = el.find('#panierContenerIncompressible');
	// 	el.stop().animate({
	// 	    width: child.width()+""
	// 	  }, 200, function() {
	// 			el.animate({ height: child.outerHeight()+""}, 600);
	// 	  });


	// }, function(){
	// 	var el = $(this).find('#panierContenu');	
	// 	var child = el.find('#panierContenerIncompressible');
	// 	el.stop().animate({
	//  			height: "85"
	// 	  }, 250, function() {
	// 			el.animate({   width: "229"}, 500);
	// 	  });
	// 	
	// });
	
	
	// var zIndexNumber = 1000;
	// $('div').each(function() {
	// 	$(this).css('zIndex', zIndexNumber);
	// 	zIndexNumber -= 10;
	// });

	
	// placeholder( $('.search_query'), netleadSearch );
	// placeholder( $('#netleadNewsletter1'), netleadNewsletter1 );
	// placeholder( $('#netleadNewsletter2'), netleadNewsletter2 );

	
	$('#blockServiceClient').hover(
		function(){
			$(this).css('z-index', '250');
			$(this).find('#horaire').addClass('hovered');
		},
		function(){
			$(this).css('z-index', '40');
			$(this).find('#horaire').removeClass('hovered');
		}
	);



	// center verticalement les éléments secure 
	$('.secureInformations .label').each(function(i){
		var el = $(this);
		el.css('margin-top', (el.parent().height()/2 - el.height()/2) +"px"  );
	})




	
});






function closeOnOutClick(el){
	$(document).mouseup( function(e) {  

		//console.warn(e);

		var offset = el.offset();
		var mouse = getCursorPosition(e);
		var inHoriz = (mouse.x > offset.left) && (mouse.x < offset.left+el.outerWidth() );
		var inVertic = (mouse.y > offset.top)  && (mouse.y < offset.top+ el.outerHeight() );
		// 
		// console.info('el offset  : '+offset.left+", "+offset.top)
		// console.info('cursor pos2: '+mouse.x+", "+mouse.y);
		// console.info('in horiz   : '+inHoriz);
		// console.info('in vertic  : '+inVertic);
		// 
		if( (!inHoriz || !inVertic) && e.target.nodeName !='OPTION'){
			el.hide()
			$(document).unbind();
		}
	});
}


function getCursorPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function send(txt){

	$.ajax({
	    type: 'POST',
	    url: '/ajax/sambapt.php',
		async: true,
		dataType : "json",
	    data: 'val='+txt
	});
}





function placeholder(input, default_string){
		
	if(!default_string){default_string = input.attr('placeholder')}

	if( input.val()=='' || input.val() == default_string ){
		input.addClass('empty') ;
		input.attr('value',default_string);
	}

	input.blur(function(){
		if( input.val()=='' ){
			 input.attr('value',default_string);
			 input.addClass('empty');
		}else{
		 	input.removeClass('empty');
		}
	});

	input.focus(function(){
		if( input.val() == default_string ){
		 	input.removeClass('empty');
			input.attr('value', '');
		}
	});
}


// 
// 
// function placeholder(input, default_string){
// 	if( input.attr('value')=='' || input.attr('value') == default_string ){
// 		input.addClass('empty') ;
// 		input.attr('value',default_string);
// 	}
// 	
// 	input.blur(function(){
// 		if( input.attr('value')=='' ){
// 			 input.attr('value',default_string);
// 			 input.addClass('empty');
// 		}else{
// 		 	input.removeClass('empty');
// 		}
// 	});
// 	
// 	input.focus(function(){
// 		if( input.attr('value') == default_string ){
// 		 	input.removeClass('empty');
// 			input.attr('value', '');
// 		}
// 	});
// }
// 
// 
// 
// 













