window.addEvent('domready', function(){

	// Startseite
	var json_response;
	var counter=0;
	if($chk($('dw_kalender_start'))){
		if(typeof($$('.dw_kalender_sub')[0]) == 'undefined'){
			var kalender_controls = new Element('div',{ id : 'kalender_controls'}).inject($('dw_kalender_start'),'after');
			var prev_link = new Element('a', { id : 'kal_prev' }).inject(kalender_controls);
			var next_link = new Element('a', { id : 'kal_next' }).inject(kalender_controls);
			new Request.JSON({url: window.location.href, onSuccess: function(response){ 
				json_response = response;
			}}).get({'json': true, 'no_cache' : 1});
			next_link.addEvent('click',function(){
				if(typeof(json_response[counter+3]) != 'undefined'){
					counter+=1;
					var margin_left = 240 * counter * (-1);
					$$('.inner')[0].set('morph',{duration:500, onComplete : function(){
						$$('.inner')[0].setStyle('margin-left',margin_left);
						$$('.inner')[0].set('morph',{duration:500}).morph({opacity:1});
						$$('.header img').dispose();
						
						$each($$('.header'),function(item){
							my_index=$$('.header').indexOf(item);
							item.innerHTML = json_response[my_index].headlines.white;
						});
						var row = counter+2;
						$$('.header')[row].innerHTML = json_response[row].headlines.green;
					}}).morph({opacity:0});
					prev_link.setStyle('opacity',1);
					if(typeof(json_response[counter+3]) == 'undefined'){
						next_link.setStyle('opacity',0.5);	
					}
				}
			});
			prev_link.addEvent('click',function(){
				if(typeof(json_response[counter-1]) != 'undefined'){
					counter-=1;
					var margin_left = 240 * counter * (-1);
					$$('.inner')[0].set('morph',{duration:500, onComplete : function(){
						$$('.inner')[0].setStyle('margin-left',margin_left);
						$$('.inner')[0].set('morph',{duration:500}).morph({opacity:1});
						$$('.header img').dispose();
						
						$each($$('.header'),function(item){
							my_index=$$('.header').indexOf(item);
							item.innerHTML = json_response[my_index].headlines.white;
						});
						var row = counter+2;
						$$('.header')[row].innerHTML = json_response[row].headlines.green;
					}}).morph({opacity:0});
					next_link.setStyle('opacity',1);
					if(typeof(json_response[counter-1]) == 'undefined'){
						prev_link.setStyle('opacity',0.5);	
					}
				}
			});
			prev_link.setStyle('opacity',0.5);
		}
	}
	


	// Programmdetailseite
	if($chk($$('#dw_kalender_detail')[0]) && $chk($$('#img_area')[0])){
		new Request.JSON({url: window.location.href, onSuccess: function(response){ 
			if(response.length > 1){
				var first=true;
				$each(response, function(item){
					if(first != true){
						var new_div = new Element('div',{
													'class'	:	'img',
													'style' : "background-image:url('"+item.img+"')"
												}).inject($$('#img_area div')[0]);

					}
					else first=false;
				});
				var control_div = new Element('div',{
							'id'	:	'controls'}).inject($('img_area'),'after');
				
				var step = 0;
				// Previous
				new Element('a',{
							'class'	:	'prev',
							'html'	:	'<span>Vorheriges</span>'}).
				inject(control_div).
				addEvent('click',function(){
					$$('#controls .next').setStyle('visibility', 'visible');	
					if(step-1 >= 0){
						step--;
						var new_left = step * 470 * (-1);
						$$('#img_area div')[0].set('morph', { duration: 1200 }).morph({ 'marginLeft'	:	new_left});
						if(step-1 < 0){					
							$$('#controls .prev').setStyle('visibility', 'hidden');	
						}
					}
				});
				
				// Next
				new Element('a',{
							'class'	:	'next',
							'html'	:	'<span>N&auml;chstes</span>'}).
				inject(control_div).
				addEvent('click',function(){
					$$('#controls .prev').setStyle('visibility', 'visible');	
					if(step+1 < response.length){
						step++;
						var new_left = step * 470 * (-1);
						$$('#img_area div')[0].set('morph', { duration: 1200 }).morph({ 'marginLeft'	:	new_left});
						if(step+1 >= response.length){					
							$$('#controls .next').setStyle('visibility', 'hidden');	
						}
					}
				});
			}
		}}).get({'eID': 'programm','uid' : $('img_area').getAttribute('rel')});
	}
	
	// Fix Headlines
	if($$('#dw_kalender_detail h2')[0]){
		if($$('#dw_kalender_detail h2')[0].getSize().y > 50){
			var top = parseFloat($$('#content .backlink').getStyle('top'));
			var new_top = top+$$('#dw_kalender_detail h2')[0].getSize().y-40;
			$$('#content .backlink').setStyle('top',new_top);
		}
	}
});



/* Spamschutz */
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-1);}
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}return String.fromCharCode(n);}
