//-( LiteboxImage )-------------------------------------------------------------

var VaultLiteGalleryToolbar = new Class( {
	
	  initialize: function( ) {
		this.node = new Element( 'div', { 'class': 'liteboxToolbar' } );
	  }
	
	, node: null
	
	, addTool: function( btn, fn ) {
		btn.addClass( 'tool' );
		
		if( fn ) {
			btn .addClass( 'hoverable' )
				.addEvent( 'click', fn )
				.addEvent( 'keypress', function( event ) {
					fireClickOnEnterKeyPress( event, this );
				 }.bind( btn ) )
		}
		else {
			btn.addClass( 'inactive' );
		}
		
		this.node.grab( btn );
	  }
	
	, render: function( container ) {
		container.grab( this.node, 'top' );
/*		this.node.setStyle(
			 'left'
			, getWindowWidth( ) / 2 - this.node.getSize( ).x / 2
		);
		
		this.node.addClass( 'uvDraggable' );
		uvdrag_attach( this.node );*/
	  }
	
} );

/* class */ function LiteGallery( ) /* extends Litebox */ {
	var litebox = Litebox.apply( this, arguments );
	
	if( litebox ) {
		return litebox;
	}
	
	if( !this.getCanvas( ) ) {
		this.setCanvas(
			document.createElement( 'div' )
		);
		
		this.getCanvas( ).id = 'galleryBox';
		this.getCanvas( ).className = 'liteboxCore loading';

		this.setIgnoreCanvasClick( true );
	}
}

extend( LiteGallery, {
 	_cache					:	[]
,	getCache				:	function( k ) {
		return LiteGallery._cache[ k ] || null;
		}
,	setCache				:	function( k, v ) {
		LiteGallery._cache[ k ] = v;
		return LiteGallery;
		}

,	_pipeline				:	[]
,	inPipeline				:	function( k ) {
		return LiteGallery._pipeline[ k ] || null;
		}
,	addToPipeline			:	function( k ) {
		LiteGallery._pipeline[ k ] = 1;
		return LiteGallery;
		}
,	clearFromPipeline		:	function( k ) {
		LiteGallery._pipeline[ k ] = 0;
	}
} );

