//helper functions

function stop(e) {
	if (!e) e = window.event;
	(e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	return false;
}

function prevDef(e) {
	if (!e) e = window.event;
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	return false;
}    

function log(message) {
	if (typeof console != 'undefined' && typeof console.log != 'undefined') {
		console.log(message);
	}
};

$(document).ready(function(){
	//Remove noJs bodyclass
	$("body").removeClass("noJs").addClass("activeJs");
	//initfaq();
	initnav();
	initSlideShow();
});

function initfaq() {
    $("#faqlisting p").slideUp();
    $("#faqlisting h2").click(function() {
        $(this).next().slideToggle();
        $("#faqlisting h2").not(this).next().slideUp();
    })
}


function initnav() {
    
	//jQuery.easing.def = "easeOutElastic";

	$("#nav li").mouseover(function(){
		$(this).find("a")
			.stop().animate({ top: 12 }, 1000, 'easeOutElastic')
			.css('background-position','-112px 0')
	});
	$("#nav li").mouseout(function(){
		$(this).find("a")
			.stop().animate({ top: 0 }, 190, function(){$(this).css('background-position','0 0')})
	});
	
}

function initSlideShow() {
	
	var slideShow = $("#slideShow");
	var slideWidth = slideShow.width();
	
	//calculate canvas width
	var totalslides= $(".slide",slideShow).length-1;
	var canvasWidth = -(totalslides-1)*slideWidth;	
	var slideSpeed = 3;
	var WaitTime = 5;
	var timeUnit = 600; //timeUnit = seconds
	var easing = 'easeOutQuint';
	var speed = slideSpeed * timeUnit;
	var slideTimer = null;
	var activeSlide = 0; //which slide is active
	var loop = "false"; 
	var indexmode = "number"; // [number|image|thumb] default="number"
	
		
	//init directaccess ----------

	$(".slide",slideShow).each(function(i){
		var indexlist = $(document.createElement('li'));
		var indexlink = $(document.createElement('a')).attr('href','javascript:void(0)');
		var indexbutton;
		if (indexmode=="thumb") { indexbutton = indexlist.append(indexlink.append($(".slider-bg",$(this)).clone())); } 
		else { indexbutton = indexlist.append(indexlink.append(i+1));	}
		$(".directaccess ul").append(indexbutton);
	})
	
	var thumbWidth = $(".directaccess li",slideShow).outerWidth();
	
	$(".directaccess a").bind("click",function(e){
		stop(e);
		var position = $(".directaccess a").index($(this));
		slide(position);
	})

	if (loop == "true") {		
		$("#canvas", slideShow).append($(".slide:first",slideShow).clone());
	}

	//init nextPrev ----------

	slideShow.bind("mouseenter", function () {
		$(".prevnext",slideShow).show();
		clearInterval(slideTimer)
	});

	slideShow.bind("mouseleave", function () {
		$(".prevnext",slideShow).hide();
		clearInterval(slideTimer);
		autoplay();
	});

	$(".next",slideShow).bind("click",function(e){slideNext();});
	$(".prev",slideShow).bind("click",function(e){slidePrev();});

	//init slide magic ----------

	function autoplay()  {
		slideTimer = setInterval(function(){slideNext()},WaitTime * timeUnit)
	}

	function slideNext() {
		if (activeSlide<totalslides) {slide(activeSlide+1)} 
		else {
			if (loop == "true") {loopslide()}
			else {slide(0)}
		}
	}

	function slidePrev() {
		if (activeSlide==0) {slide(totalslides)} 
		else {slide(activeSlide-1)}
	}

	function slide(pos) {
		$("#canvas",slideShow).animate({"margin-left":-slideWidth*pos},speed/2,easing);
		$(".current",slideShow).animate({"margin-left":pos*thumbWidth},timeUnit/2,easing);
		activeSlide = pos;
	}
	function loopslide() {
		$("#canvas",slideShow).animate({"margin-left":-slideWidth*(activeSlide+1)},speed/2,easing,function(){$("#canvas",slideShow).css({"margin-left":0})})
		$(".current",slideShow).animate({"margin-left":0},timeUnit/2,easing);
		activeSlide = 0;
	}
	
	//start the show
	autoplay();
	
}

