//Auto scroll functions
var scroller = null;

function startScroll(obj, n) {
	
	if(scroller != null)
		stopScroll();
	
	if(n > 0)	
		scroller = setInterval(function() { scrollToRight(obj) }, 100)
	else
		scroller = setInterval(function() { scrollToLeft(obj) }, 100)
}

function stopScroll() {
	clearInterval(scroller);
	scroller = null;
}

function scrollToLeft(obj) {
	obj.scrollLeft -= 10
}

function scrollToRight(obj) {
	obj.scrollLeft += 10
}

function autoWidth(obj) {
	
	//Enlarge list to contain all images
	var links = obj.getElementsByTagName('img');
	
	if(links.length == 0)
		return false;
	
	var w = links.length * (120);
	w -= 13;
	
	/*for(i = 0; i < links.length; i++)		
		w += Element.extend(links[i]).width + 19;*/
	
	var list = obj.getElementsByTagName('ul');
	var finalW = (w+3)+'px';
	
	if(list.length > 0) {
		$(list[0]).css('width', finalW);
	}
	
}

//Set everything up
$(document).ready(function() {

	if($('.photoslide').size() > 0) {

		$('.photoslide').each(function(k, img) {
			
			//Auto adjust container width
			autoWidth(img)
			$(img).children(':first').css('opacity', 0.4);
			$(img).prev().css('opacity', 0.4);
			
			
		})
		
		$('.imgselectorContainer').each(function(k, div) {
			
			$(div).bind('mouseover', function() {
				$(this).children(':first').next().css('opacity', 1)			
				$(this).children(':first').next().next().children(':first').css('opacity', 1)			
			})
			
			$(div).bind('mouseout', function() {
				$(this).children(':first').next().css('opacity', 0.4)			
				$(this).children(':first').next().next().children(':first').css('opacity', 0.4)			
			})
			
		})
		
		$('.imgselector a').each(function(k, a) {
			
			var link = a.href;
			$(a).parent().bind('click', function() {
				location.href = link;
			})
				
		})
		
		$('.prev').each(function(k, a) {
	
			$(a).bind('mouseover', function() {
				startScroll($(this).parent().next().children(':first').get(0), -1);
			})
			$(a).bind('mouseout', function() {
				stopScroll();
			})
			
		})
		
		$('.next').each(function(k, a) {
			
			$(a).bind('mouseover', function() {
				startScroll($(this).parent().next().children(':first').get(0), 1);
			})
			$(a).bind('mouseout', function() {
				stopScroll();
			})
			
		})
		
	}	

});
