var Preload = {
	imgs : Array,
	preloadimgs: Array,
	init : function(in_imgs) {
		this.preloadimgs = in_imgs;
		this.run();
	},
	run : function() {
		for(x=0; x < this.preloadimgs.length; x++) {
			this.imgs[x] = new Image();
			this.imgs[x].src = this.preloadimgs[x];
		}
	}
}

function SlideShow (in_ad_ids, in_target, in_delay, in_load_url)
{
	this.loadUrl     = in_load_url;
	this.adIds       = in_ad_ids;
	this.targetDiv   = $('#' + in_target);
	this.slideDiv    = $(this.targetDiv).find('.slide');
	this.nextLink    = $(this.targetDiv).find('.next-slide');
	this.prevLink    = $(this.targetDiv).find('.prev-slide');
	this.delay       = in_delay * 1000;
	this.currentad   = 0;
	this.timer       = 0;

	this.run = function() {
		var me = this;
		$(me.targetDiv).mouseover(function() {
			me.stoptimer();
		});
		$(me.targetDiv).mouseout(function() {
			me.starttimer();
		});
		if($(me.prevLink)) {
			$(me.prevLink).click(function(e) {
				e.preventDefault();
				me.moveprevious();
			});
		}
		if($(me.nextLink)) {
			$(me.nextLink).click(function(e) {
				e.preventDefault();
				me.movenext();
			});
		}
		me.starttimer();
	}
	
	this.movenext = function() {
		var me = this;
		me.currentad += 1;
		if (me.currentad >= me.adIds.length) {
			me.currentad = 0;
		}
		me.setupad();
	}
	
	this.moveprevious = function() {
		this.currentad -= 1;
		if (this.currentad < 0) {
			this.currentad = this.adIds.length - 1;
		}
		this.setupad();
	}
	
	this.starttimer = function() {
		var me = this;
		clearTimeout(me.timer);
		me.timer = setTimeout(function(){me.movenext();}, me.delay)
	}
	
	this.stoptimer = function() {
		var me = this;
		clearTimeout(me.timer);
		me.timer  = 0;
	}
	
	this.setupad = function() {
		me = this;
		$.get(me.loadUrl + "&id=" + me.adIds[me.currentad], function(data){
			$(me.slideDiv).html(data);
		});
		//alert(me.slideDiv);
		me.starttimer();
	}
	
	this.changead = function(req)
	{
		$(me.slideDiv).html(req.responseText);
	}
	
	this.run();
}


var AdRotator = {
	adImages     : Array,
	adIds        : Array,
	adTypes      : Array,
	linkUrl      : Array,
	targetDiv    : Object,
	targetDivID  : Number,
	targetLink   : Object,
	prevLink     : Object,
	nextLink     : Object,
	targetImg    : Object,
	currentad    : Number,
	timer        : Number,
	imgHeight    : Number,
	imgWidth     : Number,
	delay        : 15000,
	init : function(in_ad_ids, in_target, in_delay, in_load_url) {
		this.adIds       = in_ad_ids;
		this.loadUrl     = in_load_url;
		this.targetDiv   = $(in_target);
		this.slideDiv    = this.targetDiv.elmsByClass("slide")[0];
		this.nextLink    = this.targetDiv.elmsByClass("next-slide")[0];
		this.prevLink    = this.targetDiv.elmsByClass("prev-slide")[0];
		this.delay       = in_delay * 1000;
		this.currentad   = 0;
		this.timer       = 0;
		this.run();
	},
	movenext : function() {
		this.currentad += 1;
		if (this.currentad >= this.adIds.length) {
			this.currentad = 0;
		}
		this.setupad();
	},
	moveprevious : function() {
		this.currentad -= 1;
		if (this.currentad < 0) {
			this.currentad = this.adIds.length - 1;
		}
		this.setupad();
	},
	starttimer : function() {
		var me = this;
		clearTimeout(me.timer);
		me.timer = setTimeout(function(){me.movenext();}, me.delay)
	},
	stoptimer : function() {
		clearTimeout(this.timer);
		this.timer  = 0;
	},
	setupad : function() {
		me = this;
		DOMAssistant.AJAX.get(me.loadUrl + "&id=" + this.adIds[this.currentad], function(ret) {
			$(me.slideDiv).replaceContent(ret);
		});
		this.starttimer();
	},
	run : function() {
		var me = this;
		me.targetDiv.addEvent("mouseover", function() {
			me.stoptimer();
		});
		me.targetDiv.addEvent("mouseout", function() {
			me.starttimer();
		});
		if(me.prevLink) {
			$(me.prevLink).addEvent("click", function(e) {
				me.moveprevious();
				DOMAssistant.preventDefault(e);
			});
		}
		
		if(me.nextLink) {
			$(me.nextLink).addEvent("click", function(e) {
				me.movenext();
				DOMAssistant.preventDefault(e);
			});
		}
		me.setupad();
	}
};
