﻿var RotatingBaner = Object;
RotatingBaner.baners = Array;
RotatingBaner.banerCount = 0;
RotatingBaner.currentBaner = 0;
RotatingBaner.pe = false;
RotatingBaner.divId = '';

RotatingBaner.setup = function(divId) {
	this.divId = divId;
	$(this.divId).innerHTML = '';
}

RotatingBaner.add = function(url,image,mode,bg) {
	var bghtml = '';	
	if(bg) { bghtml = "background-color: "+bg+";"; }
	var html = '<div id="rBaner'+this.banerCount+'" 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') {
		$('rBaner'+this.banerCount).onclick = function() { window.open(url); }
		$('rBaner'+this.banerCount).style.cursor = 'pointer';
		var so = new SWFObject(image, "baner_swf", "340", "165", "7", "");
		so.addParam("wmode","transparent");
		so.write('rBaner'+this.banerCount);
	}
	
	this.banerCount++;
		
}

RotatingBaner.next = function() {
	RotatingBaner.currentBaner++;
	if(RotatingBaner.currentBaner >= RotatingBaner.banerCount) {
		RotatingBaner.currentBaner = 0;
	}
	RotatingBaner.show(RotatingBaner.currentBaner);
}

RotatingBaner.show = function(id) {
	var divs = $(this.divId).getElementsByClassName('rBaner');
	for(var i = 0; i < divs.length; i++) {
		Element.hide(divs[i]);
	}	
	
	Element.setOpacity($('rBaner'+id),0);
	new Effect.Opacity('rBaner'+id, {duration:0.5, from:0, to:1.0});
	Element.show('rBaner'+id);
	this.changeButton(id);
}

RotatingBaner.manualSwitch = function(id) {
	clearInterval(this.pe);
	this.show(id);
}

RotatingBaner.changeButton = function(id) {
	var act = document.getElementsByClassName('act',$('naviDiv'));
	if(act[0]) { act[0].className = 'nact'; }
	$('bz'+id).className = 'act';
}

RotatingBaner.renderButtons = function() {
	var html = '';
	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;';
		}
		html += '<div id="bz'+i+'" onclick="RotatingBaner.manualSwitch('+i+');" class="nact" '+style1+'><div class="arr"></div><div class="btn" style="'+style+'">'+(i+1)+'</div></div>';
	}
	return html;
}

RotatingBaner.renderNavi = function() {
	$(this.divId).style.position = 'relative';
	var naviDiv = '<div id="naviDiv" class="naviDiv"><div class="naviDivBg"></div><div style="position: absolute; left: 80px; bottom: 0px;">';
	naviDiv += this.renderButtons();
	naviDiv += '</div></div>';
	$(this.divId).innerHTML = $(this.divId).innerHTML+naviDiv;	
}

RotatingBaner.init = function() {
	this.renderNavi();
	this.show(0);	
	this.pe = setInterval(RotatingBaner.next,12*1000);
}