var imageCycler = {

	'paths': ["/user_files/image_cycler/dealpirate_valentine_577x238.png","/user_files/image_cycler/carrington_event_577x238.jpg","/user_files/image_cycler/577x238_mobile_graphic.jpg","/user_files/image_cycler/577-x-238-tax-tips-window_oxendale.jpg","/user_files/image_cycler/577x238_trust_flipper.jpg","/user_files/image_cycler/ksed_big_d_and_bubba.jpg","/user_files/image_cycler/578x233_ksed_school_closings.jpg","/user_files/image_cycler/578x233_ksed_red_solo_cup_flyaway.jpg"],
	'links': ["http://www.shopdealpirate.com/","http://www.koltcountry.com/contests/view/contest/2283/win_tickets_to_see_rodney_carrington","http://www.koltcountry.com/section/view/free_mobile_apps/177/","/section/view/tax_tips/174/","http://www.koltcountry.com/experts/view/all/","/shows/view/on_the_show/4/","/section/view/cancellations_and_closures/58/","http://www.bigdandbubba.com/pages/contests/tobykeithredcup/"],
	'times': ["5","5","5","5","5","5","5","5"],
	'target': ["_blank","_top","_top","_top","_blank","_top","_top","_blank"],
	'currentImage': 0,
	'currentTimeout': false,
	'init': function() {

		var image = new Image();
		for (var i=0; i<this.paths.length; i++){
			image.src = this.paths[i];
		}

		this.changeImage(0,false);

	},
	'changeImage': function(t,realClick) {
		var innerHTMLText 		= '';

		if (this.links[t] != '') innerHTMLText += '<a href="' + this.links[t] + '" target="' + this.target[t]  + '">';
		innerHTMLText += '<img src="' + this.paths[t] + '" class="imageCyclerImage">';
		if (this.links[t] != '') innerHTMLText += '</a>';
		
		//Fix the double '?' caused by output_add_rewrite_var(), simply search for ?session and replace with &session
		innerHTMLText	= innerHTMLText.replace('?session','&session');

		document.getElementById("imageCyclerImageContainer").innerHTML = innerHTMLText;

		//Highlight the current image.
		document.getElementById("menuButton_" + this.currentImage).className = "menuButton";
		document.getElementById("menuButton_" + t).className = "menuButton current";

		this.currentImage = t;

		if( this.currentImage+1 <= this.paths.length - 1 && !realClick) {
			this.currentTimeout = window.setTimeout('imageCycler.changeImage(' + parseInt(this.currentImage+1) + ',false)', this.times[this.currentImage] * 1000);
		} else if( this.currentImage == this.paths.length - 1 ) {	// Start the cycle again
			this.currentTimeout = window.setTimeout('imageCycler.changeImage(0,false)', this.times[0] * 1000);
		}

	},
	'selectImage': function (t) {

		this.changeImage(t,true);
		window.clearTimeout(this.currentTimeout);

	},
	'prevImage': function () {

		if( this.currentImage-1 >= 0) {
			this.changeImage(this.currentImage-1,true);
		} else	{
			this.changeImage(this.paths.length-1,true);
		}

		window.clearTimeout(this.currentTimeout);

	},
	'nextImage': function () {

		if( this.currentImage+1 <= this.paths.length - 1) {
			this.changeImage(this.currentImage+1,true);
		} else {
			this.changeImage(0,true);
		}

		window.clearTimeout(this.currentTimeout);

	}

}

window.onload = function() { imageCycler.init(); };

