/*
	Queue Plug-in
	
	Features:
		*Adds a cancelQueue() method for cancelling the entire queue.
		*All queued files are uploaded when startUpload() is called.
		*If false is returned from uploadComplete then the queue upload is stopped.
		 If false is not returned (strict comparison) then the queue upload is continued.
		*Adds a QueueComplete event that is fired when all the queued files have finished uploading.
		 Set the event handler with the queue_complete_handler setting.
		
	*/

var SWFUpload;
if (typeof(SWFUpload) === "function") {
	SWFUpload.queue = {};
	
	SWFUpload.prototype.initSettings = (function (oldInitSettings) {
		return function () {
			if (typeof(oldInitSettings) === "function") {
				oldInitSettings.call(this);
			}
			
			this.customSettings.queue_cancelled_flag = false;
			this.customSettings.queue_upload_count = 0;
			
			this.settings.user_upload_complete_handler = this.settings.upload_complete_handler;
			this.settings.user_upload_start_handler = this.settings.upload_start_handler;
			this.settings.upload_complete_handler = SWFUpload.queue.uploadCompleteHandler;
			this.settings.upload_start_handler = SWFUpload.queue.uploadStartHandler;
			
			this.settings.queue_complete_handler = this.settings.queue_complete_handler || null;
		};
	})(SWFUpload.prototype.initSettings);

	SWFUpload.prototype.startUpload = function (fileID) {
		this.customSettings.queue_cancelled_flag = false;
		this.callFlash("StartUpload", [fileID]);
	};

/******************************************************/

	SWFUpload.prototype.ShowInfo = function () {	
		if(!document.getElementById("uploadedFilesInfo")){
			ht  = '<div class="uploadedFiles" id="uploadedFilesInfo">';
				ht += '<div class="auto-helper">'
					ht  += "Быстрое заполнение полей (выбранное поле у всех треков):<br/>";
					ht  += "<input value='' style='margin-left:0;'>";
					ht  += '<input id="chooseField" class="comboBox value0" style=""/>';
					ht  += "<a href='javascript:;' class='profileButton ok' onClick='autocompletetrack()'>Применить</a>";
					ht += '</div><br clear="All"/><br/>';
					ht += "Проверьте правильность названий треков и добавтье теги:";
			ht += '</div>';
			$('.uploaderHello').replaceWith(ht);
			
			$("#chooseField").combobox({
				data: ['Исполнитель', 'Альбом', 'Теги', 'Год'],
				arrowHTML: function() {
								return $('<a href="javascript:;" class = "ui-combobox-arrow" style="margin-top:5px;"></a>');
							}
			});
		}
		this.uploadedFilesWrapper = document.getElementById("uploadedFilesInfo");
		if (!document.getElementById('uploadedFilesConfirm')) {
			var h ="";
			h += "<div class='uploadedInfo' id='uploadedFilesConfirm'>";
			h += "<div>Добавьте загруженные треки в свои плейлисты:</div>";
			h += "<div class='uploadedFileForm'>";
			h += "<div class='uploadedFileName'>";
			h += "<div class='myPlaylists'><ul></ul></div>";
			h += "<div class='chosenPlaylists'><ul></ul></div>";			
			h += "<a class='newPlaylist' href='javascript:;' onclick='Uploader_Object(\"showAddpList\");'>Добавить новый плейлист</a><br style='clear:both'/>";						
			h += "</div>";
			h += "</div>";
			h += "<span style='display:block;margin:18px 0 5px 0;'>Для завершения загрузки нажмите кнопку Подтвердить.</span>";
			h += '<a href="javascript:;" class="profileButton" style="margin-left:7px;" onclick="confirmUploads();">Подтвердить</a>';
			h += "</div>";	

			$("#uploaderWnd .content").append(h);
		}

		Uploader_Object('plRefresh');
		$('a.uploaderButton').css('background', 'url("/media/images/uploader_finilize.gif") no-repeat top left');
		$('#uploaderBottomBanner').css('background-position', '0px -50px');
		uploaderState = 2;
	}

	SWFUpload.prototype.cancelQueue = function () {
		this.customSettings.queue_cancelled_flag = true;
		this.stopUpload();
		
		var stats = this.getStats();
		while (stats.files_queued > 0) {
			this.cancelUpload();
			stats = this.getStats();
		}
	};
	
	SWFUpload.queue.uploadStartHandler = function (file) {
		$('a.uploaderButton').css('background', 'url("/media/images/uploader_on.gif") no-repeat top left');
		$('#uploaderBottomBanner').css('background-position', '0px -25px');		
		uploaderState = 1;
		var returnValue;
		if (typeof(this.customSettings.user_upload_start_handler) === "function") {
			returnValue = this.customSettings.user_upload_start_handler.call(this, file);
		}
		
		// To prevent upload a real "FALSE" value must be returned, otherwise default to a real "TRUE" value.
		returnValue = (returnValue === false) ? false : true;
		
		this.customSettings.queue_cancelled_flag = !returnValue;

		return returnValue;
	};
	
	SWFUpload.queue.uploadCompleteHandler = function (file) {
		var user_upload_complete_handler = this.settings.user_upload_complete_handler;
		var continueUpload;
		if (file.filestatus === SWFUpload.FILE_STATUS.COMPLETE) {
			this.customSettings.queue_upload_count++;
		
		}

		if (typeof(user_upload_complete_handler) === "function") {
			continueUpload = (user_upload_complete_handler.call(this, file) === false) ? false : true;
		} else {
			continueUpload = true;
		}
		var stats = this.getStats();		

			if (stats.files_queued == 0) 
				if(this.customSettings.queue_upload_count!=0)
					this.ShowInfo();	

		if (continueUpload) {
			if (stats.files_queued > 0 && this.customSettings.queue_cancelled_flag === false) {
				this.startUpload();
			} else if (this.customSettings.queue_cancelled_flag === false) {
				this.queueEvent("queue_complete_handler", [this.customSettings.queue_upload_count]);
				this.customSettings.queue_upload_count = 0;
			} else {
				this.customSettings.queue_cancelled_flag = false;
				this.customSettings.queue_upload_count = 0;
			}	
		}
	};
}