
$(document).ready(function() {
	$('.embedbutton').click(function() {
		$('.embedcode').animate({height:'163px'}, 500, function() {
	  });
	});
	$('.embedclose').click(function() {
		$('.embedcode').animate({height:'1px'}, 500, function() {
	  });
	});
});


//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()})
	//$(".embedbutton").each(function () { $(this).initEmbedcode()})
});


jQuery.fn.initSlideShow = function() {
	
	var slideShow = $(this);
	var slideWidth = slideShow.width();
	
	//calculate canvas width
	var totalslides= $(".rssItem",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 ----------

	$(".rssItem",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") {		
		$("rss", slideShow).append($(".rssItem: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 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) {
		$("#rss",slideShow).animate({"margin-left":-slideWidth*pos},speed/2);
		$(".current",slideShow).animate({"margin-left":pos*thumbWidth},timeUnit/2);
		activeSlide = pos;
	}
	function loopslide() {
		$("#rss",slideShow).animate({"margin-left":-slideWidth*(activeSlide+1)},speed/2,function(){$("#rss",slideShow).css({"margin-left":0})})
		$(".current",slideShow).animate({"margin-left":0},timeUnit/2);
		activeSlide = 0;
	}
	
	//start the show
	autoplay();	
}



		
