﻿// Subscription events from the iframe Flash Object =================================================================
// This evnet fires whenever files are added or removed from the Upload dialog
function fnFilesChanged(fileCount)
{
	winUpload.disableUpload(fileCount == 0);
}

// This event fires after all files have been uploaded
// arUploadedFiles - [ {fileName: 'test1.jpg', size: 13423}, ... ] (size is in bytes)
function fnUploadComplete(arUploadedFiles)
{
	winUpload.disableButtons(true);
	winUpload.hide();
	
	// set a delay so the hide effect completes and then do whatever you need to do
	window.setTimeout("window.location.reload(true)", 350);
}

// This event fires if the upload is cancalled from the progress dialog
function fnUploadCancel()
{
	winUpload.disableButtons(false);
}

// Singleton
var winUpload = new function wu()
{
	var win = null;
	var init = false;
	var firstLoad = true; 
	var f = null; 
	var fo = null;
	
	function getGalleryId()
	{
		var qString = new PageQuery(document.location.search)
		return qString.getValue("img_gallery_id");
	};
	
	return {
		show: function(el)
		{
			if (!init)
			{
				//Ext.get("divUpload").setStyle("position", "relative");
				win = new Ext.Window({
						title:'File Upload', 
						contentEl:'divUpload',
						layout:'fit', 
						width:460,
						height:230,
						maximizable:false,
						resizable:true, 
						closeAction:'hide',
						plain:false, 
						modal:true,
						buttons: [
							{ text:'Upload', handler: this.upload, disabled: true }, 
							{ text:'Cancel', handler: this.hide }
						], 
						buttonAlign:'right'
				});
				
				win.on("show", this.winShow);
				win.on("hide", this.winHide);
				init = true;
			}
			if (document.getElementById('headerHide')) 
			{
				document.getElementById('headerHide').style.display = "none"; 
			}
			win.show(el);
		},
		
		// Create the flash object and hookup events on the first showing -- fixes a bug in IE
		winShow: function()
		{
			if (firstLoad)
			{
				f = getIframDoc("imgUp");
				
				// TICKET: 44412 HACKs for mac FF 3.0.3 - 10/21/2008 DA
				if (!f.writeFlash)
				{
					setTimeout("winUpload.winShow()", 100);
					return;
				}				
				if (!f.document.getElementById('divFlash'))
				{
					setTimeout("winUpload.winShow()", 100);
					return;
				}
				
				
				f.writeFlash();
				fo = f.flashUpload
					
				if (fo && fo.checkFlash())
				{	
					fo.on("FilesChange", parent.fnFilesChanged);
					fo.on("UploadComplete", parent.fnUploadComplete);
					fo.on("UploadCancel", parent.fnUploadCancel);
					fo.parentContainer = winUpload;
					
					// Specify file types and allowed options	- docs, flash, images, video, all
					fo.fileTypes = ['images'];
					fo.allowImageProps = false;
					fo.allowVideoProps = false;
					
					// Single file Mode
					fo.singleFileMode = false;
					
					// Initialize the grid so we can add the correct number of columns
					f.fileGrid.init();
				}
				
				Ext.get("divUpload").setVisible(true);
				f.renderButtons();
				firstLoad = false;
			}
			
			// Flash 10 adjustments - TS 10/27/2008 
			// SetTimeout is being called to give the movie time to initialize - Ugly
			setTimeout( function() {		
				if (fo.SetFileTypes) { fo.SetFileTypes(['images']); }		
				if (fo.SetSingleFileMode) { fo.SetSingleFileMode(false); }
			}, 200);
			
		},
		
		winHide: function()
		{
			fo.removeAll();
			if (document.getElementById('headerHide')) 
			{
				document.getElementById('headerHide').style.display = ""; 
			}
		},
		
		hide:	function()
		{
			fo.removeAll();
			win.hide();
		},
		
		setHeight: function(iHeight)
		{
			win.setHeight(iHeight);
		},
		
		getFiles: function()
		{
			return fo.getFiles();
		},
	
		disableUpload: function(bDisable)
		{
			if (bDisable) { win.buttons[0].disable(); }
			else { win.buttons[0].enable(); }
		},
	
		disableButtons: function(bDisable)
		{
			if (bDisable) 
			{ 
				win.buttons[0].disable(); 
				win.buttons[1].disable();
				win.tools.close.hide();
			}
			else 
			{ 
				win.buttons[1].enable(); 
				if (f.fileGrid.fileCount() > 0) { win.buttons[0].enable(); }
				else { win.buttons[0].disable(); }
				win.tools.close.show();
			}
		}, 
		
		upload: function()
		{
			winUpload.disableButtons(true);
			
			// set the file upload options
			fo.testMode = false; // testMode does a file exists check, but doesn't actually upload any files
			fo.createThumbs = true;
			fo.thumbsDirectory = 'thumbs'; // if the createThumbs option is specified, this folder will be used for storage
			fo.thumbSize = 120;
			fo.resizeTo = 450;	// 0 means no resize
			fo.uploadDirectory = "images/gallery" + getGalleryId();	// Don't include the root HOA folder
	
			//alert(fo.uploadDirectory);
			fo.upload();
		}
	};
	
}();

function getGalleryId()
{
	var qString = new PageQuery(document.location.search)
	return qString.getValue("img_gallery_id");
}

