// JavaScript Document
function Slideshow(scope, containerDiv, isHome){
	//alert("Scope " + scope);
	var container = $(scope).find(containerDiv);

	var _currentIndex = 0;
	var _containerWidth = container.width();
	var _numberOfImages = $(container).find("ul li").length;
	var _intervalID = null;
	var _autoPlayRunning = false;
	var _slideshowSpeed = 10000;
	
	$(scope).find(containerDiv+" ul").css("width", _numberOfImages*_containerWidth);

	_currentIndex = 1;
	
	var last = $(scope).find(containerDiv+" ul li:last").clone();
	var first = $(scope).find(containerDiv+" ul li:first").clone();
	
	$(scope).find(containerDiv+" ul").prepend(last);
	$(scope).find(containerDiv+" ul").append(first);

	$(scope).find(containerDiv+" ul").css("width", _numberOfImages*_containerWidth+(_containerWidth*2));
	$(scope).find(containerDiv+" ul").css('left', "-"+_containerWidth+"px");

	if(isHome){
		
		container.width($(window).width());
		container.height($(window).height());
		$(window).bind('resize', resize_background);
		$(window).bind('blur', stopAutoPlay);
		$(window).bind('focus', startAutoPlay);
		resize_background();

		$(".arrow.left.home").click(function(){
			stopAutoPlay();
			if(_currentIndex == 1){
				moveToResetLeft(--_currentIndex);
			}else{
				moveTo(--_currentIndex);
			}
			startAutoPlay();
		});
		
		$(".arrow.right.home").click(function(){
			stopAutoPlay();
			if(_currentIndex == _numberOfImages){ 
				moveToResetRight(++_currentIndex);
			}else{
				moveTo(++_currentIndex);
			}
			startAutoPlay();		
		});
		
		//startAutoPlay();
		
	}else{
		$(scope).find(".arrow.left").click(function(){
			if(_currentIndex == 1){
				moveToResetLeft(--_currentIndex);
			}else{
				moveTo(--_currentIndex);
			}
			
		});
		
		$(scope).find(".arrow.right").click(function(){
			if(_currentIndex == _numberOfImages){
				moveToResetRight(++_currentIndex);
			}else{
				moveTo(++_currentIndex);
			}
			
			
		});	
	}

	
	$(scope).find(".thumb-item").click(function(){
		//alert("thumb "+$(this).attr('id') +" click");
		
		//$(this).addClass("active");
		_currentIndex = $(this).attr('id')-0;
		
		moveTo(_currentIndex);
	});
	
	
	function moveTo(index){
		var position = -(index*_containerWidth);
		$(scope).find(containerDiv+" ul").animate({ left: position }, 500);
		updateInfo(_currentIndex);
		highlightThumb(index);
		if(isHome){
			setArrow();
		}
	}
	
	function moveToResetRight(index){
		var position = -(index*_containerWidth);
		$(scope).find(containerDiv+" ul").animate({ left: position }, 500,'easeOutQuad', function(){$(scope).find(containerDiv+" ul").css('left','-'+_containerWidth+'px'); });		
		_currentIndex=1;
		highlightThumb(_currentIndex);
		updateInfo(_currentIndex);
		if(isHome){
			setArrow();
		}
	}
	
	function moveToResetLeft(index){
		var position = 0;
		var onCompletePosition = _numberOfImages*_containerWidth;
		$(scope).find(containerDiv+" ul").animate({ left: position }, 500,'easeOutQuad', function(){$(scope).find(containerDiv+" ul").css('left','-'+onCompletePosition+'px'); });		
		_currentIndex=_numberOfImages;
		highlightThumb(_currentIndex);
		updateInfo(_currentIndex);
		if(isHome){
			setArrow();
		}
	}
	
	function updateInfo(index){
		var title = $(scope).find("ul li:eq("+index+")").find(".slideshow-title").text();
		var legend = $(scope).find("ul li:eq("+index+")").find(".slideshow-legend").text();
		$(scope).find("#slideshow-info-title").text(title);
		$(scope).find("#slideshow-info-legend").text(legend);
	}
	
	function highlightThumb(index){
		$(scope).find(".thumb-item").removeClass('active');
		$(scope).find("#thumb-list li:eq("+(index-1)+") a").addClass('active');
	}
	
	function setArrow(){
		var img = $(scope).find("ul li:eq("+_currentIndex+") img");
		var arrows = $('#home-arrow-container a');
		arrows.removeClass('light');
		arrows.removeClass('dark');
		arrows.addClass(img.attr('rel'));
	}
	
	function autoplay(){
		//alert('autoplay');
		if(_currentIndex == _numberOfImages){ 
			moveToResetRight(++_currentIndex);
		}else{
			moveTo(++_currentIndex);
		}
	}
	
	function startAutoPlay(){
		if(_autoPlayRunning == true) return;
		_intervalID = setInterval(function() { autoplay();	}, _slideshowSpeed);
		_autoPlayRunning = true;
	}
	
	function stopAutoPlay(){
		clearInterval(_intervalID);
		_intervalID = null;
		_autoPlayRunning = false;
	}
	
	function on_blur(){
		stopAutoPlay();
	}
	
	function resize_background()
	{   
			stopAutoPlay();
	
			var bg = $('#background img');
			
			var ratio = 8/5;
			bg.width($(window).width());
			bg.height(($(window).width() / ratio));
			
			if (bg.height() < $(window).height()) {
				bg.height($(window).height());
				bg.width($(window).height() * ratio);
			}
			
			container.width(bg.width());
			container.height(bg.height());
			_containerWidth = container.width();
			$(scope).find(containerDiv+" ul").css("width", _numberOfImages*_containerWidth+(_containerWidth*2));
			var position = (_currentIndex*_containerWidth);
			
			$(scope).find(containerDiv+" ul").css('left', "-"+position+"px");	
			
					
			startAutoPlay();
			
	}
		
}
