//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);
	}
};

//init function

$(document).ready(function() {
	//Remove noJs bodyclass
	$("body").removeClass("noJs").addClass("activeJs");
	//start slideShow
	jQuery("#slideShow").each(function(){jQuery(this).initSlideShow()})
	
	$( "#accordion" ).accordion({collapsible:true,active:false,autoHeight:false});		
	$("#contactform").validate();
});


jQuery.fn.initSlideShow = function() {
	
	var slideShow = $(this);
	var slideWidth = slideShow.width();
	
	//calculate canvas width
	var totalslides= $(".slide",slideShow).length-1;
	var canvasWidth = -(totalslides-1)*slideWidth;	
	var slideSpeed = 1;
	var WaitTime = 10;
	var timeUnit = 600; //timeUnit = seconds
	var speed = slideSpeed * timeUnit;
	var slideTimer = null;
	var activeSlide = 0; //which slide is active
	var loop = "true"; 
	var indexmode = "thumb"; // [number|image|thumb] default="number"
	
		
	//init directaccess ----------

	$(".slide",slideShow).each(function(i){
		checklink(this);		
		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));	}
		$(".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());
	}

	//helper function to add link to slide image

	function checklink(slide) {
		var thelink = $('a.slidelink',slide);
		if (!thelink.length) {return false}
		var slidelink = $(thelink).attr('href');
		if (slidelink != '') {
			$(slide).find('img').css({'cursor':'hand','cursor':'pointer'}).click(function(e) {
				window.open(slidelink);
				return false;
			});
		}
	}

	//init nextPrev ----------

	slideShow.bind("mouseenter", function () {
		$(".prevnext",slideShow).fadeIn(350);
		clearInterval(slideTimer)
	});

	slideShow.bind("mouseleave", function () {
		$(".prevnext",slideShow).fadeOut(50);
		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);
		$(".current",slideShow).animate({"margin-left":pos*thumbWidth},timeUnit/2);
		activeSlide = pos;
	}
	function loopslide() {
		$("#canvas",slideShow).animate({"margin-left":-slideWidth*(activeSlide+1)},speed/2,function(){$("#canvas",slideShow).css({"margin-left":0})})
		$(".current",slideShow).animate({"margin-left":0},timeUnit/2);
		activeSlide = 0;
	}
	
	//start the show
	autoplay();
	
}

/* Modernizr 2.0.6 (Custom Build) | MIT & BSD
 * Build: http://www.modernizr.com/download/#-backgroundsize-borderimage-boxshadow-flexbox-hsla-multiplebgs-opacity-rgba-textshadow-cssanimations-csscolumns-generatedcontent-cssgradients-cssreflections-csstransforms-csstransforms3d-csstransitions-cssclasses-teststyles-testprop-testallprops-prefixes-domprefixes
 */
