﻿/******************************************************************************************************************
* @name: cWatermark 
* @type: jQuery
* @author: Urban Björkman
* @version: x
* @requires x
*******************************************************************************************************************/
(function ($) {
    $.fn.extend({
        cWatermark: function (watermark, options) {

            var defaults = { classname: '' };
            var o = $.extend(defaults, options);


            return this.each(function () {
                var watermarkDefaultValue = watermark;
                var validator = $(this).closest("form").validate();

                if ($(this).val() == "") {
                    $(this).val(watermarkDefaultValue);
                    $(this).addClass(o.classname);
                    if (!(validator && validator.settings && validator.settings.rules[this.name] && validator.settings.rules[this.name].required))
                        $(this).addClass("jsIgnoreValidation");
                }

                $(this).click(function () {
                    if ($(this).val() == watermarkDefaultValue) {
                        $(this).removeClass(o.classname);
                        $(this).removeClass("jsIgnoreValidation");
                        $(this).val("");
                    }
                });

                $(this).blur(function () {
                    if ($(this).val() == "") {
                        $(this).val(watermarkDefaultValue);
                        $(this).addClass(o.classname);
                        if (validator && validator.settings && validator.settings.rules[this.name] && validator.settings.rules[this.name].required)
                            return;
                        $(this).addClass("jsIgnoreValidation");
                    }
                });
                var el = this;
                $(this).closest("form").submit(function () {
                    if ($(el).val() == watermarkDefaultValue)
                        $(el).val("");
                });
                if (validator && validator.settings && validator.settings.ignore.indexOf(".jsIgnoreValidation") < 0)
                    validator.settings.ignore += ",.jsIgnoreValidation";
            });
        }
    });
})(jQuery);

// Checkbox Validation
jQuery.validator.addMethod("checkrequired", function (value, element) {
    var checked = false;

    checked = $(element).is(':checked');
    return checked;
}, '');

jQuery.validator.unobtrusive.adapters.addBool("mandatory", "checkrequired");

// Intelligent validation error highligters NOTE: Must be manually attached to the validator on your form.
function ValErrorHighlight(a, b, c) {
    var e = a.type == "radio" || a.type == "checkbox" ? $(a).parent() : $(a);
    e.addClass(b).removeClass(c);
}
function ValErrorUnhighlight(a, b, c) {
    var e = a.type == "radio" || a.type == "checkbox" ? $(a).parent() : $(a);
    e.addClass(c).removeClass(b);
}

/******************************************************************************************************************
* @name: IframeAutoHeight 
* @type: jQuery
* @author: Mattias Högnäs
* @version: x
* @requires x
*******************************************************************************************************************/
(function ($) {
    $.fn.IframeAutoHeight = function (spec) {

        var options = $.extend({
            heightOffset: 0,
            minHeight: 0,
            callback: function (newHeight) { }
        }, spec);

        var iframe = this;

        $(this).each(function () {

            function resizeHeight() {
                var content = $(iframe).contents();
                var root = ($.browser.webkit) ? content.find("html") : content.find("body");
                var newHeight = $(root).outerHeight();
                if (newHeight < options.minHeight) {
                    newHeight = options.minHeight + options.heightOffset;
                }
                $(iframe).height(newHeight);
                options.callback({ newFrameHeight: newHeight });
            }

            $(this).ready(function () {
                var delayedResize = function () {
                    resizeHeight(this);
                };
                setTimeout(delayedResize, 500);
            });

        });
    };
} (jQuery));


