var timer;
var autoDelay = 3500;
var fadeDelay = 500;
$(document).ready(function() {
  $('.scrollableArea').width(($('.scrollableArea img').length)*168+2);
  if($('#admintoolbox').size() == 0){
	$('#slider').smoothDivScroll({ 
	  autoScroll: "onstart" , 
	  autoScrollDirection: "left",
	  autoScrollStep: 2, 
	  autoScrollInterval: 50,	
	  startAtElementId: "startAtMe", 
	  visibleHotSpots: "always"
	});
  }
  $('#slider .sliderCont img').click(function(){
	var newUrl = $(this).attr('src').replace('small/','');
	var newTxt = $(this).parent().parent().find('.text').text();
	$(this).parent().parent().parent().find('.activ').removeClass('activ');
	$(this).addClass('activ');
	$('#headimg img').attr('src', newUrl);
	if(newTxt != ''){
	  $('#headTxt').html(newTxt).show();
	}else 
	  $('#headTxt').hide();
  });
	
  $('#contentLeft').click(function() {
	timer = 0;
	prevItem($(this).parent().parent());
  });
  $('#contentRight').click(function() {
	timer = 0;
	nextItem($(this).parent().parent());
  });

  $('img.preview').click(function () {
	var _new = $(this).attr("src");
	var _old = $("#detailImage").attr("src");

	$("#detailImage").attr("src",_new).stop(true,true).hide().fadeIn();
	$(this).attr("src",_old);
  });

  $('.slider .button').click(function () {
	var goTo = $(this).attr('id').substr(4);
	goToItem(goTo);
  });
	
	
  if($('#admintoolbox').size() == 0 && $('.ContentSlider').size() > 0){
	timer=setTimeout("nextItem()",autoDelay);  
  }
  
//  $(".gallery").lightBox();
	
});

function goToItem(index){
  var next = $('#content'+index);
  var contentSlider = next.parent();
  var active = contentSlider.find('.contents.active');
  var activeButton = contentSlider.find('.button.active');

  active.removeClass('active').fadeOut(fadeDelay,function(){
	activeButton.removeClass('active');
	next.addClass('active').fadeIn(fadeDelay);
	$('#goto'+index).addClass('active');
	contentSlider.find('.active');	
  });
}

function nextItem(contentSlider){
  if(timer > 0)
	timer=setTimeout("nextItem()",autoDelay+2*fadeDelay); 
  contentSlider = contentSlider || $('.ContentSlider');
  var currentId = contentSlider.find('.contents.active').attr('id').substr(7);
  var anz = contentSlider.find('.contents').size();
  var newId = 1;
  if(currentId < anz) newId = parseInt(currentId)+1;
  goToItem(newId);


  if($('#admintoolbox').size() > 0){
	initpage();
  }
}
function prevItem(contentSlider){
  var currentId = contentSlider.find('.contents.active').attr('id').substr(7);
  var newId = currentId-1;
  if(newId == 0){
	newId = contentSlider.find('.contents').size();
  }
  goToItem(newId);

  if($('#admintoolbox').size() > 0){
	initpage();
  }
}
