﻿/*
Name: jQuery Koujala Overlay Plugin
Version: 1.1 
Last Updated: 01/21/2010
Author: Murali Koujala
Copyright(c): 2010 
License: Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/
(function($) {
    $.fn.koverlay = function(options) {
        //koverlay Plugin CSS Start;
        var css_koverlay = { "border": "solid 1px gray", "background-color": "#97B9FF" };
        var css_koverlayheader = { "font-family": "Verdana, sans-serif", "font-size": "22px", "font-weight": "normal", "color": "#000" };
        var css_koverlaybutton = { "font-family": "Verdana, sans-serif", "font-size": "14px", "font-weight": "bold", "color": "#000" };
        var css_koverlaycontent = { "padding": "3px", "border": "solid 2px #cfae78", "background-color": "#111"};
        //koverlay Plugin CSS End;
        function template() {
            var html = [];
            html[html.length] = '           <div class="css_koverlaycontent"></div>';
            return html.join('');
        };
        if (this.length > 1) {
            var html = [];
            html[html.length] = '<div id="__koujalaMessage" style="display: none;">';
            html[html.length] = 'Selector must result in a unique DOM Element.';
            html[html.length] = '<br/>You have applied "' + this.selector + '" as a selector which is a collection of ' + this.selector.length + ' items';
            html[html.length] = '</div>';
            $(document.body).append(html.join(''));
            $("#__koujalaMessage").koverlay({ title: "Error", background: true });
            return;
        }

        //defaults;
        var defaults = {
            background: true,
            scroll: false,
            title: 'Title',
            dynamic: false
        };
        //apply user settings to defaults;
        options = $.extend(defaults, options);

        //load the overlay background div element once;
        var id_background = '__koujalaBackground';
        if ($('#' + id_background).size() == 0) {
            //append the overlay background element to document body;
            $(document.body).append('<div id="' + id_background + '"></div>');
            $('#' + id_background).css({ 'position': 'absolute', 'top': '0px', 'left': '0px' });
            $('#' + id_background).css({ 'display': 'none' });
            $('#' + id_background).css({ 'z-index': '9998' });
            $('#' + id_background).css({ 'height': $(window).height() });
            $('#' + id_background).css({ 'width': $(window).width() });
        }

        //apply background rules for new set of overlays;
        if (options.background == true) {
            $('#' + id_background).css({ 'background-color': 'black' });
            $('#' + id_background).css({ 'opacity': '0.7' });
        }
        else {
            $('#' + id_background).css({ 'background-color': 'black' });
            $('#' + id_background).css({ 'opacity': '0.0' });
        }

        //check if there are overlay divs loaded already;
        var id_overlay = '__koujalaOverlay';
        var intCount = $('div[id^="__koujalaOverlay"]').size();
        if (intCount > 0) {
            $('#' + id_overlay + eval(intCount - 1)).fadeOut(500);
        }
        //Set a new ID for overlay div;
        id_overlay = '__koujalaOverlay' + intCount;

        //load the overlay div element;
        var html = [];
        html[html.length] = '<div id="' + id_overlay + '"></div>';

        //append the overlay element to document body;
        $(document.body).append(html.join(''));

        //append the html for overlay layout to div overlay;
        $('#' + id_overlay).html(template());

        //now apply css classes to elements
        $('#' + id_overlay + ' .css_koverlay').css(css_koverlay);
        $('#' + id_overlay + ' .css_koverlayheader').css(css_koverlayheader);
        $('#' + id_overlay + ' .css_koverlaybutton').css(css_koverlaybutton);
        $('#' + id_overlay + ' .css_koverlaycontent').css(css_koverlaycontent);

        //update the content placeholder element with html from the element;
        $('#' + id_overlay + ' .css_koverlayheader').html(options.title);

        //move actual node to inside overlay container node
        $('#' + id_overlay + ' .css_koverlaycontent').append($(this.selector));
        $(this.selector).css("display", "block");
        $(this.selector).css("dynamic", options.dynamic);
        $('#' + id_overlay).css({ 'position': 'absolute', 'display': 'none', 'z-index': '9999' });

        //get the height and width of the overlay;
        var intHeight = $('#' + id_overlay).height();
        var intWidth = $('#' + id_overlay).width();

        $('#' + id_overlay).css('height', intHeight);
        $('#' + id_overlay).css('width', intWidth);
        $('#' + id_overlay).css('top', ($(window).height() - intHeight) / 2);
        $('#' + id_overlay).css('left', ($(window).width() - intWidth) / 2);

        $(window).resize(function() {
            var intTop = $(window).scrollTop();
            var scrollTop = 0;
            if (!options.scroll) {
                scrollTop = $(window).scrollTop();
            }
            $('#' + id_overlay).css('top', ($(window).height() - intHeight) / 2 + scrollTop);
            $('#' + id_overlay).css('left', ($(window).width() - intWidth) / 2);
            $('#__koujalaBackground').css('height', $(window).height() + intTop);
            $('#__koujalaBackground').css('width', $(window).width());
        });

        $(window).scroll(function() {
            var intTop = $(window).scrollTop();
            var scrollTop = 0;
            if (!options.scroll) {
                scrollTop = $(window).scrollTop();
            }
            $('#' + id_overlay).css('top', ($(window).height() - intHeight) / 2 + scrollTop);
            $('#' + id_overlay).css('left', ($(window).width() - intWidth) / 2);
            $('#__koujalaBackground').css('height', $(window).height() + intTop);
            $('#__koujalaBackground').css('width', $(window).width());
        });

        //trigger resize to ensure scroll heights are taken into account;
        $(window).trigger('resize');

        //display the overlay background;
        $('#__koujalaBackground').fadeIn(250);

        //display the overlay now;
        $('#' + id_overlay).fadeIn(500);
        return this;
    };
    $.fn.koverlay.hide = function() {
        var id_background = '__koujalaBackground';
        var id_overlay = '__koujalaOverlay';

        var intCount = $('div[id^="__koujalaOverlay"]').size();
        if (intCount < 2) {
            $('#' + id_background).fadeOut(500);
        }
        else {
            id_overlay = '__koujalaOverlay' + eval(intCount - 2);
            $('#' + id_overlay).fadeIn(250);
        }
        id_overlay = '__koujalaOverlay' + eval(intCount - 1);
        $('#' + id_overlay).fadeOut(250);

        //get the id of actual overlay div
        var id = $('#' + id_overlay + ' .css_koverlaycontent div').attr("id");
        //find out if the overlay is meant to be dynamic, if yes we do not need to retain the DIV
        var blnRemove = $('#' + id).css("dynamic");
        if (!blnRemove) {
            $(document.body).append($('#' + id));
            $('#' + id).css("display", "none");
        }
        $('#' + id_overlay).remove();
    };
})(jQuery);

(function($) {
    $.fn.kcookie = function() {
        return this;
    };
    $.fn.kcookie.read = function(strCookieField) {
        var strCookie = '; ' + document.cookie + '; ';
        var intBegin = strCookie.indexOf('; ' + strCookieField + '=');
        if (intBegin !== -1) {
            var val_begin = (intBegin * -1 - strCookieField.length - 3) * -1;
            var qs_val = strCookie.substring(val_begin, strCookie.indexOf('; ', val_begin));
            if (qs_val !== '') {
                return unescape(qs_val.replace(/\+/g, ' '));
            }
            else {
                return '';
            }
        }
        else {
            return '';
        }
    };
    $.fn.kcookie.create = function(name, value, mins) {
        var expires = '';
        if (mins) {
            var date = new Date();
            date.setTime(date.getTime() + (mins * 60 * 1000));
            expires = '; expires=' + date.toGMTString();
        }
        else {
            expires = '';
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    }
})(jQuery);

(function($) {
    $.fn.kjson = function(idScript, urlScript) {
        var elementHead = document.getElementsByTagName("head").item(0);
        var elementScript = document.getElementById(idScript);
        if (elementScript) {
            elementHead.removeChild(elementScript);
        }
        elementScript = document.createElement("script");
        elementScript.setAttribute("id", idScript);
        elementScript.setAttribute("type", "text/javascript");
        elementScript.setAttribute("charset", "utf-8");
        elementScript.setAttribute("src", urlScript);
        elementHead.appendChild(elementScript);
    }
})(jQuery);

(function($) {
    var logger = [];
    $.fn.klog = function(strMessage) {
        logger[logger.length] = strMessage;
    }
    $.fn.klog.display = function() {
        var arrHtml = [];
        arrHtml[arrHtml.length] = '<div id="id_log1" style="height:400px;overflow:auto;">';
        for (var i = 0; i < logger.length; i++) {
            arrHtml[arrHtml.length] = logger[i] + '<br/>';
        }
        arrHtml[arrHtml.length] = '</div>';
        $(document.body).append(arrHtml.join(''));
        $('#id_log1').koverlay({title: "Log Viewer"});
    }
})(jQuery);

$(document).keyup(function(e) {
    if (e.ctrlKey && e.shiftKey && e.keyCode == 76) {
        $().klog.display();
    }
    if (e.keyCode == 27) {
        $().koverlay.hide();
    }
});
