/*
 * @link 		: http://www.2improve.nl/
 * @copyright 	: 2006 2improve
 * @author		: H.R.G. Vrolings <henry@2improve.nl>
 * @package		: PHPictureshop
 * @version 	: 0.2

 *    This file is part of PHPictureshop.
 *
 *    PHPictureshop is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation;

 *    PHPictureshop is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.

 *    You should have received a copy of the GNU General Public License
 *    along with PHPictureshop; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
    
*/

_ps = function()
{
	this.abspath 			= '';
	this.oIP				= '';
	this._debugging			= false;		// show debug info?
	this.oFlashversion		= 0;
	this.maxFileSize		= 750; // in kb (1000 = 1 MB)
	this.oResize 			= false;
	this.oResizer			= false; // at startup there is no window resizer
	this.oActiveWindow		= false; // at startup there is no active window
	this.oCropperContainer	= false;
	this.oCurrentWindows	= 0; // startup with 0 windows (used for creating unique window ID's)
	this.oCurrentWindowCount = 0; // used to count current open windows
	this.oMaxWindows		= 3; // maximum number of windows user may open ( 0 means unlimited )
	this.oCurrentZindex		= 0; // startup with 0 z-index
	this.oOrg_image			= new Array();
	this.oZoom 				= false;
	this.oOverImage 		= false;
	this.oImg_drag 			= false;
	this.oLoad_width;
	this.oOrg_width;
	this.oOrg_height;
	this.oLoad_height;
	this.oTool; 						// current selected tool
	this.oLoad 				= false; 	// editor is not yet loaded (i.v.m. mousemove e.a.)
	this.oThe_image;
	this.oInput 			= false; 	// Is user entering text in textfield ( don't capture keypress for shortcuts )
	this.oCropping 			= false;	

	
	
	/*########################################################################################
	
		START	: STARTUP-FUNCTIONS
	
	########################################################################################*/
	
	this.clear_undo = function()
	{
		iupdate.location.href = 'includes/process.php?org='+this.oThe_image+'&clearall';
	}

	// get the browsertype
	this.getbrowser = function()
	{
		this.browser = navigator.userAgent;
		if(this.browser.indexOf('MSIE') != -1 && this.browser.indexOf('Opera') == -1) 					this.browser = "IE";
		else if(this.browser.indexOf('Gecko') != -1) 													this.browser = "GECKO";
		else this.browser = 'not compatible';
		//this._debug('Browser: ' + this.browser + " | ");
	};
	
	this.set_Flashversion = function(version) // gets version from flashversion_detect.swf
	{
		var oSplit 	= version.split(" ");
		var oSplit	= oSplit[1].split(",");
		var version	= oSplit[0];
		this.oFlashversion = version;
	}
	
	this.check_flashversion = function()
	{
		oE 				= document.createElement("EMBED");
		oE.src 			= 'files/flashversion_detect.swf';
		oE.width 		= '1px';
		oE.height 		= '1px';
		document.body.appendChild(oE);	
	}
	
	this._debug = function(oString)
	{
		this.getE('debug').innerHTML += oString;
	}
	
	this.init = function()
	{	
		this.check_flashversion();
		this.clear_undo();
		this.getbrowser();
		this._mouse			= new _mouse();
		this._tools			= new _tools();
		this._ajax			= new _ajax();		
		if(this._debugging == true)
		{
			this.getE('debug').style.display = '';
			this.getE('iupdate').style.display = '';
		}
		if(this.abspath == '') this.abspath = document.location.toString().replace(this.basename(document.location.toString()),'');
		this.oCmenu = this.getE('contextmenu');
		this.oOverlay 			= this.getE('overlay');
		this.oSettings 			= this.getE('settings');	
		this.getE('file').value = this.oThe_image;	
		this.load_sequence();	
	}
	
	this.oSeq_id = 0;
	this.oSeq = new Array('Loading menu','Loading fonts','Loading images','Checking GD library version','Checking GD JPG support','Checking GD GIF Read support','Checking GD GIF Create support','Checking GD PNG support');
	
	this.load_sequence_show = function()
	{
		document.getElementById('dev_output').value = '1';
		if ( http_load.readyState == 4 ) 
		{	
				document.getElementById('dev_output').value 		= '2';	
				var response 										= http_load.responseText;			
				document.getElementById('loadoutput').innerHTML 	= response;
				_ps.oSeq_id++;
				if(_ps.oSeq.length > _ps.oSeq_id-1) _ps.load_sequence();
		}
	}	
	
	this.load_sequence = function()
	{
		if(this.oSeq.length > this.oSeq_id)
		{
				http_load = this.new_ajax();
				http_load.open('GET','includes/live.php?sequence='+this.oSeq[this.oSeq_id],true);
				http_load.onreadystatechange = this.load_sequence_show;
				http_load.send(null);
		}
		else
		{		
			    document.getElementById('loadoutput').innerHTML 		= 'Detecting flash 8 player :';
				if(this.oFlashversion > 8) 								document.getElementById('loadoutput').innerHTML 	+= ' true (' + this.oFlashversion + ')';
				else 													document.getElementById('loadoutput').innerHTML 	+= ' false (' + this.oFlashversion + ')';
				setTimeout('_ps.end_load_sequence()',500);
		}
	}
	
	this.end_load_sequence = function()
	{
		this.hide_settings();
		this.oLoad = 'done';
	}
	
	this.hide_settings = function()
	{
		this.oSettings.style.display = 'none';
		this.oOverlay.style.display = 'none';
		this.getE('splash').style.display = 'none';
	}
	
	this.oKeydown = function(e)
	{
		//document.getElementById('keycodes').select();
		if(this.oInput != true)
		{
			if(window.event) var unicode = window.event.keyCode; //IE
			else if(e.which) var unicode = e.which; // firefox
			else var unicode = 'undefined';
				//var unicode=e.keyCode? e.keyCode : e.charCode
				_ps.getE('keycodes_output').value = unicode;
				if(_ps.getE('keycodes_output').value == '61' 	|| _ps.getE('keycodes_output').value == '107') 	if(_ps.oZoom == false) _ps.oZoom = 10;
				if(_ps.getE('keycodes_output').value == '109' 	|| _ps.getE('keycodes_output').value == '45') 	if(_ps.oZoom == false) _ps.oZoom = -10;
				if(_ps.getE('keycodes_output').value == '48' 	|| _ps.getE('keycodes_output').value == '96')  	if(_ps.oInput == false) _ps.realsize();
				if(_ps.getE('keycodes_output').value == '49' 	|| _ps.getE('keycodes_output').value == '97')  if(_ps.oInput == false) _ps.fit_on_screen();
				_ps._tools.zoom();
		}
	}
	
	/*########################################################################################
	
		END		: STARTUP-FUNCTIONS
		START	: LOAD/SAVE FUNCTIONS
	
	########################################################################################*/
	
	this.openfile = function()
	{
		if(((this.oCurrentWindowCount+1) <= this.oMaxWindows) || this.oMaxWindows == 0)
		{
			if(!this.getE('open_file'))
			{
				var open_div = document.createElement('DIV');
				open_div.style.position = 'absolute';
				open_div.style.width 	= '100%';
				open_div.style.height 	= '100%';
				open_div.style.top 		= '0';
				open_div.style.left		= '0';
				open_div.style.zIndex 	= 2000;
				open_div.className 		= 'transparent';
				open_div.id				= 'open_file';
				open_div.innerHTML = '<table width="100%" style="height:100%;"><tr><td align="center" valign="middle"><embed src="files/fileUpload.swf?maxFileSize='+this.maxFileSize+'" wmode="transparent" width="425" /></td></tr></table>';
				document.body.appendChild(open_div);
			}
			else
			{
				this.getE('open_file').innerHTML = '<table width="100%" style="height:100%;"><tr><td align="center" valign="middle"><embed src="files/fileUpload.swf?maxFileSize='+this.maxFileSize+'" wmode="transparent" width="425" /></td></tr></table>';
				this.getE('open_file').style.display = '';
			}
		}
		else alert('Reached predefined maximum number of windows.\nTo continue please close 1 window first.');
	}
	
	this.set_oIMG_title = function(oTitle)
	{
		this.newWindow('files/originals/'+this.oIP+"_"+oTitle);
		this.hide_dialog();
	}
	
	this.hide_dialog = function()
	{
		setTimeout('_ps.hide(\'open_file\');',1500);
	}
	
	
	this.newWindow = function(oIMG_file)
	{
		this.oCurrentWindows++;
		this.oCurrentWindowCount++;
		this.oCurrentZindex++;
		var oWin 				= document.createElement("DIV");
		oWin.id  				= 'window_' + this.oCurrentWindows;
		oWin.style.zIndex		= this.oCurrentZindex;
		oWin.style.position		= 'absolute';;
		oWin.onmousedown		= function() { _ps.switchWindow(this.id);}
		
		// pad naar orginele file opslaan
		this.oOrg_image['window_' + this.oCurrentWindows] = oIMG_file;
		
		var oWindowContent		= document.createElement("DIV");
		oHTML 		= '<table style="width:100%;height:100%;border:solid 1px #fbfcfd;" cellpadding="0" cellspacing="0" onclick="_ps.switchWindow(\'window_' + this.oCurrentWindows+'\');">\n';
		oHTML 		+= '	<tr>\n';
		oHTML 		+= '		<td class="dragimgwindow" id="dragimgwindow_' + this.oCurrentWindows+'" ondblclick="_ps.fit_on_screen();"><div style="float:left">'+this.basename(oIMG_file)+'</div><div style="float:right"><img src="components/images/close.gif" onclick="_ps.closewindow(\'window_' + this.oCurrentWindows+'\');" class="windowbutton" /></div></td>\n';
		oHTML 		+= '	</tr>\n';
		oHTML 		+= '	<tr>\n';
		oHTML 		+= '	<td valign="top" align="left">\n';
		
		oHTML 		+= '		<div id="image_container_' + this.oCurrentWindows+'" class="imgarea" style="width:100%;height:100%;overflow:scroll;">\n';			
		oHTML 		+= '			<table style="width:100%;height:100%;" cellpadding="0" cellspacing="2">\n';
		oHTML 		+= '				<tr><td colspan="3" style="font-size:1px;">&nbsp;<\/td><\/tr>\n';
		oHTML 		+= '				<tr>\n';
		oHTML 		+= '				<td>&nbsp;<\/td>\n';
		oHTML 		+= '				<td width="5" height="5" valign="top" align="center" valign="middle"><div id="cropImageContainer_'+this.oCurrentWindows+'" class="cropImageContainer"><img src="'+oIMG_file+'" alt="" class="the_image" id="the_image_'+this.oCurrentWindows+'" style="background-image:url(components/images/loading.gif);background-repeat:no-repeat;background-position:center center;"  onmouseover="_ps.oOverImage = true;" onmouseout="_ps.oOverImage = false;"  onclick="_ps.switchWindow(\'window_' + this.oCurrentWindows+'\');" \/><\/div><\/td>\n';
		oHTML 		+= '				<td>&nbsp;<\/td>\n';
		oHTML 		+= '				<\/tr>\n';
		oHTML 		+= '				<tr><td colspan="3" style="font-size:1px;">55&nbsp;<\/td><\/tr>\n';
		oHTML 		+= '				<\/table>\n';
		oHTML 		+= '		</div>\n';
		
		oHTML 		+= '	</td>\n';
		oHTML 		+= '</tr>\n';
		oHTML 		+= '<tr>\n';
		oHTML 		+= '<td class="windowstatusbar">&nbsp;';	
		oHTML		+= '</td>\n';
		oHTML 		+= '</tr>\n';
		oHTML 		+= '</table>\n';
		
		oWindowContent.innerHTML 	= oHTML;
		oWindowContent.style.width 	= '100%';
		oWindowContent.style.height = '100%';
		oWin.appendChild(oWindowContent);
		this.getE('workarea').appendChild(oWin);
		
		this.oActiveWindow 		= this.getE(oWin.id);
		
		var newResizer 			= document.createElement("DIV");
		newResizer.id  			= 'resizer_' + this.oCurrentWindows;
		newResizer.className 	= 'resizer';
		if(this._debugging == true) newResizer.style.backgroundColor = '#FF9900';
		newResizer.onmousedown 	= function() { _ps.oResize=true; _ps.switchWindow(oWin.id); }
		newResizer.onmouseup 	= function() { _ps.oResize=false; }
		newResizer.style.zIndex	= parseInt(this.oCurrentZindex) + 1;
		this.getE('workarea').appendChild(newResizer);
		
		
		new Draggable(oWin.id,{handle:'dragimgwindow_' + this.oCurrentWindows,constraint:false});
		new Draggable('resizer_' + this.oCurrentWindows);
		
		this.getE('file').value			= oIMG_file;
		this.oIMG 						= this.getE("the_image_"+this.oCurrentWindows);
		this.oWindow 					= this.getE(oWin.id);
		this.oIMG_container 			= this.getE('image_container_' + this.oCurrentWindows);
		this.oResizer					= this.getE('resizer_' + this.oCurrentWindows);
		
		setTimeout('_ps.switch2new()',500);
		
		this.getE('windowcontainer').innerHTML += '<div id="windowcontainer_'+oWin.id+'" onclick="_ps.switchWindow(\''+ oWin.id+'\');" class="submenu_item" onmouseover="this.style.backgroundColor=\'#b2b4bf\';" onmouseout="this.style.backgroundColor=\'#FFFFFF\';">'+this.basename(oIMG_file)+'</div>';
		iupdate.location.href = 'includes/process.php?createpreview=1&oWindow='+oWin.id+'&file='+oIMG_file;
	}
	
	this.switch2new = function()
	{
		this.oOrg_width 				= this.oIMG.offsetWidth;
		this.oOrg_height 				= this.oIMG.offsetHeight;
		this.fit_on_screen();
		this.oCropperContainer 			= "cropImageContainer_"+this.oCurrentWindows;
		Cropper.idCropImage 			= "the_image_"+this.oCurrentWindows;
		Cropper.idContainer 			= "cropImageContainer_"+this.oCurrentWindows;
		Cropper.idShield 				= "divShield_"+this.oCurrentWindows;
		Cropper.idImageCropSelection 	= "imageCropSelection_"+this.oCurrentWindows;
		
		Cropper.divSelectionLeft 		= "divSelectionLeft_"+this.oCurrentWindows;
		Cropper.divSelectionTop 		= "divSelectionTop_"+this.oCurrentWindows;
		Cropper.divSelectionRight 		= "divSelectionRight_"+this.oCurrentWindows;
		Cropper.divSelectionBottom 		= "divSelectionBottom_"+this.oCurrentWindows;
		Cropper.init();
		
		this.resetCropper();
	}
	
	this.switchWindow = function(oWindow_id)
	{
		this.oActiveWindow 				= this.getE(oWindow_id);
		var oCurrent_id 				= oWindow_id.replace("window_","");
		this.oCurrentZindex++;
		this.oCurrentZindex++;

		
		this.oIMG 						= this.getE("the_image_"+oCurrent_id);
		this.oWindow 					= this.getE(oWindow_id);
		this.oWindow.style.zIndex 		= this.oCurrentZindex;
		this.oIMG_container 			= this.getE('image_container_' + oCurrent_id);
		this.oResizer					= this.getE('resizer_' + oCurrent_id);
		this.oResizer.style.zIndex		= parseInt(this.oCurrentZindex) + 1;
		var NewFile						= this.oIMG.src.toString().replace(this.abspath,"");
		NewFile							= NewFile.split('?');
		this.getE('file').value			= NewFile[0];
		
		
		this.oCropperContainer 			= "cropImageContainer_"+oCurrent_id;
		Cropper.idCropImage 			= "the_image_"+oCurrent_id
		Cropper.idContainer 			= "cropImageContainer_"+oCurrent_id;
		Cropper.idShield 				= "divShield_"+oCurrent_id;
		Cropper.idImageCropSelection 	= "imageCropSelection_"+oCurrent_id;
		Cropper.divSelectionLeft 		= "divSelectionLeft_"+oCurrent_id;
		Cropper.divSelectionTop 		= "divSelectionTop_"+oCurrent_id;
		Cropper.divSelectionRight 		= "divSelectionRight_"+oCurrent_id;
		Cropper.divSelectionBottom 		= "divSelectionBottom_"+oCurrent_id;
		
		this.resetCropper();
	}
	
	this.closewindow = function(oID)
	{
		this.hide(oID);
		var oCurrent_id 				= oID.replace("window_","");
		this.hide('resizer_'+oCurrent_id);
		this.hide('windowcontainer_'+oID);
		this.oCurrentWindowCount--;
		
		iupdate.location.href = 'includes/process.php?org='+this.oOrg_image[oID]+'&clearwindow='+oID;
	}
	

	/*########################################################################################
	
		END		: LOAD/SAVE FUNCTIONS
		START	: COMMON FUNCTIONS
	
	########################################################################################*/
	
	this.error = function(error)
	{
		this._tools.hide_all_settings();
		this.oSettings.style.display = '';
		this.oOverlay.style.display = '';
		this.getE('filter_preview').style.display = 'none';
		this.getE('settings:error').style.display = '';			
		this.getE('settings:error').innerHTML = error;	
	}
	
	this.getE = function(id)
	{
		return document.getElementById(id);
	}
	
	this.showhide= function(id)
	{
		t = this.getE(id);
		if(t.style.display == '') t.style.display = 'none';
		else t.style.display = '';
		
	}
	
	this.hide= function(id)
	{
		this.getE(id).style.display = 'none';
	}
	
	this.new_ajax = function()
	{
		if (this.browser == 'IE' ) return new ActiveXObject("Microsoft.XMLHTTP");
		else return new XMLHttpRequest();
	}
	
	this.trim = function(string)
	{
        var s = string.replace(/^\s*/, "");
        var s = string.replace(/&nbsp;/, "");
        return s.replace(/\s*$/, "");
    }
    
    this.realsize = function()
	{
		if(_ps.oCurrentWindowCount < 1) return false;
		this.oIMG.width 	= this.oOrg_width;
		this.getE('perc').value = 100;
		new_W								= this.getWindowWidth() - 200;
		new_H								= this.getWindowHeight() - 200;
	    
		this.oWindow.style.width 			= new_W + 44;
		this.oWindow.style.height 			= new_H + 80;
		
		this.oIMG_container.style.width 	= new_W + 40;
		this.oIMG_container.style.height 	= new_H + 40;
	}
    
    this.fit_on_screen = function()
	{
		if(_ps.oCurrentWindowCount < 1) return false;
		new_W								= this.getWindowWidth() - 200;
		new_H								= this.getWindowHeight() - 200;
	    
		this.oWindow.style.width 			= new_W + 44;
		this.oWindow.style.height 			= new_H + 80;
		
		this.oIMG_container.style.width 	= new_W + 40;
		this.oIMG_container.style.height 	= new_H + 40;
		
		this.oWindow.style.display 			= '';	
		this.oWindow.style.top 				= '3px';
		this.oWindow.style.left 			= '90px';		
		oWindow_H							= parseInt(this.oWindow.offsetHeight);// - 40;
		new_W								= parseInt(this.oWindow.offsetWidth);// - 20;
		oPerc 								= (new_W/oWindow_H) * 100;
		new_H 								= (this.oOrg_height/oPerc) * 100;	
		while(new_H > oWindow_H)
		{
			oPerc = oPerc + 25; // verkleiningsfactor
			new_H = (this.oOrg_height/(oPerc)) * 100;
			new_W = (this.oOrg_width/(oPerc))  * 100;		
			this.getE('function').value = oPerc;
		} 	
		if(new_W < this.oOrg_width) 	this.oIMG.width 	= new_W - 40;	
		else 							this.oIMG.width 	= this.oOrg_width - 40;	
		
		this.getE('perc').value = (parseInt(this.oIMG.width)/parseInt(this.oOrg_width))*100;
		
		this.resetResizer();
		
		
	}
	
	this.resetResizer = function()
	{
		if(_ps.oCurrentWindowCount < 1) return false;
		this.oResizer.style.left	= parseInt(this.oWindow.offsetLeft) + parseInt(this.oWindow.offsetWidth) - 10;
		this.oResizer.style.top 	= parseInt(this.oWindow.offsetTop) + parseInt(this.oWindow.offsetHeight) - 10;
		
	}
	
	this.getWindowWidth = function() 
	{
		var windowWidth = 0;
		if (typeof(window.innerWidth) == 'number') windowWidth = window.innerWidth;
		else 
		{
			if (document.documentElement && document.documentElement.clientWidth) windowWidth = document.documentElement.clientWidth;
			else 
			{
				if (document.body && document.body.clientWidth) windowWidth = document.body.clientWidth;
			}
		}
		return windowWidth;
	}


	this.getWindowHeight = function() 
	{
		var windowHeight = 0;
		if (typeof(window.innerHeight) == 'number') windowHeight = window.innerHeight;
		else 
		{
			if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
			else 
			{
				if (document.body && document.body.clientHeight) windowHeight = document.body.clientHeight;
			}
		}
		return windowHeight;
	}
	
	this.basename = function(oPath)
	{ 
		oPath = oPath.toString();
		oPath = oPath.split('?');
		oPath	= oPath[0];
		oSplit 	= oPath.split('\/');
		oFile 	= oSplit[oSplit.length-1];
		return oFile;	
	}
	
	this.check_numeric_input = function(oThis)
	{
		oD1 = oThis.value;
		oD = parseInt(oD1);
		if(isNaN(oD) && oD != '-') oD = 0;	
		if(oD != oD1) oThis.value = oD;	
	}
	
	this.check_constrain = function(type,value,target,inputfield_id)
	{
		oIn_ = this.getE(inputfield_id);
		if(oIn_.value == 'true')
		{
					if(type == 'width')
					{
						percentage		=	value/this.oOrg_width;
						targetvalue		=   percentage*this.oOrg_height;
					}
					if(type == 'height')
					{
						percentage		=	value/this.oOrg_height;
						targetvalue		=   percentage*this.oOrg_width;
					}
					
					_ps.getE(target).value = parseInt(targetvalue);
		}
	}
	
	this.constrain_img = function(oThis,inputfield_id)
	{
		oIn_ = this.getE(inputfield_id);
		if(this.basename(oThis.src) == 'constrain_no.gif') 
		{
			oThis.src = 'components/images/constrain_yes.gif';
			oIn_.value = 'true';
			
			oCheck_input = inputfield_id.replace("_constrain",""); // nu hebben we de id voor width en height field (b.v. van resize_constrain => resize
			
			if(this.getE(oCheck_input+'_width').value != '') 		this.check_constrain('width',this.getE(oCheck_input+'_width').value,oCheck_input+'_height',inputfield_id);
			else if(this.getE(oCheck_input+'_height').value != '') 	this.check_constrain('height',this.getE(oCheck_input+'_height').value,oCheck_input+'_width',inputfield_id);
		}
		else
		{
			oThis.src = 'components/images/constrain_no.gif';
			oIn_.value = 'false';
		}
	}
	
	/*########################################################################################
	
		END		: COMMON FUNCTIONS
		START	: MENU-FUNCTIONS
	
	########################################################################################*/
	
	
	this.resetCropper = function()
	{
		this.getE(Cropper.idImageCropSelection).style.width 	= this.oIMG.offsetWidth;
		this.getE(Cropper.idImageCropSelection).style.height 	= this.oIMG.offsetHeight;
		
		this.getE(Cropper.idShield).style.width 				= this.oIMG.offsetWidth;
		this.getE(Cropper.idShield).style.height 				= this.oIMG.offsetHeight;
		this.getE(Cropper.idImageCropSelection).src 			= this.oIMG.src;
		//this.oCropping = false;
		
		Cropper.reset();
		Cropper.init();
	}
	
	this.show_sub = function(id)
	{
		this.hide_all_subs();
		this.getE(id).style.display = '';
	}
	
	this.hide_all_subs = function()
	{
		oDivs = document.getElementsByTagName("DIV");
		for(i=0;i<oDivs.length;i++)
		{
			if(oDivs[i].id.substr(0,4) == 'sub_') oDivs[i].style.display = 'none';
		}
	}
	
	this.reset_img = function()
	{
		if(_ps.oCurrentWindowCount < 1) return false;
		this.oIMG.src 		= this.oOrg_image[this.oWindow.id];
		this.oIMG.removeAttribute('style');
		this.oIMG.removeAttribute('width');
		this.oOrg_width 	= this.oIMG.offsetWidth;
		this.oOrg_height 	= this.oIMG.offsetHeight;
		this.getE('function').value = '';
		this.getE('file').value = this.oOrg_image[this.oWindow.id];
		iupdate.location.href = 'includes/process.php?org='+this.oOrg_image[this.oWindow.id]+'&clearall';	
		this.fit_on_screen();
	}
	

}

