if(!window.console || typeof window.console.log !== 'function') {
	window.console = window.console || {}
	window.console.log = function() {}
}

/**************************************
 * Place holder
 *************************************/
$.fn.placeholder = function(){
	function init(obj){
		if (obj.val()!="")
			return false;
		var placeholder = obj.attr("placeholder");
		obj.val(placeholder)
		obj.addClass("placeholder")
		.blur(function(){
			if ($(this).val()=="")
			{
				$(this).val(placeholder)
				$(this).addClass("placeholder")
			}
		}).focus(function(){
			if ($(this).val()==placeholder)
			{
				$(this).val("")
				$(this).removeClass("placeholder")
			}
		});
	}
	return this.each(function(){init($(this));});
}

$(document).ready(function(){
	$("#search_input").placeholder();
});
/**************************************
 * Barcode Generator
 *************************************/
var bitsCode = new Array(10);
bitsCode[0] = new Array('0001101','0100111','1110010','000000');
bitsCode[1] = new Array('0011001','0110011','1100110','001011');
bitsCode[2] = new Array('0010011','0011011','1101100','001101');
bitsCode[3] = new Array('0111101','0100001','1000010','001110');
bitsCode[4] = new Array('0100011','0011101','1011100','010011');
bitsCode[5] = new Array('0110001','0111001','1001110','011001');
bitsCode[6] = new Array('0101111','0000101','1010000','011100');
bitsCode[7] = new Array('0111011','0010001','1000100','010101');
bitsCode[8] = new Array('0110111','0001001','1001000','010110');
bitsCode[9] = new Array('0001011','0010111','1110100','011010');

function EAN13(codes, element) {
	$(element).html('')
	var bits = getBits(codes);
	bit = bits.split("");
	var style = 'position: relative;';
	var html = '<div class="_code">'
	html += '<div class="nombre1">' + codes.charAt(0) + '</div>'
	var block = 2
	for(var i = 0; i < bit.length; i++) {
		var longBar = i==0 || i==2 || i==46 || i==48 || i==92 || i==94
		html += '<div class="' + (longBar ? 'l' : 'n') + bit[i] + '"></div>'
		if(i == 3 || i == 50) {
			html += '<div class="nombre' + block + '">'
			html += i==3 ? codes.substr(1,6) : codes.substr(7,6)
			html += '</div>'
			block++
		}
	}
	html += '</div>'
	$(element).html(html)
}

function getBits(codes) {
	var bits = "101";
	var cle = bitsCode[codes[0]][3].split("");
	for (var i=1;i<7;i++)
		bits += bitsCode[codes[i]][cle[i-1]];
	bits += "01010";
	for (var i=7;i<13;i++)
		bits += bitsCode[codes[i]][2];
	bits += "101";
	return (bits);
}
/**************************************
 * Advanced search
 *************************************/
$(document).ready(function(){
	if($.browser.msie && $.browser.version <= 7 || $.browser.mozilla && $.browser.version.replace(/\.\d\.\d$/, '') < 1.9) {
		$('body').prepend('<div style="background-color: #FBE3E4; color:#8a1f11; border: 5px solid #FBC2C4; padding: 10px;"><h1>Vous utilisez un navigateur obsolète. L\'utilisation de ProductOnline peut en être fortement affectée.</h1>Vous pouvez utiliser des navigateurs plus récents, comme <a href="http://www.mozilla-europe.org/fr/firefox/">Mozilla Firefox</a>, <a href="http://www.google.ch/chrome?hl=fr">Google Chrome</a>, <a href="http://www.apple.com/fr/safari/">Apple Safari</a>, ou encore <a href="http://www.microsoft.com/france/windows/internet-explorer/telecharger-ie8.aspx">Microsoft Internet Explorer 8</a>.</div>')
	}
	// Show/Hide advanced form
	$("#advanced_button a").click(function(){
		$("#advanced").toggle("fast");
	});
});

/**************************************
 * Contextual help
 *************************************/
var alertBox = {
	init: function(){
		alertBox.container = $("#mask");
		alertBox.container.click(alertBox.hide);
		alertBox.box = $("#alert", alertBox.container);
		alertBox.content = $("#helpContent", alertBox.container);
		$("#closeHelp").click(alertBox.hide);
		$("#alert", alertBox.container).click(function(){return false;});
		$(window).resize(alertBox.refreshPosition);
	},
	show: function(reference){
		alertBox.container.hide();
		alertBox.content.html($(reference).html());
		alertBox.container.fadeIn("fast", function(){
			alertBox.refreshPosition();
			var height = alertBox.box.show("fast");
		});
	},
	refreshPosition: function(){
		var windowHeight = $(window).height()-20;
		var boxHeight = alertBox.box.outerHeight();
		var height = (boxHeight > windowHeight)?windowHeight:boxHeight;
		alertBox.box.css('margin-top',(height/-2)+"px").css('max-height',windowHeight+"px");
	},
	hide: function(){
		alertBox.container.fadeOut("fast", function(){
			alertBox.box.hide();
		});
	}
}
$(document).ready(function(){
	alertBox.init();

	$("a.contextualHelp").each(function(){
		var reference = $(this).attr("href");
		$(this).click(function(){alertBox.show(reference)});
		$(reference).hide();
	});
});

/**************************************
 * Draft loader
 *************************************/
$(document).ready(function(){
	$(".open select").change(function(){
		window.location.href = $(this).val();
	});
});

/**************************************
 * Brand
 *************************************/
$(document).ready(function(){
	$("select[name='brand']").change(function(){
		if($(this).val() == "manual"){
			$(".brand .manual input").attr("disabled", false);
			$(".brand .manual").css("opacity","1");
		}
		else{
			$(".brand .manual input").attr("disabled", true);
			$(".brand .manual").css("opacity","0.5");
		}
	});
});

/**************************************
 * Illustration Popup"s
 *************************************/
$(document).ready(function(){
	$(".illustration a").click(function(){
		$(".illustration input").click()
		return false
	});
	$('.certifications a').click(function() {
		$('.certifications input').click()
		return false
	})
});

/**************************************
 * Forms informations
 *************************************/
$(document).ready(function(){
	$(".infos").hide();
	$("input, textarea").focus(function(){
		$(this).parents('.form').eq(0).find(".infos").show("fast");
	}).blur(function(){
		$(this).parents('.form').eq(0).find(".infos").hide("fast");
	});
});

/**************************************
 * AJR and serving calculations
 *************************************/
(function($){
	var onUnitChange = function() {
		var unit = $(this).val() == 'GRAM' ? 'g' : 'ml'
		$('.nutrition .unit').text(unit)
	}
	$(function() {
		$('#nutritionUnit').change(onUnitChange)
	})
})(jQuery);

/*************************************/
(function($){
	$(document).ready(function(){
		var select = function() {
			var id = $(this).attr('id'), name = $(this).attr('name'), context = $(this).parent();
			$("label", context).removeClass('selected').filter("label[for="+id+"]").addClass('selected');
		}

		/**************************************
		 * Allergens selection
		 *************************************/
		$("#allergensValue").change(function(){
			var value = $(this).val();
			$("input[value="+value+"]",".item.allergens").each(select).attr("checked", true);
		})

		$('.allergens input[type=radio]').change(select);
		$('.allergens input[type=radio][checked]').each(select);

		/**************************************
		 * Diet selection
		 *************************************/
		$("#dietValue").change(function(){
			var value = $(this).val();
			$("input[value="+value+"]",".item.diet").attr("checked", true).each(select);
		})

		$('.diet input[type=radio]').change(select);
		$('.diet input[type=radio][checked]').each(select);
	});
})(jQuery);
