$(document).ready(function(){

	// Zoom feature
	
	$pastille_couleur = $('.pastille_couleur');
	//Pour qu'en arrivée de page, la classe ".active" soit sur la bonne pastille de couleur
	//Je vérifie que le URL en cours correspondent à l'attribut HREF d'une des pastilles.
	$pastille_couleur.each(function(e) {
		$href = window.location.href;
		$split = $href.split('/');
		valeur = $split[$split.length-1];
		obj = $(this);
		if (valeur ==  obj.attr('href')) {
			obj.addClass('active');
		}
		else { obj.removeClass('active'); }
	});
	// Get a new color, ajax
	$pastille_couleur.click(function() {
		$t = $(this);
		var url = this.href;
		$.get(url+'?ajax=1', function(txt) {
// 			console.log('test');
			$pastille_couleur.removeClass('active');
			$t.addClass('active');
			var ret = eval('('+txt+')');
			$('#products_zoom').attr('href', ret.img_zoom);
			$('#product_img').attr('src', ret.img_normal);
			$('#price').html(ret.price);
			$('#price_sold').html(ret.price);
			$('#size_ul').html(ret.sizes);
			$('#title').html(ret.description);
			//$('#subtitle').html(ret.name+' / '+ret.color);
			$('#products_zoom').CloudZoom();
			$('.sku_name').html('Item #' + ret.sku + ' (' + ret.color + ')');
			$('#sku_hid').val(ret.sku);

			// Track ajax call with google analytics
			if(typeof(pageTracker) == 'object') {
				try {
					if(root_url) {
						// Convert absolute to relative url
						var rel = url.split(root_url);
						if(rel.length == 2) {
							url = rel[1];
						}
					}
					pageTracker._trackPageview(url);
				}
				catch(e) {}
			}
			
			// Update facebook + twitter links
			var new_url = encodeURIComponent(root_url+$t.attr('href'));
			$('#facebook_btn').attr('href', 'http://facebook.com/sharer.php?u='+new_url);
			$('#twitter_btn').attr('href', 'http://twitter.com/home?status='+new_url);
		});
		
		return false;
	});

	$('ul li.btn').hover(function(){
			$(this).find('span.mask').fadeIn(10);
		}, function(){
			$(this).find('span.mask').fadeOut('fast');
		}
	);

	$('#similar_carousel').jcarousel({
		auto: 0,
		scroll: 1,
		wrap: 'last'/*,
		initCallback: mycarousel_initCallback*/

	});

	$('#send_friend_btn').click(function() {
		$('#send_to_friend').slideToggle();
		return false;
	});

	$('#submit_friend').click(function() {
		var errMsg = '';
		var emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!emailExp.test($('#email').val())) {
			errMsg += str_err_msg_email+"\n";
		}
		if(!emailExp.test($('#email_friend').val())) {
			errMsg += str_err_msg_friend+"\n";
		}

		if(errMsg) {
			alert(errMsg);
		}
		else {
// 			console.debug($('#send_friend_form').serialize())
			$.post(ajax_url, $('#send_friend_form').serialize(true), function(ret) {
				if(ret == 1) {
					$('#send_to_friend').slideUp();
				}
				else {
					alert('Erreur lors de l\'envoi du courriel');
				}
			});
		}
		return false;

	});

	$('#cancel_friend').click(function() {
		$('#send_to_friend').slideUp();
		return false;
	});

	// Sizes
	$('#sizes_menu').removeClass('hover').hover(function(){
		$(this).children('strong').addClass('active').next('ul').fadeIn(50);
	}, function(){
		$(this).children('strong').removeClass('active').next('ul').fadeOut('fast');
	});

});

