var Coffee = {
	initialize: function()
	{
		window.addEvent('domready', Coffee.start);
	},
	start: function()
	{
		new SmoothScroll();
		Coffee.initMenu();
		Coffee.initToggles();
		Coffee.changeExtLinks();
	},
	initMenu: function()
	{
		var myList = $$('#idList li');
		myList.each(function(element) {

			if(!element.getParent().hasClass('selected'))
			{
				var fx = new Fx.Styles(element, {duration:200, wait:false});
				/*element.setStyles('background-color:#332f25;color:#c9c19f');*/
				element.addEvent('mouseenter', function(){
					fx.start({
						'padding-left': 20,
						'background-color': '#000000',
						color: '#fff'
					});
				});

				element.addEvent('mouseleave', function(){
					fx.start({
						'padding-left': 0,
						'background-color': '#ffffff',
						'color': '#000000'
					});
				});
			}

		});
	},
	initToggles: function()
	{
		var myList = $$('#content dd.toggle');
		var t;
		myList.each(function(element) {
			t = new ToggleBox(element);
		});
	},
	//
	// Open external links in a new window
	//
	changeExtLinks: function()
	{
		var as,i,islink;
		as=document.getElementsByTagName('a');
		for(i=0;i<as.length;i++){
			islink=as[i].href;
			if(islink.indexOf(window.location.hostname)==-1 &&
			   islink.indexOf("javascript:")==-1 && 
			   islink.indexOf("mailto:")==-1)
			{
				as[i].title += ' ('+islink+')';
				as[i].onclick = function(){
										window.open(this.href,'newwin','');
										return false;
									 };
				as[i].onkeypress = as[i].onclick;
			}
		}
	},
	slideShowsProps: [],
	slideShowsNodes: [],
	slideShows: [],
	addSlideshow: function(slideshow, props)
	{
		Coffee.slideShowsProps[slideshow] = props;
		Coffee.slideShowsNodes[slideshow] = $(slideshow).clone();
	},
	startSlideShow: function(slideshow)
	{
		var p = Coffee.slideShowsProps[slideshow];
		Coffee.slideShows[slideshow] = new Slideshow(slideshow, p);
	},
	stopSlideShow: function(slideshow)
	{
		var s = Coffee.slideShows[slideshow];
		if($defined(s))
		{
			$clear(s.timeout);
			s.fx.each(function(fx) { 
				fx.time = fx.options.duration = 0;
				fx.stop(true); 
			});
			s.timer = [0];
			var nElt = Coffee.slideShowsNodes[slideshow].clone();
			s.slideshow.replaceWith(nElt);
		}
	}
};

var ToggleBox = new Class({
	initialize: function(element)
	{
		this.element = element;
		this.bt = element.getFirst();
		this.lnk = this.bt.getFirst();
		this.h = element.getLast();
		
		this.mySlide = new Fx.Slide(this.h, {toggleBox:this,onComplete: function(){this.toggleBox.toggleSlideShow()}});
		this.mySlide.toggleBox = this;
		this.element.setStyles('margin:0;');
		this.mySlide.hide();
		
		this.bt.toggleBox = this;
		this.bt.onclick = function (){return false};
		this.bt.addEvent('click', function(e){
			new Event(e).stop();
			this.toggleBox.toggle();
		});
		this.opened = false;
		this.slideshow = $E('.slideshow', this.h);
		this.hasSlideShow = $defined(this.slideshow) && $defined(Slideshow)
	},
	toggle: function()
	{
		if(this.opened)
		{
			if(this.hasSlideShow)
			{
				this.onClosing = true;
				Coffee.stopSlideShow(this.slideshow.id);
			}
			this.mySlide.toggle();
			this.lnk.getFirst().setText('+');
		} 
		else
		{
			this.mySlide.toggle();
			this.lnk.getFirst().setText('-');
			this.opened = true;
		}
	},
	toggleSlideShow: function()
	{
		if(this.hasSlideShow)
		{
			if(this.onClosing)
			{
				this.opened = this.onClosing = false;
			}
			else
			{
				Coffee.startSlideShow(this.slideshow.id);
				this.opened = true;
			}
		}
	}
});


Coffee.initialize();