/******************************************************************************************************************
* @name: open_popup 
* @type: Javascript
* @author: x
* @version: x
* @requires: x
* @description: this is legacy code from old site, used in aspx in admin
* refactor or use jquery plugin 
*******************************************************************************************************************/
function open_popup(url, handler, w, h)
{
	width = screen.Width;
	height = screen.Height;
    height = (height / 2) - (h / 2);
    width = (width / 2) - (w / 2);
	if(parent.wHander)
	{
		var handler = parent.wHandler;		
	}
	else
	{
	    var handler;
	}
	handler = window.open(url, handler ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,top='+height+',left='+width);
	handler.focus();

}

/******************************************************************************************************************
* @name: indexOf
* @type: Javascript extension
* @author: Urban Björkman
* @version: x
* @requires: x
* @description: Replaces original indexOf method to work in all browser version (no indexOf in ie7)
* best replacement is jQuery.inArray but it is not useful in all cases
*******************************************************************************************************************/
//if (!Array.indexOf) {
//    Array.prototype.indexOf = function (obj, start) {
//        for (var i = (start || 0); i < this.length; i++) {
//            if (this[i] == obj) {
//                return i;
//            }
//        }
//        return -1;
//    }
//}

/******************************************************************************************************************
* @name: MenuButton
* @type: jQuery plugin
* @author: fsc
* @version: x
* @requires: 
* @description: When run on a <select> converts it to a pretty button and a sliding panel
*******************************************************************************************************************/
(function ($) {
    $.fn.extend({
        MenuButton: function (onChangeCallback, skipFirstOption) {
            this.each(function(i, e) {
                
                var outer = $('<div class="jsMenuButton divMenuButton"></div>');
                var input = $('<input type="hidden" />')
                    .attr("name", $(this).attr("name"))
                    .attr("id", $(this).attr("id"))
                    .val($(this).val());
                var button = $('<div></div>')
                    .text($(this).find(":selected").text())
                    .click(function() {
                        $(this).next().slideToggle(200);
                    });
                var lis = $.map($(this).children(skipFirstOption ? ':gt(0)' : ''), function(e) { 
                        return $("<li></li>").text($(e).text()).data('value', $(e).attr('value')).click(function() { 
                                $(this).closest(".jsMenuButton").find("input:hidden").val($(this).data('value'));
                                $(this).closest(".jsMenuButton").find("div").text($(this).text());
                                $(this).closest("ul").slideUp(200);
                                if(onChangeCallback)
                                    onChangeCallback($(this).data('value'));
                            });
                        });
                
                var list = $('<ul></ul>').attr("style", "display:none");

                $(this).after(outer);
                outer.append(input).append(button);
                outer.append(list);
                $(lis).each(function (){
                    list.append($(this));
                });

                $(this).remove();
            });
        }
    })
})(jQuery);


/******************************************************************************************************************
* @name: cTab
* @type: jQuery plugin
* @author: fsc
* @version: x
* @requires: a container elements with anchor links to divs in the same container
* @description: creates tabs from a collection of anhor links and content divs
*******************************************************************************************************************/

(function ($) {
    $.fn.extend({
        cTab : function(options) {
            //Set default options and copy overriden properties from the options argument
            var defaultOptions = {
                selectedIndex: 0,
                offsetHeight: 0,
                minHeight: 0,
                activeTabCss: 'active',
                tabContainerCss: 'divTabs',
                contentCss: 'divTabContent',
                onClickCallback: null
            };
            if(options) for(p in defaultOptions) if(options[p]) defaultOptions[p] = options[p];
            this.each(function(i, e) {
                var outer = $(this);
                var heightOffset = defaultOptions.offsetHeight || outer.children('.' + defaultOptions.tabContainerCss).height() + 15;

                var tabs = outer.children('.' + defaultOptions.tabContainerCss).show().children();
                var containers = outer.children('.' + defaultOptions.contentCss)

                var activeTab = tabs.slice(defaultOptions.selectedIndex, defaultOptions.selectedIndex + 1);
                var activeContainer = $(activeTab.attr('href'));

                containers.hide().css('position', 'absolute').width(outer.width());
                tabs.removeClass(defaultOptions.activeTabCss);

                activeTab.addClass(defaultOptions.activeTabCss);
                activeContainer.show();
                outer.height(Math.max(defaultOptions.minHeight, heightOffset + activeContainer.height()));
                $(window).load(function() { outer.height(Math.max(defaultOptions.minHeight, heightOffset + activeContainer.height())); });

                tabs.click(function() {
                    var container = $($(this).attr('href') + ':hidden');
                    if(!container.length) return false;

                    tabs.removeClass(defaultOptions.activeTabCss);
                    $(this).addClass(defaultOptions.activeTabCss);
                    containers.fadeOut();
                    container.fadeIn();
                    outer.animate({height: Math.max(defaultOptions.minHeight, heightOffset + container.height())});
                    return false;
                });
            });
        }
    })
})(jQuery);

/******************************************************************************************************************
* @name: cDefaultButton
* @type: jQuery plugin
* @author: fsc
* @version: x
* @requires: a container element and an id of the default button
* @description: binds on-key-up on all child inputs of type text, and triggers the click event of the supplied default elemet
*******************************************************************************************************************/
(function ($) {
    $.fn.extend({
        cDefaultButton: function (selector) {
            this.find('input:text').each(function () {
                $(this).keyup(function (e) { if (e.keyCode == 13) { $(selector).click(); return true; } return false; });
            });
        }
    });
})(jQuery);