extend( LiteGallery.prototype, Litebox.prototype );
extend( LiteGallery.prototype, {

	_template					:	null
,	_bridge						:	null
,	_gallery					:	null

// Has litebox assembled at least once? False for first assemble( ), true for subsequents
,	_has_assembled				:	false
,	hasAssembled				:	function( ) {
		return this._is_assembled;
	}
,	setHasAssembled				:	function( v ) {
		this._is_assembled = v;
		return this;
	}

,	getTemplate					:	function( ) {
		return this._template;
		}
,	setTemplate					:	function( v ) {
		this._template = v;
		return this;
		}

,	_galleryUri					:	null
,	getGalleryUri				:	function( ) {
		return this._galleryUri;
		}
,	setGalleryUri				:	function( uri ) {
		this._galleryUri = uri;
		
		var cache = LiteGallery.getCache( uri );
		if( cache ) {
			this.assemble( uri, cache );
			}
		else
		if( LiteGallery.inPipeline( uri ) ) {
			this._bridge.setResultCallback( bind( this, 'assemble', uri ) );
		}
		else {
			LiteGallery.addToPipeline( uri );
			this._bridge = LiteBridge.span(
				 uri
				,bind( this, 'assemble', uri )
				);
			}
		
		return this;
		}

,	didLoadCanvas		: function( ) {
		this.getCanvas( ).removeClass( 'loading' );
		this.getCanvas( ).addClass( 'loaded' );
	}
		
,	assemble					:	function( uri, response ) {
		
		LiteGallery.setCache( uri, response );
		LiteGallery.clearFromPipeline( uri );
		
		if( !this.hasAssembled( ) ) {
			this.cloakCanvas( );
		}
		setHTML( this.getCanvas( ), response.body );
		
	   	var frame = getObj( 'frame' );	
		var frameWidth = getObjWidth( frame );
		var frameHeight = getObjHeight( frame );
		
		if( this.getTemplate( ) == 'video'
		&&  response.qtUpshift ) {
			uvqt_attach( new Array( response.qtUpshift ) );
			
			// Get movie height explicitly because it's slow to attach
			frameHeight += qtUpshift[ 0 ][ 3 ];
			}
		
		var help = document.createElement( 'div' );
			addClass( help, 'help' );
			setText( help, 'Click outside this box to close it.' );
			frame.appendChild( help );
        	
        var next = getObj( 'gNext' );
        if( next ) {
	        next.onclick = bind( this, 'changeFrame', next );
	        this.preloadFrame( next );
	        }
	        
	   	var prev = getObj( 'gPrev' );
	   	if( prev ) {
	   		prev.onclick = bind( this, 'changeFrame', prev );
	   		}
	   	
	   	this.getCanvas( ).style.width = frameWidth + 'px';
	   	this.getCanvas( ).style.height = frameHeight + 'px';
	   	
	   	if( !this.hasAssembled( ) ) {
		   	this.updateCanvasPosition( );
	   		this.uncloakCanvas( );
		   	this.setHasAssembled( true );
		}
		
		this.didLoadCanvas( );
	   	
	   	return this;
		}
		
,	changeFrame					:	function( obj, e ) {
		this.setGalleryUri( obj.href.replace( 'gallery/', 'gallery/ajax/' ) );
		if( e = getEvent( e ) ) {
			return abortEvent( e );
			}
		return false;
		}
	
,	preloadFrame				:	function( obj ) {
		var uri = obj.href.replace( 'gallery/', 'gallery/ajax/' );
		
		if( !LiteGallery.getCache( uri )
		&&  !LiteGallery.inPipeline( uri ) ) {
			LiteGallery.addToPipeline( uri );
			this._bridge = LiteBridge.span(
				 uri
				,bind( LiteGallery, 'setCache', uri )
				);
			}
		}
	
,	show						:	function( ) {
		if( getObj( 'uvHardpoint_InteractiveMap' ) ) {
			addClass( getObj( 'uvHardpoint_InteractiveMap' ), 'backdrop' );
		}
		
		bind( this, Litebox.prototype.show )( );
		return this;
		}
		
,	dispose						:	function( ) {
		if( getObj( 'uvHardpoint_InteractiveMap' ) ) {
			removeClass( getObj( 'uvHardpoint_InteractiveMap' ), 'backdrop' );
		}
		
		bind( this, Litebox.prototype.dispose )( );
		removeNode( this.getFrame( ) );
		LiteboxController.clearLitebox( this._liteboxID );
		}

	} );

/* class */ function LiteMapGallery( ) /* extends LiteMap */ {
	LiteGallery.apply( this, [] );
	}

extend( LiteMapGallery.prototype, LiteGallery.prototype );
extend( LiteMapGallery.prototype, {
	show						:	function( ) {
		addClass( getObj( 'mapShell' ), 'backdrop' );
		
		bind( this, LiteGallery.prototype.show )( );
		return this;
		}
		
,	dispose						:	function( ) {
		removeClass( getObj( 'mapShell' ), 'backdrop' );
		bind( this, LiteGallery.prototype.dispose )( );
		}
	
	} );
	
	
	
	
	
	
	
	
	
	


	
	
	
	
	
	
	
/* class */ function RichMediaVideo( ) /* extends Litebox */ {
	Litebox.apply( this, [] );
	
	this.setCanvas(
		document.createElement( 'div' )
		);
		
	this.getCanvas( ).id = 'galleryBox';
	this.getCanvas( ).className = 'richVideo';
	//this.getCanvas( ).className = 'liteboxCore';

	this.setIgnoreCanvasClick( true );
	
	
	
	}
	
