﻿function Rotator() {

	this.baners = Array;
	this.banerCount = 0;
	this.currentBaner = 0;
	this.pe = false;
	this.divId = '';
	this.config = Array;
	var ref = false;
	
	//this.setup = setup;

}

Rotator.prototype.setup = function(divIdParam,inst,btnTxt,prevNext,rotate) {
	this.config.rotate = rotate;
	this.config.btnTxt = btnTxt;
	this.config.prevNext = prevNext;
	this.divId = divIdParam;
	this.banerPrefix = this.divId+'_b';
	this.instanceName = inst;
	$(this.divId).innerHTML = '';
}

Rotator.prototype.add = function(url,image,mode,bg) {
	var bghtml = '';
	var banerId = this.banerPrefix+this.banerCount;
	
	if(!mode) { mode = 'image'; }
	if(bg) { bghtml = "background-color: "+bg+";"; }
	var html = '<div id="'+banerId+'" class="rBaner" style="display: none;'+bghtml+'">';
	switch(mode) {
		case 'image':
			html += '<a href="'+url+'" target="_blank">';
			html += '<img src="'+image+'" border="0" />';
			html += '</a>';
		break;
		case 'swf':			
		break;
	}
	html += '</div>';
	
	$(this.divId).innerHTML = $(this.divId).innerHTML+html;
			
	if(mode == 'swf') {
		$(banerId).onclick = function() { window.open(url); }
		$(banerId).style.cursor = 'pointer';
		var so = new SWFObject(image, "baner_swf", "340", "165", "7", "");
		so.addParam("wmode","transparent");
		so.write(banerId);
	}
	
	this.banerCount++;
}

Rotator.prototype.show = function(id) {
	
	var divs = $(this.divId).getElementsByClassName('rBaner');
	for(var i = 0; i < divs.length; i++) {
		Element.hide(divs[i]);
	}	
	
	Element.setOpacity($(this.banerPrefix+id),0);
	new Effect.Opacity(this.banerPrefix+id, {duration:0.5, from:0, to:1.0});
	Element.show(this.banerPrefix+id);
	this.changeButton(id);
}

Rotator.prototype.next = function() {
	this.currentBaner++;
	if(this.currentBaner >= this.banerCount) {
		this.currentBaner = 0;
	}	
	this.show(this.currentBaner);
}

Rotator.prototype.prev = function() {
	this.currentBaner--;
	if(this.currentBaner < 0) {
		this.currentBaner = this.banerCount-1;
	}	
	this.show(this.currentBaner);
}

Rotator.prototype.manualSwitch = function(id) {
	clearInterval(this.pe);
	this.show(id);
}

Rotator.prototype.changeButton = function(id) {
	var act = $(this.banerPrefix+'naviDiv').getElementsByClassName('act');
	if(act[0]) { act[0].className = 'nact'; }
	$(this.banerPrefix+'bz'+id).className = 'act';
}

Rotator.prototype.renderButtons = function() {
	var html = '';
	
	if(this.config.prevNext) {
		html += '<div onclick="'+this.instanceName+'.prev();" class="prev"></div>';
	}
	
	for(var i = 0; i < this.banerCount; i++) {
		var style = '';
		var style1 = '';
		if(i == this.banerCount-1) { 
			//style1 = 'width: 25px;';
			//style = 'border-right: solid 1px #ffffff;';
		}
		if(this.config.btnTxt) { var btnTxt = (i+1); } else { var btnTxt = ''; }
					
		html += '<div id="'+this.banerPrefix+'bz'+i+'" onclick="'+this.instanceName+'.manualSwitch('+i+');" class="nact" '+style1+'><div class="arr"></div><div class="btn" style="'+style+'">'+btnTxt+'</div></div>';		
	}
	
	if(this.config.prevNext) {
		html += '<div onclick="'+this.instanceName+'.next();" class="next"></div>';
	}
	
	return html;
}

Rotator.prototype.renderNavi = function() {
	$(this.divId).style.position = 'relative';
	var naviDiv = '<div id="'+this.banerPrefix+'naviDiv" class="naviDiv"><div class="naviDivBg"></div><div class="naviDivBgInner">';
	naviDiv += this.renderButtons();
	naviDiv += '</div></div>';
	$(this.divId).innerHTML = $(this.divId).innerHTML+naviDiv;	
}

Rotator.prototype.init = function() {
	this.renderNavi();
	this.show(0);
	var nextFn = function(scope) {
		var newFn = function() {
			scope.next();
		}
		return newFn;
	}
	if(this.config.rotate) { this.pe = setInterval(nextFn(this),12*1000); }
}