﻿jQuery(function(){
	
	
	//assign events
	jQuery('#planwindow .collapsebutton').click(collapsePlanWindow);
	jQuery('#planwindow .reshowbutton').click(expandPlanWindow);
	
	//static info panels
	jQuery('#infopanels li h4').each(function(i,e){
		jQuery(e).click(function(){
			jQuery('#planwindow #infopanels li.open .infocontent').slideUp('fast');
			jQuery('#planwindow #infopanels li').removeClass('open').eq(i).addClass('open');
			jQuery('#planwindow #infopanels li.open .infocontent').slideDown('fast');
		})
	}).css({cursor:'pointer'})
	
	jQuery('#planwindow #infopanels li .infocontent').css({display:'none',opacity:.8}).eq(0).css({display:'block',opacity:1})
	jQuery('#planwindow #infopanels li').eq(0).addClass('open');
	//jQuery('#planwindow').draggable({zIndex:400,ghosting:true,revert:false,opacity:1}); 
	loadAndPlaceEvents();
	//checkLiveVideoAge();
	//poller = setInterval('checkLiveVideoAge()',10000);
	loadAndPlaceTopUnort();
	//minimizeVideo();		
})

function collapsePlanWindow(){
	jQuery('#planwindow').animate({width:'200px'},200,'easeOutExpo',function(){}).addClass('closed').removeClass('open');
	jQuery('#planwindow #planlogo').animate({paddingLeft:'4%'},200,'easeOutExpo',function(){});
	// jQuery('#planwindow #planwindowback').animate({opacity:.6},200,'easeOutExpo',function(){});
	jQuery('#planwindow #events').animate({width:'92%',padding:'4%'},200,'easeOutExpo',function(){
		jQuery('#planwindow .reshowbutton').fadeIn();
	});
	jQuery('#planwindow #planinfo,#planwindow .collapsebutton,#planwindow #info, #infolive, #planwindow .eventtext').hide();
}

function expandPlanWindow(){
		jQuery('#planwindow').animate({width:'600px',right:'0px'},200,'easeOutExpo',function(){}).addClass('open').removeClass('closed');
		jQuery('#planwindow #planlogo').animate({paddingLeft:'2%'},200,'easeOutExpo',function(){});
		// jQuery('#planwindow #planwindowback').animate({opacity:.8},200,'easeOutExpo',function(){});
		jQuery('#planwindow #events').animate({width:'46%',padding:'2%'},200,'easeOutExpo',function(){
			jQuery('#planwindow #planinfo,#planwindow .collapsebutton,#planwindow #info,#infolive,#planwindow .eventtext').fadeIn();
		});

		jQuery('#planwindow .reshowbutton').hide();
}

function loadAndPlaceEvents(){
	jQuery.ajax({url:'sys/actionUnort.php',type:'GET',dataType:'xml',data:{command:'getAllEventsFuture',unort_id:''},success:function(data){
		var html='';
		// var events = data.childNodes[0].childNodes[0].childNodes;
		// console.log(data.getElementsByTagName('event'));
		jQuery(data).find('event').each(function(i,e){
			e = jQuery(e);
			var ehtml = '';
			var meta = '';
			var d = Date.parseExact(e.find('date').text(),'yyyy-MM-dd HH:mm:ss');
			if(d.between(Date.today(),Date.today().addDays(1))){
				meta += '<span class="highlightevent">heute '+d.toString('HH:mm')+'</span>';
			} else if(d.between(Date.today().addDays(1),Date.today().addDays(2))){
				meta += 'morgen '+d.toString('HH:mm');
			}else {
				meta += d.toString('d. MMM HH:mm');
			}

			if(e.attr('unort_id') !=0) {
				meta += ' <a href="news/">News</a>';
			}
			
			ehtml += '<div class="eventmeta">'+meta+'</div>';
			ehtml += '<div class="eventtitle pmhead"><a href='+e.find('link').text()+'>'+e.find('title').text()+'</a></div>';
			ehtml += '<div class="eventtext">'+e.find('description').text()+'</div>';
			ehtml = '<div class="event">'+ehtml+'</div>';
			
			html += ehtml;
			
		})
		
		
		jQuery('#eventholder').html(html);
		
	}})
}

function loadAndPlaceTopUnort()
{
jQuery.ajax({url:'sys/actionUnort.php',type:'GET',dataType:'xml',data:{command:'getTopUnortHtml',sort:'date',unort_id:''},success:function(data){	
	jQuery('#topUnort').html(jQuery(data).find('unort').text());	
}})
jQuery.ajax({url:'sys/actionUnort.php',type:'GET',dataType:'xml',data:{command:'getNewUnortCount',unort_id:''},success:function(data){
	var count = jQuery(data).find('count').text();
	
	var html = 'Neue Unorte von heute: <a href="sys/list_all_unorte.php?date=24">'+count+'</a>';  
		
	jQuery('#countNewUnort').html(html);	
}})
}

var age;
var poller;
var initalload;
function checkLiveVideoAge(){
	jQuery.get('sys/getQikLive.php',{justage:'1'},function(d){
		var newage = parseFloat(d);
		if(newage < age || age == null){
			if(!initalload){
				loadAndPlaceLiveVideo();
				initalload = true;
			} else {
				jQuery('#livewindow').addClass('live');
			}
			age = newage;
		} else if(age>1) {
			jQuery('#livewindow').removeClass('live');
		}
	});
}

function loadAndPlaceLiveVideo(){
	jQuery.get('sys/getQikLive.php?v=2',null,function(d){
		jQuery('#videoholder').html(d);
		jQuery('.collapsevideo').click(collapseVideo);
		jQuery('.expandvideo').click(expandVideo).css({cursor:'pointer'});
		jQuery('.zoomvideo').click(zoomVideo);
		jQuery('.minimizevideo').click(minimizeVideo);
	});
	
}


function collapseVideo(){
	jQuery('#videocontent').slideUp();
	jQuery('#livewindow').addClass('closed').removeClass('open');
	minimizeVideo();
}
function expandVideo(){
	jQuery('#videocontent').slideDown();
	jQuery('#livewindow').addClass('open').removeClass('closed');
	minimizeVideo();
}
function zoomVideo(){
	jQuery('#livewindow').addClass('zoom').removeClass('mini');
	collapsePlanWindow();
}
function minimizeVideo(){
	jQuery('#livewindow').addClass('mini').removeClass('zoom');
}


 

