var uploader = {
	uploader_path:"plugins/uploader/",
	uploader_vars:"", // default for this project
	uploader:"", // upload.php
	max_file_size: 4*1024,
	extens: "*.jpg;*.jpeg;*.gif;*.png",
	host: window.location.host,
	
	init: function(_objID){		
		this.buttont_uploadID = 	_objID+"_button";
		this.input_uploadID = 	_objID+"_input";
		this.progress_uploadID = 	_objID+"_progress";
		this.info_uploadID = 	_objID+"_info";
		this.swf_uploadID = 	_objID+"_uploader";

		$("#"+_objID).replaceWith('<div style="float: left;"><input readonly="readonly" id="'+this.input_uploadID+'"/></div><div style="float: left;"><div id="'+this.swf_uploadID+'"></div></div><br/><span id="'+this.progress_uploadID+'"></span><br/><span id="'+this.info_uploadID+'"></span><br/>');
		this.run_flash_uploader();	
					
        $("#"+this.buttont_uploadID).click(function(){
			uploader.browse();
		});	
	},	
	swf_uploadID: null,	
	buttont_uploadID: null,
	input_uploadID: null,
	flash_uploader: null,
	get_flash_uploader: function(){
		//if(navigator.appName.indexOf("Microsoft") != -1) uploader.flash_uploader =  eval("window."+this.swf_uploadID);
    	//else 
    	uploader.flash_uploader = eval("window.document."+this.swf_uploadID);
	},
	run_flash_uploader: function (){
		var flashvars = {
		  host: "http://"+this.host,
		  uploader_vars: uploader.uploader_vars,
		  extens: uploader.extens,
		  uploader: this.uploader
		};

		var params = {
			wmode: 'transparent'
		};
	
		var attributes = {
		  id: this.swf_uploadID,
		  name: this.swf_uploadID
		};
		
		swfobject.embedSWF(this.uploader_path+"uploader.swf", this.swf_uploadID, 300, 24, "9.0.0",this.uploader_path+"expressInstall.swf", flashvars, params, attributes);
	},
	
	browse: function (){ 
		uploader.get_flash_uploader(); 	
	    uploader.flash_uploader.js_browse();			
	},
	
	prepare_to_upload: function(file_name,file_size){
		if (this.max_file_size >= file_size/1000 ){
			$("#"+this.input_uploadID).attr("value",file_name);
			$("#"+this.info_uploadID).html( file_name + " ("+Math.round(file_size/10000)/100 +" MB) ");
		}else{
			alert("plik jest zbyt duzy! "+this.max_file_size+">"+file_size/1000);
		}
	},
	
	cancel: function(){
		this.reset_form();
	},
	upload_file: function (){	
		this.flash_uploader.js_upload_file();
	},
	
	upload_progress: function(__progress){
		$("#"+this.progress_uploadID).html(" upload progress: "+__progress+"%");
	},
	uploaded_ok: function (){
		this.add_input_file_to_form($("#"+this.input_uploadID).attr("value"));
		this.reset_form();
	},
	reset_form: function(){
		$("#"+this.input_uploadID).attr("value","");
		$("#"+this.info_uploadID).empty();
		$("#"+this.progress_uploadID).empty();

	},
	add_input_file_to_form: function(__file){
		$("#"+this.info_uploadID).after('<br/><input type="hidden" name="_files[]" value="'+__file+'"/><span style="border-bottom: solid 1px #eee;">'+__file+'</span>');
	}
}