var schedCurrTime = '';
var schedCurrDate = '';
var sched = [];
var schedIDs = [];
var schedCurrItem = -1;
var currVideo = [];

startScheduler = function(){
	fuel_plugin('schedule','schedule_get','url:flowcenter1.flowworks.de/schedule-wobbler.xml','loadSchedule');
	fuel_plugin('','time','','loadTime');
	updateTime();
};
loadSchedule = function(r){
	var resp, clip, tsched, i, j, schedHTML;
	resp = r.evalJSON();
	if(resp.action == 'ok' && resp.status == 'found'){
		
		$('schedule_playlist').style.width = resp.schedule.clips.clip.length*2*111+'px';
		$('schedule_playlist').innerHTML = '';
		schedIDs = [];
		
		tsched = [];
		sched = resp.schedule.clips.clip;
		
		schedHTML = '';
		
		for(i=0;i<resp.schedule.clips.clip.length;i++){
			clip = resp.schedule.clips.clip[i];
			
			clip.id = clip.CLIPID;
			
			clip.CLIPNAME = clip.CLIPNAME.replace('wobbler.tv - ', '');
			clip.CLIPNAMEPLAYER = escape(clip.CLIPNAME);
			
			clip.thumburl = makeSmallThumbURL(clip.THUMBNAIL);
			
			clip.starttime = mkStartTime(clip.TIME);
			clip.endtime = mkEndTime(clip.starttime, Math.round(clip.DURATION/1000));
			
			clip.duration = makeSecsToTime(Math.round(clip.DURATION/1000));
			
			schedHTML += sched_thumb.evaluate(clip);
			
			schedIDs.push(clip.CLIPID);
		}
		
		$('schedule_playlist').innerHTML = schedHTML;
		$('schedule_playlist').style.left = '0px';
		
		updateSchedule();
	}
};
updateSchedule = function(){
	var i, it, id, l;
	l = -1;
	it = [];
	for(i=0;i<sched.length;i++){
		if(isTimeActual(sched[i].TIME.split(' ')[0], sched[i].TIME.split(' ')[1], (parseInt(sched[i].DURATION,10)/1000))) {
			it.push(i);
			l = i;
		}
	}
	
	if(it.length > 0){
		id = sched[it[it.length-1]].CLIPID;
		if(schedCurrItem != id){
			if(schedCurrItem>=0) $('sched-'+schedCurrItem).setStyle({marginTop:'18px',height:'66px',border:'0px',backgroundColor:''});
			schedCurrItem = id;
			$('sched-'+id).setStyle({marginTop:'4px',height:'80px',border:'3px solid #73be1e',backgroundColor:'#fff'});
			scrollSchedule();
		}
		//if(it[it.length-1] >= sched.length-10) fuel_plugin('schedule','schedule_get','url:flowcenter1.flowworks.de/schedule-wobbler.xml','loadSchedule');
	}
	
	//alert(l + ' -- ' + (sched.length-46));
	
	if(l >= Math.round(sched.length*3/4) && sched.length>0 && l>0){
		//fuel_plugin('schedule','schedule_get','url:flowcenter1.flowworks.de/schedule-wobbler.xml','loadSchedule');
		startScheduler();
	} else setTimeout(updateSchedule, 1000);
};
scrollSchedule = function(){
	if(schedCurrItem>=0) new Effect.Move($('schedule_playlist'), {x:(document.viewport.getWidth()/2-$('sched-'+schedCurrItem).positionedOffset().left-55)-$('schedule_playlist').positionedOffset().left, mode:'relative'});
};
loadTime = function(r){
	var t;
	try{
		if(r.length > 0){
			t = r.split(' ');
			schedCurrDate = t[0];
			schedCurrTime = t[1];
		} else {
			var dt = new Date();
			schedCurrDate = dt.getDate()+'.'+(dt.getMonth()+1)+'.'+dt.getFullYear();
			schedCurrTime = dt.getHours()+':'+dt.getMinutes()+':'+dt.getSeconds()+':00';
		}
	}catch(e){}
	
	setTimeout(loadTime, 600000);
};
updateTime = function(){
	var t, i;
	t = schedCurrTime.split(':');
	for(i=0;i<3;i++){t[i] = parseInt(t[i], 10);}
	t[2]++;
	if(t[2]>=60){ t[2]-=60; t[1]++; }
	if(t[1]>=60){ t[1]-=60; t[0]++; }
	if(t[0]>=24){ fuel_plugin('','time','','loadTime'); }
	schedCurrTime = t[0]+':'+t[1]+':'+t[2]+':00';
	setTimeout(updateTime, 1000);
};
function isTimeActual(sdate, stime, dur){
	var currTimestamp = mkDate2Timestamp(schedCurrDate)+mkTime2Secs(schedCurrTime);
	
	var startTimestamp = mkDate2Timestamp(sdate)+mkTime2Secs(stime);
	var stopTimestamp = mkDate2Timestamp(sdate)+mkTime2Secs(stime)+parseInt(dur,10);
	
	return (currTimestamp >= startTimestamp && currTimestamp <= stopTimestamp);
};

function mkTimestamp(date, time){
	return mkDate2Timestamp(date)+mkTime2Secs(time);
};
function mkDate2Timestamp(date){
	date = date.split('.');
	var dsecs = new Date(date[2], date[1]-1, date[0], 0, 0, 0).getTime()/1000;
	return dsecs;
};
function mkTime2Secs(time){
	time = time.split(':');
	var secs = parseInt(time[2],10);
	secs += parseInt(time[1],10)*60;
	secs += parseInt(time[0],10)*60*60;
	return secs;
};
