var a;
var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;

function swap() {
	this.className = "msieFix";
	if (a != undefined) {
		a.className = 'trigger';
	}
}

function swapback() {
	this.className = "trigger";
	if (a != undefined) {
		a.className = 'msieFix';
	}
}

function swapfocus() {
	this.parentNode.parentNode.parentNode.className = "msieFix";
}

function swapblur() {
	this.parentNode.parentNode.parentNode.className = "trigger";
}

function TJK_dropDown() {
	if (document.getElementById) {
		var LI = document.getElementsByTagName("li");
		var zLI = LI.length;
		for ( var k = 0; k < zLI; k++) {
			if (LI[k].id) {
				LI[k].className = "trigger";
			}
			if (LI[k].parentNode.parentNode.className.match('trigger')) {
				LI[k].firstChild.onfocus = swapfocus;
				LI[k].firstChild.onblur = swapblur;
			}
			if (LI[k].className.match('trigger')) {
				LI[k].onmouseover = swap;
				LI[k].onmouseout = swapback;
			}
			if (LI[k].className.match('msieFix')) {
				a = LI[k];
			}
		}
	}
}

function emptyModule() {
	var lc = 0, rc = 0, p = 0;
	lc = $('#leftColumn').height();
	rc = $('#rightColumn').height();
	if (lc > rc + 40 && $('#fbPush').length==0) {
		$('#emptyModule').css('display', 'block');
		$('#emptyModuleBg').css('height', parseInt(lc - (rc + 27)) + 'px');
		if ($.browser.msie && $.browser.version.substring(0, 1) === '6') {
			$('#emptyModuleBg').css('height', parseInt(lc - (rc + 47)) + 'px');
		}
	}
	if (lc < rc  && $('#fbPush').length==0) {
		$('#leftBgWarp').css('height', parseInt(rc - 53) + 'px');
	}
}

function setEmptyModule() {
	setTimeout('setEmptyModuleHeight()', 300);
}

function setEmptyModuleHeight() {
	var lc = 0, p = 0;
	lc = $('#leftColumn').height();
	p = $('#pushes').height();
	if ($.browser.msie && $.browser.version.substring(0, 1) === '6') {
		$('#emptyModule').css('display', 'block');
		$('#emptyModuleBg').css('height', parseInt(lc - (p + 46)) + 'px');
	} else {
		$('#emptyModule').css('display', 'block');
		$('#emptyModuleBg').css('height', parseInt(lc - (p + 26)) + 'px');
	}
}

function menu(form, id, href) {
	var frm = document.forms[form];
	frm.action = href;
	var el = document.createElement("input");
	el.type = "hidden";
	el.name = "menuId";
	el.value = id;
	frm.appendChild(el);
	frm.submit();
}

function setFooterWidth() {
	var w = (($('#footerMenu > li').size() - 1) / 2) * 20;
	$('#footerMenu > li').each(function() {
		w += $(this).width();
	});
	$('#footerMenu').css('width', w + 'px');
}

function fakeFbFader() {
	if ($('#fbPush').css('display') == 'none') {
		$('#fakePush').fadeOut('slow', function () {
			$('#fbPush').fadeIn('slow');
		});
	} else {
		$('#fbPush').fadeOut('slow', function () {
			$('#fakePush').fadeIn('slow');
		});
	}
}

function divStep4Switcher(val) {
	switch (val) {
			case "1":
			case "2":
				$("#question8a").hide();
				$("#question8b").show();
			break;
			
			case "3":
				$("#question8a").show();
				$("#question8b").hide();
			break;
			
			default:
				$("#question8a").hide();
				$("#question8b").hide();
	}
}

function divStep1RestitutionSwitcher(val) {
		if (val == 2) {
			$('#div_merchandising_comm').show();
		} else {
			$('#div_merchandising_comm').hide();
		}
	}

function goStep(num) {
	$('#nextStep').val(num);
	$("form.form_restitution").submit();
}

function initiateEventDefaultValuesTextarea() {
	var defaultValue = 'Ecrire un commentaire';

	$('textarea.areainput').each( function () {
			if ($(this).val() == '') {
				$(this).val(defaultValue);
			}
		}
	);

	$('textarea.areainput')
	.focus(function() {
		var element = $(this);
		(element.val()==defaultValue) ? element.val(''):'';
	})
	.blur(function() {
		var element = $(this);
		(element.val()=='')?element.val(defaultValue):'';
	})
}

function initiateStep2Restitution() {
    $(".item-recipe .part-image").hover(function(){
        $(this).find(".notehover").show();
    }, function(){
        $(this).find(".notehover").hide();
    });
    
    $(".part-notation li a").hover(function(){
        $(this).parents(".part-notation").find("li").removeClass("highlight");
        $(this).parent("li").addClass("highlight");
    });
    
    
    $("#smenu-step2 a").click(function(){
        $("#smenu-step2 li").removeClass("active");
        $(this).parent("li").addClass("active");
        
        var vhref = $(this).attr("href");
        $(".all-recipes").hide();
        $(vhref).show();
    });
}

function step5_SetPrice(i){
	$("#price").val(i);
	$(".list-prixeuro li a").each(function(){
		$(this).attr("class", "");
	});
	for (j = 1; j <= i; j++) {
		$("#price"+j).attr("class", "active");
	}
}

$(document).ready(function() {
	$('#fbPush').css('display', 'none');
	setInterval('fakeFbFader()', 10000);
	TJK_dropDown();
	$('input:radio').checkBox();
	$('input:checkbox').checkBox();
	$('#type').sSelect();
	$('#country').sSelect( {
		ddMaxHeight : '85px'
	});
	emptyModule();
	setFooterWidth();
	initiateEventDefaultValuesTextarea();
	initiateStep2Restitution();
	
	divStep4Switcher($("#question7-1:checked, #question7-2:checked, #question7-3:checked").val());
	$("#question7-1, #question7-2, #question7-3").change(function() { divStep4Switcher($(this).val()); });
	
	
	$(".show").click(function() {
		$(this).parent().next().slideToggle(300, function() {
			setEmptyModuleHeight();
		}, function() {
			setEmptyModuleHeight();
		});

		if ($(this).parent().hasClass("active")) {
			$(this).parent().removeClass("active");
		} else {
			$(this).parent().addClass("active");
		}
	});
	
	$("#form_login .link #lnkOubli").click(function () {
		$('#oubli').attr('checked', true);
		$('#form_login').parents('form').submit();
	});
	
	$("#steps ul li a").each(function() {
		$(this).click(function () {
			var regexp = /Etape (\d)/;
			goStep($(this).html().match(regexp)[1]);
		})
	})
	
	divStep1RestitutionSwitcher($("#merchandising-1:checked, #merchandising-2:checked, #merchandising-3:checked").val());
	$("#merchandising-1, #merchandising-2, #merchandising-3").click(function() {divStep1RestitutionSwitcher($(this).val());});
	
	$(".roundedCornersImage").corner();
});