/*
IMPORTANT ADVISE
The following code is protected by copyright 
 *  (c) 2009 Davide Rolando
except for the functions that don't contain the notice:
	// Copyright © 2009 Davide Rolando//

Thus you can't use any of the copyright protected code 
without first obtaining written permission. 
Please email  webmaster@subsedit.com to obtain written consent.
*/
	
//google.load("jquery", "1.3.2");

	function reportLog(report, busy){
	// Copyright © 2009 Davide Rolando//
	if (typeof busy == 'undefined' ) busy = false;
	if(busy == false){
		$('#alertlog').html('');
	} else {
		$('#alertlog').html('<img src="images/bar-loader_large.gif">');
	}
	//jslog.error("stringa : " + $('#alertlog').html());
		var theid = 'reportLog';
		$('#'+theid).show();
		/*
		var temp = $(theid).innerHTML;
		$(theid).innerHTML = Now()+" " + report + "\n" + temp ; //String.fromCharCode(13)
		*/
		//var temp = $(theid).value;
		//$(theid).value = Now()+" " + report + "\n" + temp ; //String.fromCharCode(13)
		$('#'+theid).text(report); //String.fromCharCode(13)
		var tid = setTimeout( function(){$('#'+theid).hide()}, 5000 );

	}
	
	function addLog(rMessage, isbusy){
	// Copyright © 2009 Davide Rolando//
		//alert(rMessage);
		if(isbusy == true){
		rMessage += "";
			$('#alertlog').innerHTML = '<img src="images/bar-loader_small.gif">' + rMessage.substr(0, 2)+" %";
		}else{
			$('#alertlog').innerHTML = rMessage;
		}
		//alert(document.getElementById('alertlog').innerHTML);
	}
	
	var risultato = '';
	function uploadsubs(el, chunk, sessionName){
		var txt = $("#"+el).val();
		var txtlength = txt.length;
		var needloop = Math.ceil(eval(txtlength / chunk));
	
		//$('#wrap').append(txtlength + " needloop "+needloop);
		var expo ='';
	
		for(var i = 0; i < (needloop); i++){
		
			var start = i*chunk;
			var txtsend = txt.substr(start,chunk);
			
			count = (i == eval(needloop-1)) ? needloop : i ;
			
			$.ajax({
			  type: "POST",
			  url: "resources/subscore.php",
			  data: ({
			  	action: 'upload',
			  	uptxt: txtsend,  
			  	num: count,
			  	nloop: needloop,
			  	session: sessionName
			  }),
			  async: false,
			  success: function(data) {	  
				//expo += ( data );	  
				$('#counter').text("Loading... " + Math.ceil((i/needloop)*100) + "%");
				
				if(i == eval(needloop-1)){
					jslog.debug("loop " + i + " ontotal " + needloop + " response " + data); //jslog
				
					risultato = data;
			  	}
			  }
			});

		} 
		$('#counter').text('');
		return risultato;
	}	

	function altertoggle(el,num,ln){
		var tt = $('#'+el+'1');
		//alert(tt.length)
		for(var i = 1 ; i < eval(ln+1); i++){
			if(i!=num){
				$('#'+el+i).filter(':visible').slideToggle('slow');
				//$('#'+el+i).hide();
			} else {
				$('#'+el+i).slideToggle('slow');
				//filter(':hidden')
			}
		}
	}
		
	function setTimeValues(tval, sign){
	// Copyright © 2009 Davide Rolando//
	
		tval = (sign == true) ? tval : Math.abs(tval);
		var hr = Math.floor(tval / 3600);
		//var m = Math.floor(Math.abs(tval/60));
		var m = Math.floor((tval-3600*hr) / 60);
		//var s = Math.round(Math.abs(tval)*10)/10;
		var s = Math.round((tval % 60)*1000)/1000;
		
		if(Number(hr)<10){hr = '0'+hr;}
		if(Number(m)<10){m = '0'+m;}
		if(Number(s)<10){s = '0'+s;}
		//if(toString(s).indexOf(".") == -1){s += '.0';}				
		//alert('ore '+hr+' minuti '+m+' secondi '+s);
		//$('sldtimecode').value = 'ore '+hr+' minuti '+m+' secondi '+s;.
		//alert(hr+':'+m+':'+s)
		return hr+':'+m+':'+s;
	}

	function time(str){
		sec=parseInt(str);
		hours=pad(Math.floor(sec/3600));
		minutes=pad(Math.floor((sec%3600)/60));
		seconds=pad((sec%3600)%60);
		return hours + ":" + minutes + ":" +seconds;
	}

	function pad(num){
	 num=num+"";
	 if(num.length==1){
	     num="0" + num;
	 }
	 return num;   
	}

	function toTimestamp(minute,secs,msec){
		
		var minute=parseInt(minute);		
		var secs=parseInt(secs);
		
		//jslog.info(Number(msec));
		if(Number(msec) == 0){
			var millisec = "000";	
		} else if(Number(msec) < 100){
			var millisec = "0" + msec;
		} else if(Number(msec) < 10){
			var millisec = "00" + msec;				
		} else {
			var millisec = msec;
		}
		
		
		//hours=pad(Math.floor(sec/3600));
		hours = '00';
		
		//minutes=pad(Math.floor((sec%3600)/60));
		//seconds=pad((secs%3600)%60);
		return hours + ":" + pad(minute) + ":" + pad(secs) +  "," + millisec;
	}

	function toTimeCode(seconds){
		seconds = Number(seconds);
		if(seconds < 0 ){seconds = Number(0.1);}
		var intp = parseInt(seconds);
		
		//var msec = Math.round(eval((seconds - intp)*1000)*1000)/1000;
		var msec = parseInt(eval(seconds*1000 - intp*1000));
		
		if(Number(msec) == 0){
			var millisec = "000";	
		} else if(Number(msec) < 100){
			var millisec = "0" + msec;
		} else if(Number(msec) < 10){
			var millisec = "00" + msec;				
		} else {
			var millisec = msec;
		}	
		
		sec=parseInt(seconds);

		hours=pad(Math.floor(sec/3600));
		minutes=pad(Math.floor((sec%3600)/60));
		seconds=pad((sec%3600)%60);
		return hours + ":" + minutes + ":" +seconds+ "," + millisec;
	}


	
	function toSeconds(timestamp, sign){
		var tt = timestamp.split(":");
		var secs = tt[2].split(",");
		
		hr = parseInt(tt[0]);
		mm = parseInt(tt[1]);
		ss = (secs[0]);
		
		jslog.debug(hr); //jslog
		jslog.debug(mm); //jslog
		jslog.debug(ss); //jslog
		
		s = (sign == '-') ? parseInt('-1') : Number('1');
		
		var seconds = eval(eval(hr*3600) + eval(mm*60) + eval(ss) + eval(secs[1]/1000));
		jslog.debug(seconds); //jslog
		jslog.debug("Con Segno : " + eval(seconds*s));
		return eval(seconds*s);
	}

	//to be improved..
	function validateSRT(txt){
		jslog.info("source length:" + txt.length);
		var itsok = true;
		if(txt.length < 33) itsok = false;
	
		return itsok;
	}

