/**
 * Case Studies
 * @author Kevin Dew <kev@redbullet.co.uk>
 * @copyright Kevin Dew 2008
 * Depencies: Mootools 1.2
 */

window.addEvent('domready', function()
{
	if(!$('psuContent') || !$('psuSlide') || !$('psuSelector'))
		return;

	
	var selected = 0;

	var links = 0;
	
	var scroll = false;
	
	//make on clicks and work out whats selected
	$each($$('div#psuSelector a'), function(el)
	{
		
		if(el.hasClass('active'))
			selected = links;
		
		el.psuPosition = links;
		
		el.addEvent('click', function(e)
		{
			if(scroll)
				scroll.cancel();
			
			$each($$('div#psuSelector a'), function(el)
			{
				el.removeClass('active');	
			});
			
			this.addClass('active');
			
			scroll = new Fx.Scroll('psuContent').start(contentWidth * this.psuPosition, 0);
			
			new Event(e).stop();
			
			
		}.bind(el));
		
		links++;
	});
	
	
	//make slider device
	var contentWidth = $('psuContent').getSize().x;
	
	$('psuSlide').setStyle('width', contentWidth * (links + 1));
	
	$('psuContent').scrollTo(contentWidth * selected, 0);
	
	$each($$('div#psuSlide div.psuTab'), function(el)
	{
		el.removeClass('hide');
		//el.setStyle('width', contentWidth);
		
	});
	
	//left bar scroller
	if($('csLeftList') && ($('csLeftList').getScrollSize().y > $('csLeftList').getSize().y))
	{
		//create up and down elements
		var upScroll = new Element('a', {id:'listScrollUp', href:'#'});
		new Element('span').set('text', 'Up').inject(upScroll);
		new Element('div', {'class':'listScroller'}).grab(upScroll).inject($('csLeftList'), 'before');

		var downScroll = new Element('a', {id:'listScrollDown', href:'#'});
		new Element('span').set('text', 'Down').inject(downScroll);
		new Element('div', {'class':'listScroller'}).grab(downScroll).inject($('csLeftList'), 'after');
		
		var scrollAmount = 13;
		
		var scrollFx = new Fx.Scroll($('csLeftList'), {duration:0});
		
		$('csLeftList').addClass('initiated');
		
		function checkScroll()
		{
			if(!$('csLeftList'))
				return;
			
			if($('csLeftList').getScroll().y == 0)
				upScroll.removeClass('active');
			else
				upScroll.addClass('active');

			if($('csLeftList').getScroll().y < ($('csLeftList').getScrollSize().y - $('csLeftList').getSize().y))
				downScroll.removeClass('active');
			else
				downScroll.addClass('active');				
		}
		
		function scrollCsList(up)
		{
			if(!$('csLeftList'))
				return;
			
			if(up)
			{
				if($('csLeftList').getScroll().y <= scrollAmount)
					scrollFx.start(0, 0);
				else
					scrollFx.start(0, $('csLeftList').getScroll().y - scrollAmount);
			}
			else
			{				
				if(($('csLeftList').getSize().y + scrollAmount) > $('csLeftList').getScrollSize().y)
					scrollFx.start(0, $('csLeftList').getScrollSize().y - $('csLeftList').getSize().y);
				else
					scrollFx.start(0, $('csLeftList').getScroll().y + scrollAmount);			
			}
			checkScroll();
		}
		
		var interval = false;
		
		var cancelMouse = function()
		{
			if(interval)
				$clear(interval);			
		}
		
		var cancelClick = function(e)
		{
			new Event(e).stop();
		}
		
		upScroll.addEvents(
		{
			/*'mousedown': function()
			{
				if(interval)
					$clear(interval);
				
				interval = (function(){scrollCsList(true);}).periodical(40);
			},*/
			'mouseover': function()
			{
				if(interval)
					$clear(interval);
					
				interval = (function(){scrollCsList(true);}).periodical(50);
			},
			//'mouseup': cancelMouse,
			'mouseout': cancelMouse,
			'click': cancelClick
		});

		downScroll.addEvents(
		{
			/*'mousedown': function()
			{
				if(interval)
					$clear(interval);
				
				interval = (function(){scrollCsList(false);}).periodical(40);
			},*/
			'mouseover': function()
			{
				if(interval)
					$clear(interval);
					
				interval = (function(){scrollCsList(false);}).periodical(50);
			},
			//'mouseup': cancelMouse,
			'mouseout': cancelMouse,
			'click': cancelClick
		});			
		
		checkScroll();
		
	}

	
});

