// carousel.js

$('document').ready(function()
{
	var carousel = { children: new Array(), leftHover: false, rightHover: false, current: null };
	var $carousel = null;

	if ($('table#carousel-table')) { carouselInit(carousel); }
	
	function carouselInit()
	{
		var children = $('#carousel-table td');

		for (var i = 0; i < children.length; i++) {
			var child = {
				image: $(children[i]).children('img').attr('src'),
				label: $(children[i]).children('p:first').text(),
				links: new Array()
			};
			var link1 = $(children[i]).children('p:eq(1)').children('a:eq(0)');
			var link2 = $(children[i]).children('p:eq(2)').children('a:eq(0)');
			if (link1.length) {
				child.links.push({ href: link1.attr('href'), label: link1.text() });
			}
			if (link2.length) {
				child.links.push({ href: link2.attr('href'), label: link2.text() });
			}
			carousel.children.push(child);
		}
		
		$('table#carousel-table').replaceWith("");
		
		if (carousel.children.length) carouselBuild();
	}
	
	function carouselBuild()
	{
		$carousel = $('div#carousel');
		carousel.ul = $('ul#carousel-children');
		carousel.leftmask = $('div#carousel-mask-left');
		carousel.rightmask = $('div#carousel-mask-right');
		carousel.show = $('div#carousel-show');
		carousel.count = carousel.children.length;
		
		var html = '';
		for (var i = 0; i < carousel.count; i++) {
			var child = carousel.children[i];
			carousel.children[i].id = 'cc_'+i;
			html += '<li id="cc_' + i + '" style="left: ' + i*180 + 'px">';
			html += '<img id="cci_' + i + '" src="' + child.image + '" width="160" height="130" />';
			html += '<div class="label">' + child.label + '</div>';
			html += '</li>';
		}

		carousel.ul.html(html);
			
		for (var i = 0; i < carousel.count; i++) {
			carousel.children[i].handle = $('li#cc_'+i);
			carousel.children[i].imgHandle = $('img#cci_'+i);
			Reflection.add(document.getElementById('cci_' + i), { height: 0.5, opacity: 0.25 });
			carousel.children[i].canvasHandle = $('li#cc_'+i+' canvas');

			carousel.children[i].handle.bind('click', function(e) {
				var id = $(this).attr('id');
				clickCarouselItem(id.substr(3));
			});
			carousel.children[i].handle.bind('mouseenter', function(e) {
				var id = $(this).attr('id'); id = id.substr(3);
				carousel.children[id].imgHandle.stop();
				carousel.children[id].canvasHandle.stop();
				carousel.children[id].imgHandle.animate({ top: "-15px" }, 150);
				carousel.children[id].canvasHandle.fadeTo(150, 0);
			});
			carousel.children[i].handle.bind('mouseleave', function(e) {
				var id = $(this).attr('id'); id = id.substr(3);
				carousel.children[id].imgHandle.stop();
				carousel.children[id].canvasHandle.stop();
				carousel.children[id].imgHandle.animate({ top: "0px" }, 150);
				carousel.children[id].canvasHandle.fadeTo(150, 1);
			});
		}
		
		carousel.show.hide();
		
		$('div#carousel-show .back').live('click', function() {
			carousel.current = null;
			carousel.show.fadeOut(250, 0, function(){
				carousel.show.css('zIndex', '0');
			});
		});
		
		function clickCarouselItem(id)
		{
			var child = carousel.children[id];
			carousel.current = id;

			if (!child.links.length) return;
			if (child.links.length == 1) {
				document.location.href = child.links[0].href;
			} else {
				var html = '<div class="back">Back</div>';
				html += '<img src="' + child.image + '" width="160" height="130" />';
				html += '<ul>';
				html += '<li><a href="' + child.links[0].href + '">' + child.links[0].label + '</a></li>';
				html += '<li><a href="' + child.links[1].href + '">' + child.links[1].label + '</a></li>';
				html += '</ul>';
				html += '<div class="label">' + child.label + '</div>';
				
				carousel.show.fadeIn(150);
				carousel.show.children('.all').html(html);
				carousel.show.children('.all').fadeIn(150);
				carousel.show.children('.background').fadeTo(150, 0.9);
				carousel.show.css('zIndex', '50');
			}
			
		};	
		
		carousel.speed = 10;
		carousel.minSpeed = 10;
		carousel.maxSpeed = 30;
		carousel.acceleration = 2;

		carousel.width = ((160 + 20) * carousel.count) - 20;
		carousel.leftOffset = 0-((carousel.width) - 940);
		carousel.rightOffset = 0;
			
		var startleft = Math.round(carousel.leftOffset / 2);
		carousel.ul.css('left', startleft + 'px');
		if (startleft < 0) {
			carousel.rightmask.show();
			//carousel.rmfaded = true;
			carousel.leftmask.show();
			//carousel.lmfaded = true;
		}
				
		carousel.moveRight = function(left)
		{
			//if (carousel.leftOffset >= 0) return;
			if (left <= carousel.leftOffset) {
				carousel.rightmask.fadeTo(150, 0, function(){
					carousel.rightmask.css('zIndex', '5');
				});
				carousel.rmfaded = true;
				carousel.stopMove();
			} else {
				if (carousel.lmfaded) {
					carousel.lmfaded = false;
					carousel.leftmask.fadeTo(150, 1);
					carousel.leftmask.css('zIndex', '20');
				}
				var move = left - carousel.speed;
				if (move < carousel.leftOffset) { move = carousel.leftOffset; }
				carousel.ul.css('left', move + 'px');
			}
		};
		
		carousel.moveLeft = function(left)
		{
			if (left >= carousel.rightOffset) {
				carousel.leftmask.fadeTo(150, 0, function(){
					carousel.leftmask.css('zIndex', '5');
				});
				carousel.lmfaded = true;
				carousel.stopMove();
			} else {
				if (carousel.rmfaded) {
					carousel.rmfaded = false;
					carousel.rightmask.fadeTo(150, 1);
					carousel.rightmask.css('zIndex', '20');
				}
				var move = left + carousel.speed;
				if (move > carousel.rightOffset) { move = carousel.rightOffset; }
				carousel.ul.css('left', move + 'px');
			}
		};
		
		carousel.update = function()
		{	
			if (carousel.speed < carousel.maxSpeed) {
				carousel.speed += ((carousel.speed+carousel.acceleration)<=carousel.maxSpeed) ? carousel.acceleration : (carousel.maxSpeed-carousel.speed);
			}
			var left = carousel.ul.css('left');
			left = parseInt(left.substr(0, left.length - 2));

			if (carousel.leftHover) { carousel.moveLeft(left); }
			if (carousel.rightHover) { carousel.moveRight(left); }
		};
				
		carousel.startMove = function() {
			carousel.speed = carousel.minSpeed;
			if (carousel.interval) clearInterval(carousel.interval);
			carousel.interval = setInterval(carousel.update, 40);
		};
		
		carousel.stopMove = function() {
			if (carousel.interval) clearInterval(carousel.interval);
		};
				
		carousel.leftmask.bind('mouseenter', function(){ carousel.leftHover = true; carousel.startMove(); });
		carousel.leftmask.bind('mouseleave', function(){ carousel.leftHover = false; carousel.stopMove(); });
		
		carousel.rightmask.bind('mouseenter', function(){ carousel.rightHover = true; carousel.startMove(); });
		carousel.rightmask.bind('mouseleave', function(){ carousel.rightHover = false; carousel.stopMove(); });
		
		$carousel.bind('mousemove', function(e)
		{
			carousel.mx = e.clientX;
			carousel.my = e.clientY;
		});

	}

});

