// JavaScript Document
window.addEvent("domready", function(){
	if(document.id('carousel')) new Carousel();
	if(document.id('mini_carousel')) new MiniCarousel();
	
	if(document.id('init_blog')) document.id('init_blog').addEvent("click", function(){ showContent('blog'); return false; });
	if(document.id('init_news')) document.id('init_news').addEvent("click", function(){ showContent('news'); return false; });
});

function showContent(content){
	$$('.blog_items').setStyle("display", "none");
	$$('.news_items').setStyle("display", "none");
	document.id('init_news').getParent("div").removeClass("current");
	document.id('init_blog').getParent("div").removeClass("current");
	
	switch(content){
		case "blog":
		  $$('.blog_items').setStyle("display", "block").getParent("div");
			document.id('init_blog').getParent("div").addClass("current");
			
		  break;
		case "news":
		  $$('.news_items').setStyle("display", "block").getParent("div");
			document.id('init_news').getParent("div").addClass("current");
		  break;
	}
}

var MiniCarousel = new Class({
  initialize: function(){
		var self       = this;
    this.container = document.id('mini_carousel');
		this.sets      = this.container.getElements(".set");
		this.anchors   = this.container.getElement("#mini_carousel_dots").getElements("a");
		this.current   = 0;
		this.whiteFX = new Fx.Tween(this.container.getElement(".white"), this);
		this.timer;
		this.display_time = 4000;
		
		this.anchors.each(function(anc, i){
			anc.addEvent("click", function(){ self.goto(i); return false; });
		});
		
		this.sets.setStyle("display", "none");
		this.goto(0);
	},
	goto: function(i){
		$clear(this.timer);
		this.whiteFX.cancel();
		var self = this;
    this.whiteFX.start('opacity', 1).chain(
		  function(){
				self.sets[self.current].setStyle("display", "none");
				self.current = i;
				if(self.current > (self.sets.length-1)) self.current = 0;
				self.sets[self.current].setStyle("display", "block");
			  self.timer = self.cycleIn.delay(350, self);
				
				self.anchors.each(function(a){ a.removeClass("current"); });
				self.anchors[self.current].addClass("current");
		  }
		);	
	},
	cycleIn: function(){
    var self = this;
    this.whiteFX.start('opacity', 0).chain(
		  function(){
			  self.timer = self.cycleOut.delay(self.display_time, self);		
				
		  }
		);	
	},
	cycleOut: function(){
    var self = this;
    this.whiteFX.start('opacity', 1).chain(
		  function(){
				self.sets[self.current].setStyle("display", "none");
				self.current++;
				if(self.current > (self.sets.length-1)) self.current = 0;
				self.sets[self.current].setStyle("display", "block");
			  self.timer = self.cycleIn.delay(350, self);
				
				self.anchors.each(function(a){ a.removeClass("current"); });
				self.anchors[self.current].addClass("current");
				
				
		  }
		);	
	}
	
	
});





var Carousel = new Class({
  initialize: function(){
		var self       = this;
    this.container = document.id('carousel');
		this.sets      = this.container.getElements(".set");
		this.anchors   = this.container.getElements("li");
		this.current   = 0;
		this.whiteFX = new Fx.Tween(this.container.getElement(".white"), this);
		this.timer;
		this.display_time = 4000;
		
		this.anchors.each(function(anc, i){
			anc.addEvent("click", function(){ self.goto(i); return false; });
		});
		
		this.sets.setStyle("display", "none");
		this.goto(0);
	},
	goto: function(i){
		$clear(this.timer);
		this.whiteFX.cancel();
		var self = this;
    this.whiteFX.start('opacity', 1).chain(
		  function(){
				self.sets[self.current].setStyle("display", "none");
				self.current = i;
				if(self.current > (self.sets.length-1)) self.current = 0;
				self.sets[self.current].setStyle("display", "block");
			  self.timer = self.cycleIn.delay(350, self);
				
				self.anchors.each(function(a){ a.removeClass("current"); });
				self.anchors[self.current].addClass("current");
		  }
		);	
	},
	cycleIn: function(){
    var self = this;
    this.whiteFX.start('opacity', 0).chain(
		  function(){
			  self.timer = self.cycleOut.delay(self.display_time, self);		
				
		  }
		);	
	},
	cycleOut: function(){
    var self = this;
    this.whiteFX.start('opacity', 1).chain(
		  function(){
				self.sets[self.current].setStyle("display", "none");
				self.current++;
				if(self.current > (self.sets.length-1)) self.current = 0;
				self.sets[self.current].setStyle("display", "block");
			  self.timer = self.cycleIn.delay(350, self);
				
				self.anchors.each(function(a){ a.removeClass("current"); });
				self.anchors[self.current].addClass("current");
				
				
		  }
		);	
	}
	
	
});
