/* ---------------------------------
  @ Overlay Manager
----------------------------------- */
function overlayManager( elem, heightMatch ) { this.init(elem, heightMatch) }

overlayManager.prototype = {

	init : function( elem, heightMatch )
	{
		
		if (isUndefinedOrNull(elem, heightMatch)) {
			this.elem = elem;
			this.heightMatch = heightMatch;
		} else {
			log('Cannot initialize overlay');
		};
		
	},
	
	zoom : function( content )
	{
		// resize all
		this.resizeZoom();
		
		// show overlay and set up close handler
		var self = this;
		this.elem
			.show()
			.children('p.close a')
				.click( function() {
					self.closeZoom();
				})
		
		// set up resize handler
		window.onresize = function()
		{
			overlayMgr.resizeZoom();
		}
		
		// set html for image
		$(content).appendTo('#overlay p.image')
	},
	resizeZoom : function()
	{
		this.elem
			.children('div')
				.width( this.heightMatch.width() )
				.height( this.heightMatch.height() )
			.end()
			.children('p')
				.width( this.heightMatch.width() )
	},
	closeZoom : function()
	{
		// clear window resize handler
		window.onresize = null;
		
		// hide the overlay and clear the image
		this.elem
			.hide()
			.children('p.image')
				.empty()
		
	},
	
	
	
	
	
	
	/* -------------------------------
	  @ Zoom image functions (requires special monitoring
	------------------------------- */
	zoomImage : function( obj )
	{
		// resize all
		this.resizeZoom();
		
		// show overlay and set up close handler
		var self = this;
		this.elem
			.show()
			.children('p.close a')
				.click( function() {
					self.closeZoom();
				})
		
		// set up resize handler
		window.onresize = function()
		{
			overlayMgr.resizeZoom();
		}
		
		// create the image
		$("<img src='' width='1' height='1' alt='' />").appendTo('#overlay p.image');
		
		// clear interval
		clearInterval(this._loadinterval);
		
		// verify url and startLoad
		if (obj.path != undefined) {
			this.startLoad(obj.path);
		} else {
			console.log("ERROR - imageLoader cannot initialize load, url is undefined.");
		};
	},
	startLoad : function( path )
	{
		// set loading flag to true
		this._loading = true;
		
		// set reference image path (this will clear the previous ref)
		this.setRef();
		this.setRefSrc( path );
		
		// start monitoring interval
		var self = this;
		this._loadinterval = setInterval( function() { self.monitorLoad(); }, 500);
	},
	monitorLoad : function()
	{
		if (this.getRefWidth() > 0 && this.getRefHeight() > 0) {
			
			// clear interval
			clearInterval(this._loadinterval);
			
			// set the image
			this.setImage($('#overlay p.image img'));
			
			// set the primary image source
			this.setImageSrc( this.getRefSrc() );
			
			// set width and height
			this.setImageWidth( this.getRefWidth() );
			this.setImageHeight( this.getRefHeight() );
		
			// apply loading class to image
			this.getImage().addClass('loaded');
		
			// reset loading flag to false
			this._loading = false;
			
		}
	},
	
	/* -------------------------------
	  * Primary Image
	-------------------------------  */
	setImage : function( ref ) { this._image = ref },
	getImage : function() { return this._image; },
	
	// get/set zoom image
	setImageSrc : function( image ) { this.getImage().attr('src', image) },
	getImageSrc : function() { return this.getImage().attr('src') },
	
	// get set zoom width/height
	setImageWidth : function( width ) { this.getImage().attr('width', width) },
	getImageWidth : function() { return this.getImage().attr('width') },
	
	setImageHeight : function( height ) { this.getImage().attr('height', height) },
	getImageHeight : function() { return this.getImage().attr('height') },
	
	/* -------------------------------
	  @ Reference Image
	------------------------------- */
	setRef : function() 
	{ 
		this.clearRef();
		this._ref = new Image();
	},
	
	getRef : function() { return this._ref; },
	clearRef : function() { delete this._ref },
	
	// get/set zoom image src
	setRefSrc : function( image ) { this.getRef().src = image;},
	getRefSrc : function() { return this.getRef().src; },
	
	// get zoom image values
	getRefWidth : function() { return this.getRef().width; },
	getRefHeight : function() { return this.getRef().height; }
	

};