var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

function decode(encStr) {
	var bits;
	var decOut = '';
	var i = 0;
	for(; i<encStr.length; i += 4) {
		bits = (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
			 (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 |
			 (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
			  base64s.indexOf(encStr.charAt(i +3)) & 0xff;
		decOut += String.fromCharCode((bits & 0xff0000) >>16, 
		(bits & 0xff00) >>8, bits & 0xff);
		}
	if(encStr.charCodeAt(i -2) == 61) { return(decOut.substring(0, decOut.length -2)); }
	else if(encStr.charCodeAt(i -1) == 61) { return(decOut.substring(0, decOut.length -1));	}
	else {return(decOut)};
	}


function fresh(a,text) { var text=text || 'NULL'; var x=decode(a);temp=x.split('###');document.write('<a href="mai');document.write('lto:'+temp[1]);document.write('@');document.write(temp[0]+'">'); if (text=="NULL") { document.write(temp[1]);document.write('@');document.write(temp[0]); } else document.write(text); document.write('</a>');} 	


// Helper function, used below.
// Usage: ['img1.jpg','img2.jpg'].remove('img1.jpg');
Array.prototype.remove = function(element) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == element) { this.splice(i,1); }
  }
};


// Usage: $(['img1.jpg','img2.jpg']).preloadImages();
$.fn.preloadImages = function() {
	checklist = this.toArray();
	this.each(function() {
		$('<img>').attr({ src: this }).load(function() {
			checklist.remove($(this).attr('src'));
			});
		});
	};



/* FOOTER FUNCTIONS */
 	var expandContainer = function(callback,doScroll) {
		callback = callback || function(){};
		doScroll = doScroll || "yes";
		/*$("#footer").animate({height: "243px"}, {queue:false, duration: 1400});
		$("#outer").animate({'padding-bottom':263}, {queue:false, duration: 200});
		$("html,body").animate({ scrollTop: $(document).height() }, {queue:false, duration: 1200, complete: function() { callbaarticleck(); } });*/
		$("#footer").animate({height: "243px"}, {queue:false, duration: 800});
		// $("#footer").css('height',243);
		$("#outer").css('padding-bottom',263);
		/*if (doScroll=="yes") $("html,body").scrollTop($(document).height());*/
		callback();
		}
		
	var shrinkContainer = function() {
		$("#footer").animate({height: "50px"}, {queue:false, duration: 800});
		$("#outer").animate({'padding-bottom':70}, {queue:false, duration: 800});
		}
	
	var showGallery = function() {
		$("#if-logo").css('z-index',1);
		$("#gallery").css('width',980);
		$("#gallery").css('z-index',900);
		$("#gallery").animate({height: "243px"}, {queue:false, duration: 800, complete: function() { $("#gallery").addClass("opened"); $("#if-logo").css('z-index',10000); }});
		$("#g-content").animate({top: "10px"}, {queue:false, duration: 900});
		}
		
	var hideGallery = function(callback) {
		callback = callback || function(){};
		$("#if-logo").css('z-index',1);
		$("#gallery").animate({height: "50px"}, {queue:false, duration: 800, complete: function() { $("#gallery").css('z-index',1000); $("#gallery").css('width',600); $("#gallery").removeClass("opened"); $("#if-logo").css('z-index',10000); callback(); }});
		$("#g-content").animate({top: "100px"}, {queue:false, duration: 900});
		//callback();
		}
		
	var showActual = function() {
		$("#if-logo").css('z-index',1);
		$("#actual").css('width',980);
		$("#actual").css('z-index',900)
		$("#actual").animate({height: "243px"}, {queue:false, duration: 800, complete: function() { $("#actual").addClass("opened"); $("#if-logo").css('z-index',10000); }});
		$("#a-content").animate({top: "10px"}, {queue:false, duration: 900});
		}
		
	var hideActual = function(callback) {
		callback = callback || function(){};
		$("#if-logo").css('z-index',1);
		$("#actual").animate({height: "50px"}, {queue:false, duration: 800, complete: function() { $("#actual").css('z-index',1000); $("#actual").css('width',600); $("#actual").removeClass("opened"); $("#if-logo").css('z-index',10000); callback(); }});
		$("#a-content").animate({top: "100px"}, {queue:false, duration: 900});
		}


