var total = 0;
//var countTime = 3000;
var t;

$(document).ready(function () {
  var banner = new Array();
  var overlay = new Array();

  if (!$("#thumbnail-holder ul li img").length)
    return;

  $("#thumbnail-holder div.thumb-overlay").each(function (index, value) {
    overlay[index] = value;
    $(this).attr('id', 'banner-overlay-' + index);
    if (index == 0) {
      $(this).attr('style', 'display:block');
    } else {
      $(this).attr('style', 'display:none');
    }
  });

  $("#thumbnail-holder ul li img").each(function (index, value) {
    banner[index] = value;
    $(this).attr('id', 'banner-' + index);
    if (index == 0) {
      $(this).attr('class', 'active');
      var link = $(this).parent().attr('href');
      var path = $(this).attr('src');
      var changed = $(this).parent().attr('rel');
      $("#view a.display").attr('href', link || 'javascript:;');
      $("#view a.display")[link ? 'removeClass' : 'addClass']('no-link')
      $("#view img.display").attr('src', changed);
      $("#view img.display").fadeIn("slow");
    } else {
      $(this).attr('class', 'inactive');
    }
    total = total + 1;
  });

  $("#thumbnail-holder ul li img").click(function () {
    var now = parseInt(checkActive().substring(7));
    var overlay = now;
    if (now >= (total - 1)) {
      now = -1;
      overlay = 3;
    }
    $("#banner-overlay-" + overlay).attr("style", "display:none;");
    var next = parseInt($(this).attr('id').substring(7));

    $("#banner-overlay-" + next).attr("style", "display:block;");
    resetImg();
    $(this).attr('class', 'active')
    var link = $(this).parent().parent().attr('href');
    var changed = $(this).parent().parent().attr('rel');
    $("#view img.temp").attr('src', $("#view img.display").attr('src'));
    $("#view a.display").attr('href', link || 'javascript:;');
    $("#view a.display")[link ? 'removeClass' : 'addClass']('no-link');
    $("#view img.display").attr('src', changed);
    $("#view img.temp").show().fadeOut("slow");
    $("#view img.display").fadeIn("slow");

    clearTimeout(t);
    t = setTimeout("bannerUpdate()", countTime);
    return false;
  });

  t = setTimeout("bannerUpdate()", countTime);

  $.easing.backout = function (x, t, b, c, d) {
    var s = 1.70158;
    return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  };

  $('#thumbnail-holder').scrollShow({
    view: '#holder',
    content: '#thumb',
    easing: 'backout',
    wrappers: 'crop',
    navigators: 'a[id]',
    navigationMode: 's',
    circular: true,
    start: 0
  });
});

function resetImg() {
	$("#thumbnail-holder ul li img").each(function() {
		$(this).attr('class', 'inactive');
	});
}

function checkActive() {
	return $("#thumbnail-holder ul li img.active").attr('id');
}

function bannerUpdate() {

	var now = parseInt(checkActive().substring(7));
	var overlay = now;
	if (now >= (total-1)) {
		now = -1;
		overlay = 3;
	}
	$("#banner-overlay-"+overlay).attr("style", "display:none;");
	$("#banner-overlay-"+(now+1)).attr("style", "display:block;");
	var next = "banner-" + (now + 1);
	var link = $("#"+next).parent().parent().attr('href');
	var changed = $("#"+next).parent().parent().attr('rel');
	resetImg();
	$("#"+next).attr('class', 'active');
	
	$("#view img.temp").attr('src', $("#view img.display").attr('src'));
	$("#view a.display").attr('href', link || 'javascript:;');
	$("#view a.display")[link ? 'removeClass' : 'addClass']('no-link')
	$("#view img.display").attr('src', changed);
	$("#view img.temp").show().fadeOut("slow");
	$("#view img.display").fadeIn("slow");
	
	$("#banner-overlay-"+now).attr("style", "display:none;");
	$("#banner-overlay-" + now + 1).attr("style", "display:block;");

	if (now < 0) $('#thumbnail-holder a[id=left]:visible').click();
	else if (now == 3) $('#thumbnail-holder a[id=right]:visible').click();
	
	t = setTimeout("bannerUpdate()", countTime);
}
