var uploader = {
	uploader_path:"plugins/uploader/",
	uploader_vars:"", // default for this project
	uploader:"", // upload.php
	max_file_size: 1024*3,
	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('<input readonly="readonly" id="'+this.input_uploadID+'"/> <input type="button" id="'+this.buttont_uploadID+'" value="browse..." class="button"/><br/><span id="'+this.info_uploadID+'"></span> <span id="'+this.progress_uploadID+'"></span><span id="'+this.swf_uploadID+'"></span>');
		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: this.uploader_vars,
		  extens: "*.jpg;*.jpeg;*.gif;*.png",
		  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, 1, 1, "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.buttont_uploadID).unbind('click');
	
			$("#"+this.buttont_uploadID).click(function(){
				uploader.upload_file();
			})
			$("#"+this.buttont_uploadID).attr("value","upload...");
			$("#"+this.buttont_uploadID).after(' <input id="__cancel_widout_upload" value="X" title="Cancel" type="button" onclick="uploader.cancel();" class="button"/>');
			
			$("#"+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.buttont_uploadID).attr("value","browse...");		
		$("#"+this.input_uploadID).attr("value","");
		$("#"+this.info_uploadID).empty();
		$("#"+this.progress_uploadID).empty();
		$("#"+this.buttont_uploadID).unbind('click');
		$("#"+this.buttont_uploadID).click(function(){
			uploader.browse();
		});	
		$("#__cancel_widout_upload").remove();		
	},
	add_input_file_to_form: function(__file){
		$("#"+this.swf_uploadID).after('<br/><input type="hidden" name="_files[]" value="'+__file+'"/><span style="border-bottom: solid 1px #eee;">'+__file+'</span>');
	}
}