var PhotoPanda = new Class({

	visible : false,
	uploading : false,
	Feeback : null,
	uploaderrors : [],
	maxidx : -1,
	gooduploads: 0,
	baduploads: 0,
	
	clone : function() {
		
		var e = new Element('div',{id:'submit-photos-wrapper', style:'overflow:hidden; position:relative;'});
		e.set('html',$('submit-photos-wrapper').get('html'));
		e.replaces($('submit-photos-wrapper'));
		
		if(Context.loggedin == 1) {
			oPhotoPanda.fancynize();
		}
		
	},
	
	fancynize : function() {
		
		/* if(Browser.Engine.trident) {
			location.href = location.href + '#uploader';	
		} */
		
		//alert('moo 0');
		
		up = new FancyUpload2($('photo-upload-status'), $('photo-upload-list'), { // options object
			// we console.log infos, remove that in production!!
			verbose: false,
			
			// url is read from the form, so you just have to change one place
			url: $('submit-photos-form').action,
			
			// path to the SWF file
			path: '/js/FancyUploader/Swiff.Uploader.swf',
			
			data: {'onetimekey':$('one-time-key').get('value')},
			
			// remove that line to select all files, or edit it, add more items
			typeFilter: {
				'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
			},
			
			// this is our browse button, *target* is overlayed with the Flash movie
			target: 'image-upload-browse-link',
			
			// graceful degradation, onLoad is only called if all went well with Flash
			onLoad: function() {
				//$('photo-upload-status').removeClass('hide'); // we show the actual UI
				//$('demo-fallback').destroy(); // ... and hide the plain form
				
				// We relay the interactions with the overlayed flash to the link
				this.target.addEvents({
					click: function() {
						return false;
					},
					mouseenter: function() {
						this.addClass('hover');
					},
					mouseleave: function() {
						this.removeClass('hover');
						this.blur();
					},
					mousedown: function() {
						this.focus();
					}
				});
	
				// Interactions for the 2 other buttons
				
				$('image-upload-clear-link').addEvent('click', function() {
					up.remove(); // remove all files
					setTimeout("$('num-files').set('text',0)",100);
					$('start-upload-container').setStyle('visibility','hidden');
					return false;
				});
	
				$('image-upload-start-link').addEvent('click', function() {
					up.start(); // start upload
					return false;
				});
			},
			
			// Edit the following lines, it is your custom event handling
			
			/**
			 * Is called when files were not added, "files" is an array of invalid File classes.
			 * 
			 * This example creates a list of error elements directly in the file list, which
			 * hide on click.
			 */ 
			onSelectFail: function(files) {
				
				var messages = [];
				
				files.each(function(file, idx) {
					
					var msg = (file.validationErrorMessage || file.validationError);
					
					messages[idx] = msg;
									
					/* new Element('li', {
						'class': 'validation-error',
						html: file.validationErrorMessage || file.validationError,
						title: MooTools.lang.get('FancyUpload', 'removeTitle'),
						events: {
							click: function() {
								this.destroy();
							}
						}
					}).inject(this.list, 'top'); */
					
				}, this);
				
				oPhotoPanda.Feedback = new ErrorBox(messages);
				oPhotoPanda.Feedback.show();
				
			},
			
			/**
			 * This one was directly in FancyUpload2 before, the event makes it
			 * easier for you, to add your own response handling (you probably want
			 * to send something else than JSON or different items).
			 */
			onFileSuccess: function(file, response) {
				var json = new Hash(JSON.decode(response, true) || {});
				
				if (json.get('status') == '1') {
					//file.element.addClass('file-success');
					//file.info.set('html', '<strong>Image was uploaded:</strong> ' + json.get('width') + ' x ' + json.get('height') + 'px, <em>' + json.get('mime') + '</em>)');
				} else {
					//file.element.addClass('file-failed');
					//file.info.set('html', '<strong>An error occured:</strong> ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
				}
			},
			
			/**
			 * onFail is called when the Flash movie got bashed by some browser plugin
			 * like Adblock or Flashblock.
			 */
			onFail: function(error) {
				switch (error) {
					case 'hidden': // works after enabling the movie and clicking refresh
						ToolBox.alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
						break;
					case 'blocked': // This no *full* fail, it works after the user clicks the button
						ToolBox.alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
						break;
					case 'empty': // Oh oh, wrong path
						ToolBox.alert('Oops... A required file was not found, please be patient will we try to fix this.');
						break;
					case 'flash': // no flash 9+ :(
						ToolBox.alert('Please install the latest Adobe Flash plugin.')
				}
			}
			
		});
						
		//alert('moo');
						
	},
	
	setUploading : function(uploading) {
		if(uploading) {
			oPhotoPanda.uploading = true;
			if($chk($('checkout-link-container'))) {
				$('checkout-link-container').setStyle('visibility','hidden');
				$('photo-uploader-close-icon').setStyle('display','none');
			} else if(Context.linkid.match(/inventory/)) {
				$('photo-uploader-close-icon[' + oPhotoPanda.albumhash + ']').setStyle('display','none');
			}
			
			$('image-upload-start-link').set('value',Localization.ajaxloading);
			$('submit-photos-controls-container').setStyle('visibility','hidden');
			$('clear-link-container').setStyle('visibility','hidden');
			
			var oStartButtonFx = new Fx.Tween($('start-upload-container'), {'link':'chain', 'duration':250, 
																	
				onComplete:function() { 
					$('image-upload-start-link').dispose();
					$('start-upload-container').set('html','' + Localization.ajaxloading);
					
					$('start-upload-container').fade('in');
					
				}
				
			});
			
			oStartButtonFx.start('opacity',0);
			
			
			
		} else {
			oPhotoPanda.uploading = false;
			if($chk($('checkout-link-container'))) {
				$('checkout-link-container').setStyle('visibility','hidden');
				$('photo-uploader-close-icon').setStyle('display','block');
			} else if(Context.linkid.match(/inventory/)) {
				$('photo-uploader-close-icon[' + oPhotoPanda.albumhash + ']').setStyle('display','block');
			}
			$('start-upload-container').setStyle('visibility','hidden');
			$('submit-photos-controls-container').setStyle('visibility','visible');
			$('clear-link-container').setStyle('visibility','visible');
		}
	},
	
	hide : function() {
		
		if(!this.uploading) {
			
			new Fx.Tween($('submit-photos-wrapper'), {
				onComplete: function() {
					
					oPhotoPanda = new PhotoPanda();
					
				}
			}).start('height', 0);
			
		}
			
	},
	
	show : function() {
		
		if(Context.sitemode == 1) {
			try {
				if(Context.loggedin == 1) {
					pageTracker._trackEvent('PhotoUploader','show','logged-in');		
				} else {
					pageTracker._trackEvent('PhotoUploader','show','not-logged-in');
				}
			} catch(err) {
				//do nothing;		
			}
		}
		
		if(!this.visible && !this.uploading) {
			//this.showButtonMouseover();
			this.visible = true;
			new Fx.Tween($('submit-photos-wrapper'), {
				onComplete: function() {
					oPhotoPanda.clone();
					setTimeout("$('submit-photos-container').highlight('#85a7b1')",10);
				}
			}).start('height', $('submit-photos-container').getSize().y + 21);
		} else {
			$('submit-photos-container').highlight('#85a7b1');
		}
		
	}

});
