fake_fileinput = function(fileinput_id, button_id, filename_id) {		
	var fileinput = document.getElementById(fileinput_id);
	var button = document.getElementById(button_id);
	
	
	update_filename = function(e) {
		if (!this.filename) {
			this.filename = '';
		}
		
		if (this.value.indexOf('/') > -1) {
			parts = this.value.split('/');
			filename = parts[parts.length-1];
		} else if (this.value.indexOf('\\') > -1) {
			parts = this.value.split('\\');
			filename = parts[parts.length-1];
		} else {
			filename = this.value;
		}
		document.getElementById(filename_id).value = filename;
		
		if (filename.length > 0 && this.filename != filename) {
			this.filename = filename; // update before alert
			
			var index = filename.lastIndexOf('.');
			var extension = filename.substring(index+1, filename.length).toLowerCase();
			
			if (extension != 'gif' && extension != 'jpg' && extension != 'jpeg' && extension != 'png') {
				dialog = make_alert('dialog', 'Attention...', 'The file extension \''+extension+'\' is not a supported image type.');
				dialog.show();
			}
		}
		
		this.filename = filename;
		
		button.blur();
	}
	
	YAHOO.util.Event.addListener(fileinput, 'change', update_filename);
	YAHOO.util.Event.addListener(fileinput, 'mouseout', update_filename);
	
	
	// this currently works in ie only
	fileinput.style.cursor = YAHOO.util.Dom.getStyle(button, 'cursor');
	
	
	YAHOO.util.Event.addListener(fileinput, 'mouseover', function() {
		YAHOO.util.Dom.addClass(button, 'hover');
	});
	
	YAHOO.util.Event.addListener(fileinput, 'mouseout', function() {
		YAHOO.util.Dom.removeClass(button, 'active');
		YAHOO.util.Dom.removeClass(button, 'hover');
	});
	
	YAHOO.util.Event.addListener(fileinput, 'mousedown', function() {
		YAHOO.util.Dom.addClass(button, 'active');
		YAHOO.util.Dom.removeClass(button, 'hover');
	});
	
	YAHOO.util.Event.addListener(fileinput, 'blur', function() {
		YAHOO.util.Dom.removeClass(button, 'active');
		YAHOO.util.Dom.removeClass(button, 'hover');
	});
	
	
	YAHOO.util.Dom.setStyle(fileinput, 'opacity', 0);
	
	fileinput.style.display = 'inline';
	
	var offset = 0;
	
	fileinput.parentNode.style.position = 'relative';
	
	fileinput.style.position = 'absolute';
	fileinput.style.zIndex = '2';
	
	if (YAHOO.env.ua.ie) { // ie can't handle the big font-size correctly
		var clipwidth = 60;
		
		var height = fileinput.offsetHeight;
		var width = fileinput.offsetWidth;
		var bheight = button.offsetHeight;
		var bwidth = button.offsetWidth;

		var clip = 'rect(auto auto auto '+(width-clipwidth)+'px)';
		
		var top = Math.round(Math.abs((bheight-height)/2)+button.offsetTop-offset)+'px';
	
		var left = Math.round(Math.abs((bwidth-clipwidth)/2)-(width-clipwidth)+button.offsetLeft)+'px';
		
		YAHOO.util.Event.addListener(button, 'click', function() {
			fileinput.click();
		});
	} else {
		fileinput.style.fontSize = '500%';
		
		var height = fileinput.offsetHeight;
		var width = fileinput.offsetWidth;
		var bheight = button.offsetHeight;
		var bwidth = button.offsetWidth;
		
		var clip = 'rect(10px '+(width-10)+'px '+(bheight+10)+'px '+(width-bwidth-10)+'px)';
		
		var top = (button.offsetTop-10-offset)+'px';
		var left = (button.offsetLeft-(width-bwidth-10)-offset)+'px';
	}
		
	fileinput.style.clip = clip;
	fileinput.style.top = top;
	fileinput.style.left = left;
}




start_upload = function() {
	this.blur(); // de-focus button

	var upload_form = document.getElementById('pics_upload_form');
	YAHOO.util.Connect.setForm(upload_form, true);
	
	if (!upload_form.wait_dialog) {
		upload_form.wait_dialog = new YAHOO.widget.Panel('wait_dialog', {
			width: '240px',  
			fixedcenter:true,  
			close:false,  
			draggable:false,  
			zindex:4000, 
			modal:true, 
			visible:false
			}  
		); 
     
		upload_form.wait_dialog.setHeader('Uploading image - please wait...'); 
		upload_form.wait_dialog.setBody('<img src="images/ajax-bar.gif" width="220" height="19" alt="" />'); 
		upload_form.wait_dialog.render(document.body); 
	
	}
	
	upload_form.wait_dialog.show();
	
	var callback = {
		upload: function(o) {
			//alert(o.responseText)
			parts = o.responseText.split('~');
			
			if (parts[0] == '1') {
				dialog = make_alert('dialog', 'Image upload successful', parts[1], function() {location.reload(true);} );
							
				upload_form.reset();
				upload_form.wait_dialog.hide();
				
				dialog.show();
			} else {
				dialog = make_alert('dialog', 'Image upload failure', parts[1]);
				
				upload_form.wait_dialog.hide();
				
				dialog.show();
			}
		},
		
		failure: function(o) {
			alert(o.responseText);
		
			upload_form.wait_dialog.hide();
		}
	}

	var request = YAHOO.util.Connect.asyncRequest('POST', 'manager-upload.php', callback); 

}



YAHOO.util.Event.onDOMReady(function() {
	 fake_fileinput('upload_1', 'upload_button_1', 'upload_filename_1');
	 
	 YAHOO.util.Event.addListener('upload_button', 'click', start_upload);
});
