// Author: Al Sierra
// Version: 6.30.2008
// Description: AJAX for portfolio screen
var portfolio = {
	_dataURL: 'scripts/php/portfolio.php',
	_docRef: false,
	_divShutter: false,
	_divWindow: false,
	_imageURL: '/images/uploads/',
	_id: false,
	_currentimage: false,
	
	Init: function(inDocumentRef,loc,id)
	{
		this._docRef = inDocumentRef;
		this._dataURL = loc+this._dataURL;
		this._id = id;
		this._currentimage = 1;
		// find our shutter DIV
		this._divShutter = $('shutter_p');
		if(!this._divShutter) {
			// shutter not found, create
			this._divShutter = this._docRef.createElement('div');
			this._divShutter.id = 'shutter_p';
			this._divShutter.style.display = 'none';
			this._divShutter.style.background = '#000000';
			this._divShutter.style.top = '0px';
			this._divShutter.style.left = '0px';
			this._divShutter.style.position = 'absolute';
			this._divShutter.style.height = '100%';
			this._divShutter.style.width = '100%';
			this._docRef.body.appendChild(this._divShutter);
		}
		// find our window DIV
		this._divWindow = $('window_p');
		if(!this._divWindow) {
			// popup DIV not found, create
			this._divWindow = this._docRef.createElement('div');
			this._divWindow.id = 'window_p';
			this._divWindow.style.display = 'none';
			this._docRef.body.appendChild(this._divWindow);
			// load the dispay HTML if not already loaded
		}
	},
	DisplayImages: function() 
	{	
		$('search_working_p').style.display = 'block';
		new Ajax.Updater('window_p', this._dataURL+'?cmd=pimage&pfolio_id='+this._id, {evalScripts: true,
		onComplete: function(){
		 	$('search_working_p').style.display = 'none';
			portfolio._divWindow.show();
			new Effect.Appear(portfolio._divWindow,{duration: .1, to:1.0 });
			portfolio._divWindow.style.zIndex = 9030;		
			portfolio._divShutter.style.zIndex = 8050;
			new Effect.Appear(portfolio._divShutter,{duration: .5, from: 0.0, to:0.45 });
		}
		});
	},
	SelectNextImage: function() 
	{	
		if(this.GetImage(this._currentimage + 1)){
			this._currentimage = this._currentimage + 1;
		}
	},
	SelectPrevImage: function() 
	{	
		if(this.GetImage(this._currentimage - 1)){
			this._currentimage = this._currentimage - 1;
		}
	},
	SelectImage: function(img,pn,max) 
	{
	 	$('img_bak').src=this._imageURL+img;
	 	$('search_working_p').style.display = 'none';
	 	this._displayButtons(pn,max);
	 	this._currentimage=pn;
		portfolio.Cancel();
	},
	GetImage: function(pn) 
	{
	 	$('search_working_p').style.display = 'block';
		var msg_data = {};
		msg_data.pn = pn;
		// fire an AJAX update
		var ajaxtags = new Ajax.Request(this._dataURL+'?cmd=GetImage&pfolio_id='+this._id,{
			parameters: $H(msg_data).toQueryString(),
			asynchronous: false
		});
		if (ajaxtags.transport.statusText != "OK") {
			alert("A problem was encountered when trying to contact the server.\nPlease try again.");
			return false;
		}
		if (ajaxtags.transport.responseText.length > 0 ) {
			try {
				var JSON = eval("("+ajaxtags.transport.responseText+")");
				if (undefined!==JSON.ehandler) {
					return false;
				}
			} catch(e) {
				alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
 		}
		$('img_bak').src=this._imageURL+JSON.img;
		this._displayButtons(pn,JSON.max);
		$('search_working_p').style.display = 'none';
		return true;	
	},
	_displayButtons: function(pn,max) 
	{
		if(pn == max){
		 	$('next-p').style.display = 'none';
		} else {
		 	$('next-p').style.display = 'block';
		}
		
		if(pn <= 1){
		 	$('prev-p').style.display = 'none';
		} else{
			$('prev-p').style.display = 'block';
		}
	},
	DisplayOverview: function() 
	{	
		$('img_info').style.display = 'block';
	},
	HideOverview: function() 
	{	
		$('img_info').style.display = 'none';
	},
	Cancel: function() 
	{
		new Effect.Appear(portfolio._divWindow,{duration: .5, to:0.0 });
		new Effect.Appear(portfolio._divShutter,{duration: .5, to:0.0, afterFinish: portfolio._CancelHide })
	},
	_CancelHide: function() 
	{
		portfolio._divWindow.hide();
		portfolio._divShutter.hide();
	}
}