/**
 * @author Ryan Johnson <http://syntacticx.com/>
 * @copyright 2008 PersonalGrid Corporation <http://personalgrid.com/>
 * @package LivePipe UI
 * @license MIT
 * @url http://livepipe.net/controls/hotkey/
 * @attribution http://www.quirksmode.org/js/cookies.html
 *
 * modified by Pascal Beyeler -> removed the events
 */

var Cookie = {
	set: function(name,value,seconds){
		if(seconds){
			var d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			var expiry = '; expires=' + d.toGMTString();
		}else
			var expiry = '';
		document.cookie = name + "=" + value + expiry + "; path=/";
	},
	get: function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++){
			var c = ca[i];
			while(c.charAt(0) == ' ')
				c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	unset: function(name){
		Cookie.set(name,'',-1);
	}
};

/**
*
*	BANNER ROTATION
*
*   @copyright	Assai Dialog + Digital AG, adesso AG
*	@author     Pascal Beyeler, A. Furtner
*	@created    March 20, 2009
*
*/

var bannerRotator = {

	initialize: function() {

		this.banner_count = 3;
		this.current_banner = this.getCurrentBanner();
		this.banner_name = 'showLayer';
		this.bannerTimeout = 8000;

        //show the next banner
		showNextBanner();

		//setTimeout handler
		this.timeout = null;
		this.setTimeout();
	},

	getCurrentBanner: function() {

		var current_banner = Cookie.get('current_banner');
		if(current_banner !== null) {

			return current_banner;
		} else {
			return 0;
		}
	},

	setTimeout: function() {
		this.timeout = window.setTimeout("showNextBanner()", this.bannerTimeout);
	},

	clearTimeout: function() {
		window.clearTimeout(this.timeout);
	}

};

function showNextBanner() {
    //hide the current banner
    var currentBannerElement = document.getElementById(bannerRotator.banner_name + bannerRotator.current_banner);

    if(currentBannerElement) {
        currentBannerElement.style.display = 'none';
    }

    //display the divider
    document.getElementById('divider').style.display = 'block';

    window.setTimeout("showNextBanner1()", 100);
}

function showNextBanner1() {

        //hide the divider
        document.getElementById('divider').style.display = 'none';

        //reset the timeout
        if(bannerRotator.timeout !== null) {
            bannerRotator.clearTimeout();
        }

        //start from the beginning if there's no more banner
        var next_banner = parseInt(bannerRotator.current_banner) + 1;
        if(next_banner > bannerRotator.banner_count) {
            next_banner = 1;
        }

        //show the new banner
        document.getElementById(bannerRotator.banner_name + next_banner).style.display = 'block';
        //set the current banner and cookie
        bannerRotator.current_banner = next_banner;
        Cookie.set('current_banner', bannerRotator.current_banner, 9999999);
        //start the new timeout
        bannerRotator.setTimeout();
}

if (window.addEventListener) {
    window.addEventListener('load', function() {
        bannerRotator.initialize();
    }, false);
} else {
    window.attachEvent("onload", function() {
        bannerRotator.initialize();
    });
}

