var lastChecked, ajaxSessRev;

$(window).load(function () {

	// bind
	$('#btnSubmit').click(function(){ doSubmit(); });

	// info text
	prgBlock();

});

/* ***************************************** */
function secondsToTime(secs) {

    var hours = Math.floor(secs / (60 * 60));
    if(hours < 10) hours = '0'+hours;

    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);
    if(minutes < 10) minutes = '0'+minutes;

    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);
    if(seconds < 10) seconds = '0'+seconds;

    return hours+'h '+minutes+'m '+seconds+'s';

}

function sessRev() {

	$.post('ajax.php?a=session',{ rand: Math.random() });

}
/* ***************************************** */

function doSubmit() {

	try {

		swfu.startUpload();

		if(swfu.getStats().files_queued<=0) alert("Pentru a trimite o comanda, va rugam selectati pozele pe care doriti sa le prelucram.");
		else {
			prgBlock();
			ajaxSessRev = setInterval("sessRev();",300000);
			$('#progress').show();
		}

	} catch (ex) {
		this.debug(ex);
	}

	return false;
}

function fileQueued(file) {

	try {

		var targetID = this.customSettings.progressTarget;
		var fileName = file.name;
		fileName = (fileName.length>=25)?fileName.substr(0,25)+' ...':fileName;

		totalSize += file.size;

		if(jQuery('#flashUploadHeaderInfo').is(':visible')) jQuery('#flashUploadHeaderInfo').slideUp('fast');

		var opt_mar = $('#sg_mar').clone();
		var opt_hrt = $('#sg_hrt').clone();

		html  = '<tr id="'+file.id+'">';
		html +=	'	<td class="left"><input type="checkbox" name="chkbox" value="'+file.id+'" class="chkbox" /> <span><img src="images/file_img.png" align="absmiddle" /> '+fileName+'</span></td>';
		html +=	'	<td><input type="text" name="opt_buc[]" value="1" /></td>';
		html +=	'	<td>';
		html +=	'		<select name="opt_mar[]">';
		html +=				$(opt_mar).html();
		html +=	'		</select>';
		html +=	'	</td>';
		html +=	'	<td>';
		html +=	'		<select name="opt_hrt[]">';
		html +=				$(opt_hrt).html();
		html +=	'		</select>';
		html +=	'	</td>';
		html +=	'	<td>';
		html +=	'		<a href="javascript:;"></a>';
		html +=	'		<input type="hidden" name="fileID[]" value="" />';
		html +=	'	</td>';
		html += '</tr>';

		jQuery('#'+targetID).append(html);
		jQuery('#'+file.id+' a').click(function(){

			swfu.cancelUpload(file.id);

			jQuery('#'+file.id).remove();

		});

		// multi select
		$('input[name=chkbox]').click(function(event){
			if(!lastChecked) {
				lastChecked = this;
				return;
			}

			if(event.shiftKey) {
				var start = $('input[name=chkbox]').index(this);
				var end   = $('input[name=chkbox]').index(lastChecked);

				for(i=Math.min(start,end);i<=Math.max(start,end);i++) $('input[name=chkbox]')[i].checked = lastChecked.checked;
			}

			lastChecked = this;
		});

	} catch (ex) {
		this.debug(ex);
	}

}

function uploadSuccess(file, serverData) {

	try {

		jQuery('#'+file.id+' input[name="fileID[]"]').val(serverData);
		jQuery('#'+file.id+' a').remove();

		if(swfu.getStats().files_queued>0) swfu.startUpload();
		else document.forms[0].submit();

	} catch (ex) {
		this.debug(ex);
	}

}

function uploadStart(file) {

	try {
		var currentTime = new Date();
		iTime = currentTime;
	}
	catch (ex) {
	}

}

function uploadProgress(file, bytesLoaded, bytesTotal) {

	try {

		var currentTime = new Date();
		var uTime = (Math.ceil(currentTime-iTime)/1000);
		var uSpeed = Math.floor(Math.ceil(((bytesLoaded/uTime)/1024)*100)/100);

		// set current butes loaded
		b_now = bytesLoaded;
		if(b_now<b_last) b_last = 0;

		// calculate total bytes loaded
		bytesSec = b_now - b_last;
		totalUpld += bytesSec;

		// time
		timeU = Math.floor(((((totalSize-totalUpld)/uSpeed)/60)/10)*100)/100;

		// update progress bar
		percentComplete  = Math.floor((totalUpld * 100) / totalSize);
		percentCompleteB = percentComplete*2.66;
		percentCompleteB = 266-percentCompleteB;
		percentCompleteB = '-'+percentCompleteB+'px';

		$('#progress_bar span').css('backgroundPosition',percentCompleteB+' top');
		$('#progress_bar p').text(percentComplete+'%');

		if(percentComplete>=50) $('#progress_bar p').css('color','#fff');

		// update progress info
		progress_info  = 'Viteza: '+uSpeed+' kb/s<br/>';
		progress_info += 'Timp ramas: '+secondsToTime(timeU);

		if($('#progress_info').is(':hidden')) $('#progress_info').show();
		$('#progress_info').html(progress_info);

		// set b_last bytes loaded
		b_last = bytesLoaded;

	} catch (ex) {
		this.debug(ex);
	}

}
