/* ---------------------------------
  @ flickr photo manager
----------------------------------- */
function photoManager( thumbnails, popup, items ) { this.init(thumbnails, popup, items) };

photoManager.prototype = {

	init : function( thumbnails, popup, items )
	{
		if (thumbnails != undefined && popup != undefined && items != undefined) {
			
			// set refs to elements
			this.thumbnails = thumbnails;
			this.popup = popup;
			
			// resize the popup
			this.resizePopup();
			window.onresize = function() { photoMgr.resizePopup() };
			
			// get the image
			this.image = this.popup.find('img');
			
			// set the image to the current <img> element
			this.setImage(this.image);
			
			// set up thumbnails
			var self = this;
			$.each(
				items,
				function(i, item)
				{
					var elem = $(self.thumbnails[i]);
					elem
						.attr('src', item.media.m.replace('_m.jpg', '_s.jpg'))
						.parent()
							.attr('href', 'javascript:void(null);')
							.attr('title', item.title)
							.click( function() {
							
								self.open(item.media.m.replace('_m.jpg', '.jpg'));
							
								/* omniture
								var s=s_gi('ascanceautopartslive');
								s.linkTrackVars='events,eVar11,channel,hier1,prop1';
								s.linkTrackEvents='event10';
								s.events='event10';
								s.eVar11=item.title;
								s.channel='Cruz Blog';
								s.hier1='Cruz Blug, Photo Gallery';
								s.prop1=item.title;
								s.tl(this,'o', item.title);
								*/
								
							});
				});
			
			// close button
			var self = this;
			this.popup
				.click( function() {
					self.close();
				});
			
			// also put close function on overlay
			overlayMgr.elem
				.click( function() {
					self.close();
				});
			
		} else {
			log('Cannot initialize');
		};
	},
	
	open : function( link )
	{
		// if data is not null, activate it
		if (link != null && link != undefined) {
			
			// show overlay
			overlayMgr.show();
			
			// make sure image is hidden
			this.image.hide();
			
			// then show popup
			this.popup.show();
		
			// load the image
			this.load(link);
		
		};
	},
	
	close : function()
	{
		overlayMgr.hide();
		
		// hide popup and image
		this.popup.hide();
		this.image.hide();
		
		// remove border class
		this.image.removeClass('loaded');
		
		// set image src to null
		this.setImageSrc('javascript:void(null);');
		
		// set width and height
		this.setImageWidth( 1 );
		this.setImageHeight( 1 );

		// set flag
		this.isopen = false;
	},
	
	getImageY : function( h )
	{
		// current scrollPosition
		var sT = $(window).scrollTop();
		var vH = $(window).height();

		// set target y
		return (sT + (vH/2) - (h/2));
	},
	setImageY : function( y ) { this.image.css('top', y) },
	resetImageY : function( h ) { this.setImageY( this.getImageY( h ) ) },
	
	resizePopup : function()
	{
		// resize and reposition popup to show just loader
		this.popup.width( $(window).width() );
		this.popup.height( $(window).height() );
	},
	
	
	
	/* -------------------------------
	  @ Load functions
	------------------------------- */
	load : function( url )
	{
		// clear interval
		clearInterval(this._loadinterval);
		
		// verify url and startLoad
		if (url != undefined) {
			this.startLoad(url);
		} else {
			console.log("ERROR - imageLoader cannot initialize load, url is undefined.");
		};
	},
	startLoad : function( url )
	{
		// set loading flag to true
		this._loading = true;
		
		// set reference image path (this will clear the previous ref)
		this.setRef();
		this.setRefSrc(url);
		
		// start monitoring interval
		var self = this;
		this._loadinterval = setInterval( function() { self.monitorLoad(); }, 500);
	},
	monitorLoad : function()
	{
		if (this.getRefWidth() > 0 && this.getRefHeight() > 0) {

			var w = this.getRefWidth();
			var h = this.getRefHeight();

			// clear interval
			clearInterval(this._loadinterval);
			
			// set the primary image source
			this.setImageSrc( this.getRefSrc() );
			
			// set width and height
			this.setImageWidth( w );
			this.setImageHeight( h );

			// set image y-position - pass along the height just to make sure (dumb IE6 bug)
			this.resetImageY( h );
			
			// add border class
			this.image.addClass('loaded');
		
			// show image, then fade it in
			this.image.fadeIn('slow');
			
			// 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; }
	
};

