jQuery.fn.uploadVideo = function(opt){
var form = this[0].form;
$(form).after('<iframe name="'+opt.trip+'" style="display:none;height:400px;left:-300px;position:relative;width:500px;"></iframe>');
var $iframe = $(form).next(); var $APC_input = $('input#progress_key',form);
var first_time = true;
var can_upload = true;
form.target = opt.trip;
opt.progressbar1.hide();

this.change(function(){
	if($(this).val()==""){alert('Select at least one file to upload');return false;};
	if(!checkExt($(this).val())) return false;
	opt.progressbar1.show();
	var UID = Math.round(10000*Math.random())+'0'+Math.round(10000*Math.random());
	form.action = opt.upload_over_url+'?upload_id='+UID+'&trip='+opt.trip;
	$APC_input.val(UID);
	can_upload = false;
	form.submit();
	$(this).val('');
	opt.progressbar1.show();
	var get_progress = function(){
		$.post(opt.get_progress_url,{UID: UID}, function(data){
			if(data.status == 'error')
				{hide_progress();return;}
			if(data.status == 'finished')
				{hide_progress();return;}
			set_progress(data.currentsize,data.totalsize);
			if(data.currentsize != data.totalsize)
				setTimeout(get_progress,400);
		},'json');
	};
	setTimeout(get_progress,100);
});


var iframe_onload = function(){
	if(first_time) {first_time=false;return;}
	//alert('loaded')
	opt.refresh_function();
	hide_progress();
};
$iframe.load(iframe_onload);
var set_progress = function(s,t){
	var p = parseInt((s/t)*100); var $p = $('p',opt.progressbar1);
	opt.progressbar2.width(p+'%');
	if(p<100)
		$p.html(p+' %');
	else
		$p.html('100 % - Converting.....');
}
var hide_progress = function(){
	opt.progressbar2.width('0%');
	opt.progressbar1.hide();
};
};


function checkExt(value)
{
    if(value=="")return true;
	var exts = "avi|mpeg|mpg|wmv|flv";
    var re = new RegExp("^.+\.("+exts+")$","i");
    if(!re.test(value))
    {
        alert("This file extension is not allowed: \n" + value + "\n\nOnly these extensions are allowed: "+exts.replace(/\|/g,',')+" \n\n");
        return false;
    }
    return true;
}
