/* Author:

*/

function launchTimer(name,fn,time,type) {
	// window[name] could be replaced by this[name] to preserve scope
	window[name] = type ? setTimeout(fn, time) : setInterval(fn, time);
	return window[name];
}
function clearTimer(name,type) {
	window[name] = type ? clearTimeout(window[name]) : clearInterval(window[name]);
	window[name] = null;
}

var $slideshow	= $("#slideshow"),
	$slides		= $slideshow.find("#slides-container img"),
	current		= "current",
	fadeSpeed	= 1000,
	timerSpeed	= 3000;

function slide() {
		if (!$slides.hasClass(current)) {
			$slides.fadeTo(0,0);
			$slides.first().addClass(current).fadeTo(fadeSpeed,1);
		}
		else {
			var $this = $slides.filter("."+current);
			if ($this.is($slides.last())) {
				$slides.first().addClass(current).stop(true,true).fadeTo(fadeSpeed,1);
			}
			else {
				$this.next().addClass(current).stop(true,true).fadeTo(fadeSpeed,1);
			}
			$this.removeClass(current).stop(true,true).fadeTo(fadeSpeed,0);
		}
	clearTimer("slideshow",true);
	launchTimer("slideshow", "slide()",timerSpeed,true);
}

$(document).ready(function(){
	
	// launch slideshow
	slide();
	
	$("#nav li").find("ul").slideUp(0);
	$("#nav li").has("ul").hover(function(){
		$(this).addClass("current").children("ul").stop(true, true).slideDown();
	}, function() {
		$(this).removeClass("current").children("ul").stop(true, true).delay(300).slideUp();
	});
	
	// use js to avoid _blank (not valid)
    $("a[href^='http://']").click(function(){
      if(($(this).attr("class"))=='internal'){
        return true;
      }
      window.open(this.href);
      return false;
    });

});
