﻿function getposOffset(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;
}

function toggleLoginBox(loginButtonID, loginBoxDivID, userNameTextBoxID, passwordTextBoxID)
{

    var objLoginBox = $('#' + loginBoxDivID + '');


    if (objLoginBox == null)
        return;

    if (objLoginBox.css('display') == 'block') {
        objLoginBox.css('display', 'none');
    }
    else {
        try {
            var objButton = $('#' + loginButtonID + '');
            if (objButton == null)
                return;

            var offsetTotal = $(objButton).offset();
            var offsetParentLeft = offsetTotal.left;
            var offsetParentTop = offsetTotal.top;
            
        var iLeft = offsetParentLeft - parseInt($(objLoginBox).css('width')) + parseInt($(objButton).css('width')) - 1;
        var iTop = offsetParentTop + parseInt($(objButton).css('width'));

        objLoginBox.css('left', iLeft.toString() + "px");
        objLoginBox.css('top', iTop.toString() + "px");

        objLoginBox.css('display', 'block');
        var objUserName = $('#' + userNameTextBoxID + '');
        var objPassWord = $('#' + passwordTextBoxID + '');

        if ($(objUserName).val() == '')
            objUserName.focus();
        else
            objPassWord.focus();
        }
        catch (exc) {
        }
    }
}        
    
function setFocus(element)
{
    if(element)
        element.focus();
}

// ******* Handle ads ********

function isInnerRightAdsLoaded() {
    var adNames = new Array(2);
    adNames[0] = "innerRight1";
    adNames[1] = "innerRight2";
    return isAdsLoaded(adNames);
}

function isRightWideAdsLoaded() {
    var adNames = new Array(5);
    adNames[0] = "wideScreen1";
    adNames[1] = "wideScreen2";
    adNames[2] = "wideScreen3";
    adNames[3] = "wideScreen4";
    adNames[4] = "wideScreen5";
    return isAdsLoaded(adNames);
}

function isRightAdsLoaded() {
    var adNames = new Array(1);
    adNames[0] = "right";
    return isAdsLoaded(adNames);
}

function isLeftAdsLoaded() {
    var adNames = new Array(1);
    adNames[0] = "leftFooter";
    return isAdsLoaded(adNames);
}


//*******************

function insertAdImageIfAdsLoaded(adsLoaded, divId, isClub, isOuterRightBanner) {
    if (adsLoaded)
        insertAdImageTopOfDiv(divId, isClub, isOuterRightBanner);
}

function changeWidthWhenRightAdsIsLoaded(adsLoaded, divId, newWidth) {
    var divObject = document.getElementById(divId);
    if (divObject != null && adsLoaded)
        divObject.style.width = newWidth;
}

function changeLeftMarginWhenRightAdsIsLoaded(adsLoaded, divId, newMargin) {
    var divObject = document.getElementById(divId);
    if (divObject != null && adsLoaded)
        divObject.style.marginLeft = newMargin;
}

function changeBottomMarginWhenAdIsLoaded(adName, divId, newMargin) {
    var divObject = document.getElementById(divId);
    if (divObject != null && isAdLoaded(adName))
        divObject.style.marginBottom = newMargin;
}

function insertAdImageTopOfDiv(divId, isClub, isOuterRightBanner) {
    var ad = document.getElementById(divId);

    var imageToInsert;
    if (isClub) {
        if (isOuterRightBanner)
            imageToInsert = createAdImage("/images/design/sponsors/ad_idrottonline_white.gif", "97", "7");
        else
            imageToInsert = createAdImage("/images/design/sponsors/ad_idrottenspartner_transparent.gif", "87", "7");
    }
    else {
        if (isOuterRightBanner)
            imageToInsert = createAdImage("/images/design/sponsors/ad_white.gif", "40", "7");
        else
            imageToInsert = createAdImage("/images/design/sponsors/ad_transparent.gif", "40", "7");
    }


    if (ad != null && ad.childNodes.length > 0)
        ad.insertBefore(imageToInsert, ad.childNodes[0]);
}

function createAdImage(url, width, height) {
    var newImage = new Image();

    //Create image
    newImage.src = url;
    newImage.className = "AdTomaIdrottOnlineTextImage";
    newImage.width = width;
    newImage.height = height;
    newImage.border = "0";
    newImage.style.margin = "3px 0";

    return newImage;
}

function isAdsLoaded(adNames) {
    var i;
    var adsLoaded = false;

    for (i = 0; i < adNames.length; i++) {
        if (isAdLoaded(adNames[i])) {
            adsLoaded = true;
            break;
        }
    }

    return adsLoaded;
}

function isAdLoaded(adName) {
    if (window.Fusion == null || window.Fusion.adComponents == null || window.Fusion.adComponents[adName] == null || window.Fusion.adComponents[adName] == "")
        return false;
    else
        return true;
}


// ******* END Handle ads ********

function saveScrollPosition(scrollPosInputId, scrollingDivId) {
    var scrollPosInput = document.getElementById(scrollPosInputId);
    var scrollingDiv = document.getElementById(scrollingDivId);
    if (scrollingDiv && scrollPosInput) {
        scrollPosInput.value = scrollingDiv.scrollTop;
    }
}
function resetScrollPosition(scrollPosInputId, scrollingDivId) {
    var scrollPosInput = document.getElementById(scrollPosInputId);
    var scrollingDiv = document.getElementById(scrollingDivId);
    if (scrollingDiv && scrollPosInput) {
        scrollingDiv.scrollTop = scrollPosInput.value;
    }
}