extend( RichMediaVideo.prototype, Litebox.prototype );
extend( RichMediaVideo.prototype, {

	embedVideo					:	function( video_id ) {

		this.cloakCanvas( );
		setHTML(
			this.getCanvas( ),
			'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
			'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0"'+
			'id="player" width="450" height="392"><param name="movie"'+
			'value="http://content.fliqz.com/components/5b61da2cb99a43efa8e93e77592d2d7e.swf" />'+
			'<param name="allowFullScreen" value="true" />'+
			'<param name="wmode" value="transparent" />'+
			'<param name="AllowScriptAccess" value="always" />'+
			'<param name="flashvars" value="file='+video_id+'&" />'+
			'<embed name="player" src="http://content.fliqz.com/components/5b61da2cb99a43efa8e93e77592d2d7e.swf"'+
			'width="450" height="392" allowFullScreen="true" wmode="transparent"'+
			'AllowScriptAccess="always" flashvars="file='+video_id+'&"'+
			'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" >'+
			'</embed></object>'+
			'<div class="closeLite">'+
				'<a href="#" onclick="bind( LiteboxController.getFrontLitebox( ), LiteGallery.prototype.dispose )( ); return false;">close [ x ]</a>'+
			'</div><div style="clear:right;"></div>'
		);

	   	this.getCanvas( ).style.width = '460px';
	   	this.getCanvas( ).style.height = '425px';

	   	this.updateCanvasPosition( );
	   	this.uncloakCanvas( );
	   	
	   	return this;
	}
});



/*class*/ function VaultLiteGallery( liteboxID, asset ) /*extends LiteGallery*/ {
	this.shouldPaint = true;
	
	var litebox = LiteGallery.apply( this, [ liteboxID ] );
	
	if( litebox ) {
		litebox.shouldPaint = false;
		litebox.show( );
		return;
	}
	
	this.getCanvas( ).id = 'vaultGalleryBox_'+liteboxID;
	this.getCanvas( ).className = this.getCanvas( ).className+' galleryVaultBox';
	
	var caption = { description: '', name: '', website: '' };
	if( asset.caption ) {
		caption.description = asset.caption;
	}
	
	if( asset.credit ) {
		caption.name = asset.credit;
	}
	if( asset.credit_site ) {
		caption.website = asset.credit_site;
	}
	
	this.asset = asset;
	this.caption = caption;
	this.setIgnoreCanvasClick( true );
	
	this.show( );
}

