// Déplie une partie du menu
function montre(id) {
	var d = document.getElementById(id);
	for ( var i = 1; i <= 15; i++) {
		if (document.getElementById('smenu' + i)) {
			document.getElementById('smenu' + i).style.display = 'none';
		}
	}
	if (d) {
		d.style.display = 'block';
	}
}

function Openwindow(page, titre, largeur, hauteur, options) {
	var top = (screen.height - hauteur) / 2;
	var left = (screen.width - largeur) / 2;
	window.open(page, titre, "top=" + top + ",left=" + left + ",width="
			+ largeur + ",height=" + hauteur + ",resizable=1,scrollbars=1,"
			+ options);
}

function showAvis(marchandRefId, type) {
	link = '/marchands/popupavis?id=' + marchandRefId + '&type=' + type;
	Openwindow(link, 'Avis', '400', '500', 'scrollbars=no');
}

function asynchronousCall(spanId, url, params) {
	jQuery.ajax( {
		type :'POST',
		processData :true,
		url :url,
		data :params,
		dataType :'html',
		success : function(data) {
			// alert(data);
		jQuery('#' + spanId).html(data);
	}
	});
}

// Ajout du plugin de recherche
function addPlugin(name, ext, cat, type) {
	if ((typeof window.sidebar == "object")
			&& (typeof window.sidebar.addSearchEngine == "function")) {
		window.sidebar
				.addSearchEngine(
						"http://www.jeuxdesocietepascher.net/js/jeuxdesocietepascher.src",
						"http://www.jeuxdesocietepascher.net/images/favicon.ico",
						"JeuxdeSocietepasCher",
						"Guide d'achat de jeux de société");
	} else {
		alert("Ce plugin ne s'installe que sous Mozilla Firefox");
	}
}

gLoadSpinnerUrl = '/images/spinner.gif';
gFailImage = '/images/image_load_error.gif';

function LoadImage(pSelector, pCallback) {
	var loader = $(pSelector);
	loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
	LoadThisImage($(img), loader, pCallback);
}

function LoadThisImage(loader, pCallback) {
	image_src = loader.attr('src');
	img = new Image();
	$(img).hide();
	$(img).load( function() {
		cb_js = loader.get(0).getAttribute('onload');
		onload_cb = function() {
			eval(cb_js);
		}
		loader.html(this);
		loader.removeClass('loadable-image');
		loader.removeAttr('src');
		loader.removeAttr('onload');
		$(this).show();

		if (onload_cb) {
			onload_cb($(this));
		}
		if (pCallback) {
			cb = pCallback;
			cb($(this));
		}
	}).error( function() {
		$(this).attr('src', gFailImage).show();
	}).attr('src', image_src).show();
}

function LoadAllImages() {
	$('.loadable-image').each( function() {
		var loader = $(this);
		loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
		LoadThisImage(loader);
	});
}

ValidateAjax = {
	initialize : function(formid, endpoint) {
		end_url = endpoint;
		form_id = '#' + formid;

		$(form_id + ' input').blur( function() {
			var formElementID = $(this).attr('id');
			ValidateAjax.doValidate(formElementID);
			return false;
		})
	},

	doValidate : function(id) {
		var url = end_url;
		var data = $(form_id).serialize();
		$.post(url, data,
				function(response) {
					$('#' + id).parent().find('.errors').remove();
					if (response[id])
						$('#' + id).parent().append(
								ValidateAjax.getHTML(response[id]));
				}, 'json');

	},
	getHTML : function(errArray) {
		var o = '<ul class="errors">';
		$.each(errArray, function(key, value) {
			o += '<li>' + value + '</li>';
		});
		o += '</ul>';
		return o;
	}
}

$(document).ready(
		function() {

			$("ul.notes-echelle").addClass("js");
			$("ul.notes-echelle li").addClass("note-off");

			$("ul.notes-echelle input")
					.focus(
							function() {
								$(this).parents("ul.notes-echelle").find("li")
										.removeClass("note-focus");
								$(this).parent("li").addClass("note-focus");
								$(this).parent("li").nextAll("li").addClass(
										"note-off");
								$(this).parent("li").prevAll("li").removeClass(
										"note-off");
								$(this).parent("li").removeClass("note-off");
							}).blur(
							function() {
								$(this).parents("ul.notes-echelle").find("li")
										.removeClass("note-focus");
								if ($(this).parents("ul.notes-echelle").find(
										"li input:checked").length == 0) {
									$(this).parents("ul.notes-echelle").find(
											"li").addClass("note-off");
								}
							}).click(
							function() {
								$(this).parents("ul.notes-echelle").find("li")
										.removeClass("note-checked");
								$(this).parent("li").addClass("note-checked");
							});

			$("ul.notes-echelle li").mouseover( function() {
				$(this).nextAll("li").addClass("note-off");
				$(this).prevAll("li").removeClass("note-off");
				$(this).removeClass("note-off");
			});

			$("ul.notes-echelle").mouseout(
					function() {
						$(this).children("li").addClass("note-off");
						$(this).find("li input:checked").parent("li").trigger(
								"mouseover");
					});

			$("ul.notes-echelle input:checked").parent("li").trigger(
					"mouseover");
			$("ul.notes-echelle input:checked").trigger("click");

		});

$(document).ready(
		function() {

			$("ul.notes-static").addClass("js");
			$("ul.notes-static li").addClass("note-off");

			$("ul.notes-static input").click(
					function() {
						$(this).parents("ul.notes-static").find("li")
								.removeClass("note-checked");
						$(this).parent("li").addClass("note-checked");
					});

			$("ul.notes-static li").focus( function() {
				$(this).nextAll("li").addClass("note-off");
				$(this).prevAll("li").removeClass("note-off");
				$(this).removeClass("note-off");
			});

			$("ul.notes-static input:checked").parent("li").trigger("focus");
			$("ul.notes-static input:checked").trigger("click");

		});
