var HeroBox = new Class ({
	Implements: [Options,Events],
	
	options: {
		project: null,
		slides: [],
		startIndex: 0, 
		slideDuration: 6000,
		transitionDuration: 1000,
		transitionType: Fx.Transitions.Expo.easeInOut,
		wrap: true
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.setupSlides(this.options.slides);
	},
	
	setupSlides: function(slide){
		var self = this;
		
		self.initialColor = $("herobox-nav").getElements(".slider-button")[self.options.startIndex].getStyle("border-left-color");
		self.current_slideIndex = self.options.startIndex;
		self.next_slideIndex = self.currentSlideIndex+1;
		self.currentSlide = self.options.slides[self.current_slideIndex];
		self.clicked_slideIndex = null;
		self.clickable = true;
		
		$("herobox").setStyle("background-color",self.initialColor);
		$("herobox").getElements(".slide").setStyle("display","block");
		
		//Ignore this crap unless you are IE6 -------------------------------------------
		
		var IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)  
		&& (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);

		if(IE6){
			
			if(self.current_slideIndex == 0){
				$("herobox").setStyle("background","url('../styles/images/bg-tile-blue.png')");
			} else if(self.current_slideIndex == 1){
				$("herobox").setStyle("background","url('../styles/images/bg-tile-pink.png')");
			} else if(self.current_slideIndex == 2){
				$("herobox").setStyle("background","url('../styles/images/bg-tile-green.png')");
			}
		}
		
		//Stop ignoring------------------------------------------------------------------
		
		self.options.slides.each(function(slide,index){
			if(index == self.current_slideIndex){
				slide.fade("show");
			} else {
				slide.fade("hide");
			}
		});
		
		self.cycleSlides = function(passed_slideIndex){
			
			if(passed_slideIndex == null){
				self.current_slideIndex = (self.current_slideIndex >= self.options.slides.length-1) ?  0 : self.current_slideIndex+1;
			} else {
				clearInterval(self.timer);
				self.current_slideIndex = passed_slideIndex;
			}
			self.currentSlide = self.options.slides[self.current_slideIndex];
			
			self.nextColor = $("herobox-nav").getElements(".slider-button").getStyle("border-left-color");

			self.animateSlides(self.currentSlide);
		};
		
		self.timer = self.cycleSlides.periodical(self.options.slideDuration);
		
		self.animateSlides = function(){
			self.options.slides.each(function(slide,index){
				index != self.current_slideIndex ? self.animateOut(slide) : self.animateIn();
			});
		};
		
		if(self.options.project == "nulli"){
				
			self.animateIn = function(){
				
				self.clickable = false;
				(function(){ self.clickable = true; }).delay(self.options.transitionDuration-150);
				
				self.currentSlide.setStyles({
					"margin-left" : "-500px"
				});
			
				self.animateBgFx = new Fx.Tween($("herobox"), {
					duration: self.options.transitionDuration,
				    transition: 'Expo:InOut',
				    link: 'cancel',
				    property: 'background-color'
				});

				self.animateInFx = new Fx.Tween(self.currentSlide, {
				    duration: self.options.transitionDuration,
				    transition: self.options.transitionType,
				    link: 'cancel',
				    property: 'margin-left'
				});
				
				self.animateTextFx = new Fx.Tween(self.currentSlide.getElement(".copy"), {
				    duration: self.options.transitionDuration+50,
				    transition: self.options.transitionType,
				    link: 'cancel',
				    property: 'margin-left'
				});
				
				self.animateIconFx = new Fx.Tween(self.currentSlide.getElement(".icon"), {
				    duration: self.options.transitionDuration+75,
				    transition: self.options.transitionType,
				    link: 'cancel',
				    property: 'margin-left'
				});
				
				self.animateButtonFx = new Fx.Tween(self.currentSlide.getElement(".action-button"), {
				    duration: self.options.transitionDuration+100,
				    transition: self.options.transitionType,
				    link: 'cancel',
				    property: 'margin-left'
				});
			
				self.animateBgFx.start(null, self.nextColor[self.current_slideIndex]);
				self.animateInFx.start("-500px", "0px");
				self.animateTextFx.start("-500px", "0px");
				self.animateIconFx.start("-500px", "0px");
				self.animateButtonFx.start("-500px", "0px");
		
				self.currentSlide.fade("in");
				
				//Ignore this crap unless you are IE6 -------------------------------------------
				
				var IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)  
				&& (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);

				if(IE6){
					self.altBGFx = new Fx.Tween($("herobox"), {
						duration: self.options.transitionDuration,
					    transition: 'Expo:InOut',
					    link: 'cancel',
					    property: 'background-image'
					});
					self.animateBgFx = new Fx.Tween($("herobox"), {
						duration: self.options.transitionDuration,
					    transition: 'Expo:InOut',
					    link: 'cancel',
					    property: 'background'
					});
					
					if(self.current_slideIndex == 0){
						self.altBGFx.start(null, "url('../styles/images/bg-tile-blue.png')");
					} else if(self.current_slideIndex == 1){
						self.altBGFx.start(null, "url('../styles/images/bg-tile-pink.png')");
					} else if(self.current_slideIndex == 2){
						self.altBGFx.start(null, "url('../styles/images/bg-tile-green.png')");
					}
					self.animateBgFx.start(null, 'none');
				}
				
				//Stop ignoring------------------------------------------------------------------
				
			};
		
			self.animateOut = function(slide){
			
				self.animateOutFx = new Fx.Tween(slide, {
				    duration: self.options.transitionDuration,
				    transition: self.options.transitionType,
				    link: 'cancel',
				    property: 'margin-left'
				});
				self.animateOutFx.start("0px", "150px");
			
				slide.fade("out");
			};
			
			$("herobox-nav").getElements(".slider-button").each(function(button,index){
				button.addEvent("click",function(evt){
					evt.stop();
					self.options.transitionDuration = 500;
					if (index != self.current_slideIndex && self.clickable == true) self.cycleSlides(index);
					
				});
			});
			
		} else {
			
			self.animateIn = function(){
				self.currentSlide.fade("in");
			};
		
			self.animateOut = function(slide){
				slide.fade("out");
			};	
			
		}
	}
	
});

