/* ScrollGallery :: Image scrolling gallery
 * Author: D. Carter
 * Created: 2009-01-16
 */

/*	==========================================
	BEGIN CORE FUNCTIONALITY
*/
var ScrollGallery = function(_da,_pa,_wi,_op) { /* data (source images), parent, width, optional parameters */
	this._da = _da;
	if (!this._da) throw new Error( 'ScrollGallery class invoked without image array.' );
	this._pa = Build.fetch(_pa);
	if ( !_pa || typeof(this._pa) != 'object' ) throw new Error( 'ScrollGallery class invoked without target parent.' );
	this._op = ( typeof(_op) == 'object' ? _op : new Object() );
	this._wi = ( typeof(_wi) == 'number' ? _wi : 450 );
	this.construct();
	this._currentblock = 0;
	this.init();
};
ScrollGallery.prototype.init = function() {
	this._next.onclick = this.goNext.scopeTo(this);
	this._prev.onclick = this.goPrev.scopeTo(this);
};
ScrollGallery.prototype.goNext = function() {
	if ( !this.isAnimating() ) {
		this._currentblock++;
		if ( this._currentblock > this._blocks.length-1 ) {
			this._currentblock = 0;
			/*this._inner.style.marginLeft = this._blocks[this._blocks.length-1].offsetWidth + 'px'; // METHOD 001 */
			this._animBackToStart = true;
		}
		this.renderToCurrent();
		
		setTab(this._currentblock,this._blocks.length);

	};
	return false; // FOR ANCHOR
};
ScrollGallery.prototype.goPrev = function() {
	if ( !this.isAnimating() ) {
		this._currentblock--;
		if ( this._currentblock < 0 ) {
			this._currentblock = this._blocks.length-1;
			this._inner.style.marginLeft = ( 0 - this._scrollpanel1.offsetWidth ) + 'px';
		}
		this.renderToCurrent();
		
		setTab(this._currentblock,this._blocks.length);
	};
	return false; // FOR ANCHOR
};
ScrollGallery.prototype.skipToIndex = function() {
	if ( !this._owner.isAnimating() ) {
		this._owner._currentblock = parseInt( this.id.split('_')[1] );
		this._owner.renderToCurrent();
		
		setTab(this._currentblock,this._blocks.length);
	}
	return false; // FOR ANCHOR
};

ScrollGallery.prototype.skipToIndexPass = function(_index) {
	if ( !this.isAnimating() ) {
		this._currentblock = parseInt( _index );
		this.renderToCurrent();
		
		setTab(this._currentblock,this._blocks.length);
	}
	return false; // FOR ANCHOR
};

