function callbackFunction(obj){
	jQuery('#rotator .current').html(obj.getCurrent());
}

function Rotator(obj){
	var index;
	var r_timer;
	var maxElements;
	var first;
	var fadeType="dualFade";
	var deltaAnimation=500;
	var rotatorObject=jQuery(obj);
	var rotatorPaused=true;
	var deltaRotatorChange=8000;
	var callback='';
	
	var self=this;
	rotatorObject.find('.rotatorSubstories .substory').click(function(){
		self.trigger(jQuery(this).attr('counter'));
		self.stop();
	});
	
	this.init=function(){
		rotatorPaused=false;
		index=1;
		maxElements=rotatorObject.find(".rotatorSubstories .substory").length;
		rotatorObject.find('.mainImage .mainStory').each(function(){
			if(jQuery(this).hasClass('initial'))
			{
				jQuery(this).css('z-index',1);
			}
			else
			{
				jQuery(this).hide();	
			}
		});
		jQuery(obj+" .mainImage").children(".mainStory").css("position", "absolute");
		self.play();
	}
	this.setCallback=function(cf){
		callback=cf;
	}
	this.clean=function(val){
		var mod=val%maxElements;
		if(mod==0)
		{
			mod=maxElements;
		}
		return mod;
	}
	this.getCurrent=function(){
		return index;
	}
	this.getMax=function(){
		return maxElements;
	}
	this.getRotator=function(){
		return rotatorObject;
	}
	this.isCurrent=function(i){
		if(i==index){
			return true;
		}
		return false;
	}
	this.trigger=function(i){
		i=parseInt(i);
		rotatorObject.find('.rotatorSubstories .substoryOn').removeClass('substoryOn');
		var nextSubstory=rotatorObject.find('.rotatorSubstories .substory_'+i);
		nextSubstory.addClass('substoryOn');
		self.showStory(i);
		callbackFunction(self);
		index=i;
	}
	this.showStory=function(i)
	{
		self.animateDualFade(i);
	}
	this.animateDualFade=function(i){
		if(!self.isCurrent(i))
		{
			index=parseInt(i);
			if(jQuery.browser.msie && jQuery.browser.version == 6.0)																									//  Degrade for smoothness in IE6
			{
				rotatorObject.find(".mainImage .mainStoryOn").hide(); 							    	                              //  Fade out Animation of main story
			}
			else
			{
				rotatorObject.find(".mainImage .mainStoryOn").fadeOut(deltaAnimation); 	                                //  Fade out Animation of main story
			}
			rotatorObject.find(".mainImage .mainStoryOn").removeClass("mainStoryOn").addClass("mainStoryOff");
			if(jQuery.browser.msie && jQuery.browser.version == 6.0)                                            			//  Degrade for smoothness in IE6
			{
				rotatorObject.find(".mainImage .mainStory_"+i).show();
			}
			else
			{
				rotatorObject.find(".mainImage .mainStory_"+i).fadeIn(deltaAnimation);
			}
			rotatorObject.find(".mainImage .mainStory_"+i).addClass("mainStoryOn").removeClass("mainStoryOff");
		}
	}
	this.play=function(){
//		alert('play!!!');
			r_timer=setInterval(
				function(e){
					self.next();
			}, deltaRotatorChange);
			rotatorPaused=false;
	}
	this.stop=function(){
		clearInterval(r_timer	);
		rotatorPaused=true;
	}
	this.pause=function(){
		if(rotatorPaused==true){
			self.play();
		}
		else{
			self.stop();
		}
	}
	this.next=function(){
		self.trigger(self.clean(index+1));
	}
	this.prev=function(){
		self.trigger(self.clean(index-1));
	}
	this.sleep=function(){
		rotatorObject.find('.rotatorSubstories .substory').unbind('click');
		self.stop();
	}
	this.wake=function(){
		rotatorObject.find('.rotatorSubstories .substory').click(function(){
			self.trigger(jQuery(this).attr('counter'));
			self.play();
		});
	}
	self.init();
}


