
// 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.css({ display: "none"});
    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({
                    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;
}

/**
 * jQuery.labelify - Display in-textbox hints
 * Stuart Langridge, http://www.kryogenix.org/
 * Released into the public domain
 * Date: 25th June 2008
 * @author Stuart Langridge
 * @version 1.3
 */
$.fn.labelify = function(settings) {
  settings = jQuery.extend({
    text: "title",
    labelledClass: ""
  }, settings);
  var lookups = {
    title: function(input) {
      return $(input).attr("title");
    },
    label: function(input) {
      return $("label[for=" + input.id +"]").text();
    }
  };
  var lookup;
  var jQuery_labellified_elements = $(this);
  return $(this).each(function() {
    if (typeof settings.text === "string") {
      lookup = lookups[settings.text]; // what if not there?
    } else {
      lookup = settings.text; // what if not a fn?
    }
    // bail if lookup isn't a function or if it returns undefined
    if (typeof lookup !== "function") { return; }
    var lookupval = lookup(this);
    if (!lookupval) { return; }
 
    // need to strip newlines because the browser strips them
    // if you set textbox.value to a string containing them    
    $(this).data("label",lookup(this).replace(/\n/g,''));
    $(this).focus(function() {
      if (this.value === $(this).data("label")) {
        this.value = this.defaultValue;
        $(this).removeClass(settings.labelledClass);
      }
    }).blur(function(){
      if (this.value === this.defaultValue) {
        this.value = $(this).data("label");
        $(this).addClass(settings.labelledClass);
      }
    });
    
    var removeValuesOnExit = function() {
      jQuery_labellified_elements.each(function(){
        if (this.value === $(this).data("label")) {
          this.value = this.defaultValue;
          $(this).removeClass(settings.labelledClass);
        }
      });
    };
    
    $(this).parents("form").submit(removeValuesOnExit);
    $(window).unload(removeValuesOnExit);
    
    if (this.value !== this.defaultValue) {
      // user already started typing; don't overwrite their work!
      return;
    }
    // actually set the value
    this.value = $(this).data("label");
    $(this).addClass(settings.labelledClass);
 
  });
};

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();
			})
		})
	});
}