// JavaScript Document
window.addEvent("domready", function() {	
	//var _sideBarHandler = new SideBarHandler();
});


//--- Sidebar Slideshow
var SideBarHandler = new Class({
    items : null,
    timerId : null,
    initialize: function() {
	var sidebar = $("sidebar_content");
	
	//--- No sidebar content, or elements to slide in the sidebar, do nothing.
	if(!sidebar || sidebar.getChildren().length == 0)
	    return;
	
	//--- Get the first slide and make it active.
	sidebar
	    .addClass("slideshow")
	    .getElement("li:first-child")
	    .addClass("current")
	    .setStyles({visibility:"visible", opacity: "1"})
	
	//--- Don't want to just override the LI style in css, as lots of elements inside the slideshow use LI's too.
	sidebar
	    .getChildren("li")
	    .addClass("slide");

	//--- If the current is taller then the sidebar, expand the height of the sidebar.
    	this.ensureHeight(sidebar.getElement("li.current"));
	
	sidebar
	    .addEvent("mouseenter", function() {
		if(this.timerId != null) {
		    this.stop();
		}
	    }.bind(this))
	    .addEvent("mouseleave", function() {
		if(this.timerId == null) {
		    this.start();
		}
	    }.bind(this));
	
	
	this.start();
    },
    start: function() {
	
	this.timerId = this.nextSlide.bind(this).periodical(5000);
    },
    stop: function() {
	$clear(this.timerId);
	
	this.timerId = null;
    },
    nextSlide : function() {
	var current = $$("#sidebar_content li.current");
	var next = current.getNext();
	
	if(next == null || next[0] == null)
	    next = current.getParent().getFirst();
		
	current.morph({opacity: [1, 0], left: [0, 150]})
		
	current.removeClass("current");
	
	this.ensureHeight(next);
	
	next.setStyles({visibility:"visible", opacity: 0, left: 0})
	next.morph({opacity: [0, 1]})
	next.addClass("current");
	
    },
    ensureHeight : function(slideToShow) {
	if(!slideToShow)
	    return;
	    
	var sidebar = $("sidebar");
	var sidebarSize = sidebar.getSize();

	var newSlideSize = slideToShow.getSize();		
	
	if(newSlideSize.y > sidebarSize.y) {
	    sidebar.tween("height", sidebarSize.y, newSlideSize.y + 10);
	    return;
	}
	
	if(newSlideSize.y < 440 && sidebarSize.y != 450) {
	    sidebar.tween("height", sidebarSize.y, 450);
	    return;
	}
    }
})



































