/**
 * @copyright 2008 Juergen Vogel, jv-webentwicklung.de && waider-mediendesign.de
 */

var wm= {}

wm.init= function() {
	
	if($j('#refbox'))		wm.refbox.init();
}

$j(document).ready(  wm.init );

wm.refbox = {
	
	speed:1500,
	switchTime: 5500,
	
	images:[],
	buttons:[],
	active:0,
	auto:true,
	timeout:'',
	
	init: function() {
		
		$j("#refbox img").each( function(i,obj) {
			
			wm.refbox.images[i]=obj;
			if($j(obj).css("display")!="none")	
				this.active=i;		
		} );		
		
		$j("#refbox #refbox_index li a").each( function(i,obj) {
			
			$j(obj).click( function() { wm.refbox.setImage(i) })
			wm.refbox.buttons[i]=obj;
		});
		
		$j("#refbox span a").click(  function(i,ele){
			wm.refbox.controller(this);
		} );
		
		this.startAuto();
		
	},
	
	setImage: function(pId) {
		
		
		if(this.auto==true) $j("#refbox span a").trigger('click');		
		this.effect(pId);
		this.active=pId;
	},
	
	effect: function(pNext) {
		
		$j(this.images[this.active]).fadeOut( this.speed );
		$j(this.buttons[this.active]).removeClass('over');
		
		$j(this.images[pNext]).fadeIn( this.speed );
		$j(this.buttons[pNext]).addClass('over');
		
		if( this.auto==true)
			this.startAuto();			
	},
	
	startAuto: function(){
		
		this.stopAuto();			
		this.timeout = window.setTimeout( this.autoEffect, this.switchTime);
	},
	
	stopAuto: function(){		
		
		if(this.timeout)
			window.clearTimeout(this.timeout);		
	},
	
	controller: function(pController) {
		
		$j(pController).toggleClass('play');
		( pController.innerHTML=='Pause' ) ? pController.innerHTML='Start'  : pController.innerHTML='Pause';
		
		if (this.auto==true) {
			this.stopAuto();
			this.auto=false;
		}else {
			this.startAuto();
			this.auto=true;
		}
	},
	
	autoEffect: function() {
		
		var myNext= ( wm.refbox.active+1 > wm.refbox.images.length-1 ) ? 0 : wm.refbox.active+1;
		wm.refbox.effect(myNext);
		wm.refbox.active=myNext;	
	}	
}