/*
* Hover functionality for Top Menu
*/
navHover = function() {
    var menu = document.getElementById("topmenu");
    if (menu) {
        var lis = menu.getElementsByTagName("LI");
        for (var i = 0; i < lis.length; i++) {
            lis[i].onmouseover = function() {
                this.className += " iehover";
            }
            lis[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" iehover\\b"), "");
            }
        }
    }
}
if (getInternetExplorerVersion() <= 8 && window.attachEvent) window.attachEvent("onload", navHover);
window.onresize = repositionOverlay;

/*
* Overlay functions
*/
function closeAdminFlash() {
    closeOverlay('overlay-content-admin', true);
}

function closeOnsiteFlash() {
    closeOverlay('overlay-content-onsite', true);
}

function showOverlayForm(overlayContentId, divWidth, divHeight) {      
    var overlay = document.getElementById('overlay');
    overlay.className = 'overlay-on';

    var popup = document.getElementById(overlayContentId);
    popup.className = 'overlay-content-on';
    var pos = overlayPosition(divWidth, divHeight);
    popup.style.left = pos.left + 'px';
    popup.style.top = pos.top + 'px';
}

function closeOverlay(overlayContentId, refresh) {
    var overlay = document.getElementById('overlay');
    var popup = document.getElementById(overlayContentId);

    if (overlay) overlay.className = 'overlay-off';
    if (popup) popup.className = 'overlay-content-off';

    // Refresh page after close
    if(refresh) window.location.reload(true);
}

function overlayVisible() {
    var overlay = document.getElementById('overlay');
    if (overlay) {
        if (overlay.className == 'overlay-on') return true;
    }
    return false;
}

function repositionOverlay() {
    var currentOverlayClassName = 'overlay-content-on';
    var currentOverlayFlashClassName = 'overlay-flash';
    var popup;
    var flash;

    var popups = getElementsByClassName(currentOverlayClassName, null);
    if (popups.length == 1) popup = popups[0];
    var popupsDiv = getElementsByClassName(currentOverlayFlashClassName, popup);
    if (popupsDiv.length == 1) flash = popupsDiv[0];
    if (flash) {
        var pos = overlayPosition(flash.style.width, flash.style.height);
        popup.style.left = pos.left + 'px';
        popup.style.top = pos.top + 'px';    
    }
}




/*
* Bulletin Overlay Album functions
*/
var bulletinThmbs = new Array();
var selectedThmb = 1;   
function ShowBulletinImages(index, imgArray) {
    if (index < 1 || index > 6) index = 1;
    selectedThmb = index;
    bulletinThmbs = imgArray;
    showOverlayForm('overlay-content-bulletin-images', 750, 0);
    SelectThumbnail();
}

function SelectThumbnail() {
    var divImage = document.getElementById('bulletinimage');
    if (divImage) {
        divImage.src = "arch/_img/" + bulletinThmbs[selectedThmb - 1];
        divImage.width = 720;
    }
}

function NextThumbnail(increment) {
        var i = (selectedThmb + increment); 
        if (i < 1) i = bulletinThmbs.length;
        if (i > bulletinThmbs.length) i = 1;
        selectedThmb = i;
        SelectThumbnail();
}

/*function getImgSize(imgSrc) {
    var newImg = new Image();
    newImg.src = imgSrc;
    var height = newImg.height;
    var width = newImg.width;
    //alert('The image size is ' + width + '*' + height);
}*/

function ShowRemoveIcon(elem, show) {
    if (elem) {
        if (show) {
            elem.src = 'gfx/thmb-remove.gif';
        } else {
            elem.src = 'gfx/trans.gif';
        }
    }
}

/*
* List Visibility
*/
function toggleVisibility(elem,id) {
    var item = document.getElementById(id);
    if (!item) return;
    var months = getElementsByClassName('months', item);
    if (!months) return;
    var month = months[0];
 
    if (month.style.display == 'none') {
        month.style.display = 'block';
        elem.className = 'arrow-down';
    } else {
        month.style.display = 'none';
        elem.className = 'arrow';
    }
}

/*
* External Window
*/
function openWindow(url) {
    window.open(url);
} 


/*
* Common Functions
*/
function windowSize() {
    var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
    var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
    var x = window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft);
    var y = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);

    return { 'width': width, 'height': height, 'x': x, 'y': y }
};

function overlayPosition(divWidth, divHeight) {
    divWidth = parseInt(divWidth);
    divHeight = parseInt(divHeight);
    if (divWidth == null || divWidth == '') divWidth = 700;
    if (divHeight == null || divHeight == '') divHeight = 700;
    
    var left = (windowSize().width / 2) - (divWidth / 2);
    var top = 30;
    if (divHeight > 0) top = ((windowSize().height / 2) + windowSize().y) - (divHeight / 2);
    if (top < 10) top = 10;
    //if (windowSize().height < 850) top = 5;
    //if (windowSize().height > 1200) top = 100;
    return { 'top': top, 'left': left }
};

function checkNumeric(value) {
    var anum = /(^\d+$)|(^\d+\.\d+$)/
    if (anum.test(value)) return true;
    return false;
}

function fixchars(val) {
    return val.replace(/['_,%|`"~#]/g, "");
}

function ShareOnFacebook() {
    var link = 'http://www.facebook.com/share.php?u=';
    link += encodeURI(location.href);
    link += '&t=' + encodeURI(document.title);
    return link;
}

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function getElementsByClassName(classname, node) {
    if (!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for (var i = 0, j = els.length; i < j; i++)
        if (re.test(els[i].className)) a.push(els[i]);
    return a;
}

function unCryptEmail(s1, s2) {
    location.href = 'mailto:' + s2 + '@' + s1;
}
