var http = false;

http = getHTTPObject();
var NORMAL_STATE = 4;
var convertID;
var uploadID;
var pos = 0;

extArray = new Array(
'.avi',
'.wmv',
'.m4v',
'.mov',
'.mp4',
'.mov',
'.mkv',
'.mpeg',
'.mpg',
'.mpe',
'.asf',
'.flv',
'.swf',
'.3gp'
);


function update_upload_progress() {
	progress_key = document.getElementById('progress_key').value;   
    var d = new Date()
    var t = d.getTime()
	http.open('GET','video_upload_process.php?progress_key='+progress_key+'&count='+t, true);
	http.onreadystatechange=function() {
	    if(http.readyState == NORMAL_STATE) {
            
            var resp = eval('(' + http.responseText + ')');
            var current = parseInt(resp['current']);
            var total = parseInt(resp['total']);
            var rate = parseInt(resp['rate']/1024);
            var pct = parseInt(260*(current/total));  
                      
            current = roundNumber(current/1048576,2);
            total = roundNumber(total/1048576,2);
            if(!resp['done']) {                 
                document.getElementById('downloaded').style.width = ''+pct+'px';
                document.getElementById('downloadtext').innerHTML = current +"Mb iš "+total+"Mb";
            } else {
                stop_upload_progress();
                document.getElementById('downloaded').style.width = ''+pct+'px';
                document.getElementById('downloadtext').innerHTML = current +"Mb iš "+total+"Mb";
            }
	    }
    }
	http.send(null);
}


function start_upload() {
    document.getElementById('progressbar').style.display = 'block';
    document.getElementById('videosubmit').style.display = 'none';
	uploadID = window.setInterval("update_upload_progress()", 2000);
}

function cancel_upload() {
	clearInterval(uploadID);
    
    document.getElementById('progressbar').style.display = 'none';
    document.getElementById('videosubmit').style.display = 'block';
    
	http.open('GET','video_upload_cancel.php?progress_key='+progress_key, true);
	http.onreadystatechange=function() {
	    if(http.readyState == NORMAL_STATE) {
            
	    }
    }
	http.send(null);
}

function stop_upload_progress() {
    clearInterval(uploadID);
    start_convert_progress();
}

function start_convert_progress() {
    convertID = window.setInterval("update_convert_progress()", 1000);
}

function stop_convert_progress() {
    document.getElementById('progressbar').style.display = 'none';
    document.getElementById('videosubmit').style.display = 'block';
    clearInterval(convertID);
}

function update_convert_progress () {
    document.getElementById('downloadtext').innerHTML = "Konvertuojama video byla. Prašome palaukti.";
    document.getElementById('downloaded').style.width = '52px';
    pos = pos + 1;
    if (pos >8) pos = 0;
    margin = 26*pos;
    document.getElementById('downloaded').style.marginLeft = ''+margin+'px';
}

function validateUploadForm (thisform){

	var videoname = thisform.videoname;
	var description = thisform.description;
	var videopath = thisform.videopath;
	var category = thisform.category;

    if(!isCategory(category, "Nepasirinka kategorija")) return false;
	if(!isEmpty(videoname, "Ne visi laukai užpildyti")) return false;
	if(!isEmpty(description, "Ne visi laukai užpildyti")) return false;
	if(!isEmpty(videopath, "Ne visi laukai užpildyti")) return false;
    if(!validate_extension(videopath,"Netinkamas failo tipas")) return false;
    
    start_upload();
    
    return true;
}

function validateAdminUploadForm (thisform){

	var videoname = thisform.videoname;
	var description = thisform.description;
	var videopath = thisform.videopath;

	if(!isEmpty(videoname, "Ne visi laukai užpildyti")) return false;
	if(!isEmpty(description, "Ne visi laukai užpildyti")) return false;
	if(!isEmpty(videopath, "Ne visi laukai užpildyti")) return false;
    if(!validate_extension(videopath,"Netinkamas failo tipas")) return false;
    
    start_upload();
    
    return true;
}

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0) {
		alert(helperMsg);
		elem.focus();
		return false;
	}
	return true;
}

function isCategory(elem, helperMsg){
	if(elem.value == 'none') {
		alert(helperMsg);        
		elem.focus();
		return false;
	}
	return true;
}

function validate_extension(elem,helperMsg) {
	allowSubmit = false;
    file = elem.value;
    
	if (!file) return false;
    
    while (file.indexOf("\\") != -1)
	    file = file.slice(file.indexOf("\\") + 1);
	
    ext = file.slice(file.indexOf(".")).toLowerCase();
    
    for (var i = 0; i < extArray.length; i++) {
        if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    
    if (allowSubmit) {
        return true;
    } else {
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

