﻿/* adds dom ready functionality for elements within update panels */
(function($) {
    $.domJaxReady = function(callback) {
        $(function() {
            callback();
            if (Sys) {
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() { callback(); });
            }
        });
    }
})(jQuery);

/*Shortcut methods*/
var $show = function(elem) { Sys.UI.DomElement.setVisible(elem, true) };
var $hide = function(elem) { Sys.UI.DomElement.setVisible(elem, false) };

/*Start Legacy shortcut methods */
var layeron = function(elem) { Sys.UI.DomElement.setVisible(elem, true) };
var layeroff = function(elem) { Sys.UI.DomElement.setVisible(elem, false) };
var getlayer = function(elem) { return $get(elem) };

/*End Legacy shortcut methods */
if (typeof (Happy) == 'undefined') {
    var Happy = {
        __namespace: true,
        __typeName: "Happy"
    };
}
Happy.GRU = Happy.GRU ? Happy.GRU : {
    __namespace: (function() { return (this.__namespace ? this.__namespace : true) })(),
    __typeName: (function() { return (this.__typeName ? this.__typeName : "Happy.GRU") })()
};
Happy.GRU.Web = Happy.GRU.Web ? Happy.GRU.Web : {
    __namespace: (function() { return (this.__namespace ? this.__namespace : true) })(),
    __typeName: (function() { return (this.__typeName ? this.__typeName : "Happy.GRU.Web") })()
};
Happy.GRU.Web.Scripts = Happy.GRU.Web.Scripts ? Happy.GRU.Web.Scripts : {
    __namespace: (function() { return (this.__namespace ? this.__namespace : true) })(),
    __typeName: (function() { return (this.__typeName ? this.__typeName : "Happy.GRU.Web.Scripts") })()
};
Happy.GRU.Web.Scripts.Common = Happy.GRU.Web.Scripts.Common ? Happy.GRU.Web.Scripts.Common : {
    __namespace: (function() { return (this.__namespace ? this.__namespace : true) })(),
    __typeName: (function() { return (this.__typeName ? this.__typeName : "Happy.GRU.Web.Scripts.Common") })()
};

//////////////////////////// Table select all ///////////////////
Happy.GRU.Web.Scripts.Common.allTableSelectAll = new Array();
Happy.GRU.Web.Scripts.Common.TableSelectAll = function(holderId, selectAllId) {
    this.holderId = holderId;
    this.selectAllId = selectAllId;
    //this.unselectAllId  = unselectAllId;
    this.holder = $get(this.holderId);
    this.selectAll = $get(this.selectAllId);
    //this.unselectAll    = $get(unselectAllId);
    this.chks = new Array();
    this.Initialize();
};
Happy.GRU.Web.Scripts.Common.TableSelectAll.prototype.Initialize = function() {
    var inputs = this.holder.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute && inputs[i].getAttribute('type').toLowerCase() == 'checkbox') {
            this.chks.push(inputs[i]);
        }
    }
    if (this.selectAll) {
        $addHandler(this.selectAll, 'click', this.SelectClick.bind(this));
    }
};
Happy.GRU.Web.Scripts.Common.TableSelectAll.prototype.SelectClick = function(event) {
    var target = event.target;
    if (target.getAttribute('allSelected')) {
        if (target.getAttribute('allSelected') == 'false') {
            this.SetAllMessages(true);
            target.setAttribute('allSelected', 'true');
            try { target.innerHTML = 'Unselect all'; } catch (e) { };
        } else if (target.getAttribute('allSelected') == 'true') {
            this.SetAllMessages(false);
            target.setAttribute('allSelected', 'false');
            try { target.innerHTML = 'Select all'; } catch (e) { };

        };
    } else {
        target.setAttribute('allSelected', 'true');
        this.SetAllMessages(true);
        try { target.innerHTML = 'Unselect all'; } catch (e) { };
    };
};
Happy.GRU.Web.Scripts.Common.TableSelectAll.prototype.SetAllMessages = function(checked) {
    for (var i = 0; i < this.chks.length; i++) {
        this.chks[i].checked = checked;
    };
};
Happy.GRU.Web.Scripts.Common.AddTableSelectAll = function(holderId, selectAllId) {
    Happy.GRU.Web.Scripts.Common.allTableSelectAll.push(new Happy.GRU.Web.Scripts.Common.TableSelectAll(holderId, selectAllId));
};

