﻿
function LifeStyleBar(bar1id, bar2id, rotationmillis, fadermillis, categoryid, bannertypeid, lastBannerID) {

    var myBase = this;
    var divs = [];
    var div1 = document.getElementById(bar1id);
    var div2 = document.getElementById(bar2id);

    divs.push(div1);
    divs.push(div2);

    var selectedDiv = 0;

    var getBannerXml;
    var frontAlpha = 100;

    var rotateTimeoutID=0





    this.SetOpacity = function(div, opacity) {
        div.style.filter = "alpha(Opacity=" + opacity + ")";
        if (opacity == 100) {
            div.style.opacity = "1";
        } else if (opacity > 9) {
            div.style.opacity = "0." + opacity;
        } else {
            div.style.opacity = "0.0" + opacity;
        }

    }

    this.ProcessSelectedDiv = function() {
        divs[selectedDiv].style.zIndex = 100;
        myBase.SetOpacity(divs[selectedDiv], 100);
    }

    this.ProcessNoneSelectedDiv = function() {
        divs[1 - selectedDiv].style.zIndex = 99;
        myBase.SetOpacity(divs[1 - selectedDiv], 100);
    }

    

    this.Rotate = function() {
        frontAlpha -= 4;


        myBase.SetOpacity(divs[selectedDiv], frontAlpha < 0 ? 0 : frontAlpha);

        //alert(frontAlpha + ", " + divs[selectedDiv].style.opacity + ", " + divs[selectedDiv].style.filter);

        //alert(rotateTimeoutID);
        if (frontAlpha <= 0) {
            selectedDiv = 1 - selectedDiv;
            //divs[1 - selectedDiv].style.display = "none";
            myBase.ProcessSelectedDiv();
            myBase.ProcessNoneSelectedDiv();

            this.loadNonSelectedBanner();
        } else {
            rotateTimeoutID = setTimeout(function() { myBase.Rotate(); }, fadermillis / 25); //5% intervals?
        }


    }


    
    
    var bannerXmlLoaded = false;
    this.BeginRotation = function() {

        if (!bannerXmlLoaded) {//if xml isn't ready then wait a half second before trying again
            rotateTimeoutID = setTimeout(function() { myBase.BeginRotation(); }, 500);
            return;
        }

        frontAlpha = 100;
        myBase.SetOpacity(divs[selectedDiv], frontAlpha);
        //alert(fadermillis);
        rotateTimeoutID = setTimeout(function() { myBase.Rotate(); }, fadermillis / 25); //5% intervals?
        //alert(rotateTimeoutID);
    }


    this.loadNonSelectedBanner = function() {
        myBase.initXml();
        var url = "/XmlData/banner.aspx?categoryid=" + categoryid + "&bannertypeid=" + bannertypeid + "&notbannerid=" + lastBannerID;
        //alert(url);
        url += randCode();
        //alert(url);
        getBannerXml.open("GET", url, true);
        bannerXmlLoaded = false;
        getBannerXml.onreadystatechange = function() { myBase.xmlLoaded(); };
        getBannerXml.send(null);
        setTimeout(function() { myBase.BeginRotation(); }, rotationmillis);
    }

    this.xmlLoaded = function() {
        if (getBannerXml.readyState != 4)
            return;

        var res = getBannerXml.responseXML;

        var content = '';

        if (res) {
            if (res.childNodes.length > 0) {
                for (var num = 0; num < res.childNodes.length; num++) {
                    if (res.childNodes[num].tagName == 'banner') {
                        lastBannerID = res.childNodes[num].getAttribute("bannerid");
                        //document.title = lastBannerID;
                        if (res.childNodes[num].hasChildNodes) {
                            var items = res.childNodes[num];

                            if (items.childNodes.length > 0) {
                                for (var c = 0; c < items.childNodes.length; c++) {
                                    if (items.childNodes[c].nodeValue.length > 0 && items.childNodes[c].nodeValue.charCodeAt(0) != 10) {
                                        content = items.childNodes[c].nodeValue;
                                        break;
                                    }
                                }
                            }

                        }
                        num = res.childNodes.length; //only expecting to use one widget here, but future proof at least 
                    }

                }
            }
        }

        divs[1 - selectedDiv].innerHTML = content;
        bannerXmlLoaded = true;


    }


    this.initXml = function() {
        if (!getBannerXml) {
            if (window.XMLHttpRequest) {
                getBannerXml = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                getBannerXml = new ActiveXObject("Microsoft.XMLHTTP");
            }
        } else {
            getBannerXml.abort();
        }
    }


    this.ProcessSelectedDiv();
    this.ProcessNoneSelectedDiv();
    this.loadNonSelectedBanner();
}
