// Quick uploads
// Copyright (c) 2006
// Muzaffar Mahkamov


function QUHandler(formName, resultName)
{
	this._mForm = YAHOO.util.Dom.get(formName);
	this._mResultForm = YAHOO.util.Dom.get(resultName);
	
	this.handleSuccess = function(o)
	{
		if(o.responseText !== undefined)
		{
			var result = o.responseText;
			this.processResult(result);
		}
		
		this._mProgressDlg.hide();
	}
	
	this.processResult = function(data)
	{
		var result = data.parseJSON();
		
		if(result.code == 0)
		{
			PIX.showReport(result['messages']);

			YAHOO.util.Dom.setStyle(this._mForm.id, 'display', 'block');
		}
		else if(result.code == 1)
		{
			if(result.thumb >= 0)
			{
				this._mResultForm.blogthumb.value = '<a href="http://' + result.sitedomain + '/view' + result.image + '/' + result.imagefile + '"><img src="http://' + result.sitedomain + '/thumb' + result.thumb + '/' + result.thumbfile + '" title="' + result.description + '" /></a>';			
				this._mResultForm.forumthumb.value = '[url=http://' + result.sitedomain + '/view' + result.image + '/' + result.imagefile + '][img]http://' + result.sitedomain + '/thumb' + result.thumb + '/' + result.thumbfile +'[/img][/url]'
				this._mResultForm.ljthumb.value = this._mResultForm.blogthumb.value;
				this._mResultForm.directthumb.value = 'http://' + result.sitedomain + '/thumb' + result.thumb + '/' + result.thumbfile;
			}
			
			this._mResultForm.bloglink.value = '<a href="http://' + result.sitedomain + '/"><img src="http://' + result.sitedomain + '/img' + result.image + '/' + result.imagefile + '" title="' + result.description + '" /></a>';
			this._mResultForm.forumlink.value = '[url=http://' + result.sitedomain + '/][img]http://' + result.sitedomain + '/img' + result.image + '/' + result.imagefile + '[/img][/url]';
			this._mResultForm.ljlink.value = this._mResultForm.bloglink.value;
			this._mResultForm.directlink.value = 'http://' + result.sitedomain + '/img' + result.image + '/' + result.imagefile;
						
			YAHOO.util.Dom.setStyle(this._mForm.id, 'display', 'none');
			YAHOO.util.Dom.setStyle(this._mResultForm.id, 'display', 'block');
		}
	}
	
	this.callback =
	{
	   upload: this.handleSuccess,
	   argument: [],
	   scope: this
	}
	
	var val = new Validator();
	
	val.AddRequired('title');
	val.AddRequired('pic');
//	val.AddEmail('email');
	val.AddRequired('code');
	
	if(val.Check())
	{	
		PIX.hideReport();
		
		var formObject = this._mForm;
	
		YAHOO.util.Connect.setForm(formObject, true);
	
		var cObj = YAHOO.util.Connect.asyncRequest('POST', 
											   '/quick/', 
												this.callback);
				
		this._mProgressDlg = new YAHOO.widget.Panel("wait", 
								{ width:"240px", 
								  fixedcenter:true, 
								  underlay:"shadow", 
								  close:false, 
								  visible:true, 
								  draggable:true, 
								  modal:true, 
								  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
								  } 
								 );
								 
		this._mProgressDlg.setHeader("Идет загрузка...");
		this._mProgressDlg.setBody('<img src="\/img\/progress.gif"\/>');

		this._mProgressDlg.render(document.body);
	}
	else
	{
		PIX.showReport(val.errors);
	}
}