/////////////////////////// Updating update progress ////////////////////
Happy.GRU.Web.Scripts.Common.allModalUpdatingAreas = new Array();
Happy.GRU.Web.Scripts.Common.ModalUpdatingArea = function(updPrgrId, updatedAreaId) {
    this.active = false;
    this.updPrgrId = updPrgrId;
    this.updatedAreaId = updatedAreaId;
    this.AddHandlers();
};
Happy.GRU.Web.Scripts.Common.ModalUpdatingArea.prototype.AddHandlers = function() {
    Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(this.StartUpdate.bind(this));
    Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(this.StopUpdate.bind(this));
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(this.StartUpdate.bind(this));
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this.StopUpdate.bind(this));
};
Happy.GRU.Web.Scripts.Common.ModalUpdatingArea.prototype.StartUpdate = function() {
    this.active = true;
    this.UpdateProgress();
};
Happy.GRU.Web.Scripts.Common.ModalUpdatingArea.prototype.StopUpdate = function() {
    this.active = false;
};
Happy.GRU.Web.Scripts.Common.ModalUpdatingArea.prototype.UpdateProgress = function() {
    if ($get(this.updPrgrId)) {
        var updPrgr = $get(this.updPrgrId);
        if ($get(this.updatedAreaId)) {
            //$get('scrollingPaginator').style.position = '';
            //alert('$get(\'scrollingPaginator\').style.position: ' + $get('scrollingPaginator').style.position)            
            var list = $get(this.updatedAreaId);
            var b = Sys.UI.DomElement.getBounds(list);
            //var j = Sys.UI.DomElement.getLocation(list);
            //alert('j.x: ' + j.x + '\nj.y: ' + j.y + '\nb.height: ' + b.height + '\nb.width: ' + b.width)
            updPrgr.style.position = 'absolute';
            //updPrgr.style.zIndex = '10'
            //updPrgr.style.top = b.y + 'px';
            //updPrgr.style.left = b.x + 'px';
            //updPrgr.style.top = j.y + 'px';
            //updPrgr.style.left = j.x + 'px'
            updPrgr.style.height = b.height + 'px';
            updPrgr.style.width = b.width + 'px';
            //updPrgr.style.height = list.offsetHeight + 'px';
            //updPrgr.style.width = list.offsetWidth + 'px';
            updPrgr.style.background = '#EDF1F1';
            updPrgr.style.filter = 'alpha(opacity=85)';
            updPrgr.style.opacity = '0.85';
            updPrgr.style.zIndex = '11';

            if (this.active) setTimeout(this.UpdateProgress.bind(this), 50);
        };
    };
};
Happy.GRU.Web.Scripts.Common.AddModalUpdatingArea = function(updPrgrId, updatedAreaId) {
    this.allModalUpdatingAreas.push(new Happy.GRU.Web.Scripts.Common.ModalUpdatingArea(updPrgrId, updatedAreaId));
};
Happy.GRU.Web.Scripts.Common.setDocumentCookie = function(oDoc, sName, sValue, bSessionCookie) {
    if (bSessionCookie) {
    }
    if (sName.length < 1)
        return;
    if (0 < sValue.length) {
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + 365 * 24 * 60 * 60 * 1000);
        if (!bSessionCookie) {
            oDoc.cookie = "" + sName + "=" + sValue + "; ";
        } else {
            oDoc.cookie = "" + sName + "=" + sValue + "; " + "expires=" + expDate.toGMTString();
        }
    } else {
        oDoc.cookie = sName + "=";
    }
};


Happy.GRU.Web.Scripts.Common.setCookie = function(name,value,expires,path,domain,secure){
			var today = new Date();
		
			if(expires){
				expires = expires*1000*60*60*24;
			}
		
			var expires_date = new Date(today.getTime() + (expires));
		
			name = name;
			value = value;
			expires = ((expires) ? ";expires=" + expires_date.toGMTString() : "");
			path = ((path) ? ";path=" + path : "");
			domain = ((domain) ? ";domain=" + domain : "");
			secure = ((secure) ? ";secure" : "");
			today.setTime(today.getTime());
		
			document.cookie = name + "=" + value + expires + path + domain + secure;

    
};
Happy.GRU.Web.Scripts.Common.setNamedCookie = function(sName, sValue) {
    setDocumentCookie(document, sName, sValue);
};
Happy.GRU.Web.Scripts.Common.setBrowserSessionNamedCookie = function(sName, sValue) {
    this.setDocumentCookie(document, sName, sValue, true);
};
Happy.GRU.Web.Scripts.Common.deleteDocumentCookie = function(oDoc, sName) {
    oDoc.cookie = sName + "=";
};
Happy.GRU.Web.Scripts.Common.deleteCookie = function(sName) {
    deleteDocumentCookie(document, sName);
};
Happy.GRU.Web.Scripts.Common.fetchDocumentCookie = function(oDoc, sName) {
    var sValue = "";
    var index = 0;
    if (oDoc.cookie)
        index = oDoc.cookie.indexOf(sName + "=");
    else
        index = -1;
                
    if (index < 0) {
        sValue = "";
    } else {
        var countbegin = (oDoc.cookie.indexOf("=", index) + 1);
        if (0 < countbegin) {
            var countend = oDoc.cookie.indexOf(";", countbegin);
            if (countend < 0) countend = oDoc.cookie.length;
            sValue = oDoc.cookie.substring(countbegin, countend);
        } else {
            sValue = "";
        }
    }
    return sValue;
};
Happy.GRU.Web.Scripts.Common.fetchNamedCookie = function(sName) {
    return this.fetchDocumentCookie(document, sName);
};
Happy.GRU.Web.Scripts.Common.findNodeNamed = function(node, id) {
    //recursively loops through all the childnodes of this node to find one with id
    var next = node.firstChild;
    while (next) {
        if (next.id == id) {
            return next;
            break;
        } else if (next.firstChild) {
            ret = this.findNodeNamed(next, id);
            if (ret) {
                return ret;
                break;
            }
        }
        next = next.nextSibling;
    }
};

// Fixing the top of the homepage for correct modal popup display

function AdjustDropDown() {
    document.getElementById("head").style.zIndex = "0";

    var iframeElements = document.getElementsByTagName("iframe");
    for (i = 0; i < iframeElements.length; i++) {
        iframeElements[i].style.visibility = "hidden";
    }

}
function UnAdjustDropDown() {
    document.getElementById("head").style.zIndex = "10"

    var iframeElements = document.getElementsByTagName("iframe");
    for (i = 0; i < iframeElements.length; i++) {
        iframeElements[i].style.visibility = "visible";
    }

}


// Fix for GR Player

window.onload = function SetGRPlayer() {

    if (document.getElementById('GRPlayerContainer') != null) {
        document.getElementById("GRPlayerContainer").style.visibility = "visible";
    } else {
        return false;
    }
};
