String.prototype.contains = function (value, ignorecase) { if (ignorecase) { return (this.toLowerCase().indexOf(value.toString().toLowerCase()) != -1); } else { return this.indexOf(value) != -1; } };

/* =================================== 
Modal Window Script :: Starts here 
=================================== */
function showModalWindow(modalId, callback, callbackParameters) {
    var modal = document.getElementById(modalId);
    if (modal != null) { modal.style.display = "block"; }
    if (typeof (callback) == 'function') { callback(callbackParameters); }
}
function hideModalWindow(modalId, callback) {
    var modal = document.getElementById(modalId);
    if (modal != null) { modal.style.display = "none"; }
    if (typeof (callback) == 'function') { callback(); }
}

// callback function of onShowImage and onHideImage
function onHideImage() { $('#image_container_loading').show(); }
function onShowImage(imageUrl) {
    var imageControl = $('<img />').hide().attr('src', imageUrl);
    $(imageControl).prependTo($('#image_container').empty()).bind("load", function () {
        
        $(imageControl).resize2({ 'width': 1000 }).show();
        $('.modal-window-content-container').animate({ 'width': '1000px', 'left': '20px' }, 500, function () { });
        $('#image_container_loading').hide();
        window.scrollTo(0, 50);
    });
}
//$(document).ready(function () { $('<img id="image_container_loading" />').attr('src', '../App_Themes/Theme02/Images/loading.gif').after($('#image_container')).show(); });
/* =================================== 
Modal Window Script :: Ends here 
=================================== */

/*------------PageQuery--------------*/

var pageQuerySearch = new PageQuery(window.location.search);
var pageQueryLocation = new PageQuery(window.location.hash);
function getHashFragment(key) { if (!pageQueryLocation) { pageQueryLocation = new PageQuery(window.location.hash); } return unescape(unescape(pageQueryLocation.getValue(key))); }

function getQueryString(key) { if (!pageQuerySearch) { pageQuerySearch = new PageQuery(window.location.search); } return unescape(unescape(pageQuerySearch.getValue(key))); }

function PageQuery(query) {
    if (query.length > 1) { this.q = query.substring(1, query.length); } else { this.q = null; } this.keyValuePairs = new Array();

    if (this.q) { for (var i = 0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } };

    this.getValue = function (s) {
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            var pair = this.keyValuePairs[j];
            var values = pair.split('=');
            var key = values.shift();
            if (key == s) { return values.join("="); }
        }
        return false;
    };
}


