/**
 * @author Juergen Vogel, jv-webentwicklung.de
 * @copyright: waider-mediendesign.de
 */

var mcs= {}

mcs.init= function() {
	
	if($('#header_rotate'))		mcs.refbox.init();
}

$(document).ready(  mcs.init );

mcs.refbox = {
	
	speed:2000,
	switchTime: 4000,
	
	images:[],
	buttons:[],
	active:0,
	auto:true,
	timeout:'',
	
	init: function() {
		
		$("#header_rotate img").each( function(i,obj) {
			
			mcs.refbox.images[i]=obj;
			if($(obj).css("display")!="none")	
				this.active=i;		
		} );		
		
		this.startAuto();		
	},
	
	setImage: function(pId) {
		
		this.effect(pId);
		this.active=pId;
	},
	
	effect: function(pNext) {
		
		$(this.images[this.active]).fadeOut( this.speed );
		$(this.buttons[this.active]).removeClass('over');
		
		$(this.images[pNext]).fadeIn( this.speed );
		$(this.buttons[pNext]).addClass('over');
		
		if( this.auto==true)
			this.startAuto();			
	},
	
	startAuto: function(){
		
		this.timeout = window.setTimeout( this.autoEffect, this.switchTime);
	},
	
	autoEffect: function() {
		
		var myNext= ( mcs.refbox.active+1 > mcs.refbox.images.length-1 ) ? 0 : mcs.refbox.active+1;
		mcs.refbox.effect(myNext);
		mcs.refbox.active=myNext;	
	}	
}