

var rotatingPromo = {
	settings : {
		duration : 250,
		doautorotate : true,
		addcontrolbtns : true
	},	
	elements:{
		carousel : null,
		list : null,
		btnNext : null,
		btnPrev : null,
        controlBtns : null,
        timer : null,
		timeout:null
	},
	position : 0,
	isAnimating : false,
	width:null,
	init: function(carousel,doautorotate,addcontrolbtns){
		var _this = this;
		this.elements.carousel = carousel;
		this.settings.doautorotate = doautorotate;
		this.settings.addcontrolbtns = addcontrolbtns;
		
		this.elements.list = carousel.getElements('.promo-list')[0];
		this.width = this.elements.list.getChildren("li.promo-item")[0].getSize().x;
		this.elements.list.setStyle(
			"width",  ($$("li",this.elements.list).length*this.width) +"px");
		
		this.elements.btnPrev = this.elements.carousel.getElements(".carousel-btn-prev")[0];
		this.elements.btnNext = this.elements.carousel.getElements(".carousel-btn-next")[0];
		if(addcontrolbtns){
        	this.generateControls();
		}
		this.initButtonEvents();
		if(doautorotate){
			var autoRotate = this.rotate.bind(this,1);		
			this.elements.timer = { 
					tick : false,			
					timeout : null,
					onTick:autoRotate,
					start:function(){
						if(_this.elements.timer.tick == false){
							_this.elements.timer.tick = true;
							_this.elements.timer.tock();
						}
					},
					tock : function(){
						
						if(_this.elements.timer.tick==false){
							return;	
						}
						var  myChain = new Chain;
						myChain.chain(
							function(){
								_this.elements.timeout = _this.elements.timer.onTick.delay(_this.constants.autoRotationDuration);
							},
							function(){
								_this.elements.timer.tock();
							}
						);
						while(myChain.callChain()){
							myChain.callChain();
						}
					},
					stop:function(){
						_this.elements.timeout = $clear(_this.elements.timeout);
						_this.elements.timer.tick = false;
					}
				};
			
			this.elements.timer.start();
		}
		return self;
	},
	
	generateControls: function(){       
        controlBtnsContainer = new Element("ul", {
										   "class":"carousel-control-list"
										   });
		controlBtnsContainer.inject(this.elements.btnPrev , "after");
        var _this = this;
		this.elements.list.getChildren("li").each(function(ele,indx){
			var btnclass = "control-btn" + ( indx == 0 ? " selected" : "");
            tempLi =  new Element("li" , {"class":btnclass,indx:indx});
			tempLi.inject(controlBtnsContainer,"bottom");
            
			var promoSkip = this.promoSkip.bind(this,indx);
			
			 tempLi.addEvent('click',promoSkip);
        
		} , _this);
        
        this.elements.controlBtns = controlBtnsContainer.getChildren("li");
		controlWidth = (this.elements.controlBtns.length *(this.elements.controlBtns[0].getSize().x +
			parseInt(this.elements.controlBtns[0].getStyle("marginLeft")) +
			parseInt(this.elements.controlBtns[0].getStyle("marginRight")) 
		));
        
       controlBtnsContainer.setStyles({
            "width": controlWidth + "px" ,
            "left" : (controlBtnsContainer.getParent().getSize().x - controlWidth)/2 + "px"
            });
    },
	
    highlightControlBtn : function(){
       if(this.settings.addcontrolbtns){
	   		this.elements.controlBtns.each(function(ele,indx){
       			ele.removeClass("selected");
       		});
        	this.elements.controlBtns[this.position].addClass("selected");
	   }
    },
    
    promoSkip: function(indx){
        var _this = this;
      	if(this.settings.doautorotate){
			this.elements.timer.stop();
		}
        if(indx == this.position){
            return;
        }    
        
        var skip = function(){
            while(_this.position != indx){
                _this.advancePromo();
            }
            _this.highlightControlBtn();
			
			_this.elements.carousel.get('tween').chain(
				
				function(){
					if(_this.settings.doautorotate){
						var fnc = function(){_this.elements.timer.start(); }
						fnc.delay(_this.settings.duration/2);
						fnc();
					}
				}
			);
			_this.elements.carousel.tween('opacity',1);
        };
        
		this.elements.carousel.get('tween').chain(
				function(){
					var fnc = function(){skip(); }
					fnc.delay(_this.settings.duration/2);
					fnc();
				}
			);     
		this.elements.carousel.tween('opacity',0);
    },
    
	advancePromo : function(){
        var _this = this;
		var obj = this.elements.list.getChildren("li:first-child")[0];
        obj.inject(this.elements.list,'bottom');			
        this.setPosition(this.position+1);
    },
	
	initButtonEvents : function(){
		var prev = this.rotate.bind(this,-1);
		var next = this.rotate.bind(this,1);
		var _this = this;
		
		this.elements.btnNext.addEvent('mouseenter',function(){
			this.addClass("carousel-btn-next-hover");
		})
		
		this.elements.btnNext.addEvent('mouseleave',function(){
			this.removeClass("carousel-btn-next-hover");
		});
		
		this.elements.btnNext.addEvent('click',next);
		
		this.elements.btnPrev.addEvent('mouseenter',function(){
			this.addClass("carousel-btn-next-hover");
		})
		
		this.elements.btnPrev.addEvent('mouseleave',function(){
			this.removeClass("carousel-btn-next-hover");
		});
		
		this.elements.btnPrev.addEvent('click',prev);
		if(this.settings.doautorotate){
			var btnFadeIn = function(){
				
				if (getInternetExplorerVersion() == -1){   
					_this.elements.btnPrev.fade("in");
					_this.elements.btnNext.fade("in");
				}else{
					_this.elements.btnPrev.setStyle("opacity",1);
					_this.elements.btnNext.setStyle("opacity",1);
				}
			}
			
			var btnFadeOut = function(){
				if (getInternetExplorerVersion() == -1){ 
					_this.elements.btnPrev.fade("out");
					_this.elements.btnNext.fade("out");
				}else{
					_this.elements.btnPrev.setStyle("opacity",0);
					_this.elements.btnNext.setStyle("opacity",0);
				}
			}
			
			_this.elements.carousel.addEvent('mouseenter',btnFadeIn);
			_this.elements.carousel.addEvent('mouseleave',btnFadeOut);  
		}
		
	},
	
	rotate : function(dir){
		if (this.isAnimating){ return; }
		if (dir==-1){
			this.prev(true);
		} else 
		if (dir==1){
			this.next(true);
		}
        this.highlightControlBtn();
	},
	
	prev : function(btn){		
		var _this = this;
        if(btn!=null && this.settings.doautorotate){
            this.elements.timer.stop();
        }
		var obj = this.elements.list.getChildren("li:last-child")[0];
		
		var k = obj.clone();
		k.setStyle('marginLeft',(-this.width)+"px");
		k.inject(_this.elements.list,'top');	
		_this.isAnimating = true;	
		k.get('tween').chain(
				function(){
					obj.dispose();
					_this.play();
					_this.isAnimating = false;
					if(btn!=null && _this.settings.doautorotate){
						_this.elements.timer.start();
					}		  
				}
			);
		k.tween('marginLeft',0);
        this.setPosition(this.position-1);
	},
    
	
	next : function(btn){		
        var _this = this;
		if(btn!=null && this.settings.doautorotate){
            this.elements.timer.stop();
        }
		var obj = this.elements.list.getChildren("li:first-child")[0];
		_this.isAnimating = true;
		obj.get('tween').start('marginLeft',(-this.width)).chain(
				function(){
					obj.inject(_this.elements.list,'bottom');
					obj.setStyle('marginLeft',0);
					_this.play();
					_this.isAnimating = false;
					if(btn!=null && _this.settings.doautorotate){
						_this.elements.timer.start();
					}		  
				}
			);
		
        this.setPosition(this.position+1);
	},
    
    setPosition : function(pos){
        if(pos <0){
            this.position = this.elements.controlBtns.length - 1;
        }else if(pos == this.elements.controlBtns.length){
            this.position = 0;
        }else{
            this.position = pos;
        }
    },
	
	play : function(){
		
	},
	
	statics : {
		something : null
	},
	
	constants : {		
        autoRotationDuration : 5000,
        pauseAfterSkipDuration : 2000
	}
	
};

