﻿var BannerOptionSelector = Class.create({
    initialize: function() {
        this.on_Background = '';
        this.off_Background = '';
        this.rotation_Background = '';
        this.summary = '';
        this.background_ClientID = '';
        this.summary_ClientID = '';

        this.backgroundObj;
        this.summaryObj;
    }
});

var BannerOptionMain = Class.create({
    initialize: function() {
        this.background = '';
        this.headline = '';
        this.summary = '';
    }
});

var BannerOption = Class.create({
    initialize: function() {
        this.selector = new BannerOptionSelector();
        this.main = new BannerOptionMain();
        this.link_Url = '';
        this.link_InNewWindow = false;
    }
});

var Banner = Class.create({
    initialize: function(baseName, rotationTime) {
        this.RotationStatusValues = { Started: 0, Paused: 1, Stopped: 2 };

        this.rotationTime = rotationTime;
        this.baseName = baseName;
        this.options = [];

        this.main_background_ClientID = baseName + '_main';
        this.main_headline_ClientID = baseName + '_main_headline';
        this.main_summary_ClientID = baseName + '_main_summary';

        this.backgroundObj = $(baseName + '_main');
        this.headlineObj = $(baseName + '_main_headline');
        this.summaryObj = $(baseName + '_main_summary');

        this.selectedIndex = -1;
        this.rotationId = -1;
        this.rotationStatus = this.RotationStatusValues.Stopped;
        this.imageLoadingCompleted = false;

        this.captureMainEvents();
    },
    addOption: function(option) {
        this.options.push(option);
        option.selector.background_ClientID = this.baseName + '_selector_' + this.options.length;
        option.selector.summary_ClientID = this.baseName + '_selector_' + this.options.length + '_summary';

        option.selector.backgroundObj = $(this.baseName + '_selector_' + this.options.length);
        option.selector.summaryObj = $(this.baseName + '_selector_' + this.options.length + '_summary');

        // Setting events
        this.captureOptionEvents(option, this.options.length - 1);

        // Showing initial data
        option.selector.backgroundObj.style.background = 'url(' + option.selector.off_Background + ')';
        option.selector.summaryObj.innerHTML = option.selector.summary;

        // If this option is the first one, show it
        if (this.selectedIndex == -1) {
            this.selectNode(0);
            this.selectedIndex = 0;
        }
    },
    selectNode: function(index, useRotationImage) {

        useRotationImage = useRotationImage || false;

        if (this.selectedIndex != index) {
            this.headlineObj.innerHTML = this.options[index].main.headline;
            this.summaryObj.innerHTML = this.options[index].main.summary;

            if (useRotationImage)
                this.options[index].selector.backgroundObj.style.background = 'url(' +
													this.options[index].selector.rotation_Background + ')';
            else
                this.options[index].selector.backgroundObj.style.background = 'url(' +
										this.options[index].selector.on_Background + ')';

            blendImage(this.backgroundObj, this.options[index].main.background, .15);

            if (this.selectedIndex > -1 && this.selectedIndex != index)
                this.options[this.selectedIndex].selector.backgroundObj.style.background = 'url(' +
										this.options[this.selectedIndex].selector.off_Background + ')';
        }

        this.selectedIndex = index;
    },
    start: function() {
        // Making sure the images have been loaded
        if (!this.imageLoadingCompleted) {
            var myClass = this;
            setTimeout(function() { myClass.start(); }, 250);
            return;
        }

        // Initiating Animation
        if (this.rotationStatus != this.RotationStatusValues.Started) {
            this.rotationStatus = this.RotationStatusValues.Started;

            var myClass = this;

            function timerRelay() {
                var index = myClass.selectedIndex;
                if (myClass.options.length == ++index) {
                    index = 0;
                }
                myClass.selectNode(index, true);

                myClass.rotationId = timerRelay.delay(myClass.rotationTime);
            }

            myClass.rotationId = timerRelay.delay(myClass.rotationTime);
        }
    },
    pause: function() {
        window.clearTimeout(this.rotationId);
        this.rotationStatus = this.RotationStatusValues.Paused;
    },
    stop: function() {
        window.clearTimeout(this.rotationId);
        this.rotationStatus = this.RotationStatusValues.Stopped;
    },
    captureOptionEvents: function(option, index) {
        var node = option.selector;
        var myClass = this;

        Event.observe(node.backgroundObj, 'mouseover',
	    function() {
	        myClass.selectNode(index);
	        if (myClass.rotationStatus == myClass.RotationStatusValues.Started)
	            myClass.pause();
	    });

        Event.observe(node.backgroundObj, 'mouseout',
	    function() {
	        if (myClass.rotationStatus == myClass.RotationStatusValues.Paused)
	            myClass.start();
	    });

        Event.observe(node.backgroundObj, 'click',
	    function() {
	        var o = myClass.options[index];

	        if (!o.link_InNewWindow)
	            window.location = o.link_Url;
	        else
	            window.open(o.link_Url);
	    });
    },
    captureMainEvents: function() {
        var myClass = this;

        Event.observe(this.backgroundObj, 'mouseover',
	    function() {
	        if (myClass.rotationStatus == myClass.RotationStatusValues.Started)
	            myClass.pause();
	    });

        Event.observe(this.backgroundObj, 'mouseout',
	    function() {
	        if (myClass.rotationStatus == myClass.RotationStatusValues.Paused)
	            myClass.start();
	    });

        Event.observe(this.backgroundObj, 'click',
	    function() {
	        var o = myClass.options[myClass.selectedIndex];

	        if (!o.link_InNewWindow)
	            window.location = o.link_Url;
	        else
	            window.open(o.link_Url);
	    });
    },
    show: function() {
        var aImages = new Array();
        var myClass = this;

        for (var i = 0; i < this.options.length; i++) {
            aImages.push(this.options[i].main.background);
            aImages.push(this.options[i].selector.on_Background);
            aImages.push(this.options[i].selector.off_Background);
            aImages.push(this.options[i].selector.rotation_Background);
        }
        var a = new ImagePreloader(aImages, function() {
            myClass.imageLoadingCompleted = true;
            $(myClass.baseName + '_loading').hide();
        });
    }
});