﻿var update_ticks = 2000;
var anim_duration = 2000;
var anim_step = 60;
var banners = Array(2);

var init = function () {
    banners[0] = new animateFade("bannerl,bannerl_img,bannerl_txt,bannerl_href,l", 0);
    banners[1] = new animateFade("bannerr,bannerr_img,bannerr_txt,bannerr_href,r", 1);
    getBannerSrc(0);
    getBannerSrc(1);
    waitforfirstload();
};

var waitforfirstload = function () {
    if (banners[0].showTime == null || banners[1].showTime == null)
        setTimeout(waitforfirstload, 100);
    else
        update();
}

var update = function () {
    var param = "";
    for (i = 0; i < banners.length; i++)
        banners[i].doEvents();
    setTimeout(update, update_ticks);
};

var getBannerSrc = function (nBannerIndex) {
    var xmlhttp = null;

    // Mozilla
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", 'sponsorxml.aspx?id=' + banners[nBannerIndex].id + '&date=' + new Date().getTime(), true);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var sRet = xmlhttp.responseText;
            sRet = sRet.split('+').join('%20');
            sRet = unescape(sRet);
            //document.getElementById('trace').innerHTML += "<br/>" + sRet;
            banners[nBannerIndex].setNextBanner(sRet.split('|'));
        }
    }

    xmlhttp.send(null);
};


// animateFade
var animateFade = function (elemparam, index) {
    this.setOptions(elemparam, index);
};

// prototype its helper functions
animateFade.prototype = {
    $: function (id) {
        return document.getElementById(id);
    },

    setOptions: function (elemparam, index) {
        var elem = elemparam.split(',');

        // set options
        this.fadeState = 0;
        this.src = null;
        this.element = this.$(elem[0])
        this.imgelement = this.$(elem[1])
        this.txtelement = this.$(elem[2])
        this.hrefelement = this.$(elem[3])
        this.id = elem[4];
        this.index = index;
    },

    setNextBanner: function (src) {
        this.src = src;
        this.hideTime = this.newTick() + eval(this.src[2]) + anim_duration;
        this.showTime = this.newTick();
    },

    doEvents: function () {
        if (this.showTime != null && this.showTime < this.newTick()) {
            this.show();
            this.showTime = null;
        }
        if (this.hideTime != null && this.hideTime < this.newTick()) {
            getBannerSrc(this.index);
            this.hide();
            this.hideTime = null;
        }
    },

    newTick: function () {
        return new Date().getTime();
    },

    returnElapsedTicks: function () {
        return this.currentTick - this.lastTick;
    },

    trace: function (txt) {
        var elem = this.$('trace');
        if (elem != null)
            elem.innerHTML = elem.innerHTML + "<br>" + txt;
    },

    show: function () {
        this.element.style.opacity = 0;
        this.element.style.filter = 'alpha(opacity = 0)';
    
        this.fadeState = 1 // default -1 fade out, 1 fade in
        this.fadeTimeLeft = anim_duration; // preset to passed duration, then overwrite
        this.lastTick = this.newTick();

        this.imgelement.src = '/img/sponsors/' + this.src[1];
        if (this.src[3] != "#")
        {
            this.txtelement.innerHTML = "<a class='banner_link' target='_blank' href=" + this.src[3] + ">" + this.src[0] + "</a>";
            this.hrefelement.href = this.src[3];
        }
        else
        {
            this.txtelement.innerHTML = this.src[0];
            this.hrefelement.href = "http://www.sikart.ch"; // 22.11.2011 GLo, nicht "#"
        }

        this.animate();
    },

    hide: function () {
        this.fadeState = -1 // default -1 fade out, 1 fade in
        this.fadeTimeLeft = anim_duration; // preset to passed duration, then overwrite
        this.lastTick = this.newTick();
        this.animate();
    },

    animate: function () {
        if (this.lastTick == -1)
            this.lastTick = this.newTick();
        this.currentTick = this.newTick();
        var elapsedTicks = this.returnElapsedTicks();

        // helper function, called in the timeout below, to recurse while maintaining correct reference of "this"
        var self = this;
        var helper = function () {
            self.animate();
        };

        // fade
        this.fadeTimeLeft -= elapsedTicks;
        var newOptVal = this.fadeTimeLeft / anim_duration;
        newOptVal = (this.fadeState == -1) ? newOptVal : (1 - newOptVal);

        this.element.style.opacity = newOptVal;
        this.element.style.filter = 'alpha(opacity = ' + (newOptVal * 100) + ')';

        this.lastTick = this.currentTick;
        this.currentTick = this.newTick();

        if (this.fadeTimeLeft > 0)
            setTimeout(helper, anim_step);
    }
};