;window.Modernizr=function(a,b,c){function D(a,b){var c=a.charAt(0).toUpperCase()+a.substr(1),d=(a+" "+p.join(c+" ")+c).split(" ");return C(d,b)}function C(a,b){for(var d in a)if(k[a[d]]!==c)return b=="pfx"?a[d]:!0;return!1}function B(a,b){return!!~(""+a).indexOf(b)}function A(a,b){return typeof a===b}function z(a,b){return y(o.join(a+";")+(b||""))}function y(a){k.cssText=a}var d="2.0.6",e={},f=!0,g=b.documentElement,h=b.head||b.getElementsByTagName("head")[0],i="modernizr",j=b.createElement(i),k=j.style,l,m=":)",n=Object.prototype.toString,o=" -webkit- -moz- -o- -ms- -khtml- ".split(" "),p="Webkit Moz O ms Khtml".split(" "),q={},r={},s={},t=[],u=function(a,c,d,e){var f,h,j,k=b.createElement("div");if(parseInt(d,10))while(d--)j=b.createElement("div"),j.id=e?e[d]:i+(d+1),k.appendChild(j);f=["&shy;","<style>",a,"</style>"].join(""),k.id=i,k.innerHTML+=f,g.appendChild(k),h=c(k,a),k.parentNode.removeChild(k);return!!h},v,w={}.hasOwnProperty,x;!A(w,c)&&!A(w.call,c)?x=function(a,b){return w.call(a,b)}:x=function(a,b){return b in a&&A(a.constructor.prototype[b],c)};var E=function(a,c){var d=a.join(""),f=c.length;u(d,function(a,c){var d=b.styleSheets[b.styleSheets.length-1],g=d.cssRules&&d.cssRules[0]?d.cssRules[0].cssText:d.cssText||"",h=a.childNodes,i={};while(f--)i[h[f].id]=h[f];e.csstransforms3d=i.csstransforms3d.offsetLeft===9,e.generatedcontent=i.generatedcontent.offsetHeight>=1},f,c)}([,["@media (",o.join("transform-3d),("),i,")","{#csstransforms3d{left:9px;position:absolute}}"].join(""),['#generatedcontent:after{content:"',m,'";visibility:hidden}'].join("")],[,"csstransforms3d","generatedcontent"]);q.flexbox=function(){function c(a,b,c,d){a.style.cssText=o.join(b+":"+c+";")+(d||"")}function a(a,b,c,d){b+=":",a.style.cssText=(b+o.join(c+";"+b)).slice(0,-b.length)+(d||"")}var d=b.createElement("div"),e=b.createElement("div");a(d,"display","box","width:42px;padding:0;"),c(e,"box-flex","1","width:10px;"),d.appendChild(e),g.appendChild(d);var f=e.offsetWidth===42;d.removeChild(e),g.removeChild(d);return f},q.rgba=function(){y("background-color:rgba(150,255,150,.5)");return B(k.backgroundColor,"rgba")},q.hsla=function(){y("background-color:hsla(120,40%,100%,.5)");return B(k.backgroundColor,"rgba")||B(k.backgroundColor,"hsla")},q.multiplebgs=function(){y("background:url(https://),url(https://),red url(https://)");return/(url\s*\(.*?){3}/.test(k.background)},q.backgroundsize=function(){return D("backgroundSize")},q.borderimage=function(){return D("borderImage")},q.boxshadow=function(){return D("boxShadow")},q.textshadow=function(){return b.createElement("div").style.textShadow===""},q.opacity=function(){z("opacity:.55");return/^0.55$/.test(k.opacity)},q.cssanimations=function(){return D("animationName")},q.csscolumns=function(){return D("columnCount")},q.cssgradients=function(){var a="background-image:",b="gradient(linear,left top,right bottom,from(#9f9),to(white));",c="linear-gradient(left top,#9f9, white);";y((a+o.join(b+a)+o.join(c+a)).slice(0,-a.length));return B(k.backgroundImage,"gradient")},q.cssreflections=function(){return D("boxReflect")},q.csstransforms=function(){return!!C(["transformProperty","WebkitTransform","MozTransform","OTransform","msTransform"])},q.csstransforms3d=function(){var a=!!C(["perspectiveProperty","WebkitPerspective","MozPerspective","OPerspective","msPerspective"]);a&&"webkitPerspective"in g.style&&(a=e.csstransforms3d);return a},q.csstransitions=function(){return D("transitionProperty")},q.generatedcontent=function(){return e.generatedcontent};for(var F in q)x(q,F)&&(v=F.toLowerCase(),e[v]=q[F](),t.push((e[v]?"":"no-")+v));y(""),j=l=null,e._version=d,e._prefixes=o,e._domPrefixes=p,e.testProp=function(a){return C([a])},e.testAllProps=D,e.testStyles=u,g.className=g.className.replace(/\bno-js\b/,"")+(f?" js "+t.join(" "):"");return e}(this,this.document);
		
