var vidcount = 1;
var tabcount = 1;
function getid(id) {
    return document.getElementById(id);
}
function email() {
    if (getid('sendemil')) {
        getid('sendemil').setAttribute('href', 'mailto:enter email address?subject= Brunswick Group website&body=The sender of this email thought that you may find the following page(s) from Brunswick: %0a%0a' + document.title + ': ' + window.location);
    }
}
function attachevents() {
    var chksldiv = document.getElementsByTagName("div");
    for (var i = 0; i < chksldiv.length; i++) {
        if (chksldiv[i].className.indexOf('vedioOuterContainer') != -1) {
            chksldiv[i].setAttribute('id', 'vcontainer' + vidcount);
            chksldiv[i].style.display = 'none';
            if (vidcount == 1) {
                getid('vcontainer' + vidcount).style.display = "block";
            }
            vidcount++;
        }
        if (chksldiv[i].className.indexOf('tabContaienr') != -1) {
            chksldiv[i].setAttribute('id', 'vcontainer' + vidcount);
            chksldiv[i].style.display = 'none';
            if (vidcount == 1) {
                getid('vcontainer' + vidcount).style.display = "block";
            }
            vidcount++;
        }
    }
    if (getid('tabContainer')) {
        var chksli = getid('tabContainer').getElementsByTagName("li");
        for (var i = 0; i < chksli.length; i++) {
            chksli[i].childNodes[0].setAttribute('href', 'javascript:void(0);');
            chksli[i].setAttribute('id', 'vtab' + tabcount);
            chksli[i].onclick = changetab;
            if (tabcount == 1) {
                getid('vtab' + tabcount).className = "selected";
            }
            tabcount++;
        }
    }
    if (getid('tabInsights')) {
        getid('tablnks').style.display = "block";
        var chksli = getid('tabInsights').getElementsByTagName("li");
        for (var i = 0; i < chksli.length; i++) {
            chksli[i].childNodes[0].setAttribute('href', 'javascript:void(0);');
            chksli[i].setAttribute('id', 'vtab' + tabcount);
            chksli[i].onclick = changetab1;
            if (tabcount == 1) {
                getid('vtab' + tabcount).className = getid('vtab' + tabcount).className + "selected";
            }
            tabcount++;
        }
    }
}
function changetab() {
    var tabno = this.id.replace('vtab', '');
    for (var i = 1; i < tabcount; i++) {
        getid('vtab' + i).className = "";
        getid('vcontainer' + i).style.display = "none";
    }
    getid('vtab' + tabno).className = "selected";
    getid('vcontainer' + tabno).style.display = "block";
}
function changetab1() {
    var tabno = this.id.replace('vtab', '');
    for (var i = 1; i < tabcount; i++) {
        if (getid('vtab' + i).childNodes[0].className.indexOf('Selected')) {
            getid('vtab' + i).childNodes[0].className = getid('vtab' + i).childNodes[0].className.replace('Selected', '');
        }
        getid('vcontainer' + i).style.display = "none";
    }
    getid('vtab' + tabno).childNodes[0].className = getid('vtab' + tabno).childNodes[0].className + "Selected";
    getid('vcontainer' + tabno).style.display = "block";
}
/*	Home page flash resize	*/
function adjustHeight(height) {
    if (window.document["homeMap"]) {
        window.document["homeMap"].height = height;
        window.document["homeMap"].width = 480;
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds["homeMap"])
            document.embeds["homeMap"].height = height;
        window.document["homeMap"].width = 480;
    }
    else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    {
        document.getElementById("homeMap").height = height;
        window.document["homeMap"].width = 480;
    }
}
/*	Language switching	*/
var langN = 0;
var langM = 0;
var mw = 0;
var nw = 0;
function langSwap() {
    aLinks = getid('swapChk').getElementsByTagName('a')
    divs = getid('swapChk').getElementsByTagName('div')
    for (i = 0; i < aLinks.length; i++) {
        if (aLinks[i].getAttribute('rel') == 'selectLang') {

            aLinks[i].id = 'a' + nw;
            nw++;
            aLinks[i].onclick = showLangCnt;
            getid('a0').className = 'langSelected';
        }
    }
    for (j = 0; j < divs.length; j++) {
        if (divs[j].className == 'swapLang') {
            divs[j].style.display = 'none';
            //			mw=langM++;
            divs[j].id = 'div' + mw;
            mw++
            getid('div0').style.display = 'block';
        }
    }


}
strId = 0;
function showLangCnt() {
    langId = this.id.replace('a', 'div');
    langIdA = this.id;
    getid(langId).style.display = 'block';
    getid(langIdA).className = 'langSelected';

    if ((langId != 'div0') && (strId == 0) && (getid('div0').style.display == 'block')) {
        getid('div0').style.display = 'none'
        getid('a0').className = 'langSelection';
    }
    if (strId != langId) {
        if (strId) {
            getid(strId).style.display = 'none';
            getid(strIdA).className = 'langSelection';
        }
        strId = langId;
        strIdA = strId.replace('div', 'a');
    }
}
/*	page redirection on flash map click	*/
function zoneselection(str) {
    urlStart = ('"' + window.location + '"').indexOf('supported-charities');
    window.location = ('"' + window.location + '"').substring(1, urlStart) + 'supported-charities/' + str + '-supported-charities.aspx';
}

