var video_upload=
{
	ready: 0,
	oInterval: null,
	total_time: 0,
	getFlash: function(movieName){
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
    	return (isIE) ? window[movieName] : document[movieName];
	},
	openFile: function(){
		this.getFlash("VideoUpload").openFileDialog();
	},
	fileSelected: function(fname, fsize, ftype){
		$('#file_name').html(fname);
		$('#file_size').html(this.getFileSize(fsize));
		$('#upload_file_name').html(fname);
		$('#upload_size').html(this.getFileSize(fsize));
		$("#upload_status").html("Chuẩn bị");
		this.ready=1;
	},
	uploadError: function(index, code, extra)
	{
		if (index==1)
		{
			alert("Bạn đã chọn file không hợp lệ. Có thể đó không phải là file video hoặc kích thước quá lớn");
		}
		else
		{
			alert('Có lỗi trong quá trình đăng tải clip. Có thể do lỗi đường truyền hoặc server đang quá tải. Xin bạn vui lòng làm lại sau ít phút nữa');
			this.cleanUp();
			window.setTimeout("window.location.reload(true);", 3000);
		}
	},
	getFileSize: function(size){
		if (size<1024)
		{
			return size.toString() + " B";
		}
		var kb=Math.round(size/1024*10)/10;
		if (kb<1024)
		{
			return kb.toString() + " K";
		}
		var mb=Math.round(kb/1024*10)/10;
		return mb.toString() + " M";
	},
	uploadStart: function(index){
		$("#upload_status").html("Đang upload");
	},
	uploadCompleted: function(index){
		$("#upload_status").html("Hoàn thành");
		$("#pbar").width('' + '100%');
  	  	$("#pbar_text").html('' + '100%');
		this.cleanUp();
		$("#upload_done").show();
		$('#wait_msg').hide();
		var rate = parseInt(loaded/this.total_time);
		$("#upload_rate").html('' + this.getFileSize(rate) + '/s');
	},
	uploadProgress: function(index, loaded, total){
		var filePercent=Math.round(loaded/total*100);
		if (filePercent>=100)
		{
			filePercent=99;
		}
		$("#pbar").width('' + filePercent + '%');
  	  	$("#pbar_text").html('' + filePercent + '%');
  	  	
		var rate = parseInt(loaded/this.total_time);
		$("#upload_rate").html('' + this.getFileSize(rate) + '/s');
	},
	doUpload: function(){
		var t2 = this.getFlash("VideoUpload");
		t2.uploadFile();
	},
	readCookie: function(cname){
		var c=document.cookie;
		var ca=c.split(";");
		for (var i=0; i<ca.length; i++)
		{
			var st=jQuery.trim(ca[i]);
			var ar=st.split("=");
			if (ar.length>1)
			{
				st=jQuery.trim(ar[0]);
				if (st==cname)
				{
					return jQuery.trim(ar[1]);
				}
			}
		}
		return "";
	},
	checkUploadData: function()
	{
		if ($("#txttitle").val()=='')
		{
			alert('Bạn phải nhập tiêu đề cho clip');
			$("#txttitle").focus();
			return false;
		}

		if ($("#txtdescription2").val()=="")
		{
			alert('Bạn phải nhập một số mô tả cho clip');
			return false;
		}

		if ($("#clip_cat_id").val()=="0")
		{
			alert('Bạn phải chọn một chuyên mục cho clip');
			return false;
		}

		if ($("#tags").val()=="")
		{
			alert('Bạn cần đặt một số từ khóa cho clip');
			return false;
		}
		return true;
	},
	upload: function()
	{
		if (this.ready==0)
		{
			alert("Bạn hãy chọn file video để upload");
			return false;
		}
		if (!this.checkUploadData())
		{
			return false;
		}
		var so=this.getFlash("VideoUpload");
		so.setPostData("title", $("#txttitle").val());
		so.setPostData("description", $("#txtdescription2").val());
		so.setPostData("tags", $("#tags").val());
		so.setPostData("category", $("#clip_cat_id").val());
		if ($('#share_public')[0].checked)
		{
			so.setPostData("public", 0);
		}
		else if ($('#share_friend')[0].checked)
		{
			so.setPostData("public", 1);
		}
		else if ($('#share_private')[0].checked)
		{
			so.setPostData("public", 2);
		}
		
		
		if ($('#share_comment_public')[0].checked)
		{
			so.setPostData("comment", 0);
		}
		else if ($('#share_comment_friend')[0].checked)
		{
			so.setPostData("comment", 1);
		}
		else if ($('#share_comment_private')[0].checked)
		{
			so.setPostData("comment", 2);
		}
		so.setUploadStatus();
		//$('#progress_start').hide();
		$('#dvinfo').hide();
		$('#progress_bar').show();
		$('#upload_header').get(0).scrollIntoView();
		this.oInterval=window.setInterval("video_upload.countTimer()", 1000);
		this.doUpload();
	},
	countTimer: function(){
		this.total_time+=1;
		$("#upload_time").html('' + this.total_time + ' giây.');
	},
	cleanUp: function()
	{
		if (this.oInterval)
		{
			window.clearInterval(this.oInterval);
		}
	}
}