/**
 * jQuery.MSMAccordion - Accordion menu
 * Bertrand Mansion, http://www.mamasam.com
 * @copyright Bertrand Mansion
 * @version 1.0
 */
$.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.SearchBoxText = function(text) {
  var self = this;
  this.find('#searchbox').each(function() {
    var box = $(this);
    var value = box.val();
    if (value == "") {
      var submit = self.find('#searchbutton').click(function() {
        if (box.val() == text) {
          box.val("");
        }
      });
      box.val(text);
      box.addClass("inactive");
      box.focus(function () {
        if (box.val() == text) {
            box.val("").removeClass("inactive");
        }
      });
      box.blur(function () {
        if (box.val() == "") {
          box.val(text);
          box.addClass("inactive");
        }
      });
    }
	});
	return this;
};


/* Stylize les boutons submit */

$.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;
            }
            // Do not use submit for button name
            button.form.submit();
            return false;
        });
        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({
                    backgroundColor: "#eee",
                    fontWeight: "bold"
                });
            }
        });
    });
    return this;
}

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


var mySettings; // used by markitup, here to make sure it is undefined


