var intervalID;
var intervalID2;
var id;
var file;
var lastSize;
var flvLink = false;
function btnClick() {
	var type = document.getElementById('type').value;
	document.getElementById('graphdiv').style.visibility = 'visible';
	var youtubeURI = document.getElementById('txtyoutubeURL').value;
	var re = new RegExp("youtube.com");
	var re2 = new RegExp(".flv");
	var error = false;
	// need to check input first here
	if(!youtubeURI.match(re) && !youtubeURI.match(re2)) {
		showError("Please enter a valid youtube or direct .flv link below");
		error = true;
	}
	if(youtubeURI.length == 0) {
		showError("Please enter a youtube or direct .flv link below");
		error = true;
	}
	if(!error) {
		if(youtubeURI.match(re2) && !youtubeURI.match(re)) {
			flvLink = true;
		}
		var qstr = 'url=' + escape(youtubeURI) + '&type=' + type;
		// disable form inputs
		document.getElementById('txtyoutubeURL').disabled = true;
		document.getElementById('type').disabled = true;
		document.getElementById('btndownload').disabled = true;
		// post
		xmlhttpPost('/lib/getFLVinfo.php',qstr,"updatePage");
	}
}
function showError(str) {
	document.getElementById('graphdiv').innerHTML = str;
}
function confirmUnload() {
	
}
function xmlhttpPost(strURL,qstr,toDo) {
	strURL += '?' + qstr;
	
	var http = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
	   http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	   http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	http.abort();
	http.open("GET", strURL , true);
	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(toDo == "updatePage") {
				updatepage(http.responseText);
			} else if(toDo == "downloading") {
				updateFileSize(http.responseText);
			} else if(toDo == "convert") {
				convert(http.responseText);
			} else if(toDo == "converting") {
				updateConverting(http.responseText);
			}
		}
	}
	http.send(null);
	
}
function prepareDownload() {
	var gdiv = document.getElementById('graphdiv');
	var info = file.split('.');
	var flink = info[0];
	gdiv.innerHTML = gdiv.innerHTML + '<br /><a href="/download.php?f=' + flink + '">Download your file</a>';
	document.getElementById('another').style.display = "block";
}
function updateConverting(size) {
	var gdiv = document.getElementById('graphdiv');
	if(size == lastSize) {
		gdiv.innerHTML = "File Conversion complete!";
		clearInterval(intervalID2);
		prepareDownload();
	} else {
		if(lastSize == 0 ) {
			//gdiv.innerHTML = "Preparing conversion";
		} else {
			gdiv.innerHTML = "STEP 2: Converting... the current size of the file is " + size;
		}
		lastSize = size;
	}
	
}
function convert(str) {
	lastSize = 0;
	intervalID2 = setInterval('xmlhttpPost("/lib/getLocalSize.php","f=' + file + '.mp3&converting=1","converting");', 1000);
}

function updateFileSize(str) {
	var info = str.split("|");
	var currentSize = info[0];
	var totalSize = info[1];
	var percent = Math.round( currentSize / totalSize * 100 );
	
	document.getElementById('tblpercent').style.display = 'block';
	//document.getElementById('percent').width = percent + "%";
	document.getElementById('percent').style.width = percent + "%";
	document.getElementById('returninfo').innerHTML = "Caching video: " + percent + " % completed";
	if(percent == "100") {
		clearInterval(intervalID);
		convertVideo();
	}
}
function convertVideo() {
	var gdiv = document.getElementById('graphdiv');
	gdiv.innerHTML = 'Converting video: This may take up to 5 minutes<br /><img src="/images/converting.gif" />';
	xmlhttpPost('/lib/convertFLV.php','f=' + file,"convert");	
	//alert('STEP 2: Converting video.  Not implimented yet, make ajax request to php script that will shell out ffmpeg.  Need to figure out when ffmpeg is finished. Then serve download.');
	//document.getElementById('graphdiv').innerHTML = '');
}
function updatepage(str){
   	var gdiv = document.getElementById('returninfo');
	gdiv.innerHTML = "Preparing file cache";
	id = str;
	file = str + '.flv';
	intervalID = setInterval('xmlhttpPost("/lib/getLocalSize.php","f=' + file + '&converting=0","downloading");', 1000);
	
}
