var eggBack = 30000;

$.ajaxSetup({ scriptCharset: "iso-8859-1" , contentType: "application/json; charset=iso-8859-1"});


/* cite */
top.citeBox;

$(document).ready(
		  function() {
			$.getJSON("http://www.begeisterung.de/cgi-bin/cite.cgi",
				  function(data) {
				     top.citeBox = document.createElement('div');
  					$("#imgLeft").after(top.citeBox);
				     
				     addCite(data["cite"], data["author"]);
				  }
				  );
		  }
		  );

function addCite(text,author) {
   top.citeBox.className = 'box';
   top.citeBox.style.marginBottom = '1.5em';
   var cite = document.createElement('p');
   cite.className = 'cite';
		     
   var citeString = '"Bissiger als der innere Schweinehund"';
   if (text) {
      text = text.replace(/&apos;/g, "'").replace(/&quot;/g, "'");
      citeString = '"'+text+'"';
   };
		     
   cite.appendChild(document.createTextNode(citeString));
		     
   top.citeBox.appendChild(cite);
   if (author) {
      var citeAuthor = document.createElement('p');
      author = author.replace(/&apos;/g, "'").replace(/&quot;/g, "'");
      citeAuthor.className = 'author';
      citeAuthor.appendChild(document.createTextNode(author));
      top.citeBox.appendChild(citeAuthor);
   };
};


/* searchBox */
var searchVar = 'Suche ...';
$(document).ready(
		  function () {
		     var searchBox = document.getElementById('search');
		     if (searchBox) {
//			var searchVar = 'Suche ...';
			with (searchBox) {
			   value = searchVar;
			   setAttribute('onblur','searchBoxShow(this,"'+searchVar+'")');
			   setAttribute('onfocus','searchBoxDel(this,"'+searchVar+'")');
			};
		     };
		  }
		  );
function searchBoxShow(obj, info) {
  if (obj && ("" == obj.value))
     obj.value = info;
}
function searchBoxDel(obj, info) {
  if (obj && (info == obj.value) )
    obj.value = "";
}



/* EggScript */
var topLineWidth = null;
$(document).ready(
		  function () {
		     topLineWidth = $("#topLine").width();
		     topLineWidth -= (topLineWidth / 15) + 120;
	     	     var egg = document.createElement('a');
		     egg.setAttribute('id','egg');
		     egg.setAttribute('href','http://www.dasbunteei.de/');
			$("#topLine").append(egg);
		     window.setTimeout("eggUp()", Math.floor(Math.random() * eggBack));
		  }
		  );
function eggUp () {
   var leftVal = (Math.floor(Math.random() * topLineWidth)) + 'px';
   with ($("#egg")) {
      css('left',leftVal);
      css('display','block');
      animate({height: "20px", marginTop: "-20px"}, 1500,'swing',eggDown);
   };
};
function eggDown () {
      $("#egg").animate({height: "0px", marginTop: "0px"},1500,'swing',callEgg);
};

function callEgg () {
   $("#egg").css('display','none');
   window.setTimeout("eggUp()", Math.floor(Math.random() * eggBack));
};



/* Druck-Info: */
$(document).ready(
		  function () {
		     var url = document.createElement('p');
		     url.setAttribute('id','url');
		     var now = new Date();
		     var nowH = now.getHours();
		     if (nowH < 10) nowH = '0'+nowH;

		     var nowM = now.getMinutes();
		     if (nowM < 10) nowM = '0'+nowM;
			
		     var downloadDate = new String(now.getDate() + '.'
						   +(now.getMonth() + 1) + '.'
						   + now.getFullYear()+ ' um '
						   + nowH + ':'
						   + nowM + ' Uhr');

		     

		     url.appendChild(document.createTextNode('Heruntergeladen am ' + downloadDate + ' unter ' + document.URL + ' .'));
		     $("#copyright").before(url);
		  }
		  );