var donttriggermainmenu=null;


$(document).ready(function(){

	/* hauptmenü hover */
	$("#main-menu > li").hover(function() { if (!donttriggermainmenu) $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });
	
	/* header formulare */
		/* noscript buttons ausblenden */
		$(".usescript").show(); /* grafische buttons anzeigen, gegenpart zu <noscript> */
		
		$("#suche form img.usescript").click(function(e) {
													  $(this).parent("form").submit();
													  });
		
		$(".nlsearch form img.usescript").click(function(e) {
													  $(this).parent("form").submit();
													  });
		
		/* inline label */
		$('#header input[title]').each(function() {
			if($(this).val() === '') $(this).val($(this).attr('title'));			
			$(this).focus(function() { if($(this).val() == $(this).attr('title')) $(this).val(''); });
			$(this).blur(function() { if($(this).val() === '') $(this).val($(this).attr('title')); });
			});		   

	/* header overlays */
	var storezindex=null;
	$("ol#side-menu li[rel]").hover(function() { donttriggermainmenu=true; storezindex=$("#"+$(this).attr("rel")).css("z-index"); $("#"+$(this).attr("rel")).stop(true,true).slideDown("fast").css("z-index",10000); }, function() { donttriggermainmenu=null; $("#"+$(this).attr("rel")).stop(true,true).slideUp("fast").css("z-index",storezindex); });

	/* gallery navi einblenden */
	$("#g-navi").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });

	$(".article .article-full").css("display", "none");
	$(".button").addClass("hide");
	/* read article button */	
	
	$(".toggle").click(function(e) {
		e.preventDefault(); 
		shrinkContainer();
		if ($("#gallery").css("height")=="243px") hideGallery(null);
		if ($("#actual").css("height")=="243px") hideActual(null);
		var but = $(this);
		var help = $(but).text();
		var text = $(but).attr("rel");
		var rel=help;
		//var text = $(but).text() == 'Artikel schließen' ? 'Artikel lesen' : 'Artikel schließen';
		
		$(but)
			.text(text)
			.attr("rel",rel)
			.toggleClass("open")
			.parent(".article").toggleClass("read");
			$(but).siblings(".button").toggleClass("hide");	
			$(but).siblings(".article-full").slideToggle("slow", function() {
					
		});
	 });

	/* archiv navi einblenden */
	$("#p-archive-nav").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });
	
	/* archiv navi display current year */
	$("#p-archive-nav li a").click(function(){
		var elem = $(this);
		$("#p-archive-nav div.active").html( $(elem).attr("class") );
	});
	
	/* download popup window */
	/*$(".article a.download").colorbox({width:"547", inline:true, href: function(){
		var group = $(this).attr("rel");
		return "#popup-"+group;
	}, onComplete: function(){
		$("#cboxPrevious, #cboxNext").css("display","none");
	}});*/
	
	
	$(".article a.download").colorbox({width:"547", inline:true, href: function(){
		$("#popup-image > div > h2").html($("h2",$(this).parent()).html());
		$("#popup-thumb").attr("src",$("img",$(this).parent()).attr("src"));
		$.get("/incs/ajax/getImageLinks.ajax.php", { presseID:$(this).attr("rel") },function(data){	
			//if (console) console.log(data);
			$("#popup-image > div > ul").html(data);
			});
		return "#popup-image";
	}, onComplete: function(){
		$("#cboxPrevious, #cboxNext").css("display","none");
	}});
	
	/* downloadcenter */
		
		/* Add click to list items */
		$(".downloads li[rel='group'] > a").click(function(e) {
			e.preventDefault();
			$(this).siblings("ul").slideToggle();
			$(this).toggleClass("open");
			});

		/* Add class last to last li in lists */		
		$(".downloads ul li:last-child").addClass("last");
		
	/* newsletter popup window */
	$("#header .nlbutton").colorbox({width:"547", inline:true, href: function(){
		//if ($("#nlregemail").val()==$("#nlregemail").attr("title")) return false; else {																		
		if (1==1) { 
		//$("#popup-image > div > h2").html($("h2",$(this).parent()).html());
		//$("#popup-thumb").attr("src",$("img",$(this).parent()).attr("src"));
		/*$.get("/work/twintower/incs/ajax/getImageLinks.ajax.php", { presseID:$(this).attr("rel") },function(data){	
			$("#popup-nl > div > ul").html(data);
			});*/
		$("#nl-email").val(($("#nlregemail").attr("title")==$("#nlregemail").val()) ? "" : $("#nlregemail").val());
		$('#popup-nl input[title]').each(function() {
			if($(this).val() === '') $(this).val($(this).attr('title'));			
			$(this).focus(function() { if($(this).val() == $(this).attr('title')) $(this).val(''); });
			$(this).blur(function() { if($(this).val() === '') $(this).val($(this).attr('title')); });
			});		   
			
		
		return "#popup-nl";
		}
	}, onComplete: function(){
		$("#cboxPrevious, #cboxNext, #cboxTitle").css("display","none");
	}});
	
	
	/* footer gallery button */
	$("#g-button").click(function() {
		 if ($("#gallery").css("height")=="243px") {
			 shrinkContainer();
			 hideGallery(null);
			 setTowerStatus("gallery","hide");
			 }
		 else {
			 if ($("#actual").css("height")=="243px") {
				 hideActual(showGallery);
				 }
			 else {
				expandContainer(showGallery);
				}
			 setTowerStatus("gallery","show");
			 }
		 
		 });
	
	/* footer actual button */
	$("#a-button").click(function() {
		 if ($("#actual").css("height")=="243px") {
			 shrinkContainer();
			 hideActual(null);
			 setTowerStatus("actual","hide");
			 }
		 else {
			 if ($("#gallery").css("height")=="243px") {
				 hideGallery(showActual);
				 }
			 else {
				expandContainer(showActual);
				}
			 setTowerStatus("actual","show");
			 }
		 
		 });
	
	
	/* Session setzen, welcher tower geöffnet ist */
	function setTowerStatus(which,stat) {
		$.get("/incs/ajax/saveTowerStatus.ajax.php", { which:which, status:stat },function(data){	
			//if (console) console.log(data);
			});
		}
	
	/* actual onload anzeigen */
	/*expandContainer(showActual, "no");*/
	if (($(window).height()>600) && (!$("#container").hasClass("vis"))) { /* nur aufmachen, wenn der content hoch genug ist */
		if ($("#inshowgallery").attr("value")==1) expandContainer(showGallery, "no");
		if ($("#inshowactual").attr("value")==1) expandContainer(showActual, "no");
		}
	else {
		if ($("#inshowgallery").attr("value")==1) $("#gallery").addClass("opened");
		if ($("#inshowactual").attr("value")==1) $("#actual").addClass("opened");
		}

	/* mehrlesen content anzeigen */
	$("a.mehrlesen").click(function(e) {
			shrinkContainer();
			if ($("#gallery").css("height")=="243px") hideGallery(null);
			if ($("#actual").css("height")=="243px") hideActual(null);
			$(this).next("div.cont_mehrlesen").slideToggle();
			
			var help = $(this).html();
			var text = $(this).attr("rel");			
			
			//var text= $(this).html() == 'Schließen' ? 'Mehr lesen' : 'Schließen';
			$(this).html(text).attr("rel",help);
			e.preventDefault();
			});

   /* NEWSLETTER */	
   $(".regNL").click(function(e) {
							  $.get("/incs/ajax/newsletter.ajax.php", 
									{ 
									anab: ($("#nl-ab").attr("checked")) ? "ab" : "an",
									email: ($("#nl-email").attr("title")==$("#nl-email").val()) ? null : $("#nl-email").val(),
									name: ($("#nl-name").attr("title")==$("#nl-name").val()) ? null : $("#nl-name").val(),
									vorname: ($("#nl-vorname").attr("title")==$("#nl-vorname").val()) ? null : $("#nl-vorname").val(),
									month: ($("#nl-month").attr("checked")) ? "ja" : "nein",
									gastro: ($("#nl-gastro").attr("checked")) ? "ja" : "nein"
									},
									function(data) {
										$("#nl-error").hide();
										var temp=data.split("###");
										//if (console) console.log(data);
										if (temp[0]=="ERR") {
											$("#nl-error").show();
											$("#nl-error").html(temp[1]);													
											}
										if (temp[0]=="SUCC") {
											$("#nl-cont").hide();
											$("#nl-thx").html(temp[1]).show();
											}
										$('#header .nlbutton').colorbox.resize();
									});
							  e.preventDefault();
							  });
   
   
   /* NEWSLETTER POPUP ÖFFNEN */
   $("#popup-nl input[name='nl-type']").click(function() {
													   if ($("#nl-ab").attr("checked")) { $("#anInfo").hide(); $("#abInfo").show();  } else { $("#anInfo").show(); $("#abInfo").hide(); }
													   $('#header .nlbutton').colorbox.resize();
													   });


	/* gallery switch footer */
   var showGalID=null;
   var galTitle=null;
   
	$("#gal-switch > li > a").click(function() {
		galTitle=$(this).html();
		showGalID=$(this).attr("rel");									 
		$("#sliderGallery > ul")
			.fadeOut("slow", function() {
				$("#controlsx").remove();
				$("#prevBtn2").remove();
				$("#nextBtn2").remove();
				$.get("/incs/ajax/getSliderContent.ajax.php", { galID:showGalID },function(data){	
					// console.log(data);
					/*$("#sliderGallery #prevBtn2").hide();
					$("#sliderGallery #nextBtn2").hide();
					$("#sliderGallery #controlsx").hide();*/
					$("#g-navi > h3").html(galTitle);
					$("#sliderGallery > ul").html(data).fadeIn("slow", function() {
					
					$("#sliderGallery").easySlider({
								auto: false, 
								continuous: false,
								numeric: true,
								controlsShow: true,
								prevId: 		'prevBtn2',
								prevText: 		'Previous',
								nextId: 		'nextBtn2',	
								nextText: 		'Next',
								numericId:		'controlsx'
							});		
 					if ($("#slideGallery2").val()!="true") { $("#nextBtn2").hide();	}
 					$("#sliderGallery ul").css("margin-left",0);
					 $("#sliderGallery a[rel='gal']").colorbox({current:''});
					 });	
					
					});
		})
	});





	/* scroll to press item hash */
	if ($("#showpressdetail").val()>0) {
		/*var o=$("#article-"+$("#showpressdetail").val());
		var offset=o.offset();
		$(document).scrollTop(offset.top);*/
		setTimeout(function(){ $("#article-"+$("#showpressdetail").val()+" a.toggle").click(); },1500);							
		}
	
	
	/* lightbox content */
	$("#sliderContent > ul > li > a").click(function(e) { e.preventDefault(); });	/* damit das bild nicht in einem neuen fenster aufgehtm bevor die lightbox initialisiert wurde. */
	setTimeout(function() {
		// console.log("go");
		$("#sliderGallery a[rel='gal']").colorbox({current:''});
		$("#sliderContent a[rel='gal2']").colorbox({current:''});
		}, 2000);
	
	setTimeout(function() {
						
		/* slider welcome */
		$("#sliderStimmung").easySlider({
			auto: true, 
			continuous: true,
			pause:	5000,
			numeric: true,
			hoverPause: true,
			controlsShow: true,
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			numericId:		'controls'
		});
		
		/* slider content */
		$("#sliderContent").easySlider({
			auto: true, 
			continuous: true,
			pause:	5000,
			numeric: true,
			hoverPause: true,
			controlsShow: true,
			prevId: 		'prevBtn3',
			prevText: 		'Previous',
			nextId: 		'nextBtn3',	
			nextText: 		'Next',
			numericId:		'controlsy'
		});
		
		/* slider gallery */
			$("#sliderGallery").easySlider({
				auto: false, 
				continuous: false,
				numeric: true,
				controlsShow: true,
				prevId: 		'prevBtn2',
				prevText: 		'Previous',
				nextId: 		'nextBtn2',	
				nextText: 		'Next',
				numericId:		'controlsx'
			});
		if ($("#slideGallery").val()!="true") { $("#nextBtn2").hide();	}
		
	},1000);
});
	
