/*
Uploadify v2.1.0
Release Date: August 24, 2009

Copyright (c) 2009 Ronnie Garcia, Travis Nickels

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
 * Cyber Image Manager
 *
 *
 * @package		Cyber Image Manager
 * @author		WSDLab
 * @copyright	Copyright (c) 2010, WSDLab.
 * @link		http://www.wsdlab.ru/
 * @since		Version 1.0
 * @file 		/js/jquery.uploadify.js
 */
 
 /*
	+ Добавленна поддержка языков
	- Убран по моему не нужны параметр 
		cancelImg      : 'cancel.png', // The path to the cancel image for the default file queue item container
		все описание этого обьекта находятся теперь в css
	* Исправленны ишибки в функции uploadifySettings теперь она работает корректно
*/
if(jQuery)(function(jQuery){jQuery.extend(jQuery.fn,{uploadify:function(options){jQuery(this).each(function(){settings=jQuery.extend({id:jQuery(this).attr('id'),uploader:'uploadify.swf',script:'uploadify.php',expressInstall:null,folder:'',height:30,width:110,wmode:'opaque',scriptAccess:'sameDomain',fileDataName:'Filedata',method:'POST',queueSizeLimit:999,simUploadLimit:1,queueID:false,displayData:'percentage',onInit:function(){},onSelect:function(){},onQueueFull:function(){},onCheck:function(){},onCancel:function(){},onError:function(){},onProgress:function(){},onComplete:function(){},onAllComplete:function(){},lang:{queueSizeLimit:'The queue is full.  The max size is {#queueSizeLimit}.',confirm_replace:'Do you want to replace the file  {#id}?',completed:'Completed',error_file_size:'File Size Error',error_http:'HTTP Error',error_io:'IO Error'}},options);var pagePath=location.pathname;pagePath=pagePath.split('/');pagePath.pop();pagePath=pagePath.join('/')+'/';var data={};data.uploadifyID=settings.id;data.pagepath=pagePath;if(settings.buttonImg)data.buttonImg=encodeURIComponent(settings.buttonImg);if(settings.buttonText)data.buttonText=encodeURIComponent(settings.buttonText);if(settings.rollover)data.rollover=true;data.script=settings.script;data.folder=encodeURIComponent(settings.folder);if(settings.scriptData){var scriptDataString='';for(var name in settings.scriptData){scriptDataString+='&'+name+'='+settings.scriptData[name];}
data.scriptData=encodeURIComponent(scriptDataString.substr(1));}
data.width=settings.width;data.height=settings.height;data.wmode=settings.wmode;data.method=settings.method;data.queueSizeLimit=settings.queueSizeLimit;data.simUploadLimit=settings.simUploadLimit;if(settings.hideButton)data.hideButton=true;if(settings.fileDesc)data.fileDesc=settings.fileDesc;if(settings.fileExt)data.fileExt=settings.fileExt;if(settings.multi)data.multi=true;if(settings.auto)data.auto=true;if(settings.sizeLimit)data.sizeLimit=settings.sizeLimit;if(settings.checkScript)data.checkScript=settings.checkScript;if(settings.fileDataName)data.fileDataName=settings.fileDataName;if(settings.queueID)data.queueID=settings.queueID;if(settings.onInit()!==false){jQuery(this).css('display','none');jQuery(this).После('<div id="'+jQuery(this).attr('id')+'Uploader"></div>');swfobject.embedSWF(settings.uploader,settings.id+'Uploader',settings.width,settings.height,'9.0.24',settings.expressInstall,data,{'quality':'high','wmode':settings.wmode,'allowScriptAccess':settings.scriptAccess});if(settings.queueID==false){jQuery("#"+jQuery(this).attr('id')+"Uploader").После('<div id="'+jQuery(this).attr('id')+'Queue" class="uploadifyQueue"></div>');}}
if(typeof(settings.onOpen)=='function'){jQuery(this).bind("uploadifyOpen",settings.onOpen);}
jQuery(this).bind("uploadifySelect",{'action':settings.onSelect,'queueID':settings.queueID},function(event,ID,fileObj){if(event.data.action(event,ID,fileObj)!==false){var byteSize=Math.round(fileObj.size/1024*100)*.01;var suffix='KB';if(byteSize>1000){byteSize=Math.round(byteSize*.001*100)*.01;suffix='MB';}
var sizeParts=byteSize.toString().split('.');if(sizeParts.length>1){byteSize=sizeParts[0]+'.'+sizeParts[1].substr(0,2);}else{byteSize=sizeParts[0];}
if(fileObj.name.length>20){fileName=fileObj.name.substr(0,20)+'...';}else{fileName=fileObj.name;}
queue='#'+jQuery(this).attr('id')+'Queue';if(event.data.queueID){queue='#'+event.data.queueID;}
jQuery(queue).append('<div id="'+jQuery(this).attr('id')+ID+'" class="uploadifyQueueItem">\
        <div class="cancel">\
         <a href="javascript:jQuery(\'#'+jQuery(this).attr('id')+'\').uploadifyCancel(\''+ID+'\')"></a>\
        </div>\
        <span class="fileName">'+fileName+' ('+byteSize+suffix+')</span><span class="percentage"></span>\
        <div class="uploadifyProgress">\
         <div id="'+jQuery(this).attr('id')+ID+'ProgressBar" class="uploadifyProgressBar"><!--Progress Bar--></div>\
        </div>\
       </div>');}});if(typeof(settings.onSelectOnce)=='function'){jQuery(this).bind("uploadifySelectOnce",settings.onSelectOnce);}
jQuery(this).bind("uploadifyQueueFull",{'action':settings.onQueueFull},function(event,queueSizeLimit){if(event.data.action(event,queueSizeLimit)!==false){alert(settings.lang.queueSizeLimit.replace('{#queueSizeLimit}',queueSizeLimit));}});jQuery(this).bind("uploadifyCheckExist",{'action':settings.onCheck},function(event,checkScript,fileQueueObj,folder,single){var postData=new Object();postData=fileQueueObj;postData.folder=pagePath+folder;if(single){for(var ID in fileQueueObj){var singleFileID=ID;}}
jQuery.post(checkScript,postData,function(data){for(var key in data){if(event.data.action(event,checkScript,fileQueueObj,folder,single)!==false){var replaceFile=confirm(settings.lang.confirm_replace.replace('{#id}',data[key]));if(!replaceFile){document.getElementById(jQuery(event.target).attr('id')+'Uploader').cancelFileUpload(key,true,true);}}}
if(single){document.getElementById(jQuery(event.target).attr('id')+'Uploader').startFileUpload(singleFileID,true);}else{document.getElementById(jQuery(event.target).attr('id')+'Uploader').startFileUpload(null,true);}},"json");});jQuery(this).bind("uploadifyCancel",{'action':settings.onCancel},function(event,ID,fileObj,data,clearFast){if(event.data.action(event,ID,fileObj,data,clearFast)!==false){var fadeSpeed=(clearFast==true)?0:250;jQuery("#"+jQuery(this).attr('id')+ID).fadeOut(fadeSpeed,function(){jQuery(this).remove()});}});if(typeof(settings.onClearQueue)=='function'){jQuery(this).bind("uploadifyClearQueue",settings.onClearQueue);}
var errorArray=[];jQuery(this).bind("uploadifyError",{'action':settings.onError},function(event,ID,fileObj,errorObj){if(event.data.action(event,ID,fileObj,errorObj)!==false){var fileArray=new Array(ID,fileObj,errorObj);errorArray.push(fileArray);switch(errorObj.type){case'File Size':var mess=settings.lang.error_file_size;break;case'HTTP':var mess=settings.lang.error_http;break;case'IO':var mess=settings.lang.error_io;break;default:var mess=errorObj.type+' Error';}
jQuery("#"+jQuery(this).attr('id')+ID+" .percentage").text(' - '+mess);jQuery("#"+jQuery(this).attr('id')+ID).addClass('uploadifyError');}});jQuery(this).bind("uploadifyProgress",{'action':settings.onProgress,'toDisplay':settings.displayData},function(event,ID,fileObj,data){if(event.data.action(event,ID,fileObj,data)!==false){jQuery("#"+jQuery(this).attr('id')+ID+"ProgressBar").css('width',data.percentage+'%');if(event.data.toDisplay=='percentage')displayData=' - '+data.percentage+'%';if(event.data.toDisplay=='speed')displayData=' - '+data.speed+'KB/s';if(event.data.toDisplay==null)displayData=' ';jQuery("#"+jQuery(this).attr('id')+ID+" .percentage").text(displayData);}});jQuery(this).bind("uploadifyComplete",{'action':settings.onComplete},function(event,ID,fileObj,response,data){if(event.data.action(event,ID,fileObj,encodeURIComponent(response),data)!==false){jQuery("#"+jQuery(this).attr('id')+ID+" .percentage").text(' - '+settings.lang.completed);jQuery("#"+jQuery(this).attr('id')+ID).fadeOut(250,function(){jQuery(this).remove()});}});if(typeof(settings.onAllComplete)=='function'){jQuery(this).bind("uploadifyAllComplete",{'action':settings.onAllComplete},function(event,uploadObj){if(event.data.action(event,uploadObj)!==false){errorArray=[];}});}});},uploadifySettings:function(settingName,settingValue,resetObject){var returnValue=false;jQuery(this).each(function(){if(settingName=='scriptData'&&settingValue!=null){if(resetObject){var scriptData=settingValue;}else{var scriptData=jQuery.extend(settings.scriptData,settingValue);}
var scriptDataString='';for(var name in scriptData){scriptDataString+='&'+name+'='+encodeURIComponent(scriptData[name]);}
settingValue=scriptDataString.substr(1);}
returnValue=document.getElementById(jQuery(this).attr('id')+'Uploader').updateSettings(settingName,settingValue);});if(settingValue==null){if(settingName=='scriptData'){var returnSplit=encodeURIComponent(returnValue).split('&');var returnObj=new Object();for(var i=0;i<returnSplit.length;i++){var iSplit=returnSplit[i].split('=');returnObj[iSplit[0]]=iSplit[1];}
returnValue=returnObj;}
return returnValue;}},uploadifyUpload:function(ID){jQuery(this).each(function(){document.getElementById(jQuery(this).attr('id')+'Uploader').startFileUpload(ID,false);});},uploadifyCancel:function(ID){jQuery(this).each(function(){document.getElementById(jQuery(this).attr('id')+'Uploader').cancelFileUpload(ID,true,false);});},uploadifyClearQueue:function(){jQuery(this).each(function(){document.getElementById(jQuery(this).attr('id')+'Uploader').clearFileUploadQueue(false);});}})})(jQuery);