
var firstIndex = 0;
var secondIndex = 1;
var lastIndex = 2;
var teaserInterval = 600;
var firstTeaserPosition = -teaserInterval * (firstIndex);
var secondTeaserPosition = -teaserInterval * (secondIndex);
var lastTeaserPosition = -teaserInterval * (lastIndex);



$(document).ready(function() {
    function getCurrentTeaserPosition() {
	var currentPositionString = $("#teaser-items").css("left");
	return parseInt(currentPositionString.substring(0, currentPositionString.indexOf('px')));
    }
    function getCurrentTeaserSection(val) {
	switch (val){
	case firstTeaserPosition:
		return 'first';
	case secondTeaserPosition:
		return 'second';
	case lastTeaserPosition:
		return 'last';
	}
    }

    function setArrowIndicator(currentSection) {
	switch (currentSection){
	case 'first':
		$("#bg-first").addClass('selected-teaser');
		$("#bg-second").removeClass('selected-teaser');
		$("#bg-last").removeClass('selected-teaser');

		$("#li-first").removeClass('non-selected');
		$("#li-second").addClass('non-selected');
		$("#li-last").addClass('non-selected');
	
		$("#view-first").removeClass('hide');
		$("#view-second").addClass('hide');
		$("#view-last").addClass('hide');
		break;
	case 'second':
		$("#bg-second").addClass('selected-teaser');
		$("#bg-first").removeClass('selected-teaser');
		$("#bg-last").removeClass('selected-teaser');

		$("#li-second").removeClass('non-selected');
		$("#li-first").addClass('non-selected');
		$("#li-last").addClass('non-selected');

		$("#view-second").removeClass('hide');
		$("#view-first").addClass('hide');
		$("#view-last").addClass('hide');
		break;
	case 'last':
		$("#bg-last").addClass('selected-teaser');
		$("#bg-first").removeClass('selected-teaser');
		$("#bg-second").removeClass('selected-teaser');
	
		$("#li-last").removeClass('non-selected');
		$("#li-first").addClass('non-selected');
		$("#li-second").addClass('non-selected');

		$("#view-last").removeClass('hide');
		$("#view-first").addClass('hide');
		$("#view-second").addClass('hide');
		break;
	}

    }
    function autoSlideShowNext() {
	var currentPosition = getCurrentTeaserPosition();
//	console.log("cP: "+currentPosition+" fP "+firstTeaserPosition+" lP "+lastTeaserPosition);
	
	if(currentPosition == lastTeaserPosition){
		return;
	} else if (currentPosition != firstTeaserPosition && currentPosition != secondTeaserPosition && currentPosition != lastTeaserPosition) {
		return;
	}else {
	
		$("#prev").addClass('active');
		$("#teaser-items").animate({
         	       left: currentPosition - teaserInterval +"px"
           	     }, 500, function(){
				var currentSection = getCurrentTeaserSection(getCurrentTeaserPosition());
				setArrowIndicator(currentSection);	
				if((getCurrentTeaserPosition()) == (lastTeaserPosition)) {
					$("#next").removeClass('active');
				}
		} );
	}
    }
    
    function autoSwitchToFirst() {
	setArrowIndicator('first');
	$("#teaser-items").animate({
                left: "-" + firstIndex*teaserInterval +"px"
                }, 500, function(){
				$("#prev").removeClass('active');
				$("#next").addClass('active');
		} );
    }
 
    $("#autoslideshow").click( function() {
	autoSlideShowNext();
    }); 


    $("#next").click( function() {
	clearInterval(autoslideShowId);
	autoSlideShowNext();
    }); 

   $("#prev").click( function() {
	clearInterval(autoslideShowId);
	var currentPosition = getCurrentTeaserPosition();
	//console.log("cP: "+currentPosition+" fP "+firstTeaserPosition+" lP "+lastTeaserPosition);

	if(currentPosition == firstTeaserPosition){
		return;
	} else if (currentPosition != firstTeaserPosition && currentPosition != secondTeaserPosition && currentPosition != lastTeaserPosition) {
		return;
	} else {	
	
		$("#next").addClass('active');
		$("#teaser-items").animate({
        	        left: currentPosition + teaserInterval +"px"
       	         }, 500, function(){
				var currentSection = getCurrentTeaserSection(getCurrentTeaserPosition());
				setArrowIndicator(currentSection);	
				if((getCurrentTeaserPosition()) == (firstTeaserPosition)) {
					$("#prev").removeClass('active');
				}
		} );
	}
	
    }); 

  $("#autoswitchtofirst").click( function() {
	autoSwitchToFirst();
	
    });

  $("#teaser-first").click( function() {
	clearInterval(autoslideShowId);
	autoSwitchToFirst();
	
    });
  $("#teaser-second").click( function() {
	clearInterval(autoslideShowId);
	setArrowIndicator('second');
	$("#teaser-items").animate({
                left: "-" + (firstIndex+1)*teaserInterval +"px"
                }, 500, function(){
				$("#prev").addClass('active');
				$("#next").addClass('active');
		} );
	
	
    }); 
  $("#teaser-last").click( function() {
	clearInterval(autoslideShowId);
	setArrowIndicator('last');
	$("#teaser-items").animate({
                left: "-" + lastIndex*teaserInterval +"px"
                }, 500, function(){
				$("#prev").addClass('active');
				$("#next").removeClass('active');
		} );	
	
    }); 

});
