/**
 * @author Dave Shepard
 */

var scroller = {
	id: 'our_clients_scroller',
	updateURL: (document.location.pathname.split('/').without("")[0] != undefined ? '/'+document.location.pathname.split('/').without("")[0] : "")+'/wp-content/themes/pai/home-featured.php',
	speed: 0.5,
	position: 0,
	transition: Effect.Transitions.sinoidal,
	update: function(){
		scroller.position = scroller.element.positionedOffset().left
		if(scroller.position == 0) {
			$('btn_previousBlock').addClassName('disabled');
		} else {
			$('btn_previousBlock').removeClassName('disabled');
		}
		if(scroller.position == scroller.lastOffset) {
			$('btn_nextBlock').addClassName('disabled');
		} else {
			$('btn_nextBlock').removeClassName('disabled');
		}
		$(scroller.id).removeClassName('animating');
	}
}

$(document).observe('dom:loaded',function(){
	if($('services_sidebar')) {
		$$('#services_sidebar .tabList a').each(function(e){
			e.observe('click',function(event){
				Event.stop(event);
				$$('#services_sidebar .tabList a','#services_description .description').invoke('removeClassName','active');
				[$(this),$(this.href.substr(this.href.indexOf('#')+1))].invoke('addClassName','active');
			}).onclick = function(){ return false; }
		});
		
		if(!document.location.hash.blank()){
			var hrefVal = document.location.hash;
			$$('#services_sidebar .tabList a').each(function(e){
				if(e.href.include(hrefVal)){
					$$('#services_sidebar .tabList a','#services_description .description').invoke('removeClassName','active');
					[$(e),$(e.href.substr(e.href.indexOf('#')+1))].invoke('addClassName','active');
				}
			})
		}
	}

	if($(scroller.id)) {
		scroller.width = $(scroller.id).getDimensions().width;
		scroller.totalElements = $$('#'+scroller.id+' .scrollerBlock ul').length;
		scroller.totalWidth = scroller.totalElements * scroller.width;
		scroller.lastOffset = 0 - scroller.totalWidth + scroller.width;
		scroller.element = $(scroller.id).select('.scrollerBlock')[0];

		$('btn_previousBlock').observe('click',function(event){
			Event.stop(event);
			if(scroller.position != 0 && !$(scroller.id).hasClassName('animating')) {
				$(scroller.id).addClassName('animating');
				new Effect.Move(scroller.element, {
					x: scroller.width,
					y: 0,
					duration: scroller.speed,
					transition: scroller.transition,
					mode: 'relative',
					afterFinish: function(){ scroller.update(); }
				});
			}
		}).onclick = function(){ return false; }

		$('btn_nextBlock').observe('click',function(event){
			Event.stop(event);
			if(scroller.position > scroller.lastOffset && !$(scroller.id).hasClassName('animating')) {
				$(scroller.id).addClassName('animating');
				new Effect.Move(scroller.element, {
					x: -scroller.width,
					y: 0,
					duration: scroller.speed,
					transition: scroller.transition,
					mode: 'relative',
					afterFinish: function(){ scroller.update(); }
				});
			}
		}).onclick = function(){ return false; }

		scroller.update();

		$$('#'+scroller.id+' .scrollerBlock li a').each(function(e){
			e.observe('click',function(event){
				Event.stop(event);
				if (!e.hasClassName('no_case_study')) {
					new Ajax.Updater('our_clients_client', scroller.updateURL, {
						method: 'post',
						parameters: {
							category_name: e.href.substr(e.href.lastIndexOf('/') + 1)
						},
						onComplete: function(){
							e.up('ul').select('a').invoke('removeClassName', 'active');
							e.addClassName('active');
						}
					});
				}
			}).onclick = function(){ return false; }
		});
	}
});




$(document).observe('load',function(){
	if($$('.jobfeed').length > 0) {
		$$('.jobfeed dl dd.job_description p').each(function(e){
			e.update(e.innerHTML.gsub(/\n|\r/,'<br />'));
		});
	}	
});
