$(document).ready(function(){
	
	var duration = 400;
	var isLoading = false;
	var Obj = $('.rightcolumn img');
	loaded = false;
	
	
  	var newsCount = $("#showcase > div").size();
  	var newsIndex = 0;
  /***************************************************************************************
  * 									Texte
  ***************************************************************************************/
  	$("#leftArrow").click(function(){
  		if(newsIndex>0){
  			newsIndex--;
  			$("#showcase").animate({
  				left: '+=600'
  			}, 200);
  		}
  	});
  	$("#rightArrow").click(function(){
  		if (newsIndex < (newsCount-1)){
  			newsIndex++;		
  			$("#showcase").animate({
  				left: '-=600'
  			}, 200);
  		}
  		else {
  			newsIndex = 0;
  			$("#showcase").animate({
  				left: '0px'
  			},200);
  		}
  	});

	
  /***************************************************************************************
  * 									Diashow
  ***************************************************************************************/
	$(window).load(preload());
	
	function preload() {
	  for(var i = 0; i < 6; i++) {
			var pictures = "/img/";
			pictures += $('body').attr('rel');
			pictures += "bild";
			pictures += i+1;
			pictures += ".jpg";
			$.ImagePreload(pictures);
		}
	}
	$('.thumb').click(function() {
	  doChange(this);
	});
	
	function doChange(thumb) {
	  if(!isLoading) {
	    isLoading = true;
	    setTimeout(function() {
	      isLoading = false;
	    }, duration + 100);
	    
	    activateThumb(thumb);
  	  var thumburl = $(thumb).attr('src');
  	  thumburl = thumburl.replace('thumb', 'bild');
  	  changePic(thumburl);
  	  activateSubtitle(thumburl);
	  }
	}
	
	function activateThumb(thumburl) {
	  $('.thumb').removeClass('active');
	  $(thumburl).addClass('active');
	}
	
	function changePic(thumburl) {
	  Obj.ImageSwitch({
			Type:"FadeIn",
      NewImage: thumburl,
      EffectOriginal: false,
      Speed: duration
    });
	};

   function activateSubtitle(thumb) {
      thumb = thumb.substring(thumb.length-5);
      text = thumb.charAt(0);
      text = '#sub' + text;
      $('.subtitle').removeClass('visible');
      $(text).addClass('visible');
    }
});
