// JavaScript Document
$(document).ready(function() {

	$(window).load(animateScrollMenu);
	
    $("a[class=scroll-left]").click(scrollLeft);
	$("a[class=scroll-right]").click(scrollRight);
	$("#scrollMenu ul li a").click(picClick);
	
});
$userClick = false;
$clickCounter = 0;
$animateScroll = true;
$allowScrollClick = true;
$moving = false;
function manageClickCounter(degree) {
	if (degree == 'add') {
	   $clickCounter++;
	} else {
		$clickCounter--;
	}
}

function scrollRight() {
	//event.stopPropagation();
	if ($animateScroll == true) {
		$animateScroll = false;
		$("#scrollMenu").children().first().clearQueue();
	}
	manageClickCounter('add');
	
	if ($("#scrollMenu").children().first().css('left') == '10px') { 
	   animateWheel();
	}
	return false;
}

function scrollLeft() {
	//event.stopPropagation();
	if ($animateScroll == true) {
		$animateScroll = false;
		
	}
	manageClickCounter('subtract');
	if ($("#scrollMenu").children().first().css('left') == '10px') { 
	   $("#scrollMenu").children().first().stop();
	   animateWheel();
	}
    return false;
}

function animateScrollMenu() {
	if($animateScroll == true) {
	   //animateRightAuto();
	   setTimeout("animateRightAuto()", 2700);
	// setInterval( "animateRightAuto()", 2700 );
	}
}

function animateRightAuto() {
	if($animateScroll == true) {
	   manageClickCounter('add');
	   $("#scrollMenu").children().first().animate({left: "-=123px"},700,shiftMenu);
	} 	
}

function animateWheel() {
	
	if ($clickCounter > 0) { 
	      $moving = true;
		  
	      //$("#scrollMenu").children().first().clearQueue();
          $("#scrollMenu").children().first().animate({left: "-=123px"},700,shiftMenu);
	  } else if ($clickCounter < 0) {
		  $moving = true;
		  
		  var lastChild = $("#scrollMenu").children().first().children().last().clone();
	      $("#scrollMenu").children().first().children().first().before($(lastChild));
		  $("#scrollMenu").children().first().children().first().children().first().click(picClick);
		  $("#scrollMenu").children().first().css('left','-113px');
		  $("#scrollMenu").children().first().children().last().remove();
	      $("#scrollMenu").children().first().animate({left: "+=123px"},700,shiftMenuBackFinish);
	  }  else if ($clickCounter == 0 ) {
		  
	  }
}


function shiftMenu() {
	var firstChild = $("#scrollMenu").children().first().children().first().clone();
	$("#scrollMenu").children().first().children().last().after($(firstChild));
	$("#scrollMenu").children().first().children().last().children().first().click(picClick);
	$("#scrollMenu").children().first().css('left','10px');
	$("#scrollMenu").children().first().children().first().remove();
	
	manageClickCounter('subtract');
	if($userClick == false) {
	slideSwitch('next');
	}
	if ($animateScroll == false) {
	   animateWheel();
	} else {
		$("#scrollMenu").children().first().animate({opacity: 1.0},4700, animateRightAuto);
	}
}
function shiftMenuBackFinish() {
	manageClickCounter('add');
	if($userClick == false) {
	slideSwitch('prev');
	}
	animateWheel();
}
function slideSwitch(direction) {
    var $active = $('#slideshow DIV.active');
 
    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
 
    // use this to pull the divs in the order they appear in the markup
	//alert($('#slideshow DIV:last').html());
    if ( direction == 'prev' ) {
		
	    var $next =  $active.prev().length ? $active.prev()
	    : $('#slideshow DIV.slide-frame:last');
	} else {
		var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV.slide-frame:first');
	}
	//var $sibs  = $active.siblings();
	//var $next  = $( $sibs[ $sibs.length + 1 ] );
    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );
 
 
    $active.addClass('last-active');
   $next.children().first().children().first().hide();
   
    $next.css({opacity: 0.0})
        .addClass('active')
		.animate({opacity: 1.0}, 700, function() {
            $active.removeClass('active last-active');
			if ($clickCounter == 0) {
			$("#slideshow DIV.active .caption-cell").css('bottom', -($("#slideshow DIV.active .caption-cell").height() +40) );
			$("#slideshow DIV.active .caption-cell")
			.delay(300)
			.show()
			.animate({bottom: 0}, 500);
			}
        });
}
function picClick() {
	$animateScroll = false;
	$userClick = true;
	var thumbId = "image" + $(this).children().first().attr("class").match(/\d+/);
    var $active = $('#slideshow DIV.active');
	
	//var $next  = $('#slideshow');
	//alert(thumbId);
	var $next  = $('.slide-frame div[class*="' + thumbId + '"]').parent();
	if ($next.attr("class") != $active.attr("class")) {
	$active.addClass('last-active');
    $next.children().first().children().first().hide();
	//$("#slideshow DIV.active .caption-cell").css('bottom', -($("#slideshow DIV.active .caption-cell").height() +40) );
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 700, function() {
            $active.removeClass('active last-active');
			if ($clickCounter == 0) {
			$("#slideshow DIV.active .caption-cell").css('bottom', -($("#slideshow DIV.active .caption-cell").height() +40) );
			$("#slideshow DIV.active .caption-cell")
			.delay(300)
			.show()
			.animate({bottom: 0}, 500);
			}
        });
}
		return false;
}
//$(function() {
//    setInterval( "slideSwitch()", 5000 );
//});  
