/**
 * jQuery mousehold plugin - fires an event while the mouse is clicked down.
 * Additionally, the function, when executed, is passed a single
 * argument representing the count of times the event has been fired during
 * this session of the mouse hold.
 *
 * @author Remy Sharp (leftlogic.com)
 * @date 2006-12-15
 * @example $("img").mousehold(200, function(i){  })
 * @desc Repeats firing the passed function while the mouse is clicked down
 *
 * @name mousehold
 * @type jQuery
 * @param Number timeout The frequency to repeat the event in milliseconds
 * @param Function fn A function to execute
 * @cat Plugin
 */

jQuery.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 10;
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			jQuery(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			})

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		})
	}
}


$(document).ready(function() {
	$('#thumbnails li').attr('style', 'width: 0px; opacity: 0; margin-left: 160px; display: none;');
	$('.thumbnail_control').attr('style', 'opacity: 0');
	
	for (var i=0;i<4;i++) {
		$('#thumb_'+i).attr('style', 'width: 170px; opacity: 1; margin-left: 0px; display: block;');
	}
	
	$('#thumbnails').hover(function() {
		$('.thumbnail_control').animate({opacity: '0.8'}, {queue: false, duration: 300});
	}, function() {
		$('.thumbnail_control').animate({opacity: '0'}, {queue: false, duration: 300});
	});
	
	$('#thumbnails li a').hover(function() {
		$(this).children('.thumb_title').show().animate({opacity: '0.6', height: '30px'}, {queue: false, duration: 300});
	}, function() {
		$(this).children('.thumb_title').animate({opacity: '0', height: '0px'}, {queue: false, duration: 300});
	});
	
	$('#tc_left').click(function() {
		if ($('#thumbnails li:animated').length == 0 && $('#thumbnails li:visible:first').prev().length > 0) {
			$('#thumbnails li:visible:last').animate({width: '0px', opacity: '0', marginLeft: '160px'}, 400, function() {
				$('#thumbnails li:visible:last').hide();
				$('#thumbnails li:visible:first').prev().animate({width: '170px', opacity: '1'}, 400).show();
			});
		}
	});
	
	$('#tc_right').click(function() {
		if ($('#thumbnails li:animated').length == 0 && $('#thumbnails li:visible:last').next().length > 0) {
			$('#thumbnails li:visible:first').animate({width: '0px', opacity: '0'}, 400, function() {
				$('#thumbnails li:visible:first').hide();
				$('#thumbnails li:visible:last').next().animate({width: '170px', opacity: '1', marginLeft: '0px'}, 400).show();
			});
		}
	});
	
	var target = 0;
	
	if ($('#sidebar_text').attr('scrollHeight') <= 233) {
		$('#sidebar_up').hide();
		$('#sidebar_down').hide();
	}
	
	$('#sidebar_up').mousehold(10, function(s) {
		if (($('#sidebar_text').scrollTop() - 10) <= (target - 10)) {
			target = $('#sidebar_text').scrollTop() - 10;
			$('#sidebar_text').animate({scrollTop: target}, {queue: true, duration: 10});
		}
	});
	
	$('#sidebar_down').mousehold(10, function(s) {
		if (($('#sidebar_text').scrollTop() + 10) >= (target + 10)) {
			target = $('#sidebar_text').scrollTop() + 10;
			$('#sidebar_text').animate({scrollTop: target}, {queue: true, duration: 10});
		}
	});
	
	$('#sidebar_text').hover(function() {
		$(this).children('.scroll').show();
	}, function() {
		$(this).children('.scroll').hide();
	});
});