$(document).ready(function() {
	
	
// CSS
	// if ($('#categories .col').eq(1).length) {
	// 	$('#categories .col:first').addClass('first');
	// 	$('#categories .col:last').addClass('last');
	// };

	var prodHeight = 0;
	$('#fullPromo .prodName').each(function(index) {
		var thisHeight = $(this).height();
		if ( thisHeight > prodHeight) {
			prodHeight = thisHeight;
		};
	});
	$('#fullPromo .prodName').height(prodHeight);
	
	// Gestion de la mise en page des colonnes sur les promos de la home
	$('#fullPromo .product:eq(0)').addClass('alpha');
	$('#fullPromo .product:eq(2)').addClass('omega');
	$('#fullPromo .product:eq(3)').addClass('alpha');
	$('#fullPromo .product:eq(5)').addClass('omega');
	

	// CSS Formulaires, ajouts de classes
	$('input:image').addClass('inputImg');
	$('input:text').addClass('inputTxt');
	$('input:checkbox').addClass('inputCbox');
	$('input:text').focus(function() {
		$(this).toggleClass('focus');
	});
	$('input:text').blur(function() {
		$(this).toggleClass('focus');
	});

// Arrondis, nécessite jquery.corner.js
	$('.ad').wrapInner('<div class="wrapper"></div><!--/wrapper-->');
	$.getScript("medias/js/jquery.corner.js", 
		function () {
			$('#main').corner("5px");
			$('.selection h2, .top h2, .menu-categories h1, .conseils, .titre, #newsletter').corner("5px");
			$('#footer').corner("top sc:#fe8700 5px")
			$('#footer').corner("bottom 5px");
			$('#search').corner("top sc:#fe8700 2px");
			$('#search').corner("bottom 5px");
			// Categories
			$('#filter').corner("5px");
			$('#sort').corner("5px");
			$('.pager').corner("5px");
			$('#bubble').corner("5px");
			$('.ad .wrapper').corner("round 3px").parent().css('padding', '1px').corner("round 5px");
		}
	);
	
	
	// Formulaire
	smartClearInput("#email");

	
	// Liens sur les produits
	$('.product').each(function(index) {
		// this.innerHTML = this + " is the element, " + index + " is the position";
		var lnk = '<a href="' + $(this).find('.prodName a').attr('href') + '" title="' + $(this).find('.prodName a').attr('title') + '"></a>';
		$(this).find('.currentPrice').wrapInner(lnk);
		$(this).find('.offer').wrapInner(lnk);
		$(this).find('img').wrap(lnk);
	});
	// Pop up affiliation
	$('.product a').click( function() {
			window.open ($(this).attr('href'),"","menubar=1,toolbar=1,directories=1,location=1,status=1,scrollbars=1,resizable=1,width=780,height=425");
			return false;
		});

	// Zoom image sur les produits
	$().mousemove(function(e){
		$('#popImage').css('top', e.pageY-80);
		$('#popImage').css('left', e.pageX+15);
	});
	
	$('.product').each(function(index) {
		if ($(this).find('img').length) {
			var URI = unescape($(this).find('img').attr('src')).split("=");
			$(this).append('<img src="medias/img/loupe.png" alt="Zoom" title="Zoom" class="zoom"/>');
			$(this).find(".zoom").attr('rel', URI[1]);
		};
	});
	$('.product .zoom').hover(function() {
		$('body').append('<div id="popImage"><img src="'+ $(this).attr('rel') +'" /></div>');
	}, function() {
		$('#popImage').hide("fast");
		$('#popImage').remove();
	});
	
	
	// Top categories
	var state = "closed";

	$('#firstCol .top .plus a').click(function() {
		if (state == "closed") {
			$('#firstCol .top li').slideDown();
			$(this).text('› Moins');
			state = "opened";
		} else if (state == "opened"){
			$('#firstCol .top li:gt(10)').slideUp();
			$(this).text('› Plus');
			state = "closed";
		}
		return false;
	});



    if($("#marquefiltre")){
        $("#marquefiltre").change(function(){
            window.location.replace(this.options[this.selectedIndex].value) ;
        });
    }

    if($('#form_contact')){
            $('#form_contact').submit(function(){
               try{

                var error = "";

                if($('#email').val()==""){

                    error+="<br />&#8250; <strong>Vous n'avez pas indiqué votre adresse email.</strong>";
                }else{

                 var regexEmail = /^[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/;
                 if($('#email').val().search(regexEmail) == -1){
                      error+="<br />&#8250; <strong>Votre adresse email est incorrecte.</strong>";
                 }
                }

                if($('#mail_content').val()==""){
                    error+="<br />&#8250; <strong>Vous n'avez pas laissé de commentaire.</strong>";
                }

                if(error!=''){
                    $('#error_content').empty();

                    $('#error_content').html(error);
                    return false;
                }else
                    return true;
               }catch(e){
                   alert(e);
                   return false;
               }
            });
        }
});



function smartClearInput (champ) {
	var searchValue = $(champ).val();
	$(champ).focus(function() {
			if ($(this).val() == searchValue) {
				$(this).val("");
			};
		});
		$(champ).blur(function() {
			if ($(this).val() == "") {
				$(this).val(searchValue);
			};
		});
}