var rotatingPromo2 = {
	settings : {
		duration : 250,
		doautorotate : true,
		addcontrolbtns : true
	},	
	elements:{
		carousel : null,
		list : null,
		btnNext : null,
		btnPrev : null,
        controlBtns : null,
        timer : null,
		timeout:null
	},
	position : 0,
	isAnimating : false,
	width:null,
	init: function(carousel,doautorotate,addcontrolbtns){
		var _this = this;
		this.elements.carousel = carousel;
		this.settings.doautorotate = doautorotate;
		this.settings.addcontrolbtns = addcontrolbtns;
		
		this.elements.list = carousel.getElements('.promo-list')[0];
		this.width = this.elements.list.getChildren("li.promo-item")[0].getSize().x;
		this.elements.list.setStyle(
			"width",  ($$("li",this.elements.list).length*this.width) +"px");
		
		this.elements.btnPrev = this.elements.carousel.getElements(".carousel-btn-prev")[0];
		this.elements.btnNext = this.elements.carousel.getElements(".carousel-btn-next")[0];
		if(addcontrolbtns){
        	this.generateControls();
		}
		this.initButtonEvents();
		if(doautorotate){
			var autoRotate = this.rotate.bind(this,1);		
			this.elements.timer = { 
					tick : false,			
					timeout : null,
					onTick:autoRotate,
					start:function(){
						if(_this.elements.timer.tick == false){
							_this.elements.timer.tick = true;
							_this.elements.timer.tock();
						}
					},
					tock : function(){
						
						if(_this.elements.timer.tick==false){
							return;	
						}
						var  myChain = new Chain;
						myChain.chain(
							function(){
								_this.elements.timeout = _this.elements.timer.onTick.delay(_this.constants.autoRotationDuration);
							},
							function(){
								_this.elements.timer.tock();
							}
						);
						while(myChain.callChain()){
							myChain.callChain();
						}
					},
					stop:function(){
						_this.elements.timeout = $clear(_this.elements.timeout);
						_this.elements.timer.tick = false;
					}
				};
			
			this.elements.timer.start();
		}
		return self;
	},
	
	generateControls: function(){       
        controlBtnsContainer = new Element("ul", {
										   "class":"carousel-control-list"
										   });
		controlBtnsContainer.inject(this.elements.btnPrev , "after");
        var _this = this;
		this.elements.list.getChildren("li").each(function(ele,indx){
			var btnclass = "control-btn" + ( indx == 0 ? " selected" : "");
            tempLi =  new Element("li" , {"class":btnclass,indx:indx});
			tempLi.inject(controlBtnsContainer,"bottom");
            
			var promoSkip = this.promoSkip.bind(this,indx);
			
			 tempLi.addEvent('click',promoSkip);
        
		} , _this);
        
        this.elements.controlBtns = controlBtnsContainer.getChildren("li");
		controlWidth = (this.elements.controlBtns.length *(this.elements.controlBtns[0].getSize().x +
			parseInt(this.elements.controlBtns[0].getStyle("marginLeft")) +
			parseInt(this.elements.controlBtns[0].getStyle("marginRight")) 
		));
        
       controlBtnsContainer.setStyles({
            "width": controlWidth + "px" ,
            "left" : (controlBtnsContainer.getParent().getSize().x - controlWidth)/2 + "px"
            });
    },
	
    highlightControlBtn : function(){
       if(this.settings.addcontrolbtns){
	   		this.elements.controlBtns.each(function(ele,indx){
       			ele.removeClass("selected");
       		});
        	this.elements.controlBtns[this.position].addClass("selected");
	   }
    },
    
    promoSkip: function(indx){
        var _this = this;
      	if(this.settings.doautorotate){
			this.elements.timer.stop();
		}
        if(indx == this.position){
            return;
        }    
        
        var skip = function(){
            while(_this.position != indx){
                _this.advancePromo();
            }
            _this.highlightControlBtn();
			
			_this.elements.carousel.get('tween').chain(
				
				function(){
					if(_this.settings.doautorotate){
						var fnc = function(){_this.elements.timer.start(); }
						fnc.delay(_this.settings.duration/2);
						fnc();
					}
				}
			);
			_this.elements.carousel.tween('opacity',1);
        };
        
		this.elements.carousel.get('tween').chain(
				function(){
					var fnc = function(){skip(); }
					fnc.delay(_this.settings.duration/2);
					fnc();
				}
			);     
		this.elements.carousel.tween('opacity',0);
    },
    
	advancePromo : function(){
        var _this = this;
		var obj = this.elements.list.getChildren("li:first-child")[0];
        obj.inject(this.elements.list,'bottom');			
        this.setPosition(this.position+1);
    },
	
	initButtonEvents : function(){
		var prev = this.rotate.bind(this,-1);
		var next = this.rotate.bind(this,1);
		var _this = this;
		
		this.elements.btnNext.addEvent('mouseenter',function(){
			this.addClass("carousel-btn-next-hover");
		})
		
		this.elements.btnNext.addEvent('mouseleave',function(){
			this.removeClass("carousel-btn-next-hover");
		});
		
		this.elements.btnNext.addEvent('click',next);
		
		this.elements.btnPrev.addEvent('mouseenter',function(){
			this.addClass("carousel-btn-next-hover");
		})
		
		this.elements.btnPrev.addEvent('mouseleave',function(){
			this.removeClass("carousel-btn-next-hover");
		});
		
		this.elements.btnPrev.addEvent('click',prev);
		if(this.settings.doautorotate){
			var btnFadeIn = function(){
				
				if (getInternetExplorerVersion() == -1){   
					_this.elements.btnPrev.fade("in");
					_this.elements.btnNext.fade("in");
				}else{
					_this.elements.btnPrev.setStyle("opacity",1);
					_this.elements.btnNext.setStyle("opacity",1);
				}
			}
			
			var btnFadeOut = function(){
				if (getInternetExplorerVersion() == -1){ 
					_this.elements.btnPrev.fade("out");
					_this.elements.btnNext.fade("out");
				}else{
					_this.elements.btnPrev.setStyle("opacity",0);
					_this.elements.btnNext.setStyle("opacity",0);
				}
			}
			
			_this.elements.carousel.addEvent('mouseenter',btnFadeIn);
			_this.elements.carousel.addEvent('mouseleave',btnFadeOut);  
		}
		
	},
	
	rotate : function(dir){
		if (this.isAnimating){ return; }
		if (dir==-1){
			this.prev(true);
		} else 
		if (dir==1){
			this.next(true);
		}
        this.highlightControlBtn();
	},
	
	prev : function(btn){
		var _this = this;
        if(btn!=null && this.settings.doautorotate){
            this.elements.timer.stop();
        }
		var obj = this.elements.list.getChildren("li:last-child")[0];
		
		var k = obj.clone();
		k.setStyle('marginLeft',(-this.width)+"px");
		k.inject(_this.elements.list,'top');	
		_this.isAnimating = true;	
		k.get('tween').chain(
				function(){
					obj.dispose();
					_this.play();
					_this.isAnimating = false;
					if(btn!=null && _this.settings.doautorotate){
						_this.elements.timer.start();
					}		  
				}
			);
		k.tween('marginLeft',0);
        this.setPosition(this.position-1);
	},
    
	
	next : function(btn){		
        var _this = this;
		if(btn!=null && this.settings.doautorotate){
            this.elements.timer.stop();
        }
		var obj = this.elements.list.getChildren("li:first-child")[0];
		_this.isAnimating = true;
		obj.get('tween').start('marginLeft',(-this.width)).chain(
				function(){
					obj.inject(_this.elements.list,'bottom');
					obj.setStyle('marginLeft',0);
					_this.play();
					_this.isAnimating = false;
					if(btn!=null && _this.settings.doautorotate){
						_this.elements.timer.start();
					}		  
				}
			);
		
        this.setPosition(this.position+1);
	},
    
    setPosition : function(pos){
        if(pos <0){
            this.position = this.elements.controlBtns.length - 1;
        }else if(pos == this.elements.controlBtns.length){
            this.position = 0;
        }else{
            this.position = pos;
        }
    },
	
	play : function(){
		
	},
	
	statics : {
		something : null
	},
	
	constants : {		
        autoRotationDuration : 5000,
        pauseAfterSkipDuration : 2000
	}
	
}







window.addEvent('domready', function() {
		$("rotatingPromo").rotatingPromo = rotatingPromo.init($("rotatingPromo"),true,true);
		$("initiativesPromo").rotatingPromo = rotatingPromo2.init($("initiativesPromo"),false,false);
	});
