﻿/* ------------------------------------------------------------   
   - Property that specifies if a page has been changed. Used
   - in the ContentToolbar control.
   ------------------------------------------------------------ */
var Global_IsDirty = false;
var Global_IsDirtyOverride = false;
var Global_MoveObjectRef = null;
var Global_DisableOnKeyDown = false;
var Global_XModifier = 0;
var Global_YModifier = 0;
var Global_MenuCache = new Array();
var Global_CurrentMenu = null;
var Global_CurrentPopup = null;
var Global_WidgetSource = '';
var Global_CurrentPopupBackground = null;
var Global_CurrentHelpWindow = null;

/* ------------------------------------------------------------   
   - Adds trim-support to the String object. (ex: strTest.trim();)
   ------------------------------------------------------------ */
String.prototype.trim = function() { 
    return this.replace(/(^\s*)|(\s*$)/g, ''); 
}

/* ------------------------------------------------------------   
   - Bubbles an event.
   ------------------------------------------------------------ */
function bubbleEvent(e) {
    var e = e || window.event; 
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

/* ------------------------------------------------------------   
   - Hides all dropdown, context and search menus.
   ------------------------------------------------------------ */
function Global_CloseAllMenus() {
    // Hides all drop down menus.
    if (typeof(arrDropDownMenus) != 'undefined') {
        for (var intIndex = 0; intIndex != arrDropDownMenus.length; intIndex ++) {
            if (document.getElementById(arrDropDownMenus[intIndex]) != null) {
                document.getElementById(arrDropDownMenus[intIndex]).style.display = 'none';
            }
        }
    }        
    //TopNavigation_TabUnselect();
    
    // Hides the favorites window.
    var objFavDiv = document.getElementById('divAddFavorites');
    if (objFavDiv != null) {
        objFavDiv.style.display = 'none';
    }
   
}


function Global_ShowDialog(strID, e) {

    var objDialogDiv = document.getElementById(strID);
    var e = e || window.event; 

    if (objDialogDiv != null) {

        objDialogDiv.style.top = e.clientY;
        objDialogDiv.style.left = e.clientX;
        objDialogDiv.style.display = '';        
    
    }

}

function Global_ShowPopup(strID, objThis, strBackgroundID, intTop) {

    var intScreenWidth = 0;

    Global_ClosePopup();

    Global_CurrentPopup = document.getElementById(strID);
    
    if (strBackgroundID != null) {
        Global_CurrentPopupBackground = document.getElementById(strBackgroundID);
        if (Global_CurrentPopupBackground != null) {
            Global_CurrentPopupBackground.style.visibility = 'visible';
        }
    }

    if (Global_CurrentPopup != null) {

        if (objThis != null) {

            if (document.all) { // IE
                intScreenWidth = document.documentElement.clientWidth;
            } 
            else { // Firefox
                intScreenWidth = window.innerWidth;
            }

            var objPos = Global_FindPos(objThis);
            with (Global_CurrentPopup.style) {
                top = String(objPos[1] + objThis.parentNode.offsetHeight) + 'px';
                if ((objPos[0] + Global_CurrentPopup.offsetWidth) > intScreenWidth) {
                    left = String(intScreenWidth - Global_CurrentPopup.offsetWidth - 10) + 'px';
                }
                else {
                    left = String(objPos[0]) + 'px';
                }
                visibility = 'visible';        
            }

        }
        else {

          if (Global_CurrentPopup != null) {
                var intScrollTop;
                Global_CurrentPopup.style.left = String((document.body.offsetWidth / 2) - (Global_CurrentPopup.offsetWidth / 2)) + 'px';
                if (intTop == null) {
                    if (document.all) { // IE
                        intScrollTop = document.documentElement.scrollTop;
                        intScrollBottom = document.documentElement.clientHeight + intScrollTop;
                        Global_CurrentPopup.style.top = ((document.documentElement.clientHeight / 2) - (Global_CurrentPopup.offsetHeight / 2)) + document.documentElement.scrollTop;
                    }
                    else { // Firefox
                        intScrollTop = window.pageYOffset;
                        intScrollBottom = window.innerHeight + intScrollTop;
                        Global_CurrentPopup.style.top = String(((window.innerHeight / 2) - (Global_CurrentPopup.offsetHeight / 2)) + window.pageYOffset) + 'px';
                      }
                      $('#' + strBackgroundID).height(intScrollBottom);
                  }
                else {
                    Global_CurrentPopup.style.top = String(intTop) + 'px';
                }
                Global_CurrentPopup.style.visibility = 'visible';
              }
        }
    
    }
    
}

function Global_ClosePopup() {

    if (Global_CurrentPopup != null) {
        if (Global_CurrentPopup.style.visibility == 'visible') {
            Global_CurrentPopup.style.visibility = 'hidden';
        }
        else {
            Global_CurrentPopup.style.display = 'none';
        }
        Global_CurrentPopup = null;
    }
    
    if (Global_CurrentPopupBackground != null) {
        Global_CurrentPopupBackground.style.visibility = 'hidden';
    }

}

function Global_ShowHelp(objThis) {

    var objPos = Global_FindPos(objThis);

    if (Global_CurrentHelpWindow == null) {
        Global_CurrentHelpWindow = document.createElement('DIV');
        with (Global_CurrentHelpWindow) {
            className = 'helpcontainer';
            style.visibility = 'hidden';
            zIndex = 1000;
        }
        document.body.appendChild(Global_CurrentHelpWindow);
    }

    Global_CurrentHelpWindow.innerHTML = objThis.getAttribute('helptext');

    with (Global_CurrentHelpWindow.style) {    
        top = String(objPos[1] - Global_CurrentHelpWindow.offsetHeight) + 'px';
        left = String(objPos[0]) + 'px';        
        visibility = 'visible';
    }

}

function Global_CloseHelp() {

    if (Global_CurrentHelpWindow != null) {
        Global_CurrentHelpWindow.style.visibility = 'hidden';
    }
    
}

/* ------------------------------------------------------------   
   - Starts a move of a specific object. Set srcElementID to
   - the ID of the element to be moved.
   - Specify this function in the onmousedown event where you want
   - the move to be executed from.
   ------------------------------------------------------------ */
function Global_MoveStart(e, strElementID, intYModifier) {

    var objCoord = null;

    if (Global_MoveObjectRef == null) {

        Global_MoveObjectRef = document.getElementById(strElementID);

    	objCoord = Global_FindPos(Global_MoveObjectRef);
    	
        Global_XModifier = e.clientX - objCoord[0];
        if (intYModifier != null) {
            Global_YModifier = Global_MoveObjectRef.parentNode.offsetTop + intYModifier;
        }
        else {
            Global_YModifier = Global_MoveObjectRef.parentNode.offsetTop;
        }
        
        if (intYModifier == null) {
            if (Global_YModifier != 0) {
                Global_YModifier += 155;
            }
            
            Global_YModifier = 50;
        }
        else {
            Global_YModifier = intYModifier;
        }
        
        
        document.body.onmousemove = Global_MoveObject;

    }
}

/* ------------------------------------------------------------   
   - Ends a move.
   - Specify this function in the onmouseup event where you want
   - the move to be executed from.
   ------------------------------------------------------------ */
function Global_MoveEnd(e) {
    Global_MoveObjectRef = null;
    document.body.onmousemove = null;
}

/* ------------------------------------------------------------   
   - Moves the object that is referenced in the Global_MobeObjectRef
   - object.
   ------------------------------------------------------------ */
function Global_MoveObject(e) {
    if (Global_MoveObjectRef != null) {
        var e = e || window.event; 
        with (Global_MoveObjectRef.style) {
            left = String(e.clientX - Global_XModifier) + 'px';
            if (document.all) { // IE
                top = String(e.clientY - 10 + document.documentElement.scrollTop - Global_YModifier) + 'px';
            }
            else { // Firefox            
                top = String(e.clientY - 10 + window.pageYOffset - Global_YModifier) + 'px';
            }
        }
    }
}

/* ------------------------------------------------------------   
   - Gets the offset position of a specific object.
   - var arrPos = Global_FindPos(document.getElementById('divTest'));
   - var intX = arrPos[0];
   - var intY = arrPos[1];
   ------------------------------------------------------------ */
function Global_FindPos(objElement) {

	var intCurrentLeft = 0
	var intCurrentTop = 0;

	if (objElement.offsetParent) {

		intCurrentLeft = objElement.offsetLeft
		intCurrentTop = objElement.offsetTop

		while (objElement = objElement.offsetParent) {
			intCurrentLeft += objElement.offsetLeft
			intCurrentTop += objElement.offsetTop
		}

	}

	return [intCurrentLeft,intCurrentTop];

}


/* ------------------------------------------------------------   
   - Global onload event that registers the onresize event.
   ------------------------------------------------------------ */
function Global_OnLoad(e) {

    var e = e || window.event; 

    if (window.attachEvent) { // IE
        window.attachEvent("onresize",function(){ Global_OnResize() });
    } 
    else { // Firefox
        window.addEventListener("resize", function() {Global_OnResize()}, false);
        Global_OnResize();
    }

}

/* ------------------------------------------------------------   
   - Resizes the DIV-tag divContentArea.
   ------------------------------------------------------------ */
function Global_OnResize() {

    var objContent = document.getElementById('divContentArea');
    var intHeightAdjuster = 0;

    if (objContent != null) {

        var objSubContentToolbar = document.getElementById('divSubContentToolbar'); // If a SubContentToolbar exists on the page.
        if (objSubContentToolbar != null) {
            intHeightAdjuster = 34;
        }
        
        var objFormContainer = document.getElementById('divFormContainer');
        if (objFormContainer != null) { // If a form container exists on the page.
            if (intHeightAdjuster == 0) {
                intHeightAdjuster = 14;
            }
        }
        else { // If a sub container exists on the page, but no form container.
            if (intHeightAdjuster != 0) {
                intHeightAdjuster = 20;
            }
        }

        if (document.all) { // IE
            var intHeight = document.documentElement.clientHeight - 80 - intHeightAdjuster;
        }
        else { // Firefox
            var intHeight = window.innerHeight - 80 - intHeightAdjuster;
        }

        objContent.style.height = String(intHeight) + 'px';
    
    }

}

/* ------------------------------------------------------------   
- Caught on the onbeforeunload event. 
- Gives the user a chance to stay on the current page.
------------------------------------------------------------ */
function Global_ConfirmLeave() {
    var hdnMessage = document.getElementById('hdnOnLeaveMessage');
    if (Global_IsDirty && !Global_IsDirtyOverride) {
        if (hdnMessage != null) {
            return document.getElementById('hdnOnLeaveMessage').value;
        } else {
            return '';
        }           
    } else {
        Global_IsDirtyOverride = false;
    }
}

/* ------------------------------------------------------------   
- Opens the add feed package window.
------------------------------------------------------------ */
function Global_OpenWidgetWindow(slug, sender, e, type) {
    var e = e || window.event;
    var intTop = 0;
    var intPopupHeight = $('#addfeed').outerHeight();
    var intPopupWidth = $('#addfeed').outerWidth();
    
    if (document.all) { // IE
        intTop = e.clientY - 10 + document.documentElement.scrollTop;
        if ((intTop + intPopupHeight) > (document.documentElement.clientHeight + document.documentElement.scrollTop)) {
            intTop = document.documentElement.clientHeight + document.documentElement.scrollTop - intPopupHeight - 10;
        }
    }
    else { // Firefox
        intTop = e.clientY - 10 + window.pageYOffset;
        if ((intTop + intPopupHeight) > (window.innerHeight + window.pageYOffset)) {
            intTop = window.innerHeight + document.documentElement.scrollTop - intPopupHeight - 10;
        }
    }

    if (type == 'addblog') {
        $('#addfeed').css('top', intTop).css('left', e.clientX - intPopupWidth).css('display', 'block').css('z-index', '1000');
    } else {
        $('#addfeed').css('top', intTop).css('left', e.clientX).css('display', 'block');
    }       
    $('#hdnWidgetID').attr('value', slug);
    $('#hdnWidgetType').attr('value', type);

    return false;
}
