﻿    var AdRotators = {};
    
    AdRotators.slideShowSpeed = 8000;
    AdRotators.crossFadeDuration = 70;
    AdRotators.marrAdRotators = [];
    AdRotators.mbolIsRunning = false;

    AdRotators.Rotator = function(Id) {
        var targetIndex = 0;
        if (isNaN(parseInt(Id))) {
            if (this.marrAdRotators.length > 1) {
                for (var x = 0; x <= this.marrAdRotators.length - 1; x++) {
                    if (this.marrAdRotators[x].Id.toLowerCase() = Id.toLowerCase()) {
                        targetIndex = x;
                        break;
                    }
                }
            }
        }
        else { targetIndex = Id; }

        return this.marrAdRotators[targetIndex];
        //        return {
        //            PicNode: this.marrAdRotators[targetIndex].PicNode,
        //            Anchor: this.marrAdRotators[targetIndex].Anchor,
        //            Properties: this.marrAdRotators[targetIndex].Properties,
        //            Timer: this.marrAdRotators[targetIndex].Timer,
        //            FadeTimer: this.marrAdRotators[targetIndex].FadeTimer,
        //            IsRunning: this.marrAdRotators[targetIndex].IsRunning,
        //            Id: this.marrAdRotators[targetIndex].Id,
        //            Index: this.marrAdRotators[targetIndex].Index
        //        };
    }
    
    AdRotators.FadeInNewImage = function(intRotatorIndex, opac, fadeInOnly) {
        try {
           var temFadeImg = this.marrAdRotators[intRotatorIndex].Anchor.lastChild;
           var imgNode = this.marrAdRotators[intRotatorIndex].PicNode;

           if (document.all) {
               this.marrAdRotators[intRotatorIndex].PicNode.style.filter = "alpha(opacity=" + String(100 - opac) + ")";
               if (!fadeInOnly) { this.marrAdRotators[intRotatorIndex].Anchor.lastChild.style.filter = "alpha(opacity=" + String(opac) + ")"; }
           }
           else {
               var decOpac = ((100 - opac) / 100);
               this.marrAdRotators[intRotatorIndex].PicNode.style.opacity = (decOpac < 0 ? "0" : "") + String(decOpac);
               if (!fadeInOnly) { this.marrAdRotators[intRotatorIndex].Anchor.lastChild.style.opacity = "0" + String(opac / 100); }
           }
           if (opac > 0) {
               this.marrAdRotators[intRotatorIndex].FadeTimer = setTimeout("AdRotators.FadeInNewImage(" + String(intRotatorIndex) + ", " + String(opac - 5) + (fadeInOnly ? ", " + fadeInOnly : "") + ");", this.crossFadeDuration);
           } else if (!fadeInOnly) { this.marrAdRotators[intRotatorIndex].Anchor.removeChild(this.marrAdRotators[intRotatorIndex].Anchor.getElementsByTagName("IMG")[1]); }
        } catch (e) {
           try {
               clearTimeout(this.marrAdRotators[intRotatorIndex].FadeTimer);
           }
           catch (ex) { }
        }
    }  

    AdRotators.SwapImageAndFade = function(intRotatorIndex, picIndex) {
        var imgParent = this.marrAdRotators[intRotatorIndex].Anchor;
        var newImg = this.marrAdRotators[intRotatorIndex].PicNode.cloneNode(true);

        newImg.style.position = 'absolute';
        newImg.style.top = '0px';
        newImg.style.left = '0px';
        newImg.style.zIndex = 1000000;
        imgParent.appendChild(newImg);

        this.marrAdRotators[intRotatorIndex].PicNode.src = this.marrAdRotators[intRotatorIndex].Properties[picIndex][2].src;
        this.marrAdRotators[intRotatorIndex].Anchor.href = this.marrAdRotators[intRotatorIndex].Properties[picIndex][1];

        this.FadeInNewImage(intRotatorIndex, 95);
    }     

    AdRotators.ChangeAd = function(intRotatorIndex) {
       var intPicsLength = this.marrAdRotators[intRotatorIndex].Properties.length;
       var picIndex = this.marrAdRotators[intRotatorIndex].Index;

       picIndex++;
       if (picIndex > (intPicsLength - 1)) { picIndex = 0; }

       try {
           this.SwapImageAndFade(intRotatorIndex, picIndex);

           this.marrAdRotators[intRotatorIndex].Timer = setTimeout('AdRotators.ChangeAd(' + String(intRotatorIndex) + ')', this.slideShowSpeed);
           this.marrAdRotators[intRotatorIndex].Index = picIndex;
       } catch (e) { }
    }

    AdRotators.StopAd = function(intRotatorIndex) {
        clearTimeout(this.marrAdRotators[intRotatorIndex].Timer);
        clearTimeout(this.marrAdRotators[intRotatorIndex].FadeTimer);
        this.marrAdRotators[intRotatorIndex].IsRunning = false;
    }

    AdRotators.RestartAd = function(intRotatorIndex) {
        //this.marrAdRotators[intRotatorIndex].Timer = setTimeout('AdRotators.ChangeAd(' + String(intRotatorIndex) + ')', this.slideShowSpeed);
        this.marrAdRotators[intRotatorIndex].IsRunning = true;
        this.ChangeAd(intRotatorIndex);
    }
    AdRotators.AdMouseOver = function(intRotatorIndex) {
        //this.StopAd(intRotatorIndex);
    }

    AdRotators.AdMouseOut = function(intRotatorIndex) {
        //this.RestartAd(intRotatorIndex);        
    }

    AdRotators.OpenLinkPage = function(intRotatorIndex) {
        window.open(this.marrAdRotators[intRotatorIndex].Properties[this.marrAdRotators[intRotatorIndex].Index][1], '');
    }

    AdRotators.RegisterAdRotator = function(objPicNode, objAncherNode, Id, arrProps, linkTarget, bolStart) {

        objPicNode.title = '';
        objPicNode.src = arrProps[0][0];
        //objPicNode.style.borderWidth = '0px';
        objAncherNode.parentNode.style.position = "relative";
        objAncherNode.target = linkTarget;
//        objAncherNode.style.position = "relative";
//        objAncherNode.style.top = "0px";
//        objAncherNode.style.left = "0";

        objPicNode.style.position = "absolute";
        objPicNode.style.top = "0px";
        objPicNode.style.left = "0px";

        //objAncherNode.style.left = (objAncherNode.offsetLeft * -1);

        for (var x = 0; x <= arrProps.length - 1; x++) {
            arrProps[x].push(new Image());
            arrProps[x][arrProps[x].length - 1].src = arrProps[x][0];
        }

        this.marrAdRotators.push({
            PicNode: objPicNode,
            Anchor: objAncherNode,
            Properties: arrProps,
            Timer: null,
            FadeTimer: null,
            IsRunning: bolStart,
            Id: Id,
            Index: 0
        });

        this.FadeInNewImage(this.marrAdRotators.length - 1, 95, true);
        this.marrAdRotators[this.marrAdRotators.length - 1].Anchor.href = this.marrAdRotators[this.marrAdRotators.length - 1].Properties[0][1];

        //objPicNode.setAttribute("onclick", "OpenLinkPage(" + String(this.marrAdRotators.length - 1) + ");");
        objPicNode.setAttribute("onmouseover", "AdRotators.AdMouseOver(" + String(this.marrAdRotators.length - 1) + ");");
        objPicNode.setAttribute("onmouseout", "AdRotators.AdMouseOut(" + String(this.marrAdRotators.length - 1) + ");");

        if (bolStart && bolStart == true) {
            this.marrAdRotators[this.marrAdRotators.length - 1].Timer = setTimeout('AdRotators.ChangeAd(' + String(this.marrAdRotators.length - 1) + ')', this.slideShowSpeed);
            this.marrAdRotators[this.marrAdRotators.length - 1].IsRunning = true;
        }
    }



    function GetRelativeElementPosition(element) {
        var result = new Object();
        result.x = 0;
        result.y = 0;
        result.width = 0;
        result.height = 0;
        if (element.offsetParent) {
            result.x = element.offsetLeft;
            result.y = element.offsetTop;
            var parent = element.offsetParent;
            while (parent) {
                result.x += parent.offsetLeft;
                result.y += parent.offsetTop;
                var parentTagName = parent.tagName.toLowerCase();
                if (parentTagName != "table" &&
                parentTagName != "body" &&
                parentTagName != "html" &&
                parentTagName != "div" &&
                parent.clientTop &&
                parent.clientLeft) {
                    result.x += parent.clientLeft;
                    result.y += parent.clientTop;
                }
                parent = parent.offsetParent;
            }
        }
        else if (element.left && element.top) {
            result.x = element.left;
            result.y = element.top;
        }
        else {
            if (element.x) {
                result.x = element.x;
            }
            if (element.y) {
                result.y = element.y;
            }
        }
        if (element.offsetWidth && element.offsetHeight) {
            result.width = element.offsetWidth;
            result.height = element.offsetHeight;
        }
        else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
            result.width = element.style.pixelWidth;
            result.height = element.style.pixelHeight;
        }
        return result;
    }      