/*	Image map content show		*/
mapn = 0;
function showAudience() {
    mapArea = document.getElementsByTagName('area');
    mapCntDiv = document.getElementsByTagName('div');
    for (i = 0; i < mapArea.length; i++) {
        mapArea[i].onmouseover = new Function('showMapCnt(' + i + ')');
    }
    for (j = 0; j < mapCntDiv.length; j++) {
        if (mapCntDiv[j].className.indexOf('ourAudiencesCol') != -1) {
            mapCntDiv[j].style.display = 'none';
            mapCntDiv[j].id = 'mapDiv' + mapn;
            mapn++;
            //			document.getElementById('mapDiv0').style.display='block';
        }
    }
}
strMapId = 0;
function showMapCnt(mapId) {
    document.getElementById('mapDiv' + (mapId)).style.display = 'block';
    if (strMapId != mapId) {
        document.getElementById('mapDiv' + (strMapId)).style.display = 'none';
        strMapId = mapId;
    }
}

/*Collapseable div*/

var m = 0;
var p = 0;
var q = 0;
var divExpi = 0;

function showFaqCnt() {
    var div = getid('showFaqCnt').getElementsByTagName('div');
    var links = getid('showFaqCnt').getElementsByTagName('a');
    //var h3links = getid('showFaqCnt').getElementsByTagName('className')=='quoTitles';
    for (i = 0; i < links.length; i++) {
        if (links[i].className.indexOf('relA') != -1) {
            m++;
            num = m - 1;
            links[i].id = 'a' + num;
            links[i].href = 'javascript:openDiv(' + num + ')';
        }
    }
    for (j = 0; j < div.length; j++) {
        if (div[j].className.indexOf('relDiv') != -1) {
            p++;
            div[j].id = 'div' + (p - 1);
            getid('a' + (p - 1)).className = 'relA';
            getid('div' + (p - 1)).style.display = 'none';
            getid('div0').style.display = 'block';
        }
        getid('a' + (0)).className = 'relASld';

        if (div[j].className.indexOf('quoTitles') != -1) {
            q++;
            div[j].id = 'quoTitles' + (q - 1);
            div[j].style.cursor = 'pointer';
            div[j].onclick = new Function('openDiv(' + (q - 1) + ')');
            getid('quoTitles0').style.display = 'none';
        }
    }


}
function openDiv(div) {
    if (divExpi != div) {
        getid('div' + div).style.display = 'block';
        getid('div' + divExpi).style.display = 'none';

        getid('a' + div).className = 'relASld';
        getid('a' + divExpi).className = 'relA';
        if (('"' + window.location + '"').indexOf('quotes') != -1) {
            getid('quoTitles' + div).style.display = 'none';
            getid('quoTitles' + divExpi).style.display = 'block';
        }

        strtN = 0;
        endN = getid('div' + div).offsetHeight;
        expand(getid('div' + div), [strtN], [endN], 0, 30, 1, 1);
        divExpi = div;
    }
}
function expand(elem, startmar, endmar, finmar, steps, intervals, powr) {
    if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
    var actStep = 0;
    elem.bgFadeInt = window.setInterval(
	function() {
	    elem.style.height = easeInOut(startmar[0], endmar[0], steps, actStep, powr) + "px ";
	    actStep++;
	    if (actStep > steps) {
	        elem.style.left = finmar;
	        window.clearInterval(elem.bgFadeInt);
	    }
	}, intervals)
}

