var formBlocked = false ;

function switchView (viewType) {
	if(viewType == 'list') {
		document.getElementById('products_moz').style.display = 'none' ;
		document.getElementById('products_list').style.display = 'block' ;
	}
	else if(viewType == 'moz') {
		document.getElementById('products_list').style.display = 'none' ;
		document.getElementById('products_moz').style.display = 'block' ;
	}
	
	$.ajax({
		type: 'POST',
		url: 'products.php',
		data: 'action=set-view&view='+viewType,
		success: function(msg) { }
	}) ;
}

var starSelected = [0, 0, 0, 0, 0, 0, 0] ;

function starOver (voteId, starId) {
	if(starSelected[voteId] == 0) {
		for(var i = 1; i <= 5; i++) {
			if(starId >= i) {
				document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_blue.gif' ;
			}
			else {
				document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_gray.gif' ;
			}
		}
	}
}

function starOut (voteId, starId) {
	for(var i = 1; i <= 5; i++) {
		if(starSelected[voteId] >= i) {
			document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_blue.gif' ;
		}
		else {
			document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_gray.gif' ;
		}
	}
}

function starClick (productUniqueId, voteId, starId) {
	if(starSelected[voteId] == 0) {
		starSelected [voteId] = starId ;

		$.ajax({
			type: 'POST',
			url: 'products.php',
			data: 'action=add-vote&productUniqueId='+productUniqueId+'&voteId='+voteId+'&starId='+starId,
			success: function(msg) {
				for(var i = 1; i <= 5; i++) {
					if(document.getElementById('star'+voteId+'_'+i)) {
						document.getElementById('star'+voteId+'_'+i).style.cursor = 'default' ;
					}
				}

				$('#voteText').fadeIn('slow', function () {
					setTimeout(function () { $('#voteText').fadeOut('slow') ; }, 2000) ;
				}) ;
			}
		}) ;
	}
}

function go_search () {
	var q = document.getElementById('q').value ;
	var c = document.getElementById('c').value ;
	var p1 = document.getElementById('p1').value ;
	var p2 = document.getElementById('p2').value ;
	if(q == '0' || q == 'Tapez un mot clef...') q = '' ;
	if(c == '0' || c == 'Toutes les catégories') c = '' ;
	var p = p1+'<'+p2 ;
	if((p1 == '0' || p1 == '0') && (p2 == '0' || p2 == '0')) p = '' ;
	
	if(c != '') {
		window.location.href = c+'?q='+encodeURIComponent(q)+'&p='+encodeURIComponent(p) ;
	}
	else {
		window.location.href = 'recherche?q='+encodeURIComponent(q)+'&p='+encodeURIComponent(p) ;
	}
}

$(document).ready(function() {
	$('#commentForm').validate({
		submitHandler: function(form) {
			if(typeof formBlocked == 'undefined' || !formBlocked) {
				formBlocked = true ;

				create_loading(form.id, 'Envoi du commentaire...', {fontWeight: 'bold', fontSize: '110%'}) ;

				jQuery(form).ajaxSubmit({
					success: function (response, type, form) {
						var obj = eval("(" + response + ')') ;

						if(obj.success) {
							window.location.reload() ;
						}
						else {
							destroy_loading(form.attr('id')) ;
							alert_msg(obj.errors.reason) ;
							formBlocked = false ;
						}
					}
				});
			}
		}
	});
});
