// Image rotation class programmed by Shawn Olson http://www.shawnolson.net
// of Master Web Designers http://www.masterwebdesigners.com
// This is a commercial JavaScript. You may only use this script if you have purchased a license from Master Web Designers

function mwd_image_rotator(imgs,name,target){
	this.imgs = imgs;
	this.target = target;
	this.name = name;
	this.replaceTarget = true;
	this.autoStart = false;
	this.switchInterval = 5000;
	this.switching = null;
	this.switchImg = mwd_image_rotator_rotate;
	this.writeRotator = mwd_image_rotator_write;
	this.imgClickFunc = mwd_image_rotator_image_click;
	this.showTotal = true;
	this.index = 0;
	this.buttons = 'button';
	this.buttonsImg;
	this.prototype;
	this.buttonEvent;
	this.eventButton = mwd_image_rotator_button_event;
	this.lastContent = 'Last';
	this.nextContent = 'Next';
	this.changeContentEvent;
	this.change = mwd_image_rotator_onchange;
}

/**
 *
 * @access public
 * @return void
 **/
function mwd_image_rotator_onchange(){
	if(this.changeContentEvent){
	  this.changeContentEvent();
	}
}

function mwd_image_rotator_button_event(e){
	// This method is called when the user does a mouseover or mousout on the buttons for last and next
	// create a function that accepts one parameter e (event)
	// set the method to the buttonEvent in your instance of the class
	// for example: myRotatorInstance.buttonEvent = myButtonEvent;
	if(this.buttonEvent){
		if (!e) {var e = window.event;}
		this.buttonEvent(e);
	}

}

function mwd_image_rotator_rotate(dir){
	switch(dir){
		case 'last':
			this.index--;
			break;

		default:
			this.index++;
	}

	if(this.index+1 > this.imgs.length){
		this.index = 0;
	} else if (this.index < 0) {
		this.index = this.imgs.length - 1;
	}

	document.getElementById('mwd_rotator_img_'+this.name).src = this.imgs[this.index][0];

	if(document.getElementById('mwd_image_rotator_index_'+this.name)){
		document.getElementById('mwd_image_rotator_index_'+this.name).innerHTML = this.index+1;
	}

	this.change();
}

function mwd_image_rotator_write(){
	var l = '';
	var r ='';
	var end = '</'+this.buttons+'>';
	var lastLabel = this.lastContent;
	var nextLabel = this.nextContent;
	if(this.imgs.length > 0){
		var total = '';
		if(this.showTotal && this.imgs.length > 1){
			total = '<span id="mwd_rotator_total_'+this.name+'"><span id="mwd_image_rotator_index_'+this.name+'">1</span> of '+this.imgs.length+'</span>';
		}

		switch(this.buttons){
			case 'a':
				this.buttons+=' href="#"';
				break;

			case 'img':
				l = ' src="'+this.lastContent+'"';
				r = ' src="'+this.nextContent+'"';
				lastLabel = '';
				nextLabel = '';
				end = '';
				break;
		}
		var nContent = '<div class="mwd_rotator_div" id="mwd_rotator_'+this.name+'"><a href="#" class="mwd_rotator_img_link" onclick="'+this.name+'.imgClickFunc()"><img class="mwd_rotator_image" id="mwd_rotator_img_'+this.name+'" src="'+this.imgs[0][0]+'" alt="src"/></a><span class="mwd_rotator_cutline" id="mwd_rotator_cutline_'+this.name+'">'+this.imgs[0][1]+'</span><div class="mwd_rotator_buttons"><'+this.buttons+l+' class="mwd_rotator_button_last" id="mwd_rotator_button_last_'+this.name+'" onclick="'+this.name+'.switchImg(\'last\')" onmouseover="'+this.name+'.eventButton(event)" onmouseout="'+this.name+'.eventButton(event)" onmousedown="'+this.name+'.eventButton(event)"> '+lastLabel+end+total+' <'+this.buttons+r+' class="mwd_rotator_button_next" id="mwd_rotator_button_next_'+this.name+'" onclick="'+this.name+'.switchImg(\'next\')" onmouseover="'+this.name+'.eventButton(event)" onmouseout="'+this.name+'.eventButton(event)" onmousedown="'+this.name+'.eventButton(event)">'+nextLabel+end+'</div></div>';
	}

	if(document.getElementById(this.target) && this.replaceTarget){
		document.getElementById(this.target).innerHTML = nContent;
	} else if(document.getElementById(this.target) && this.replaceTarget==false){
		document.getElementById(this.target).innerHTML += nContent;
	} else {
		document.write(nContent);
	}
}

function mwd_image_rotator_image_click(){
	// customize the function for when someone clicks the image
	// Current Array Data is in this.imgs[this.index]
	// to use this method, make a new method and after you create
	// an instance of the class, like this:
	// myRotatorInstance.prototype = myCustomFunction;
	if(this.prototype){
		this.prototype();
	}
}
