/*
 *
 * startPosition( x ); - Defaults at 0
 * setSpeed( x ); - Defaults at 4 seconds (4000)
 * setPerPage( x ); - Defaults at 0
 * setDimensions( width, height );
 * setLoopSize( length );
 * setDirection( direction ); - left, right, up, down (Defaults as left)
 * play();
 *
 */ 

Array.prototype.inArray = function (value) {
	
	for( var i = 0; i < this.length; i++ ) {

		if (this[i] === value) return true;

	}

	return false;

};



var decodeGallery = Class.create();

decodeGallery.prototype = {
	
	initialize: function( xmlPath, container, autoPlay ) {	
				
		this.setDefaults();
		
		this.container = container;
		
		this.loadXml( xmlPath );
				
		if( autoPlay == true ) this.play();
		
	},
	
	
	setDefaults: function() {

		this.currentPosition = 0;
		
		this.interval = 4000;
		
		this.height = 0;
		
		this.width = 0;
		
		this.viewportHeight = null;
		
		this.viewportWidth = null;
		
		this.loopLength = 0;
		
		this.direction = "left";
		
		this.perPage = 0;
		
		this.pages = new Array();
		
		this.preloaded = new Array();
		
		this.activePage = 0;
	
	},
	
	
	startPosition: function( value ) {
	
		this.currentPosition = value;
	
	},
	
	
	setSpeed: function( value ) {
	
		this.interval = value;
	
	},
	
	
	setPerPage: function( value ) {
	
		this.perPage = value;
	
	},
	
	
	setDimensions: function( width, height ) {
	
		this.width = width;
		
		this.height = height;
	
	},
	
	
	setViewportDimensions: function( width, height ) {
	
		this.viewportWidth = width;
	
		this.viewportHeight = height;
	
	},
	
	
	setLoopSize: function( width ) {
	
		this.loopLength = width;
			
	},
	
	
	setDirection: function( direction ) {
	
		this.direction = direction;
			
	},
	
	
	build: function() {
	
		this.createStructure();

		this.buildPages();
		
		this.addPage( this.activePage );
			
	},
	
	
	play: function() {
	
		this.build();
			
	},
	
	
	playLoop: function() {
	
		this.perPage = 0; //Always force single page for looping gallery
			
		this.build();
		
		this.addPage( this.activePage );
				
		this.bedStartingPosition();
				
		window.setInterval( this.slideEvent.bind( this ), this.interval );
		
	},
	
		
	loadXml: function( path ) {
	
		var xoTree = new XML.ObjTree();
		 
		this.xmlTree = xoTree.parseHTTP( path );
		 
		this.totalPhotos = this.xmlTree.decodeGallery.photo.length;
		
	},
	
	
	createStructure: function() {
	
		this.parent = $( this.container );

		var viewPort = document.createElement("div");
		viewPort.setAttribute( 'id', 'galleryViewPort' );
		viewPort.style.width = this.viewportWidth != null ? this.viewportWidth + "px" : this.width + "px";
		viewPort.style.height = this.viewportHeight != null ? this.viewportHeight + "px" : this.height + "px";
		
		this.parent.appendChild( viewPort );
	
		this.bed = document.createElement("div");
		this.bed.setAttribute( 'id', 'galleryBed' );
		this.bed.style.height = this.height + "px";
		
		viewPort.appendChild( this.bed );
		
		this.pageHolder = document.createElement("ul");
		this.pageHolder.setAttribute( 'id', 'galleryPageHolder' );
		
		this.bed.appendChild( this.pageHolder );
	
	},
	
	
	buildPages: function() {
	
		this.totalPages = this.perPage === 0 ? 1 : Math.ceil( this.totalPhotos / this.perPage );
		
		if( this.perPage === 0 ) this.perPage = this.totalPhotos;
		
		var pointer = 0;
		
		for( var i = 0; i < this.totalPages; i++ ) {
		
			this.pages[ i ] = new Array();
			
			for( var p = 0; p < this.perPage; p++ ) {
			
				this.pages[ i ].push( pointer );
				
				pointer++;
				
				if( pointer == this.totalPhotos ) break;
			
			}
		
		}	
	
	},
	
	
	addPage: function( num ) {
	
		var newPage = document.createElement( "li" );
		
		newPage.setAttribute( "id", "galleryPage_" + num );
		newPage.setAttribute( "class", "galleryPage" );
		
		var photoContainer = document.createElement( "ul" );
		
		this.preloadPage( num );

		for( var i = 0; i < this.pages[ num ].length; i++ ) {
		
			photoContainer.appendChild( this.addPhoto( this.pages[ num ][ i ] ) );
		
		}
	
		newPage.appendChild( photoContainer );
		
		this.pageHolder.appendChild( newPage );
	
	},
	
	
	preloadPhoto: function( id ) {
	
		if( !this.preloaded.inArray( id ) ) {
	
			preload = new Image();
				
			preload.src = this.xmlTree.decodeGallery.photo[ id ].filename;
			
			this.preloaded.push( id );
					
		}
		
	},
	
	
	preloadPage: function( num ) {
	
		for( var i = 0; i < this.pages[ num ].length; i++ ) {
		
			this.preloadPhoto( this.pages[ num ][ i ] );
			
		}
	
	},
	
	
	addPhoto: function( id ) {
	
		var newPhoto = document.createElement( "li" );
		
		var photo = document.createElement( "img" );
		
		photo.setAttribute( "src", this.xmlTree.decodeGallery.photo[ id ].filename );
		
		if( this.xmlTree.decodeGallery.photo[ id ].classname ) photo.setAttribute( "class", this.xmlTree.decodeGallery.photo[ id ].classname );
		if( this.xmlTree.decodeGallery.photo[ id ].link ) {
			
			photo.setAttribute( "rel", this.xmlTree.decodeGallery.photo[ id ].link );
			photo.onclick = function() { window.location = this.getAttribute( "rel" ); }
			
		}
		
		newPhoto.appendChild( photo );
	
		return newPhoto;
	
	},
	
	
	bedStartingPosition: function() {
	
		this.bed.style.position = "absolute";
		
		switch( this.direction ) {
		
			case "right":
				this.bedPosition = this.loopLength * -1;
				this.bed.style.left = this.bedPosition + "px";
				this.bedReset = this.loopLength * -1;
			break;
		
			case "up":
			break;
		
			case "down":
			break;
		
			default:
				this.bedPosition = 0;
				this.bed.style.left = this.bedPosition + "px";
				this.bedReset = 0;
			break;
		
		}
			
	},
	
	
	slideEvent: function() {

		switch( this.direction ) {
		
			case "right":
				this.bedPosition++;
				var anchor = "left";
				var trigger = 0;
			break;
		
			case "up":
				this.bedPosition--;
				var anchor = "top";
				var trigger = -1;
			break;
		
			case "down":
				this.bedPosition++;
				var anchor = "top";
				var trigger = 1;
			break;
		
			default:
				this.bedPosition--;
				var anchor = "left";
				var trigger = this.loopLength * -1;
			break;
		
		}
		
		this.bed.style[ anchor ] = this.bedPosition + "px";
		
		if( this.bedPosition == trigger ) { 
		
			this.bedPosition = this.bedReset;	

			this.bed.style[ anchor ] = this.bedReset + "px";
		
		
		}
		
	}
	
}
