
/**

	Name: 		Main Side Bar
	Purpose: 	Performs all actions required by the websites side bar
	Version: 	v1
	Author:		Dave Calvert
	
**/

var MainSideBar = Class.create();

MainSideBar.prototype = {
    
    initialize: function () {
		
		// Get the scroll bar position
		this.scrollPos = document.viewport.getScrollOffsets();
		
		// Set the height of the element
		this.setHeight();
		
		// Set the base current position
		this.currentPosition = 2;
		
		// Get total LI's in nav-block
		this.liTotal = $$('.nav-block li').length;
		
		// Set the inital arrow styles
		$('arrow-up').setStyle('opacity:0.3');
		
		// If the camera icon exists
		if($('page')){
			$('page').fade({ duration: 0, from: 1, to: 0.3 });
		}
		
		// Bind observers onto LI's
		$$('.nav-block li').each(this.setLinks.bind(this));
    
	},
	
	setLinks: function (k,v) {
	
		Event.observe($(k),'click',this.slideTo.bindAsEventListener(this));
	
	},
	
	setInitialPosition: function () {

		// Check the position of the vertical scrollbar
		if ( this.scrollPos.top > 50) {
		
			//Effect.ScrollTo($('main-container'),0, {axis:'y'});	
		
		}
	
	},
	
	setHeight: function () {
		
		if ( $('main-side-bar') ) {
			
			$('main-side-bar').setStyle('height:'+$('main-container').getStyle('height'));
			
		}
		
	},
	
	checkPosition: function () {
	
		// Check the position of the vertical scrollbar
		if ( this.scrollPos.top > 50 ) {
			this.lock();
		} else {
			this.lock();
			//this.unlock();
		}
		
	},
	
	lock: function () {
		
		// add the class that locks the toolbar
		$('nav-block').addClassName('nav-block-fixed');
		
	},
	
	unlock: function () {
		
		// add the class that locks the toolbar
		if ($('nav-block').hasClassName('nav-block-fixed')) {
			$('nav-block').removeClassName('nav-block-fixed');
		}
		
	},
	
	slideTo: function (e) {
	
		// Get Element from event
		el = Event.element(e);
	
		// Get the of the LI
		liId = this._getLiId(el);
	
		if ( liId != false ) {
			
			// Get the number off the LI's id
			nodeNumber 	= liId.id.substr(9, 1);
	
			// Set the position to scroll to
			position = this._getScrollToPosition(nodeNumber);
	
			// Scroll...
			if ( $('bar_position_'+position) ) {
				new Effect.ScrollTo($('bar_position_'+position), {queue:{scope:'sidebar'}});
			} 
			
			// Set active tab
			this.setActiveTab(position);
			
		} 
		
	},
	
	setActiveTab: function (p) {
		
		// Get the element
		el = $('side_bar_'+p);
		
		// Check to make sure it not the first or last LI
		if (el.id == 1 || el.id == this.liTotal) { 
			return false;
		} else {
		
			// Clear the current active tab
			this.removeActiveTabs();
			
			// Set the tab active
			el.down(1).addClassName('active');
			
			// Refresh the cufon
			Cufon.refresh();

			// Check arrow if required
			if ( p == 2 ) {
				$('arrow-down').setStyle('opacity:1');
				$('arrow-up').setStyle('opacity:0.3');
			} else if ( p == (this.liTotal - 1)) {
				$('arrow-up').setStyle('opacity:1');
				$('arrow-down').setStyle('opacity:0.3');
			} else {
				$('arrow-down').setStyle('opacity:1');
				$('arrow-up').setStyle('opacity:1');
			}
			
			// Set the camera icon hightlight
			if($('camera')){
				if ( p == 2 || p==1 ) {
					$('camera').appear({ duration: 1.0, from: 0.3, to: 1 });
				} else {
					$('camera').fade({ duration: 1.0, from: 1, to: 0.3 });
				}
			}
			
			// Set the page icon hightlight
			if($('page')){
				if ( p == 3 || p==4 ) {
					$('page').appear({ duration: 1.0, from: 0.3, to: 1 });
				} else {
					$('page').fade({ duration: 1.0, from: 1, to: 0.3 });
				}
			}
		
		}
		
	},
	
	removeActiveTabs: function () {
		
		$$('.nav-block li').each( function (k,v) {

			$(k).down(1).removeClassName('active');
		
		});
		
	},
	
	_getLiId: function (el) {
		
		if (el.tagName == "DIV") {

			liId 		= el.up(0);
			return liId;
			
		} else {
		
			return false;
		
		}
		
	},
	
	_getScrollToPosition: function (nodeNumber) {
		
		if ( nodeNumber == 1 ) {
			
			position = this.currentPosition - 1;
			
			// Check to make sure we're not going off the end!
			if ( position < 2) {
				position = 2;
			}

		} else if ( nodeNumber == this.liTotal ) {
			position = this.currentPosition + 1;
			
			if ( position > this.liTotal - 1) {
				position = this.liTotal - 1;
			}
			
		} else {
			position = nodeNumber;
		}
		
		// Set the currentPosition
		this.currentPosition = position;
		
		return position;
		
	}
	
};
