/*
 * Project : MACYMED - LIB
 * Author : Macymed - dabroke
 * Copyright : 2010 Macymed Sarl
 * Licence : GNU/GPL v2
 */
var MContentSlider = new Class({
	timer:0,
	timeridle:0,
	currentItem:1,
	beforeItem:0,
	fx:0,
	runnable:true,
	
	options : {
		id : 0,
		transition : Fx.Transitions.linear,
		duration : 800,
		tempo : 3000,
		widthItem : 220,
		heightItem : 200,
		sens : 0,
		sliderId:"mcs_slider0",
		nbItems:10,
		navigation:0,
		template:"default",
		use_fade:0,
		use_fade_when:1,
		idle:60000
	},

	
	initialize : function( options) {
		this.setOptions(options);
		var currentThis = this;
		
		//take care of border
		$$("#"+currentThis.options.sliderId+" .mcs_item").each(function(el){
			if(el.getStyle("border-right").toInt()){
				el.setStyle("width",el.getStyle("width").toInt()-el.getStyle("border-right").toInt());
			}
			if(el.getStyle("border-left").toInt()){
				el.setStyle("width",el.getStyle("width").toInt()-el.getStyle("border-left").toInt());
			}
			if(el.getStyle("border-top").toInt()){
				el.setStyle("height",el.getStyle("height").toInt()-el.getStyle("border-top").toInt());
			}
			if(el.getStyle("border-bottom").toInt()){
				el.setStyle("height",el.getStyle("height").toInt()-el.getStyle("border-bottom").toInt());
			}
			
			
		});
		if(currentThis.options.use_fade && ((currentThis.options.use_fade_when==1) || (currentThis.options.use_fade_when == 2))){
			$$("#"+currentThis.options.sliderId+" .mcs_item").each(function(el,index){
				if(index>0){
					el.setStyle("opacity",0);
				}
				el.fx = new Fx.Styles(el, {
					duration:currentThis.options.duration, 
					wait:false
				});
			});
		}
		if(this.options.navigation){
			$("mcs_slider_navigation"+currentThis.options.id).getFirst().addClass("mcs_"+currentThis.options.template+"_navigation_item_selected");
			$$("#mcs_slider_navigation"+currentThis.options.id+" .mcs_navigation_item").each(function(el){
				
				el.addEvent('click',function(){
					clearTimeout(currentThis.timer);
					clearTimeout(currentThis.timeridle);
					currentThis.runnable = false;
					currentThis.beforeItem = currentThis.currentItem;
					currentThis.currentItem = el.getAttribute("index");
					currentThis.move();		
					currentThis.timeridle = currentThis.idleReenable.delay(currentThis.options.idle,currentThis);
					
				})
				
				
			});
		}
		/*if($(currentThis.options.sliderId).getFirst()){
			var el = $(currentThis.options.sliderId).getFirst();
			this.options.widthItem = this.options.widthItem+el.getStyle("border-right").toInt();
			//this.options.widthItem = this.options.widthItem-el.getStyle("border-left").toInt();
			this.options.heightItem = this.options.heightItem+el.getStyle("border-top").toInt();
			//this.options.heightItem = this.options.heightItem+el.getStyle("border-bottom").toInt();
		}*/
			
		
		currentThis.fx = new Fx.Styles($(currentThis.options.sliderId), {
			transition:currentThis.options.transition,
			duration:currentThis.options.duration, 
			wait:false,
			onComplete:function(){
				currentThis.completeMove();
		}
		});
		this.move.delay(this.options.tempo,this);
		
	},
	idleReenable:function(){
		var currentThis = this;
		clearTimeout(currentThis.timeridle);
		currentThis.runnable = true;		
		currentThis.completeMove();
	},
	completeMove:function(){
		
		var currentThis = this;
		if(currentThis.options.use_fade && (currentThis.options.use_fade_when==2)){
			
			$$("#"+currentThis.options.sliderId+" .mcs_item")[currentThis.currentItem].fx.start({
				'opacity':1
			});
		}
		if(currentThis.runnable){
			
			currentThis.beforeItem = currentThis.currentItem;
			currentThis.currentItem++;
			if(currentThis.currentItem >= currentThis.options.nbItems)
				currentThis.currentItem = 0;
			currentThis.timer = currentThis.move.delay(currentThis.options.tempo,currentThis);
		}
	},
	move:function(){
		var currentThis = this;
		clearTimeout(currentThis.timer);
		if(currentThis.options.use_fade){
			$$("#"+currentThis.options.sliderId+" .mcs_item")[currentThis.beforeItem].fx.start({
				'opacity':0
			});
		}
		if(!currentThis.options.use_fade || (currentThis.options.use_fade && (currentThis.options.use_fade_when==2)) ){
			if(this.options.sens == 0){
				var position = - this.currentItem * this.options.widthItem; 
				this.fx.start({
					'left': position+"px"
				});
			}
			else if(this.options.sens == 1){
				var position = - this.currentItem * this.options.heightItem; 
				this.fx.start({
					'top': position+"px"
				});
			}
		}
		else if(currentThis.options.use_fade && (currentThis.options.use_fade_when==1)){
			
			if(this.options.sens == 0){
				var position = - this.currentItem * this.options.widthItem; 
				this.fx.start({
					'left': position+"px"
				});
				
			}
			else if(this.options.sens == 1){
				var position = - this.currentItem * this.options.heightItem; 
				this.fx.start({
					'top': position+"px"
				});
			}
			$$("#"+currentThis.options.sliderId+" .mcs_item")[currentThis.currentItem].fx.start({
				'opacity':1
			});
		}
		$$("#mcs_slider_navigation"+currentThis.options.id+" .mcs_navigation_item").each(function(el2){
			if(el2.getAttribute("index") != currentThis.currentItem){
				el2.removeClass("mcs_"+currentThis.options.template+"_navigation_item_selected");
				
			}
			else if(!el2.hasClass("mcs_"+currentThis.options.template+"_navigation_item_selected")){
				el2.addClass("mcs_"+currentThis.options.template+"_navigation_item_selected");
			}
			
		});
		 
		
	}
	
});
MContentSlider.implement(new Events, new Options);

function debugMCS(_id){
	if($(_id+"_link").getAttribute("state") == "close"){
		$(_id+"_link").setAttribute("state","open");
		$(_id).setStyle("display","block");
		$(_id+"_link").innerHTML="Close";
	}
	else{
		$(_id+"_link").setAttribute("state","close");
		$(_id).setStyle("display","none");
		$(_id+"_link").innerHTML="Open";
	}
}	

