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];
		}
	}
}

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_images, in_ad_ids, in_ad_types, in_base_url, in_target, in_delay, in_width, in_height) {
		this.adImages    = in_ad_images;
		this.adIds       = in_ad_ids;
		this.adTypes     = in_ad_types;
		this.linkUrl     = in_base_url;
		this.imgHeight   = in_height;
		this.imgWidth    = in_width;
		this.targetDiv   = document.getElementById(in_target);
		this.targetDivID = in_target.split("_")[1];
		this.targetContent = document.getElementById('promotion_content_' + this.targetDivID);
		this.nextLink    = document.getElementById('promotion_next_' + this.targetDivID);
		this.prevLink    = document.getElementById('promotion_prev_' + this.targetDivID);
		this.delay       = in_delay * 1000
		this.currentad   = 0;
		this.timer       = 0;
		this.run();
	},
	movenext : function() {
		this.currentad += 1;
		if (this.currentad >= this.adImages.length) {
			this.currentad = 0;
		}
		this.setupad(1);
	},
	moveprevious : function() {
		this.currentad -= 1;
		if (this.currentad < 0) {
			this.currentad = this.adImages.length - 1;
		}
		this.setupad(1);
	},
	starttimer : function() {
		var me = this;
		clearTimeout(this.timer);
		this.timer = setTimeout(function(){me.movenext();}, this.delay)
	},
	stoptimer : function() {
		clearTimeout(this.timer);
		this.timer  = 0;
	},
	setupad : function(in_save_view) {
		if(this.adTypes[this.currentad] == 'img') {
			$(this.targetContent).html('<a href="' + this.linkUrl + "?id=" + this.adIds[this.currentad] + '" title="" target"_blank" class="promotion_link" id="promotion_link_' + this.adIds[this.currentad] + '"><img src="' + this.adImages[this.currentad] + '" width="' + this.imgWidth + '" height="' + this.imgHeight + '" alt="" id="promotion_image_' + this.adIds[this.currentad] + '"/></a>');
		} else if (this.adTypes[this.currentad] == 'swf'){
			$(this.targetContent).html('swf');
			$(this.targetContent).html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + this.imgWidth + '" height="' + this.imgHeight + '">\n<param name="movie" value="' + this.adImages[this.currentad] + '?inid=' + this.adIds[this.currentad] + '" />\n<param name="quality" value="high" />\n<param name="menu" value="false" />\n<embed src="' + this.adImages[this.currentad] + '?inid=' + this.adIds[this.currentad] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + this.imgWidth + '" height="' + this.imgHeight + '"></embed>\n</object>');
		} else {
			
		}
		if (in_save_view != 0) {
			$.post('http://www.kellys.uk.com/advertising/ajax_views.asp', {
				id: this.adIds[this.currentad]
			});
		}
		this.starttimer();
	},
	run : function() {
		var me = this;
		//for (var i = 0; i< this.adImages.length; i++) {
		//	if (this.adTypes[i] == 'jpg' || this.adTypes[i] == 'gif' || this.adTypes[i] == 'png') {Preload.Add(this.adImages[i]);};
		//}
		//Preload.init();
		this.targetDiv.onmouseover = function() {me.stoptimer();};
		this.targetDiv.onmouseout = function() {me.starttimer();};
		this.prevLink.onclick = function() {me.moveprevious();};
		this.nextLink.onclick = function() {me.movenext();};
		this.setupad(0);
	}
};