
// Original source: http://stilbuero.de/jquery/accordion/jquery.accordion.js
// Modified by Bertrand Mansion
// Added cookies, active class, nested accordion support

$.fn.MSMAccordion = function(options) {

    if (options && options.usecookie) {
        if ($.cookie(options.usecookie) && $.cookie(options.usecookie) > 0) {
            var on = $.cookie(options.usecookie);
        }
    }
    if (on == undefined) {
        var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on - 1 : 0;
    }

    return this.each(function(i) {
        $(this).find('>dt').each(function(i) {
            if (options && options.close || i != on) {
                $(this).find('+dd').hide();
            }
            $(this).css({cursor: "pointer"});
            $(this).click(function() {
                var curr = $(this.parentNode).find('dd:visible');
                var next = $(this).find('+dd');
                if (curr[0] != next[0]) {
                    curr.slideUp('fast');
                    if (options && options.active) {
                        curr.prev().removeClass(options.active);
                    }
                } 
                if (next.is(':visible')) {
                    next.slideUp('fast');
                    if (options && options.active) {
                        $(this).removeClass(options.active);
                    }
                } else {
                    next.slideDown('fast');
                    if (options && options.active) {
                        $(this).addClass(options.active);
                    }
                }
                if (options && options.usecookie) {
                    $.cookie(options.usecookie, i, {path: '/'});
                }

            });
        });
    });
};


$.fn.MSMStyleSubmit = function() {

	this.each(function() {

		var self = $(this);

		var a = $("<a href=\"#\"></a>");
		a.attr("style", self.attr("style"));
		a.attr("id",    self.attr("id"));
		a.addClass(self.attr("class"));
    a.text(self.val());

    a.click(function(e) {
      e.preventDefault();
      var hidden = document.createElement("input");
      hidden.setAttribute("type", "hidden");
      hidden.setAttribute("name", self.attr("name"));
      hidden.setAttribute("value", self.val());
      var button = self.get(0);

      button.form.appendChild(hidden);
      if (button.onclick != undefined && button.onclick() == false) {
        return false;
      }
      if (button.form.onsubmit && typeof(button.form.onsubmit) == "function") {
        if (!button.form.onsubmit()) return false;
      }
            
      $(button.form).submit();
    });
    self.attr("id", null);
    self.hide();
    var t = a.insertBefore(self);
	});
	return this;
}

$.fn.MSMStyleMultiples = function() {

	this.each( function() {
        $(this).children("option").each(function () {
            if (this.text.charAt(0) != '-') {
                $(this).css({
                    backgroundColor: "#eee",
                    fontWeight: "bold"
                });
            }
        });
    });
    return this;
}

$.fn.MSMHideForms = function() {
	this.each( function() {
        $(this).hide();
    });
    return this;
}


$.fn.MSMSwitchImage = function (imgboxname, titleboxname) {
    var img = $(this).attr('href');    
    var title = $(this).attr('title');

    $("#"+imgboxname).empty().append('<img src="'+img+'" alt="'+title+'" />');

    if (title != undefined) {
        $("#"+titleboxname).empty().append(title);
    }
    return this;
}



function initProducts() {
	$(".prevnext > a").each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			var a = e.target;
			$(e.target).css('background', 'none').empty().html('<img src="/images/ajax-loader.gif" style="vertical-align:middle" />');
			$.getJSON(a.href, { ajax: "1" }, function(data) {
				// replace pager
				$(".prevnext").empty().html('<?=_("Pages : ")?>' + data.pager);
				// replace products
				$("#liste").empty().html(data.products);
				initProducts();
			})
		})
	});
}