var JSdocs = [
	"js/drag_drop/prototype.js",
	"js/drag_drop/scriptaculous.js",
	"js/colorpicker/color_select.js",
	"js/cropper/Toolkit.Events.js",
	"js/cropper/Cropper.js",
	"js/_ps.ajax.js",
	"js/_ps.mouse.js",		
	"js/_ps.tools.js"	
];


function _import() 
{
	for (var i=0; i<JSdocs.length; i++) document.write('<scr' + 'ipt src="'+JSdocs[i]+'" type="text\/j' + 'avascript">' + '<\/script>');
}

_import();

window.onload = function() 
{ 
	load_ps_vars();
	 _ps.init();
 	document.onmousemove 	= _ps._mouse.Move;
	document.onmousedown 	= _ps._mouse.Down;
	document.onmouseup 		= _ps._mouse.Up;
	document.onkeydown 		= _ps.oKeydown;
	document.onkeyup		= function() 
							{ 
								document.getElementById('keycodes_output').value = '';
								_ps.oZoom = false;
								
								if(_ps.oInput == false)
								{
									_ps.oIMG_container.style.overflow = 'scroll';
									
								}
							}
	document.oncontextmenu = function() { 

			_ps.oCmenu.style.left 	= _ps.oX;
			_ps.oCmenu.style.top 	= _ps.oY;	
			
			if(_ps.oCropping == true)
			{
				_ps.oCmenu.style.width = '100px';
				_ps.oCmenu.innerHTML = '<a href="javascript:void(0);" onmousedown="_ps._tools.crop_it();" class="contextitem"> &nbsp; &nbsp; &nbsp;Crop</a>';
				_ps.oCmenu.innerHTML += '<a href="javascript:void(0);" onmousedown="_ps._tools.dont_crop();" class="contextitem"> &nbsp; &nbsp; &nbsp;Cancel</a>';
				_ps.oCmenu.style.display = '';
				return false;
			}
	}
	
	window.onerror = function(msg, err_url, line) 
	{
			_ps._debug('\njs error: ' + msg + ' , ' + err_url + ' on line: ' + line);
			return true;
	}
	
	window.onunload = function() { window.open('includes/process.php?unload','','width=100,height=100'); }
}
