function callbackFunction(obj){
}

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;
		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){
		rotatorObject.find('.rotatorSubstories .substoryOn').removeClass('substoryOn');
		var nextSubstory=rotatorObject.find('.rotatorSubstories .substory_'+i);
		nextSubstory.addClass('substoryOn');
		self.showStory(i);
		callbackFunction(self);
	}
	this.showStory=function(i)
	{
		self.animateDualFade(i);
	}
	this.animateDualFade=function(i){
		if(!self.isCurrent(i))
		{
			index=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');
	}
	this.wake=function(){
		rotatorObject.find('.rotatorSubstories .substory').click(function(){
			self.trigger(jQuery(this).attr('counter'));
			self.stop();
		});
	}
	self.init();
}

function SmallRotator(obj){
	var object;
	var childContainer;
	var children;
	var max;
	var current;
	var delta=500;
	var deltaRotatorChange=10000
	var r_timer;
	var poll;
	var pollObjectHeight=0;

	self=this;

	this.clean=function(i){
		if(i>=max){
			i=0;
		}
		if(i<0){
			i=max-1;
		}
		return i;
	}

	this.init=function(obj)
	{
		object=jQuery(obj);
		children=object.find('.container .item');
		childContainer=object.find('.container');
		pollObjectHeight=object.height();
		var i=0;
		children.each(function(){
			jQuery(this).attr('i',i).addClass('item_'+i);
			i++;
		});
		max=i;
	}

	this.start=function(){
		current=0;
		children.hide();
		children.find('.item_0').show();
		r_timer=setInterval(
			function(e){
				self.next();
		}, deltaRotatorChange);
	}

	this.next=function(){
		childContainer.find('.item_'+self.clean(current)).fadeOut(delta);
		childContainer.find('.item_'+self.clean(current+1)).fadeIn(delta);
		current=self.clean(current+1);
	}

	self.init();
	self.start();
	poll=setInterval(function(e){
		var test=object.height();
		if(test!=pollObjectHeight){
			self.init(obj);
			pollObjectHeight=test;
		}
	}, 500);
}

