﻿function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function addResizeEvent(func) {
    var oldonresize = window.onresize;
    if (typeof window.onresize != 'function') {
        window.onresize = func;
    }
    else {
        window.onresize = function () {
            if (oldonresize) {
                oldonresize();
            }
            func();
        }
    }
}

addLoadEvent(alignPageBlocks);
//addResizeEvent(scaleBackgroundImage);

// On the main index page, be sure to set the main and news block to the same height
function alignPageBlocks() {
    matchBlockHeights('main_block_content', 'news_block_content');
    matchBlockHeights('gallery_block_content', 'shop_block_content');
}

function matchBlockHeights(blockContentId1, blockContentId2) {
    var block1Content = document.getElementById(blockContentId1);
    if (!block1Content)
        return;

    var block2Content = document.getElementById(blockContentId2);
    if (!block2Content)
        return;

    var block1ContentPadding = getPixVal(getStyle(blockContentId1, 'padding-top'));
    var block2ContentPadding = getPixVal(getStyle(blockContentId2, 'padding-top'));

    if (block1Content.clientHeight > block2Content.clientHeight) {
        setStyle(blockContentId2, 'height', (block1Content.clientHeight - 2 * block1ContentPadding) + 'px');
    }
    else {
        setStyle(blockContentId1, 'height', (block2Content.clientHeight - 2 * block2ContentPadding) + 'px');
    }
}

// Set background image size
function scaleBackgroundImage() {
    // Get the window properties
    var clientHeight = getClientHeight();
    var clientWidth = getClientWidth();

    // Return if these aren't correct.
    if ((clientHeight <= 0) || (clientWidth <= 0))
        return;

    // Set the bkg image size
    var background = document.getElementById('background');

    if (!background)
        return;

    var clientRatio = clientHeight / clientWidth;
    var bkgImgRation = background.height / background.width;

    if (clientRatio > bkgImgRation) {
        setStyle('background', 'height', '100%');
        setStyle('background', 'width', 'auto');
    }
    else {
        setStyle('background', 'height', 'auto');
        setStyle('background', 'width', '100%');
    }
}

function getStyle(oElmId, strCssRule) {
    var oElm = document.getElementById(oElmId);
    if (!oElm) return null;
    if (document.defaultView && document.defaultView.getComputedStyle) {
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if (oElm.currentStyle) {
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1) {
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

// Get the height of the client browser
function getClientHeight() {
    var myHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

// Get the height of the client browser
function getClientWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

// Remove 'px' from the string
function getPixVal(pixStr) {
    if (!pixStr)
        return 0;

    if (typeof (pixStr) == 'number')
        return pixStr;
    else if (typeof (pixStr) != 'string')
        return 0;

    if ((pixStr != null) && (pixStr != 'undefined')) {
        var idx = pixStr.indexOf('p');

        if (idx > 0)
            pixStr = pixStr.substr(0, idx)

        var intVal = parseInt(pixStr);

        if (intVal)
            return intVal;
        else
            return 0;
    }

    return 0;
}

// Set style for an object
function setStyle(objId, cssProperty, value) {
    if (!objId)
        return;

    var obj = document.getElementById(objId);

    if (!obj)
        return;

    obj.style[cssProperty] = value;
}


// Converts a string to camel case
function toCamelCase(s) {
    var toCamelCaseExp = /-([a-z])/;
    for (var exp = toCamelCaseExp; exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()));
    return s;
}