ScrollGallery.prototype.isAnimating = function() {
	return ( typeof(Tween) == 'function' && this._tw ? true : false );
};
ScrollGallery.prototype.renderToCurrent = function() {

	var _curDist = parseInt( this._inner.style.marginLeft ) || 0,
		_newDist = ( 0 - this.widthToIndex(this._currentblock) );
	if ( typeof(Tween) == 'function' ) {
		if ( this._tw ) return;
		/* var t = new Tween(object,property,easing,start,end,duration,suffixe); */
		var _easing = is.string(this._op['easing']) ? eval(this._op['easing']) : Tween.strongEaseOut;
		if ( this._animBackToStart ) {
			_newDist = ( 0 - this._scrollpanel1.offsetWidth );
		};
		this._tw = new Tween( this._inner.style, 'marginLeft', _easing, _curDist, _newDist, (is.number(this._op['duration']) ? this._op['duration'] : .5), 'px');
		this._tw.onMotionFinished = function() {
			if (this._animBackToStart) this._inner.style.marginLeft = '0px';
			delete this._tw;
			delete this._animBackToStart;
		}.scopeTo(this);
		this._tw.start();
	} else {
		this._inner.style.marginLeft = _newDist + 'px';
		delete this._animBackToStart;
	}
	for ( var i = 0, l = this._numberLinks.length; i < l; i++ ) {
		this._numberLinks[i].className = '';
	}
	this._numberLinks[this._currentblock].className = 'selected';
};
ScrollGallery.prototype.widthToIndex = function(_i) { /* index */
	var _w = 0; /* width */
	Loop.to( _i, function(i) {
		if ( this._blocks[i] ) {
			_w += this._blocks[i].offsetWidth;
		} else throw new Error( 'this._blocks['+i+'] doesnt exist.' );
	}.scopeTo(this) );
	return _w;
};
ScrollGallery.prototype.construct = function() {
	this._blocks = new Array();
	this._scrollpanel1 = Build.node( 'td' );
	this._scrollpanel2 = this._scrollpanel1.cloneNode(true);
	Loop( this._da, function(e) {
		if (e) {
			if ( !e['type'] || e['type'].toLowerCase() != 'html' ) {
				var img = Build.node( 'img', {
												src:e['src'],
												title:( is.string(e['title']) ? e['title'] : 'No image title provided' ),
												alt:( is.string(e['alt']) ? e['alt'] : 'No alt text provided' ),
												border:0
											} );
				if ( is.string(e['href']) ) img = Build.node( 'a', { href:e['href'] }, img );
				var b = this.buildWrapperDiv(e,img);
			} else {
				var b = this.buildWrapperDiv(e,_NBSP,e['content'],e['liveContentId']);
			}
			this._blocks.push(b);
			this._scrollpanel1.appendChild( b );
			this._scrollpanel2.appendChild( b.cloneNode(true) );
		};
	}.scopeTo(this) );

	this._inner = Build.node( 'div', {
										className:'ScrollGallery_innerWrapper',
										style:{
												width:'999999px',
												padding:( is.string(this._op['padding']) ? this._op['padding'] : '0px' )
											}
										}, [
												Build.node( 'table', { cellSpacing:0, cellPadding:0 }, [
													Build.node( 'tbody', {}, [
														Build.node( 'tr', {}, [
															this._scrollpanel1,
															this._scrollpanel2,
														] ), /* END tr */
													] ), /* END tbody */
												] ), /* END table */
												new Clear()
											] );
	this._outer = Build.node( 'div', {
										className:'ScrollGallery_outerWrapper',
										style:{
												overflow:'hidden',
												width:this._wi + 'px'
											}
										}, [
												this._inner,
												new Clear()
											] );



	this._prev = Build.node( 'a', { href:'#', className:( is.string(this._op['prevButtonClass']) ? this._op['prevButtonClass'] : '' ) }, [
											Build.node( 'span', { className:'prev_text' }, '' )
										] );
	this._next = Build.node( 'a', { href:'#', className:( is.string(this._op['nextButtonClass']) ? this._op['nextButtonClass'] : '' ) }, [
											Build.node( 'span', { className:'next_text' }, '' )
										] );
	this._numberLinks = new Array();
	this._nums = Build.node( 'div', { className:( is.string(this._op['numbersClass']) ? this._op['numbersClass'] : '' ) }, (function(){
									var a = new Array();
									Loop( this._blocks, function(e,i) {
										var aClass = ( i === 0 ? 'selected' : '' );
										var tA = Build.node( 'a', { href:'#', className:aClass, id:'scrollNumber_'+i, onclick:this.skipToIndex }, i+1 );
										tA._owner = this;
										a.push( tA );
										this._numberLinks.push( tA );
									}.scopeTo(this) );
									return a;
								}.scopeTo(this))() );

	this._pa.appendChild( this._prev );
	this._pa.appendChild( this._next );
	if ( this._op['numbers'] ) this._pa.appendChild( this._nums );
	this._pa.appendChild( this._outer );
	this._pa.appendChild( new Clear() );
};
ScrollGallery.prototype.buildWrapperDiv = function(e,img,inner,liveContentId) {
	var d =  Build.node( 'div', {
								style:{
										cssFloat:'left',
										styleFloat:'left'
									},
								className:([
										( is.string(e['wrapperClass']) ? e['wrapperClass'] : '' ),
										( is.string(this._op['imgDivWrapperClass']) ? this._op['imgDivWrapperClass'] : '' )
									].join(' '))
							}, [ img ] );
	if ( is.string(liveContentId) ) {
		d.innerHTML = '';
		d.appendChild( Build.fetch(liveContentId) );
	} else if ( is.string(inner) ) d.innerHTML = inner;
	return d;
};
/*	END CORE FUNCTIONALITY
	==========================================



	==========================================
	INCLUDED FUNCTIONS FROM SEPARATE LIBRARIES
*/