extend( VaultLiteGallery.prototype, LiteGallery.prototype );
extend(
	  VaultLiteGallery.prototype
	, {
		  asset: null
		, caption: null
		, gallery: null
		, toolbar: null
		, shouldPaint: null
		, uvt: 'gallery'  // Google Analytics tracking "action"
		
		, setGallery: function( gallery ) {
			this.gallery = gallery;
			return this;
		  }
		
		, show: function( ) {
			if( this.shouldPaintCanvas( ) ) {
			  	setHTML( this.getCanvas( ), this.paint( ) );
			  	this.didSetCanvas( );
			}
			
			bind( this, LiteGallery.prototype.show )( );
		  }
		
		, shouldPaintCanvas: function( ) {
			return this.shouldPaint;
		  }
		
		, didSetCanvas: function( ) {
			var canvas = $( this.getCanvas( ) );
			
			canvas.grab( this.getFullCaption.call(
				  this
				, this.caption.description
				, this.caption.name
				, this.caption.website
			) );
			
			var close = canvas.getElement( 'div.close' );
			if( close ) {
				close.setStyle( 'display', 'block' );
			}
			
			var tools = [];
			
			if( this.gallery ) {
				var galleryCount = this.gallery.count( );
				var ext = isIE6( ) ? 'gif' : 'png';
				
				tools.push ( [
					  Asset.image( auri( 'resource/image/litebox/prev.'+ext ), { styles: { width: 33, height: 34 }, title: 'Previous Image' } )
					, ( galleryCount > 1 ? function( ) { this.showPrevious( ); }.bind( this ) : null )
				] );
				
				tools.push( [
					 new Element( 'span', { 'class': 'counter', text: ( this.gallery.cursor + 1 )+' of '+this.gallery.count( ) } )
				] );
				
				tools.push( [
					  Asset.image( auri( 'resource/image/litebox/next.'+ext ), { styles: { width: 33, height: 34 }, title: 'Next Image' } )
					, ( galleryCount > 1 ? function( ) { this.showNext( ); }.bind( this ) : null )
				] );
			}
			
/*			tools.push( [
				  Asset.image( auri( 'resource/image/litebox/close.png' ), { styles: { width: 97, height: 96 }, title: 'Close Litebox' } )
				, function( event ) {
					LiteboxController.getFrontLitebox( ).dispose( );
					new Event( event ).stop( );
				  }.bind( this )
			] );*/
			
			if( tools.length ) {
				this.toolbar = new VaultLiteGalleryToolbar( );
				tools.each( function( tool ) {
					this.toolbar.addTool.apply( this.toolbar, tool )
				}, this );
				this.toolbar.render( this.getCanvas( ) );
			}

			// FIXME: fix to work w/ flash boxes.			
//			canvas.addClass( 'uvDraggable' );
//			uvdrag_attach( canvas );
		  }
		
		, showPrevious: function( ) {
			this.getCanvas( ).innerHTML = this.gallery.next( );
		  }
		
		, showNext: function( ) {
			this.getCanvas( ).innerHTML = this.gallery.prev( );
		  }
		
		, paint: function( canvas ) {
			var ext = isIE6( ) ? 'gif' : 'png';
			
			return '<div class="vaultLiteboxCore"><div class="close"><a href="#" onclick="LiteboxController.getFrontLitebox( ).dispose( ); new Event( event ).stop( );" onkeypress="fireClickOnEnterKeyPress( event, this );"><img src="'+auri( 'resource/image/litebox/close.'+ext )+'" alt="Close" class="hoverable" /></a></div>'+canvas+'</div>';
		  }
	
		, getFullCaption: function( description, name, website ) {
			var caption = ''
			
			if( description ) {
				caption += description;
			}
			
			if( name ) {
				var credit = '';
				credit = '&copy;'+name;
				
				if( website ) {
					credit = '<a href="'+website+'" class="uvt_'+this.uvt+'">'+credit+'</a>';
				}
				
				caption += credit ? '<div class="credit">'+credit+'</div>' : '';
			}
			
			var caption = new Element( 'div', { 'class': 'caption', html: caption } );
			
			uv_attach_links( caption );
			return caption;
		  }
	  }
);



/*class*/ function ImageVaultLiteGallery( in_asset, gallery ) /*extends VaultLiteGallery*/ {
	
	if( gallery ) {
		gallery.set( in_asset );
		this.setGallery( gallery );
	}
	
	this.shouldPaint = false;
	
	var asset = Object.append( new Asset.image( in_asset.src, { onload: this.loaded.bind( this ) } ), in_asset );
	VaultLiteGallery.apply( this, [ 'uvImageVaultGallery'+in_asset.id, asset ] );
}