//Chrome Drop Down Menu- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 1st, 06'
var cssdropdown = {
    disappeardelay: 250, //set delay in miliseconds before menu disappears onmouseout

    //No need to edit beyond here////////////////////////
    dropmenuobj: null, ie: document.all, firefox: document.getElementById && !document.all,

    getposOffset: function(what, offsettype) {
        var totaloffset = (offsettype == "left") ? what.offsetLeft : what.offsetTop;
        var parentEl = what.offsetParent;
        while (parentEl != null) {
            totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop;
            parentEl = parentEl.offsetParent;
        }
        return totaloffset;
    },

    showhide: function(obj, e, visible, hidden) {
        if (this.ie || this.firefox)
            this.dropmenuobj.style.left = this.dropmenuobj.style.top = "-500px"
        if (e.type == "click" && obj.visibility == hidden || e.type == "mouseover")
            obj.visibility = visible
        else if (e.type == "click")
            obj.visibility = hidden
    },

    iecompattest: function() {
        return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
    },

    clearbrowseredge: function(obj, whichedge) {
        var edgeoffset = 0
        if (whichedge == "rightedge") {
            var windowedge = this.ie && !window.opera ? this.iecompattest().scrollLeft + this.iecompattest().clientWidth - 15 : window.pageXOffset + window.innerWidth - 15
            this.dropmenuobj.contentmeasure = this.dropmenuobj.offsetWidth
            if (windowedge - this.dropmenuobj.x < this.dropmenuobj.contentmeasure)  //move menu to the left?
                edgeoffset = this.dropmenuobj.contentmeasure - obj.offsetWidth
        }
        else {
            var topedge = this.ie && !window.opera ? this.iecompattest().scrollTop : window.pageYOffset
            var windowedge = this.ie && !window.opera ? this.iecompattest().scrollTop + this.iecompattest().clientHeight - 15 : window.pageYOffset + window.innerHeight - 18
            this.dropmenuobj.contentmeasure = this.dropmenuobj.offsetHeight
            if (windowedge - this.dropmenuobj.y < this.dropmenuobj.contentmeasure) { //move up?
                edgeoffset = this.dropmenuobj.contentmeasure + obj.offsetHeight
                if ((this.dropmenuobj.y - topedge) < this.dropmenuobj.contentmeasure) //up no good either?
                    edgeoffset = this.dropmenuobj.y + obj.offsetHeight - topedge
            }
        }
        return edgeoffset
    },

    dropit: function(obj, e, dropmenuID) {
        if (this.dropmenuobj != null) //hide previous menu
            this.dropmenuobj.style.visibility = "hidden"
        this.clearhidemenu()
        if (this.ie || this.firefox) {
            obj.onmouseout = function() { cssdropdown.delayhidemenu() }
            this.dropmenuobj = document.getElementById(dropmenuID)
            this.dropmenuobj.onmouseover = function() { cssdropdown.clearhidemenu() }
            this.dropmenuobj.onmouseout = function() { cssdropdown.dynamichide(e) }
            this.dropmenuobj.onclick = function() { cssdropdown.delayhidemenu() }
            this.showhide(this.dropmenuobj.style, e, "visible", "hidden")
            this.dropmenuobj.x = this.getposOffset(obj, "left")
            this.dropmenuobj.y = this.getposOffset(obj, "top")
            this.dropmenuobj.style.left = this.dropmenuobj.x - this.clearbrowseredge(obj, "rightedge") + "px"
            this.dropmenuobj.style.top = this.dropmenuobj.y - this.clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + 1 + "px"
        }
    },

    contains_firefox: function(a, b) {
        while (b.parentNode)
            if ((b = b.parentNode) == a)
            return true;
        return false;
    },

    dynamichide: function(e) {
        var evtobj = window.event ? window.event : e
        if (this.ie && !this.dropmenuobj.contains(evtobj.toElement))
            this.delayhidemenu()
        else if (this.firefox && e.currentTarget != evtobj.relatedTarget && !this.contains_firefox(evtobj.currentTarget, evtobj.relatedTarget))
            this.delayhidemenu()
    },

    delayhidemenu: function() {
        this.delayhide = setTimeout("cssdropdown.dropmenuobj.style.visibility='hidden'", this.disappeardelay)
    },

    clearhidemenu: function() {
        if (this.delayhide != "undefined")
            clearTimeout(this.delayhide)
    }
}


var HttpRequest = {

    CreateXmlHTTP: function() {
        var xmlHTTP = null; try { xmlHTTP = new XMLHttpRequest(); }
        catch (e) {
            try { xmlHTTP = new XMLHttpRequest("Microsoft.XMLHTTP"); }
            catch (e) {
                try { xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); }
                catch (e) {
                    try { xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP"); }
                    catch (e) { alert("Your Browser does not support Ajax"); return false; }
                }
            }
        } return xmlHTTP;
    },

    GetData: function(fromUrl, divname) {
        var xmlHTTP; var v = document.getElementById(divname); xmlHTTP = HttpRequest.CreateXmlHTTP(); xmlHTTP.open('Get', fromUrl, true); xmlHTTP.onreadystatechange = function() {
            if (xmlHTTP.readyState == 4) {
                var result = xmlHTTP.responseText; var DivRes = document.createElement("Div"); DivRes.innerHTML = result; var s = DivRes.getElementsByTagName("Form")[0]; v.innerHTML = ""
                v.innerHTML = s.innerHTML;
            }
        }
        xmlHTTP.send(null);
    },
    GetHTMLData: function(fromUrl, divname) {
        try {
            var xmlHTTP; var v = document.getElementById(divname); xmlHTTP = HttpRequest.CreateXmlHTTP(); xmlHTTP.open('Get', fromUrl, true); xmlHTTP.onreadystatechange = function() {
                if (xmlHTTP.readyState == 4) {
                    var result = xmlHTTP.responseText; var DivRes = document.createElement("Div"); DivRes.innerHTML = result; var s = DivRes; v.innerHTML = ""
                    v.innerHTML = s.innerHTML;
                    //alert(s.innerHTML);
                }
            }
            xmlHTTP.send(null);
        } catch (e) {
            alert(e.message);
        }
    }
}