/* Referenzen & Twitter */
$(document).ready(
		  function() {
		     var url = location.href;
		     var twitter_name = 'begeisterung';
		     if (url.match(/^http:\/\/[^\/]+\/(index\.html)?$/)) {
			   $.getJSON('http://twitter.com/status/user_timeline/'+twitter_name+'.json?count=2&callback=?',
				     function(data){
					   $('div#right').append('<div class="box" id="twitter_box"><div id="twitter_logo"><a href="http://twitter.com/begeisterung"><img alt="Twitter" src="/system/logo/logo_twitter.png" style="width: 64px;"/></a></div><p id="twitter_author"><a href="http://twitter.com/begeisterung" class="linkImage">Ralf R. Strupat</a> ...</p><ul></ul></div>');
					
					for (a in data) {
					      $('#twitter_box ul').append('<li>'+toHTML(data[a]["text"])+'</li>');
					};
				     }
				     );

			with ($("#imgLeft")) {
			   css('height','150px');
			   css('overflow','hidden');
			};
			window.setTimeout("nextRef()",5000);
		     };
		  }
		  );

function nextRef () {
   var para;
      $.ajax(
	     { 
		method: "get",
		url: 'http://www.begeisterung.de/cgi-bin/shortref.cgi',
		dataType: 'html',
		success: function(para) {
		      $("#imgLeft").append(para);
		   showRef();
   		}
	     }
	     )
};

function showRef () {
   var elems = $("#imgLeft").children();
   jQuery(elems[0]).animate(
			    {
			       top: "170px"
			    },
			    1000,
			    function() {
			       elems[0].parentNode.removeChild(elems[0])
			    }
			    );
   jQuery(elems[1]).animate(
			    {
			       marginTop: "0px",
			       paddingTop: "10px"
			    },
			    1500,
			    function () {
			       window.setTimeout("nextRef()",5000)
			    }
			    );
};


// JavaScript-Email-Dechiffrier-Funktion - inspiriert von blender3d.org
function dechiffrierMailTo(mailvar) {
  var mailadvar = 'pdlowr=' + mailvar;
  var zeichen=0;
  var newmailvar="";
  for(var i=0; i < mailadvar.length; i++) {
    zeichen = mailadvar.charCodeAt(i);
    newmailvar += String.fromCharCode(zeichen - 3);
  }
  location.href = newmailvar;
}


/* Firefox < 3-Bug:*/
$(document).ready(
		  function() {
		     var ag = navigator.userAgent;
		     if (ag.search(/Firefox/)) {
			var pos = ag.search(/Firefox/);
			if (pos >= -1) {
			   var versionInfo = ag.substr(pos+8,1);
			   if (versionInfo < 3) {
				 $("#content h3 span.ei").css({'margin-bottom':'13px','display':'-moz-inline-box'});
				 $("div#mainLeft ul li a span.leftarrow").css({'margin-bottom':'0.8em','display':'-moz-inline-box'});
				 $("div#mainLeft ul li a span.rightarrow").css({'margin-bottom':'0.8em','display':'-moz-inline-box'});
				 $("a.linkImage").css({'background-position':'right 3px','display':'-moz-inline-box'});
				 $("a.linkImage:hover").css({'background-position':'right -37px','display':'-moz-inline-box'});
			   }
			};
		     };
});


function showReference (obj,event) {
   obj.style.display = 'none';
   divs = obj.parentNode.parentNode.getElementsByTagName('div');
   for (i = 0; i < divs.length; i++) {
      if (divs[i].className == 'more') {
	 divs[i].style.display = 'block';
      };
   };
};

function toHTML (string) {
   string = string
      .replace(/(http:\/\/\S*)/g, '<a href="$1" target="blank">$1<\/a>')
      .replace(/#(\S*)/g, '<a href="http://twitter.com/search?q=%23$1" target="blank">#$1<\/a>')
      .replace(/@(\S*)/g, '<a href="http://twitter.com/$1" target="blank">@$1<\/a>');
   return string;
};