extend( ImageVaultLiteGallery.prototype, VaultLiteGallery.prototype );
extend( ImageVaultLiteGallery.prototype, {
	
	  is_loaded: false
	, uvt: 'photo_gallery'
	
	, loaded: function( ) {
		this.is_loaded = true;
		this.didLoadCanvas( );
		
		if( this.shouldPaintCanvas( ) ) {
			this.show( );
		}
	  }
	
	, show: function( ) {
		this.shouldPaint = true;
		
		if( this.is_loaded ) {
			bind( this, VaultLiteGallery.prototype.show )( );
		}
	  }
	
	, paint: function( ) {
		return bind( this, VaultLiteGallery.prototype.paint )(
			'<div class="bg"><img src="'+this.asset.src+'" class="litebox_media_slide" alt="" style="width:'+this.asset.width+'px; height:'+this.asset.height+'px;" /></div>'
		);
	  }
	
	, didSetCanvas: function( ) {
		var canvas = $( this.getCanvas( ) );
		
		canvas.addClass( 'imageVaultBox' );
		canvas.setStyle( 'width', this.asset.width );
//		canvas.setStyle( 'height', this.asset.height );
		
		canvas.getElements( '.litebox_media_slide' )[0].set( 'tween', { duration: 'short' } );
		
		bind( this, VaultLiteGallery.prototype.didSetCanvas )( );
	}
	
	, showPrevious: function( ) {
		this.swap( this.gallery.prev( ) );
	  }
	
	, showNext: function( ) {
		this.swap( this.gallery.next( ) );
	  }
	
	, swap: function( asset ) {
		var canvas = this.getCanvas( );
		var slide = canvas.getElements( 'img.litebox_media_slide' )[0];
		
		slide.get( 'tween' ).addEvent( 'complete', function( ) {
			slide.addEvent(
				 'load'
				, function( ) {
					this.removeEvents( 'load' );
					this.get( 'tween' ).removeEvents( 'complete' );
					this.get( 'tween' ).addEvent( 'complete', function( ) {
						this.get( 'tween' ).removeEvents( 'complete' );
						canvas.removeClass( 'waiting' );
					}.bind( this ) );
					this.fade( 'in' );
				  }
			);
			
			slide.set( 'src', asset.src );
			slide.setStyles( { width: asset.dim_x, height: asset.dim_y } );
			canvas.setStyle( 'width', asset.dim_x );
			this.updateCanvasPosition( 'horizontally' );
			
			canvas.getElements( '.liteboxToolbar .counter' )[0].set(
				 'text'
				, ( this.gallery.cursor + 1 )+' of '+this.gallery.count( )
			);
			
			this.getFullCaption.call( this, asset.caption, asset.credit, asset.credit_site ).replaces( canvas.getElements( '.caption' )[0] );
		  }.bind( this )
		);
		
		canvas.addClass( 'waiting' );
		slide.fade( 'out' );
	  }
	
} );



var onFlashLoad, onFlashPlay, onFlashPause;

/*class*/ function FlashVaultLiteGallery( asset, gallery ) /*extends VaultLiteGallery*/ {
	
	if( gallery ) {
		this.setGallery( gallery );
	}
	
	onFlashLoad = this.onload.bind( this );
	onFlashPlay = this.onplay.bind( this );
	onFlashPause = this.onpause.bind( this );
	
	this.hardpoint = this.hardpoint+'_'+asset.filename;
	
	VaultLiteGallery.apply( this, [ this.hardpoint, asset  ] );
}

