﻿/// <reference path="jquery-1.4.2.min-vsdoc.js" />
//jQuery.helpers: functrions used to help in everyday jQuery coding

//Console Log function
cl = function (input) {
    if (console != undefined) {
        console.log(input);
    }
}

$.reminder = function () {
    cl('One line if/else:   [condition] ? [if condition is ture] : [else]');
}

$.exeptionbrowser = function (inversion) {
    var str = 'MSIE';
    if (navigator.appVersion.match(str)) {
        var version = navigator.appVersion;
        version = version.substr(version.indexOf(str) + (str.length + 1), 1);
        version = parseInt(version);
        if (version <= inversion)
            return true;
        else
            return false;
    }
    else
        return false;
}

//EXAMPLE:
/********************************************************************************************************
$('#confirmReturnManagementSubmit').sgValidation([
{ input: '#returnManagementRef', msg: 'Du måste fylla i referens' },
{ input: '#returnManagementEmail', msg: 'Du måste fylla i korrekt e-postadress', patternt: 'mail' }
]);
********************************************************************************************************/
$.fn.sgValidation = function (validate) {
    var v, flag, items, item, errorcounter;
    var msg = '';
    var mailRegExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    $(this).click(function () {
        flag = true;
        for (var i in validate) {
            v = $.extend({
                input: '',
                pattern: '',
                msg: '',
                isText: true,
                isRadio: false,
                isCheckbox: false,
                isTextarea: false,
                isDropdown: false
            }, validate[i]);

            //if input selector is defined not empty
            if ((v.input != '') && (v.input != undefined)) {
                //creating array of the objects we get from $() selection
                items = $.makeArray($(v.input));
                //looping all objects
                errorcounter = 0;
                for (var x in items) {
                    //saving current object in variable for easier access
                    item = $(items[x]);
                    //if input is type text and empty
                    if (v.isText) {
                        if (v.pattern != '') {
                            if (v.pattern == 'mail') {
                                if (!item.val().match(mailRegExp)) {
                                    flag = false;
                                    if (errorcounter == 0)
                                        msg += v.msg + '\n';
                                    errorcounter++;
                                }
                            }
                            else {
                                if (item.val() != v.pattern) {
                                    flag = false;
                                    if (errorcounter == 0)
                                        msg += v.msg + '\n';
                                    errorcounter++;
                                }
                            }
                        }
                        else {
                            if (item.val() == '') {
                                flag = false;
                                if (errorcounter == 0)
                                    msg += v.msg + '\n';
                                errorcounter++;
                            }
                        }
                    }
                } //end for (var x in items)
            } //end if ((v.input != '') && (v.input != undefined))
        } //end for

        if (!flag)
            alert(msg);

        msg = '';

        return flag;
    });
}

//TOGGLE OVERLAY
var $overlay;
$.toggleOverlay = function (o) {
    var settings = $.extend({
        opacity: 0.5,
        color: '#000',
        lock: true
    }, o)

    if ($overlay) {
        if (settings.lock)
            $(window).unbind('scroll'); //unlocking scroll
        $overlay.remove();
        $overlay = undefined;
    }
    else {
        var cs = [$('html, body').scrollLeft(), $('html, body').scrollTop()]; //getting current scroll position
        if (settings.lock)
            $(window).bind('scroll', { pos: cs }, scrollLock); //locking scroll

        $overlay = $('<div>&nbsp;</div>').css({
            'position': 'absolute',
            'left': '0',
            'top': '0',
            'width': $(document).width() + 'px',
            'height': $(document).height() + 'px',
            'background': settings.color,
            'opacity': settings.opacity,
            'z-index': '1000'
        }).prependTo('body');
    }

    function scrollLock(e) {
        $(window).scrollLeft(e.data.pos[0]).scrollTop(e.data.pos[1]);
    }
}




//COOKIE FUNCTIONS
var cookieMonster = {
    get: function (name) {
        return sgReadCookie(name);
    },
    set: function (name, value, days) {
        return sgCreateCookie(name, value, days);
    },
    kill: function (name) {
        sgDeleteCookie(name);
    }
};


var cookies_not_supported = 'COOKIES_NOT_SUPPORTED';

function sgCreateCookie(name, value, days) {
    try {
        if (navigator.cookieEnabled) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/";
            return sgReadCookie(name);
        } else {
            return cookies_not_supported;
        }
    } catch (e) {
        return cookies_not_supported;
    }
}
function sgReadCookie(name) {
    try {
        if (navigator.cookieEnabled) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return false;
        } else {
            return cookies_not_supported;
        }
    } catch (e) {
        return cookies_not_supported;
    }
}
function sgDeleteCookie(name) {
    sgCreateCookie(name,"",-1);
}




//FUNCTION FOR SG BOXILIZE UL (activate radio button inside)
$.fn.sgBoxilizeUlRadio = function () {
    $(this).find('li').click(function () {
        $(this).find('input[type=radio]').attr('checked', 'checked').blur();
    });
    return this;
}



// TAB SYSTEM FUNCTION
$.fn.sgTabSystem = function (o) {
    //settings
    var settings = $.extend({
        active_class: 'active',
        tabs_selector: '.ul_tabs li',
        panes_selector: '.div_panels .panel',
        cookie_name_extension: '_active_tab_cookie'
    }, o);

    // setting this to variable
    var $t = $(this);

    // setting active tab cookie name
    var sgts_cookie_name_active_tab = $t.attr('id') + settings.cookie_name_extension;
    // getting active tab cookie
    var sgts_cookie_value_active_tab = cookieMonster.get(sgts_cookie_name_active_tab);

    // on load
    if (sgts_cookie_value_active_tab)
        switch_active_tab($t.find(settings.tabs_selector + ' a[rel=' + sgts_cookie_value_active_tab + ']'));

    // on click
    $t.find(settings.tabs_selector + ' a').click(function () {
        cookieMonster.set(sgts_cookie_name_active_tab, this.rel, 1);
        switch_active_tab(this);
        return false;
    });

    // switch active tab function
    function switch_active_tab(tab_reference) {
        //hiding panels and un activating tabs
        $t.find(settings.tabs_selector).removeClass(settings.active_class);
        $t.find(settings.panes_selector).hide();

        //showing panel to activate and marking active on current tab
        $(tab_reference).parents('li').addClass(settings.active_class);
        $t.find(settings.panes_selector + '.' + $(tab_reference).attr('rel')).show();
    }

    return this;
}
