// JavaScript add Shower

window.addEvent('domready', function() {
	wsize = window.getSize().x;
	if(wsize > 1300) {
		maxleft = wsize - 1255;
		if(wsize > 1300)
			maxleft = maxleft - 50;
		ka_add = new ka_adShower({maxLeft: maxleft, showTime: 15000});
	}
});

/********** KA Ad Loader Class *************/
var ka_adShower = new Class({

	Implements: [Chain, Options],

	options: {
		adContainer: "ka_adcontainer",
		adStartClass: "ka_adStart",
		adEndClass: "ka_adEnd",
		requestURL: "/adContent/",
		showTime: 10000,
		maxLeft: false
	},

	initialize: function(options) {
		this.setOptions(options);
		this.counter = 0;
		// setup adcontainer
		this.adContainer = $(this.options.adContainer);
		if(!this.adContainer) {
			this.adContainer = new Element('div', { id: this.options.adContainer, style: {display:'none'}});
			this.adContainer.inject($(document.body),'bottom');
		}
		this.adContainer.set('styles', {
							 'display': 'none',
							 'position': 'absolute',
							 'z-index': '200'
		});
		this.adContainer.set('morph',{duration: 'normal', onComplete: this.callChain.bindWithEvent(this) });
		this.getAdContent();
	},
	
	getAdContent: function() {
		var adContentRequest = new Request.HTML({
			url: this.options.requestURL,
			data: {counter: this.counter },
			onFailure: function() { alert("getAdCOntent Failure"); },
			onException: function() { alert("getAdCOntent Failure"); },
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					this.adContainer.set('html','');
					this.adContainer.set('html',responseHTML);
					this.counter++;
					this.showAdd.delay(3000,this);
			}.bind(this)
		}).send();
		
	},
	
	showAdd: function() {
		this.adContainer.addClass(this.options.adStartClass);
		this.clearChain();
		this.chain(
			function() { 
				this.adContainer.setStyle('display','block');
				//this.adContainer.morph('.'+this.options.adEndClass);
				this.adContainer.morph({left: this.options.maxLeft, opacity: 1});
			},
			function() {
				this.adContainer.removeClass(this.options.adStartClass);
				this.adContainer.addClass(this.options.adEndClass);
				if(this.options.showTime) {
					this.clearAdd.delay(this.options.showTime,this);
				}
			}
		);
		this.callChain();
	},
	
	clearAdd: function() {
		this.clearChain();
		this.chain(
			function() { 
				//this.adContainer.morph('.'+this.options.adStartClass);
				this.adContainer.morph({left: '-400px', opacity: 0});
			},
			function() {
				this.adContainer.removeClass(this.options.adEndClass);
				this.adContainer.addClass(this.options.adStartClass);
				this.adContainer.setStyle('display','none');
				this.getAdContent();
			}
		);
		this.callChain();
	}
	
	
	
});
