var mysid="";
var done=false;
var ie=/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
$(document).ready(function() {
	if (ie==true) { 
		$("#upload_btn").css("opacity",0.01);
		$("#add_file").css("opacity",1);
	}
	if (chrome==true) {
		$("#add_file").css("left","-2000px");
		$("#upload_btn").click(function() {
			document.upload_form.file.click();
		});
	}
	$("#add_file").change(function() {
		mysid=rand();		
		var f=document.forms; 
		f[0].action="../cgi-bin/upload.cgi?sid=" + mysid + "&r=0";
		document.upload_form.submit();
		upload_progress();
	});
	$(".colorbox").colorbox({iframe:true, width:7800, height:4500});
	$(".popvid").click(function() {
		$("#pop_light").click();
	});
	$("#upload_form").submit(function() { 
		$(this).attr("target","ajax_frame");
	});
});
function upload_progress() {
	$("#perc_bar").css("color","#222");
	$.get("ajax.php",{"action":"get_progress", "sid":mysid },function(data) {
		var perc=eval('(' + data + ')');
		if (perc['error']==1) {
			alert("Error: File Too Large");
			return;
		}
		//$("title").html("uploading " + $("#add_file").val() + " " + perc[0] + "% completed");
		$("#perc_bar").text(parseFloat(perc[0]) + "% Done");
		if (parseInt(perc[0])>=100) {	
			done=true;
			$.get("ajax.php",{"action":"finish_upload", "sid":mysid, "big":false },function(r) {
				d=eval('(' + r + ')');
				$("#upload_btn").css("opacity",1);
				$("#upload_btn").show();
				$("#add_file").hide();
				$("#upload_btn").text("View File").attr("href","/" + d['url']);
			});
		}
		
		if (done==false) { upload_progress(); }
	});
}
function rand() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 15;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}