/* 
 * (c) 2010, Shift8
 * Author: Tom Maiaroto
 * Website: http://www.shift8creative.com/projects/agile-uploader/index.html
*/
(function($){
    var opts;
    var noURI;
    $.fn.agileUploaderSerializeFormData=function(){
        if((typeof(opts.formId)=='string')&&($('#'+opts.formId).length>0)){
            return $('#'+opts.formId).serialize();
        }
    }
$.fn.agileUploaderPreview=function(image,fileName,fileExtension,fileSize,fileId){
    var fileId=fileId;
    if((typeof(image)!='undefined')&&(noURI!==true)){
        $('#id-'+fileId+' .agileUploaderFilePreview').html('<img src="'+image+'" />');
    }else{
        $('#id-'+fileId+' .agileUploaderFilePreview').html('<img src="'+opts.genericFileIcon+'" />');
    }
    $('#id-'+fileId+' .agileUploaderFileSize').text('('+fileSize+'Kb)');
}
$.fn.agileCompleteUploader=function(){
    document.getElementById('submitButton').style.visibility = 'visible';
    document.getElementById('submitButton').style.display = 'inline';
}
$.fn.agileUploaderSubmitted=function(data){
    if(typeof(opts.updateDiv)=='string'){
        $(opts.updateDiv).empty();
        $(opts.updateDiv).append(data);
    }
    if(typeof(opts.submitRedirect)=='string'){
        window.location=opts.submitRedirect;
    }else{
        $('#agileUploaderFileList').empty();
    }
}
$.fn.agileUploaderAttachFile=function(fileName,fileExtension,fileId){
    if(opts.flashVars.file_limit==-1){
        $('#agileUploaderFileList').empty();
    }
    $("#agileUploaderInfo").animate({
        scrollTop:$("#agileUploaderInfo").attr("scrollHeight")
        },opts.attachScrollSpeed);
    var alt='';
    if($('#agileUploaderFileList li').size()%2==0){
        alt='alt';
    }
    $('#agileUploaderFileList').append('<li id="id-'+fileId+'" class="'+alt+'"><div class="agileUploaderFilePreview" style="display: none;"></div><div class="agileUploaderFileName" style="display: none;">'+fileName+'</div><div id="agileUploaderCurrentProgress"></div><div class="agileUploaderFileSize" style="display: none;"></div><div class="agileUploaderRemoveFile" style="display:none;"><a href="#" id="remove-'+fileId+'" onClick="document.getElementById(\'agileUploaderSWF\').removeFile(\''+fileId+'\'); $(\'#id-'+fileId+'\').remove(); return false;"><img class="agileUploaderRemoveIcon" src="'+opts.removeIcon+'" alt="remove" /></a></div></li>');
    if(/msie/i.test(navigator.userAgent)&&!/opera/i.test(navigator.userAgent)===true){
        $('#id-'+fileId).css('height',opts.flashVars.preview_max_height+5);
    }else{
        $('#id-'+fileId).css('height',opts.flashVars.preview_max_height);
    }
    if((typeof(opts.progressBar)=='string')&&(opts.progressBar!='percent')){
        $('#agileUploaderCurrentProgress').css('background',opts.progressBar);
    }
}
$.fn.agileUploaderCurrentEncodeProgress=function(progress){
    document.getElementById('submitButton').style.visibility = 'hidden';
    document.getElementById('submitButton').style.display = 'none';
    if(typeof(opts.progressBar)=='string'){
        if(opts.progressBar=='percent'){
            $('#agileUploaderCurrentProgress').text(parseInt(progress)+'%');
        }else{
            $('#agileUploaderCurrentProgress').css('width',parseInt(progress)+'%');
        }
    }
if(progress>=100){
    $('#agileUploaderCurrentProgress').remove();
    $('.agileUploaderFileName, .agileUploaderRemoveFile, .agileUploaderFileSize, .agileUploaderFilePreview').show();
    $('#agileUploaderRemoveAll').html('<a href="#" onClick="document.getElementById(\'agileUploaderSWF\').removeAllFiles(); $(\'#agileUploaderFileList\').empty(); $(\'#agileUploaderRemoveAll\').empty(); return false;">'+opts.removeAllText+'</a>');
}
}
$.fn.agileUploaderMaxPostSize=function(lastFile,fileId){
    $('#id-'+fileId).remove();
    $("#agileUploaderMessages").show();
    $('#agileUploaderMessages').text(opts.maxPostSizeMessage);
    clearTimeout();
    setTimeout('$("#agileUploaderMessages").fadeOut()',3000);
}
$.fn.agileUploaderFileLimit=function(lastFile,fileId){
    $('#id-'+fileId).remove();
    $("#agileUploaderMessages").show();
    $('#agileUploaderMessages').text(opts.maxFileMessage);
    clearTimeout();
    setTimeout('$("#agileUploaderMessages").fadeOut()',3000);
}
$.fn.agileUploader=function(options){
    opts=$.extend({},$.fn.agileUploader.defaults,options);
    opts.flashVars=$.extend({},$.fn.agileUploader.defaults.flashVars,options.flashVars);
    opts.flashParams=$.extend({},$.fn.agileUploader.defaults.flashParams,options.flashParams);
    opts.flashAttributes=$.extend({},$.fn.agileUploader.defaults.flashAttributes,options.flashAttributes);
    return this.each(function(){
        var data=new Image();
        data.onload=data.onerror=function(){
            if(this.width!=1||this.height!=1){
                noURI=true;
            }
        }
    data.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
    $('#'+this.id).append('<div id="agileUploaderAttachArea"><div id="agileUploaderEMBED"></div><div id="agileUploaderMessages"></div></div>');
        $.fn.agileUploaderEmbed();
        $('#'+this.id).prepend('<div id="agileUploaderRemoveAll"></div><div id="agileUploaderInfo"><ul id="agileUploaderFileList"></ul></div>');
    });
}
$.fn.agileUploaderSingle=function(options){
    if(typeof(options)=='undefined'){
        var options={};

}
delete $.fn.agileUploader.defaults.flashVars.button_up;
delete $.fn.agileUploader.defaults.flashVars.button_over;
delete $.fn.agileUploader.defaults.flashVars.button_down;
$.fn.agileUploader.defaults.flashWidth=185;
$.fn.agileUploader.defaults.flashHeight=25;
$.fn.agileUploader.defaults.flashVars.show_file_input_field='true';
$.fn.agileUploader.defaults.flashVars.show_encode_progress='true';
opts=$.extend({},$.fn.agileUploader.defaults,options);
if(typeof(options.flashVars)=='undefined'){
    options.flashVars={};

}
opts.flashVars=$.extend({},$.fn.agileUploader.defaults.flashVars,options.flashVars);
if(typeof(options.flashParams)=='undefined'){
    options.flashParams={};

}
opts.flashParams=$.extend({},$.fn.agileUploader.defaults.flashParams,options.flashParams);
if(typeof(options.flashAttributes)=='undefined'){
    options.flashAttributes={};

}
opts.flashAttributes=$.extend({},$.fn.agileUploader.defaults.flashAttributes,options.flashAttributes);
opts.flashVars.file_limit=-1;
return this.each(function(){
    $('#'+this.id).append('<div id="agileUploaderAttachArea"><div id="agileUploaderEMBED"></div><div id="agileUploaderMessages" class="agileUploaderSingleMessages"></div></div>');
    $.fn.agileUploaderEmbed();
});
}
$.fn.agileUploaderEmbed=function(){
    if(typeof($().flash)=='function'){
        $('#agileUploaderEMBED').flash({
            id:"agileUploaderSWF",
            width:opts.flashWidth,
            height:opts.flashHeight,
            src:opts.flashSrc,
            flashvars:opts.flashVars,
            bgcolor:'#efefef',
            quality:'high',
            //wmode:'transparent',
            allowscriptaccess:'always',
            classid:'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000',
            codebase:'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=',
            pluginspace:'http://get.adobe.com/flashplayer',
            version:'10.0.0'
        });
    }else
    if(typeof(swfobject)!='undefined'){
        swfobject.embedSWF(opts.flashSrc,'agileUploaderEMBED',opts.flashWidth,opts.flashHeight,"10.0.0","expressInstall.swf",opts.flashVars,opts.flashParams,opts.flashAttributes);
    }else{
        $('#agileUploaderEMBED').html('<p>You need to have either swfobject or jquery flash in order to embed.</p>');
    }
}
$.fn.agileUploaderSubmit=function(){
    document.getElementById('agileUploaderSWF').sendForm();
}
$.fn.agileUploader.defaults={
    flashSrc:'agile-uploader.swf',
    flashWidth:25,
    flashHeight:22,
    flashParams:{
        allowscriptaccess:'always'
    },
    flashAttributes:{
        id:"agileUploaderSWF"
    },
    flashVars:{
        max_height:1000,
        max_width:1000,
        jpg_quality:200,
        preview_max_height:50,
        preview_max_width:50,
		file_input_field_width: 80,
        show_file_input_field:'false',
        show_encode_progress:'false',
        js_get_form_data:'$.fn.agileUploaderSerializeFormData',
        js_submit_callback:'$.fn.agileUploaderSubmitted',
        js_encode_callback:'$.fn.agileCompleteUploader',
        js_preview_callback:'$.fn.agileUploaderPreview',
        js_attach_callback:'$.fn.agileUploaderAttachFile',
        js_encode_progress_callback:'$.fn.agileUploaderCurrentEncodeProgress',
        js_max_post_size_callback:'$.fn.agileUploaderMaxPostSize',
        js_file_limit_callback:'$.fn.agileUploaderFileLimit',
        return_submit_response:'true',
        file_filter:'*.jpg;*.jpeg;*.JPG;*.JPEG',
        file_filter_description:'Files',
        max_post_size:1536,
        file_limit:0,
        return_submit_response:'true',
        button_up:'add-file.png',
        button_over:'add-file.png',
        button_down:'add-file.png'
    },
    progressBar:'#000000',
    attachScrollSpeed:1000,
    removeIcon:'trash-icon.png',
    genericFileIcon:'file-icon.png',
    maxPostSizeMessage:'Attachments exceed maximum size limit.',
    maxFileMessage:'File limit hit, try removing a file first.',
    removeAllText:'remove all'
}
})(jQuery);