function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } }
function getNextSibling(startBrother) { var endBrother = null; try { endBrother = startBrother.nextSibling; while (endBrother.nodeType != 1) { endBrother = endBrother.nextSibling; } } catch (e) { } return endBrother; }
function getElements(className) { var elements = []; var el = document.getElementsByTagName('A'); var regexp = new RegExp("\\b" + className + "\\b"); for (var i = 0; i < el.length; i++) { if (regexp.test(el[i].className)) { elements.push(el[i]); } } return elements; }
function getElementsByClassName(className, tagName) { tagName = tagName || "*"; var elements = []; var el = document.getElementsByTagName(tagName); var regexp = new RegExp("\\b" + className + "\\b"); for (var i = 0; i < el.length; i++) { if (regexp.test(el[i].className)) { elements.push(el[i]); } } return elements; }
function thisindex(elm) { var nodes = elm.parentNode.childNodes, node; var i = count = 0; while ((node = nodes.item(i++)) && node != elm) if (node.nodeType == 1) count++; return count; }
function debug(text) { if (window.console) { if (window.console.log) { window.console.log(text); } } }


var pageQuerySearch = new PageQuery(window.location.search);
var pageQueryLocation = new PageQuery(window.location.hash);
function getHashFragment(key) { if (!pageQueryLocation) { pageQueryLocation = new PageQuery(window.location.hash); } return unescape(unescape(pageQueryLocation.getValue(key))); }
function getQueryString(key) { if (!pageQuerySearch) { pageQuerySearch = new PageQuery(window.location.search); } return unescape(unescape(pageQuerySearch.getValue(key))); }
function queryString(key) { return getQueryString(key); }
function PageQuery(query) {
    if (query.length > 1) { this.q = query.substring(1, query.length); } else { this.q = null; } this.keyValuePairs = new Array();
    if (this.q) { for (var i = 0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } };
    this.getValue = function (s) {
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            var pair = this.keyValuePairs[j];
            var values = pair.split('=');
            var key = values.shift();
            if (key == s) { return values.join("="); }
        }
         return false; 
    };
}

var UltraLearn = {};
UltraLearn.CookieManager = {
    createCookie: function(name, value, days, domain) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); } else { var expires = ""; } var cookieDomain = ""; if (domain) { cookieDomain = "; domain=" + domain; } document.cookie = name + "=" + value + cookieDomain + expires + "; path=/"; },
    readCookie: function(name) { 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 null; },
    eraseCookie: function(name) { createCookie(name, "", -1); },
    set: function(name, value, days, domain) { UltraLearn.CookieManager.createCookie(name, value, days, domain); },
    get: function(name) { return UltraLearn.CookieManager.readCookie(name); },
    remove: function(name) { UltraLearn.CookieManager.eraseCookie(name); },
    getAllCookies: function (name) { return document.cookie; }
};


var app = {}
app.cookies = UltraLearn.CookieManager;



(function ($) {
    $.fn.resize2 = function (opts) {
        var options = opts || { width: '400px', height: '400px' };
        var images = $(this);

        return images.each(function () {

            var img = this;
            var ImageActualWidth = $(img).width();
            var ImageActualHeight = $(img).height();

            var AspectRatio = ImageActualWidth / ImageActualHeight;
            debug(ImageActualWidth);
            debug(ImageActualHeight);
            debug(AspectRatio);
            if (AspectRatio != NaN && AspectRatio != Infinity) {

                if (options.width) {
                    var availWidth = options.width;

                    calcWidth = availWidth + "px";
                    calcHeight = availWidth / AspectRatio + "px";
                }
                else {
                    var availHeight = options.height;

                    calcHeight = availHeight + "px";
                    calcWidth = availHeight / AspectRatio + "px";
                }

                debug(calcWidth);
                debug(calcHeight);


                // update image width and height
                img.style.width = calcWidth;
                img.style.height = calcHeight;
            }


        });
    };
})(jQuery);