function easeInOut(minValue, maxValue, totalSteps, actualStep, powr) {
    var delta = maxValue - minValue;
    var stepp = minValue + (Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
    return Math.ceil(stepp)
}

/*Light Box */

var lm = 0;
var sm = 0;
var dm = 0;
function lightBox() {
    getid('lighboxCnt').style.display = 'none';
    var linkA = getid('lightBox').getElementsByTagName('a');
    var dsc = getid('lightBox').getElementsByTagName('div');
    for (i = 0; i < linkA.length; i++) {
        if (linkA[i].rel == 'lightbox') {
            lm++;
            linkA[i].setAttribute('id', 'link' + lm);
            getid('link' + lm).setAttribute('href', 'javascript:void(0)');
            getid('link' + lm).onclick = showLightBox;
        }
        if (linkA[i].rel == 'show') {
            sm++;
            linkA[i].setAttribute('id', 'slink' + sm);
            linkA[i].setAttribute('onclick', 'return showLightBoxImg(' + sm + ')')
        }

        if (dsc[i] != undefined && dsc[i].className == 'lighboxDsc') {
            dm++;
            dsc[i].setAttribute('id', 'dsc' + dm);
            getid('dsc' + dm).style.display = 'none';
        }
    }
}

function pagecenter() {
    if (typeof window.innerWidth != 'undefined') {
        viewportWidth = window.innerWidth;
        viewportHeight = window.innerHeight;
    } else if (document.documentElement != 'undefined') {
        viewportWidth = document.documentElement.clientWidth;
        viewportHeight = document.documentElement.clientHeight;
    } else {
        viewportWidth = document.body.clientWidth;
        viewportHeight = document.body.clientHeight;
    }
    bodyHeight = document.documentElement.scrollHeight;
    if (bodyHeight > viewportHeight) {
        bodyHeightN = bodyHeight;
        if (typeof window.innerWidth != 'undefined') {
            viewportWidth = viewportWidth - 17;
            scrollTop = document.documentElement.scrollTop;

            if (navigator.userAgent.indexOf('Safari') != -1) {
                scrollTop = document.body.scrollTop;
            }
        } else if ((document.documentElement != 'undefined')) {
            viewportWidth = viewportWidth;
            scrollTop = document.documentElement.scrollTop;
        } else {
            viewportWidth = viewportWidth;
            scrollTop = document.body.scrollTop;
        }
    } else {
        bodyHeightN = viewportHeight;
        scrollTop = 0;
    }
    getid('cover').style.display = 'block';
    getid('cover').style.width = viewportWidth + 'px';
    getid('cover').style.height = bodyHeight + 'px';
}

var imgIdU;
function showLightBox() {
    getid('lighboxCnt').style.display = 'block';

    cnt = getid('lighboxCnt').innerHTML;
    getid('lighboxCnt').innerHTML = '';

    closeSec = document.createElement('div');
    closeSec.setAttribute('id', 'closeSec');

    closeBtnLink = document.createElement('a');
    closeBtnLink.href = 'javascript:closepopup()';
    getid('cover').onclick = closepopup;

    closeBtn = document.createElement('img');
    closeBtn.src = '/images/btn_close.gif';
    closeBtnLink.appendChild(closeBtn);

    closeSec.appendChild(closeBtnLink);
    getid('lighboxCnt').appendChild(closeSec);

    next = document.createElement('a');
    next.setAttribute('id', 'nextBtn');
    next.setAttribute('href', 'javascript:nextPrev(1,"plus")');
    getid('lighboxCnt').appendChild(next);

    prev = document.createElement('a');
    prev.setAttribute('id', 'prevBtn');
    prev.setAttribute('href', 'javascript:nextPrev(-1,"minus")');
    getid('lighboxCnt').appendChild(prev);

    imagesSec = document.createElement('div');
    imagesSec.setAttribute('id', 'imagesSec');

    showBigImage = document.createElement('div');
    showBigImage.setAttribute('id', 'showImage');

    showImageDsc = document.createElement('div');
    showImageDsc.setAttribute('id', 'imageDsc');

    getid('lighboxCnt').appendChild(showBigImage);
    getid('lighboxCnt').appendChild(showImageDsc);
    getid('lighboxCnt').appendChild(imagesSec);
    getid('imagesSec').innerHTML = cnt;

    pagecenter();
    popupLeft = (viewportWidth - getid('lighboxCnt').offsetWidth) / 2;
    popupTop = (viewportHeight - getid('lighboxCnt').offsetHeight) / 2;
    getid('lighboxCnt').style.left = popupLeft + 'px';
    getid('lighboxCnt').style.top = (popupTop + scrollTop) + 'px';
    showLightBoxImg(1)
    document.documentElement.onkeyup = checkKeycode;
}



var fadecoverspeed = 3;
var strSts = 0;
function changeSize(sts) {
    if (sts == -1) {
        if (strSts != sts) {
            fadecover(document['showmap'], [10], [70], 100, 20, 10, fadecoverspeed, 0);
        }
    } else {
        if (strSts != sts) {
            fadecover(document['showmap'], [10], [70], 100, 20, 10, fadecoverspeed, 0);
        }
    }
    strSts = sts;
}
function fadecover(elem, startmar, endmar, finmar, steps, intervals, powr, stat) {

    if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
    var actStep = 0;
    elem.bgFadeInt = window.setInterval(
	function() {
	    elem.style.opacity = easeInOut(startmar[0], endmar[0], steps, actStep, powr) / 100;
	    elem.style.MozOpacity = easeInOut(startmar[0], endmar[0], steps, actStep, powr) / 100;
	    elem.style.KhtmlOpacity = easeInOut(startmar[0], endmar[0], steps, actStep, powr) / 100;
	    elem.style.filter = "alpha(opacity=" + easeInOut(startmar[0], endmar[0], steps, actStep, powr) + ")";
	    actStep++;
	    if (actStep > steps) {
	        elem.style.opacity = finmar / 100;
	        elem.style.MozOpacity = finmar / 100;
	        elem.style.KhtmlOpacity = finmar / 100;
	        elem.style.filter = "alpha(opacity=" + finmar + ")";
	        window.clearInterval(elem.bgFadeInt);
	    }
	}, intervals)
}
function easeInOut(minValue, maxValue, totalSteps, actualStep, powr) {
    var delta = maxValue - minValue;
    var stepp = minValue + (Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
    return Math.ceil(stepp)
}




function checkKeycode(e) {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode
    } else if (e) {
        keycode = e.which;
    }
    if (keycode == 27) {
        closepopup();
    }
}

function closepopup() {
    if (getid('lighboxCnt')) {
        getid('lighboxCnt').removeChild(getid('lighboxCnt').firstChild)
        getid('lighboxCnt').removeChild(getid('lighboxCnt').firstChild)
        getid('lighboxCnt').removeChild(getid('lighboxCnt').firstChild)
        getid('lighboxCnt').removeChild(getid('lighboxCnt').firstChild)
        getid('lighboxCnt').removeChild(getid('lighboxCnt').firstChild)
        getid('lighboxCnt').style.display = 'none';
        getid('cover').style.display = 'none';
    }
    if (getid('lightboxMap')) {
        getid('lightboxMap').style.display = 'none';
        getid('cover').style.display = 'none';
    }
}

function showLightBoxImg(imgId) {
    for (k = 1; k < dm + 1; k++) {
        getid('slink' + k).className = 'imgtr';
    }
    getid('slink' + imgId).className = 'imgtrN';
    getid('showImage').innerHTML = '<img src="' + getid('slink' + imgId).href + '" />';
    getid('imageDsc').innerHTML = getid('dsc' + imgId).innerHTML;
    imgIdU = imgId;
    return false;
}

function nextPrev(sts, drn) {
    if (imgIdU <= dm) {
        if (drn.indexOf('m') != -1 && imgIdU == 1) {
            imgIdU = 2;
        }
        if (imgIdU < 1) {
            imgIdU = imgIdU + 1;
        }
        if (imgIdU < dm) {
            imgIdU = imgIdU + sts;

        } else if (drn.indexOf('m') != -1 && imgIdU > 1) {
            imgIdU = imgIdU + sts;
        } else if (imgIdU == dm) {
            imgIdU = dm;
        }
    }
    showLightBoxImg(imgIdU)
}

var nml = 0;
var nmdl = 0;
function lightBoxMap() {
    mlinks = document.getElementsByTagName('a');
    mdivs = document.getElementsByTagName('div');

    for (ml = 0; ml < mlinks.length; ml++) {
        if (mlinks[ml].rel == 'lighboxMap') {
            nml++
            mlinks[ml].id = 'mapLink' + nml;
            roundedNml = Math.round(nml / 2)
            mlinks[ml].setAttribute('href', 'javascript:showLighboxMap(' + roundedNml + ')')
        }
    }
    for (md = 0; md < mdivs.length; md++) {
        if (mdivs[md].className == 'LightBoxOuterContainer') {
            nmdl++
            mdivs[md].id = 'mapDiv' + nmdl;
            mdivs[md].style.display = 'none';
        }
    }
}

function showLighboxMap(nmlId) {
    getid('lightboxMap').style.display = 'block';
    mapCnt = '<div class="LightBoxOuterContainer"><a href="javascript:closepopup()" class="imgClose"><img src="/images/img-close-lb.jpg" /></a>' + getid('mapDiv' + nmlId).innerHTML + '</div>';
    getid('lightboxMap').innerHTML = mapCnt;
    pagecenter();
    popupLeft = (viewportWidth - getid('lightboxMap').offsetWidth) / 2;
    popupTop = (viewportHeight - getid('lightboxMap').offsetHeight) / 2;
    getid('lightboxMap').style.left = popupLeft + 'px';
    getid('lightboxMap').style.top = (popupTop + scrollTop) + 'px';
    getid('cover').onclick = closepopup;
    document.documentElement.onkeyup = checkKeycode;
}
//href="javascript:printMap()"
function printMap() {
    var content = getid('lightboxMap').innerHTML;
    var pwin = window.open('', 'print_content', 'width=570,height=470');
    pwin.document.open();
    pwin.document.write('<html><head><link rel="stylesheet" type="text/css" href="/br-styles/br-styles-light-box.css" /></head><body onload="window.print()"><div id="wrapper1">' + content + '</div></body></html>');
    pwin.document.close();
    setTimeout(function() { pwin.close(); }, 1000);
}

//Checkbox
function chkBoxImg() {
    $(document).ready(function() {
        // check for what is/isn't already checked and match it on the fake ones
        var anchorEl = '<a href="#">&nbsp;</a>';
        $("#chkBox input:checkbox").each(function() {
            //		(this.checked) ? $("#"+this.id).addClass('checkbox-skinned') : $("#"+this.id).removeClass('checkbox-skinned');
            if (this.checked == true) {
                skin = 'checkbox-skinned'
            } else {
                skin = 'checkbox-skin'
            }
            $("input#" + this.id).after('<a class="' + skin + '" href="#' + this.id + '">&nbsp;</a>');
        });
        // function to 'check' the fake ones and their matching checkboxes
        $(".checkbox-skin, .checkbox-skinned").click(function() {
            if ($(this).hasClass('checkbox-skin')) {
                $(this).removeClass('checkbox-skin');
                $(this).addClass('checkbox-skinned');
            } else {
                $(this).removeClass('checkbox-skinned');
                $(this).addClass('checkbox-skin');
            }
            $(this.hash).trigger("click");
            return false;
        });
    });
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
}
//window.onload=attachevents