/* Build :: Dom Building Utility */
 var Build = {
	node: function(n, o, c ) { // n == NODENAME, ATTRIBS (o == 'object'), c == CONTENTS
		if ( !n || typeof n != 'string' ) return false;
		if ( o && o['name'] ) { // IF WE HAVE A NAME INCLUDED... SPECIAL CIRCUMSTANCES FOR IE's STUPIDITY.
			try {
				var e = document.createElement('<'+n+' name="'+o['name']+'">'); // THIS MAKES ME CRY... AND SHIVER.
			} catch(er) {
				if (!e || !e.name) {
					var e = document.createElement(n);
					e['name'] = o['name']; // THANK GOD FOR THE SANITY OF GECKO, WEBKIT, AND OPERA... SCREW YOU IE/TRIDENT.
				}
			}
		} else var e = document.createElement(n); // I <3 ONE LINE ELSE CLAUSES...
		if ( n == 'input' && !o['type'] ) o['type'] = 'text'; /* FIX FIREFOX BORDERING ISSUE */
		Build.copyProps( e, o );
		Build.content( e, c );
		return e;
	},
	content: function( e, c ) {
		if ( c && typeof c == 'object' && c.constructor == Array ) {
			if ( !c[c.length-1] ) c.pop(); /* PREVENT IE FROM SEEING LENGTH LONGER THAN ACTUAL LENGTH (due to comma error) */
			for ( var k = 0, l = c.length; k < l; k++ ) {
				e.appendChild( (typeof c[k] == 'object') ? c[k] : document.createTextNode(c[k]) );
			}
		} else if ( c ) e.appendChild( (typeof c == 'object') ? c : document.createTextNode(c) );
		return e;
	},
	copyProps: function( o, p ) {
		for ( var k in p ) {
			var isArr = false;
			try { if ( p[k].constructor && p[k].constructor == Array ) isArr = true; } catch(er){}
			if ( typeof p[k] == 'object' && !isArr ) Build.copyProps( o[k], p[k] );
			else o[k] = p[k];
		}
	},
	/* END CORE FUNCTIONALITY */
	fetch: function( o, p ) {
		if ( typeof o == 'string' ) var o = document.getElementById(o);
		if ( o && typeof(o) == 'object' ) { // FIXED SOME BLOAT, FINALLY APPROVED FUNCTION SCOPED CALLING VIA Build.fetch
			if ( typeof(p) == 'object' ) Build.copyProps(o,p);
			if ( typeof(p) == 'function' ) p.call(o);
		};
		return o;
	}
}



/* _is_ :: d__carter.js
 * author: Daniel J Carter
 * created: 2009-08-29 12:59 AM
 */
var is = {
	array: function(a) { return ( a && typeof(a)=='object' && a.constructor == Array ) ? true : false; },
	string: function(s) { return ( s && typeof(s) == 'string' ) ? true : false; },
	number: function(n) { return ( n && typeof(n) == 'number' ) ? true : false; }
};


/* Loop :: d__carter.js
 * author: Daniel J Carter
 */
var Loop = function(a,f) {
	for ( var i = 0, l = a.length; i < l; i++ ) {
		var r = ( a[i] ? f(a[i],i) : null );
		if (r) return r;
	};
	return null;
};
Loop.to = function(n,f) { for ( var i = 0; i < n; i++ ) { f(i); }; };



/* scopeTo :: d__carter.js
 * author: Daniel J Carter
 */
Function.prototype.scopeTo = function(sc) {
	var _method = this;
	var fn = (function(){ return _method.apply( sc, arguments ); });
  return fn;
};




/* CONSTANTS */
var _NBSP = '\u00a0';
var Clear = function() {
	return Build.node('div',{
				className:'ScrollGallery_clear',
				style:{
					overflow: 'hidden',
					clear: 'both',
					height: '0px',
					lineHeight:'0px',
					fontSize: '0px',
					zoom: '1'
				}
			},_NBSP)
};

/* ADDED HELPER FUNCTIONS */
function setTab(index,length) {

	var iterator;
	for(iterator = 0 ; iterator < length ; iterator++)
	{
		var tabname = "tab" + iterator;
		if(iterator == index)
		{
			document.getElementById(tabname).className = "active";
		}
		else
		{
			document.getElementById(tabname).className = "";
		}
	}
}