extend( FlashVaultLiteGallery.prototype, VaultLiteGallery.prototype );
extend( FlashVaultLiteGallery.prototype, {
	  hardpoint: 'uvFlashVaultGalleryHardpoint'
	, swiff: null
	, status: null
	, uvt: 'video_gallery'
	
	, onload: function( id ) {
		var box = LiteboxController.retrieveLitebox( id );
		
		Swiff.remote( $( box.swiff ), 'api_addEventListener', 'onPlay', 'onFlashPlay' );
		Swiff.remote( $( box.swiff ), 'api_addEventListener', 'onPause', 'onFlashPause' );
		
		this.getCanvas( ).setStyle( 'width', this.asset.dim_x );
		bind( box, VaultLiteGallery.prototype.didSetCanvas )( );
		box.didLoadCanvas( );
		this.updateCanvasPosition( );
		this.uncloakCanvas( );
		box.play( );
	  }
	
	, onplay: function( id ) {
		LiteboxController.retrieveLitebox( id ).status = 'playing';
	  }
	
	, onpause: function( id ) {
		var box = LiteboxController.retrieveLitebox( id );
		if( box.status != 'autopaused' ) {
			box.status = 'paused';
		}
	  }
	
	, play: function( ) {
		$( this.swiff ).api_play( );
	  }
	
	, pause: function( ) {
		this.status = 'autopaused';
		$( this.swiff ).api_pause( );
	  }
	
	, paint: function( ) {
		return bind( this, VaultLiteGallery.prototype.paint )( '<div id="'+this.hardpoint+'"><p class="alert">This content requires the <a href="http://get.adobe.com/flashplayer/">Adobe Flash Player &raquo;</a></p></div>' );
	  }
	
	, didSetCanvas: function( ) {
		var a = this.asset;
		
		this.getCanvas( ).addClass( 'flashVideoVaultBox' );
		
		if( getFlashVersion( ) >= a.player_version ) {
			this.swiff = new Swiff( 'http://vimeo.com/moogaloop.swf', {
				  width: a.dim_x
				, height: a.dim_y
				, container: $( this.hardpoint )
				, vars: {
					  clip_id: a.filename
					, width: a.dim_x
					, height: a.dim_y
					, js_api: 1
					, js_onLoad: 'onFlashLoad'
					, js_swf_id: this._liteboxID
				  }
				, properties: {
					 'class': 'litebox_media_slide'
				  }
				, params: {
					  allowfullscreen: 'true'
					, allowscriptaccess: 'always'
				  }
			} );
		}
		else {
			$( this.getCanvas( ) ).addClass( 'alert' );
			bind( this, VaultLiteGallery.prototype.didSetCanvas )( );
			this.didLoadCanvas( );
		}
		
		this.getCanvas( ).setStyle( 'width', a.dim_x );
		this.updateCanvasPosition( );
		
//			this.getCanvas( ).setStyle( 'height', a.dim_y );
	  }
	
	, showPrevious: function( ) {
		this.dispose( );
		var prev = this.gallery.prev( );
	  }
	
	, showNext: function( ) {
		this.dispose( );
		var next = this.gallery.next( );
	  }
	
	, hideCanvas			: function( ) {
		this.getCanvas( ).style.visibility = 'hidden';
		if( this.swiff ) {
			$( this.swiff ).style.visibility = 'hidden';
			if( this.swiff && this.status != 'paused' ) {
				this.pause( );
			}
		}
	  }
			
	, showCanvas			: function( ) {
		if( this.hasShown ) {
			$( this.swiff ).style.visibility = 'visible';
		}
		else {
			this.hasShown = true;
		}
		
		this.getCanvas( ).style.visibility = 'visible';
		if( this.swiff && this.status == 'autopaused' ) {
			this.play( );
		}
	  }
	
	, hideFrame			: function( ) {
		if( this.toolbar ) {
			this.toolbar.node.fade( 'hide' );
		}
		this.getFrame( ).style.visibility	= 'hidden';
	  }
		
	, showFrame			: function( ) {
		if( this.toolbar ) {
			this.toolbar.node.fade( 'show' );
		}
		this.getFrame( ).style.visibility = 'visible';
	  }
	
	, dispose:	function( ) {
		bind( this, Litebox.prototype.dispose )( );
	}
	
/*		, updateCanvasPosition: function( ) {
		
	  }*/
  
} );













































/* class */ function AroostookLiteMap( ) /* extends Litebox */ {
	Litebox.apply( this, [] );
	
	this.setCanvas(
		document.createElement( 'div' )
		);
		
	this.getCanvas( ).id = 'galleryBox';
	this.getCanvas( ).className = 'liteboxCore';

	this.setIgnoreCanvasClick( true );
	
	
	
	}
	
extend( AroostookLiteMap.prototype, Litebox.prototype );
extend( AroostookLiteMap.prototype, {

	prepareMap					:	function( ) {

		this.cloakCanvas( );
		setHTML(
			this.getCanvas( ),
			'<img src="'+full_auri( 'resource/aroostook/image/main_map_lg.jpg' )+'" alt="Click on a Region to View" usemap="#AroostookMapLarge" />'+
			'<div class="closeLite" style="font-size: 10px; float: right;">'+
				'<a style="color: #666666;" href="#" onclick="bind( LiteboxController.getFrontLitebox( ), LiteGallery.prototype.dispose )( ); return false;">close [ x ]</a>'+
			'</div><div style="clear:right;"></div>'
		);

	   	this.getCanvas( ).style.backgroundColor = 'white';
	   	this.getCanvas( ).style.padding = '10px 10px 20px 10px';
	   	this.getCanvas( ).style.width = '570px';
	   	this.getCanvas( ).style.height = '594px';

		

	   	this.updateCanvasPosition( );
	   	
//	   	this.getCanvas( ).style.top = '0px !important';
//	   	this.getCanvas( ).style.left = '0px !important';
	   	
	   	
	   	this.uncloakCanvas( );
	   	
	   	return this;
	}
});










