// JavaScript Document
var timer = '';
var delay = 7000; // you can change it
var count = 2; // How much items to animate
var showing = 1; //How much items to show at a time
var i = 0;
function move(i) {
  return function() {
	$('#tickerItem'+i).remove().css('display', 'none').prependTo('#tickerTearWrap');
  }
}
function shift() {
  var toShow = (i + showing) % count;
  $('#tickerItem'+toShow).stop();
  $('#tickerItem'+toShow).css({'left':0});
  $('#tickerItem'+toShow).show('fast', function(){
	  move(i);
	  var contWidth = $('#tickerTearWrap').width();
	  var itemWidth = $('#tickerItem'+toShow).width();
	  var difWidth = contWidth - itemWidth;
	  var difInc = difWidth + contWidth;
	  
	  $('#tickerItem'+toShow).animate({'left':difWidth+'px'}, 5000, function(){
		  timer = setTimeout('shift()', delay);
	  });
  });
  $('#tickerItem'+i).slideUp(0, move(i));
  i = (i + 1) % count;
}    

function showLocInfo(id){
  var toShow = id;
  clearTimeout(timer);
  $('#tickerItem'+toShow).stop();
  $('.tickerItem').css({'display':'none'});
  $('#tickerItem'+toShow).css({'left':0});
  $('#tickerItem'+toShow).show('fast', function(){
	  move(i);
	  var contWidth = $('#tickerTearWrap').width();
	  var itemWidth = $('#tickerItem'+toShow).width();
	  var difWidth = contWidth - itemWidth;
	  var difInc = difWidth + contWidth;
	  
	  $('#tickerItem'+toShow).animate({'left':difWidth+'px'}, 5000, function(){
	  });
  });
}

function mycarousel_initCallback(carousel) {
	jQuery('#scrollRight').bind('click', function() {
		carousel.next();
		return false;
	});
	
	jQuery('#scrollLeft').bind('click', function() {
		carousel.prev();
		return false;
	});
}

function showSpecials(id){
	$('.locBlockFloat a').css({'background':'none'});
	$('a#specialsLink'+id).css({'background':'url(../images/layout/hoverind.png) 0 3px no-repeat'});
	$('.specialsMenuOP').css({'display':'none'});
	$('#specialsMenu'+id).css({'display':'block'});
}

function sliderReset(){
	var contWidth = $('#tickerTearWrap').width();
	var itemWidth = $('#tickerItem0').width();
	var difWidth = contWidth - itemWidth;
	var difInc = difWidth + contWidth;
	$('#tickerItem0').stop();
	$('#tickerItem0').css({'left':0});
	$('#tickerItem0').animate({'left':difWidth+'px'}, 5000);
	setTimeout('sliderReset()', delay);
}

$(function() {
	count = $('.tickerItem').size();
	// This initialises carousels on the container elements specified, in this case, carousel1.
	//timer = setTimeout('shift()', delay);
	$('#tickerItem0').show();
	setTimeout('shift()', delay);
	
	$('#chalkBoardBlock').jScrollPane({showArrows:true, scrollbarWidth: 12});

	$('#slider').nivoSlider({
		pauseTime:7000, // How long each slide will show
		animSpeed:750,
		effect:'fade',
		manualAdvance:false
	});

	$('#carouselWrap img').css({'opacity':'0'});
	$('#carouselWrap').css({'background':'url(/images/load_animation.gif) center no-repeat'});
	$('#carouselWrap img').imgpreload(function()
	{
		$('#carouselWrap').css({'background':'none'});
		$('#carouselWrap img').css({'opacity':'1'});
		
		// load carousels
		$("#carouselWrap").jcarousel({
			initCallback: mycarousel_initCallback,
			btnNext: "#scrollRight",
			btnPrev: "#scrollLeft",
			visible: 6,
			auto: 1,
			animation: 3000,
			wrap: 'last',
			mouseWheel: true
		});
	});

	$("a.enlarge").fancybox();

});
