﻿
    function Banner(objName, BannerImageControlID, TextBannerControlID,LoopCount) {
        this.obj = objName;
        this.aNodes = [];
        this.currentBanner = 0;
        this.keepRotate = 0;
        this.BannerImageId = BannerImageControlID;
        this.textBannerId = TextBannerControlID;
        this.loopCount = LoopCount;
        this.CurrentLoop = 0;
        
    };

    Banner.prototype.add = function(bannerType, bannerPath, bannerDuration,bannerTransition, height, width, texttop, textleft, caption) {
        this.aNodes[this.aNodes.length] = new Node(this.obj + "_" + this.aNodes.length, bannerType, bannerPath, bannerDuration, bannerTransition, height, width, texttop, textleft, caption);
    };
    // Node object
    function Node(name, bannerType, bannerPath, bannerDuration,bannerTransition, height, width, texttop, textleft, caption) {
        this.name = name;
        this.bannerType = bannerType;
        this.bannerPath = bannerPath;
        this.bannerDuration = bannerDuration;
        this.bannerTransition = bannerTransition;
        this.height = height
        this.width = width;
        this.texttop = texttop;
        this.textleft = textleft;
        this.caption = caption;
        //	alert (name +"|" + bannerType +"|" + bannerPath +"|" + bannerDuration +"|" + height +"|" + width + "|" + hyperlink);
    };

    // START THE BANNER ROTATION
    Banner.prototype.start = function() {
        this.changeBanner();
        var thisBannerObj = this.obj;
        // CURRENT BANNER IS ALREADY INCREMENTED IN cahngeBanner() FUNCTION

        if (this.keepRotate == 0) {
            //alert('sleep ' + this.aNodes[this.currentBanner].bannerDuration);

            setTimeout(thisBannerObj + ".start()", this.aNodes[this.currentBanner].bannerDuration * 1);
        }
    }

    // CHANGE BANNER
    Banner.prototype.changeBanner = function() {
        var thisBanner;
        var prevBanner = this.currentBanner;

        if (prevBanner >= 0) {



            var bannerImg = document.getElementById(this.BannerImageId);
            var ImageDiv = bannerImg.parentNode;
            var cell = bannerImg.parentNode.parentNode;

            // bannerImg.src = "";
            bannerImg.src = this.aNodes[prevBanner].bannerPath;

            if (this.aNodes[prevBanner].width != "") {
                bannerImg.style.width = this.aNodes[prevBanner].width + 'px';
                ImageDiv.style.width = this.aNodes[prevBanner].width + 'px';
                cell.style.width = this.aNodes[prevBanner].width + 'px';
            }

            if (this.aNodes[prevBanner].height != "") {
                bannerImg.style.height = this.aNodes[prevBanner].height + 'px';
                ImageDiv.style.height = this.aNodes[prevBanner].height + 'px';
                cell.style.height = this.aNodes[prevBanner].height + 'px';
            }


            var bannerText = document.getElementById(this.textBannerId);

            bannerText.innerHTML = this.aNodes[prevBanner].caption;
            bannerText.style.left = (bannerImg.offsetLeft + this.aNodes[prevBanner].textleft) + 'px';
            bannerText.style.top = (bannerImg.offsetTop + this.aNodes[prevBanner].texttop) + 'px';


            //SlideDown
            if (this.aNodes[prevBanner].bannerTransition == "SlideDown" && (this.currentBanner > 0 || this.CurrentLoop > 0)) //Only scenario that is not included. Very 1st picture
            {

                $('#' + bannerImg.parentNode.id).hide();
                $('#' + bannerImg.parentNode.id).slideDown(1500);
            }

            //            $('#' + bannerImg.parentNode.id).show();
            //            $('#' + bannerImg.parentNode.id).slideUp(2000);
            //            $('#' + bannerImg.parentNode.id).slideDown(2000);


            //FadeIn
            if (this.aNodes[prevBanner].bannerTransition == "FadeIn" && (this.currentBanner > 0 || this.CurrentLoop > 0)) //Only scenario that is not included. Very 1st picture
            {
                $('#' + bannerText.id).hide();
                $('#' + bannerImg.id).hide();
                $('#' + bannerImg.id).fadeIn(1500);
                $('#' + bannerText.id).show();
            }
        }


        if (this.currentBanner < this.aNodes.length - 1) {
            this.currentBanner = this.currentBanner + 1;
        }
        else {
            this.currentBanner = 0;
            this.CurrentLoop++;

            if (this.CurrentLoop == this.loopCount) {
                this.keepRotate = 1;
            }
        }

    }


