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

jQuery.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: '/'});
                }

            });
        });
    });
};

jQuery.fn.MSMRoundCorner = function()
{

    var MSMGetImage = function(position, size, dir) {
    	    var image = $("<img>");
            image.css({width: size, height: size});

            if (jQuery.browser.msie) {
                image.attr("src", "/images/blank.gif");
                image.get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
                                         '/'+dir+'/rc'+size+position+'.png' + "',sizingMethod='scale')";
            } else {
                image.attr("src", "/"+dir+"/rc"+size+position+".png");
            }
            return image;
        }

    $(this).each(function() {

        var dir = 'images';
        if ($(this).is(".rc14px")) {
            var size = '14px';
        } else if ($(this).is(".hrc14px")) {
            var size = '14px';
            var dir = 'images/home';
        } else if ($(this).is(".rc30px")) {
            var size = '30px';
        } else {
            var size = '8px';
        }

        if ($(this).is(".corn0")) {
            var corner = $("<div>");
            corner.css({ width: size, height: size });
            corner.addClass('corner0');
            var image = MSMGetImage(0, size, dir);
            corner.append(image);
            $(this).prepend(corner);
        }
        if ($(this).is(".corn1")) {
            var corner = $("<div>");
            corner.css({ width: size, height: size });
            corner.addClass('corner1');
            corner.css("left", (this.offsetWidth - parseInt(size)) + 'px');
            var image = MSMGetImage(1, size, dir);
            corner.append(image);
            $(this).prepend(corner);
        }
        if ($(this).is(".corn2")) {
            var corner = $("<div>");
            corner.css({ width: size, height: size });
            corner.addClass('corner2');
            corner.css("left", (this.offsetWidth - parseInt(size)) + 'px');
            corner.css("margin-top", '-' + size);
            var image = MSMGetImage(2, size, dir);
            corner.append(image);
            $(this).append(corner);
        }
        if ($(this).is(".corn3")) {
            var corner = $("<div>");
            corner.css({ width: size, height: size });
            corner.addClass('corner3');
            var image = MSMGetImage(3, size, dir);
            corner.css("margin-top", '-' + size);
            corner.append(image);
            $(this).append(corner);
        }
    });
    return this;
}


jQuery.fn.MSMStyleSubmit = function() {

	this.each(function() {

		var a = $("<a>");
		var self = $(this);

		a.attr("href",  "#");
		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"});
        a.insertBefore(self);
	});
	return this;
}

jQuery.fn.MSMStyleMultiples = function() {

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

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

jQuery.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;
}
