var glCurrentImageIndex;
var gobjCurrentThumb;
var gbCanChange;

jQuery(document).ready(function() {
	
	glCurrentImageIndex = 3;
	gbCanChange = true;
	
	$(".thumbs").jCarouselLite({
        btnNext: ".divController .next",
        btnPrev: ".divController .prev",
		mouseWheel: true,
		vertical:true,
		circular: true,
		visible: 4
    });
	
	$("#divShowcase").mouseout(function() {
		gbCanChange	= true;
	});

	$("#divShowcase").mouseover(function() {
		gbCanChange	= false;
	});

	$(".divCarousel").jCarouselLite({
        btnNext: ".divBoxType2 .next",
        btnPrev: ".divBoxType2 .prev",
		visible: 1,
		auto: 10000,
		speed: 1000,
		vertical: true
    });

	$(".thumbs img").click(function() {
		showThumbnailImage(this);
	});
	
	for (i = 0; i < $(".thumbs img").length; i++) {
		$(".thumbs img")[i].itemIndex = i;
	} // for (i = 0; i < $(".thumbs img").length; i++) {

	$(".divAccordion").accordion({
		header: ".h3Header",
		autoheight: false
	});
	
	setTimeout("showNextImage();", 10000);
});

function showThumbnailImage(elElement) {
	
	$(".divMid img").attr("title", $(elElement).attr("title"));
	$(".divMid img").attr("alt", $(elElement).attr("alt"));
   	$(".divMid img").attr("src", $(elElement).attr("src"));
	$(".divMid a").attr("href", $(elElement).attr("longdesc"));

	var ul = $("ul", $(".thumbs"));
	var li = $("li", ul);
	var itemLength = li.size();
	var liSize = li[0].offsetHeight + (parseInt($.css(li[0], 'marginTop')) || 0) + (parseInt($.css(li[0], 'marginBottom')) || 0);
	var img;

	if (itemLength > 4) {

		glCurrentImageIndex = elElement.itemIndex;
	
		gobjCurrentThumb = $(elElement);
	
		var lCurrentIndex = gobjCurrentThumb.itemIndex;
	
		if (lCurrentIndex > (itemLength - 8)) {
			lCurrentIndex -= (itemLength - 8);
		} // if (lCurrentIndex > (itemLength - 8)) {
	
		if(glCurrentImageIndex <= 3) {           // If first, then goto last
			glCurrentImageIndex += (itemLength - 8);
			ul.css("top", -((glCurrentImageIndex - 1) * liSize)+"px");
		} else if(glCurrentImageIndex >= itemLength - 3) { // If last, then goto first
			glCurrentImageIndex -= (itemLength - 8);
			ul.css("top", -((glCurrentImageIndex - 1) * liSize)+"px");
		} // if(glCurrentImageIndex <= 3) {
		
		for (i = 0; i < itemLength; i++) {
			img = $(".thumbs img")[i];
			$(img).css("opacity", "0.5");
		} // 	for (i = 0; i < itemLength; i++) {
		
		// Onceki elemanlari goster
		lCurrentIndex = glCurrentImageIndex;
		
		while ((lCurrentIndex) > 0) {
			img = $(".thumbs img")[lCurrentIndex];
			$(img).css("opacity", "1.0");
			lCurrentIndex -= (itemLength - 8);
		} // while ((lCurrentIndex - 4) > 0)) {
	
		// Sonraki elemanlari goster
		lCurrentIndex = glCurrentImageIndex;
		
		while ((lCurrentIndex) < itemLength) {
			img = $(".thumbs img")[lCurrentIndex];
			$(img).css("opacity", "1.0");
			lCurrentIndex += (itemLength - 8);
		} // while ((lCurrentIndex - 4) > 0)) {
		
		ul.animate(
			{ top: -(glCurrentImageIndex * liSize) },
			200,
			null,
			null
		);
	} // if (itemLength > 4) {

	$(".divMid .imgcontainer .caption").fadeOut("fast", function () {
		if ($(".divMid .imgcontainer .caption h3").length > 0) {
			$(".divMid .imgcontainer .caption h3")[0].innerHTML = $(elElement).attr("title");
			$(".divMid .imgcontainer .caption p")[0].innerHTML = $(elElement).attr("alt");
		} // if ($(".divMid .imgcontainer .caption h3").length > 0) {																
	});

	$(".divMid .imgcontainer .caption").fadeIn(1000);
}

function showNextImage() {
	if (gbCanChange) {
		glCurrentImageIndex++;
		glCurrentImageIndex = (glCurrentImageIndex % $(".thumbs img").length);
		
		showThumbnailImage($(".thumbs img")[glCurrentImageIndex]);
	} // if (gbCanChange) {

	setTimeout("showNextImage();", 10000);
}