/* layout_refiner v1.2 -- January 2009
 - Realigns images to fit grid
 - Revises double quotes to single quotes in blockquotes
 - Provides Yellow Fading Technique for foot note descriptions
 - Checks for IE6 and puts out warning message
*/

function refine_layout() {
	revise_blockquotes();
	realign_images();
	//realign_objects();
	yellow_fading_technique();
}


/* Revise Blockquotes */

function revise_blockquotes() {
	var content, revised;
	for(var i = 0; i < document.getElementsByTagName("blockquote").length ; i++) {
		content = document.getElementsByTagName("blockquote")[i].innerHTML;
		revised = content.replace(/»/g, "›").replace(/«/g, "‹");
		document.getElementsByTagName("blockquote")[i].innerHTML = revised;
	}
}


/* Realign Images */

function realign_images() {
	var too_much, add_padding, add=0;
	for(var i = 0; i < document.getElementsByTagName("img").length ; i++) {
		if (document.getElementsByTagName("img")[i].src.indexOf('stars')==-1 &&
			document.getElementsByTagName("img")[i].src.indexOf('smilie')==-1 &&
			document.getElementsByTagName("img")[i].parentNode.parentNode.className!='flickr'
			) { //only use one 'regular' images			
			too_much = (document.getElementsByTagName("img")[i].height+2)%20; //+2 for top & bottom border
			if (too_much > 10 ) {add=10;} else {add=0;}
			add_padding = (20-too_much)/2 + add;
			document.getElementsByTagName("img")[i].style.paddingTop=Math.floor(add_padding)+"px";
			document.getElementsByTagName("img")[i].style.paddingRight=Math.floor(add_padding)+"px";
			document.getElementsByTagName("img")[i].style.paddingBottom=Math.round(add_padding)+"px";
			document.getElementsByTagName("img")[i].style.paddingLeft=Math.floor(add_padding)+"px";
		}
	}
	return;
}

/* Realign Objects */

function realign_objects() {
	var too_much, add_padding, add=0;
	for(var i = 0; i < document.getElementsByTagName("object").length ; i++) {
			too_much = (document.getElementsByTagName("object")[i].height)%20; //+2 for top & bottom border
			if (too_much > 10 ) {add=10;} else {add=0;}
			add_padding = (20-too_much)/2 + add;
			document.getElementsByTagName("object")[i].style.paddingTop=Math.floor(add_padding)+"px";
			document.getElementsByTagName("object")[i].style.paddingRight=Math.floor(add_padding)+"px";
			document.getElementsByTagName("object")[i].style.paddingBottom=Math.round(add_padding+100)+"px";
			document.getElementsByTagName("object")[i].style.paddingLeft=Math.floor(add_padding)+"px";
	}
	
	return;
}


/* IE Check */

function ie_check() {	   
	ua = navigator.userAgent; 
	links='<p style="color:red;">Bitte steig auf <a href="http://www.microsoft.com/germany/windows/internet-explorer/default.aspx">Internet Explorer 8</a> um oder infomiere dich über <a href="/ueber/webstandards/">überlegene Alternativen</a>. Danke!</p><p style="color:red;><abbr title="Postscriptum">PS:</abbr> <a href="http://www.flickr.com/photos/bensite/3152185686/sizes/o/in/set-72057594121837890/">So</a> sieht diese Seite in einem zeitgemäßen Browser aus.</p><p style="color:red;"><a href="#" onclick="document.getElementById(\'ie_warning\').style.display=\'none\';document.cookie=\'bensite.net=ie_warning_hide\';return false;">Warnung ausblenden [X]</a></p></div><hr />';
	
	
	if ( ua.indexOf('Opera') == -1 && document.cookie.indexOf("bensite.net=ie_warning_hide")==-1 ) {
		// the browser claims to be IE, and is not Opera
		//works because there is no 'text' inside wrapper, just other 'HTML'		
		
		if (ua.indexOf('MSIE 5') != -1 ) {

			document.getElementById("wrapper").innerHTML='<div id="ie_warning"><h1 style="color:red;">&raquo;Redmond, wir haben ein Problem!&laquo;</h1><p style="color:red;">Du benutzt eine Uralt-Version von <strong>Internet Explorer</strong>. Diese Seite basiert auf Webstandards, die dein Browser nicht ausreichend unterstützt. Du kannst zwar auf alle Inhalte zugreifen, die Seite wird aber ohne <abbr title="Cascading Stylesheets">CSS</abbr> ausgeliefert.</p>' + links;
		} else if (ua.indexOf('MSIE 6') != -1 ) {
			document.getElementById("wrapper").innerHTML='<div id="ie_warning"><h1 style="color:red;">&raquo;Redmond, wir haben ein Problem.&laquo;</h1><p style="color:red;">Du benutzt den veralteten <strong>Internet Explorer 6</strong>. Diese Seite basiert auf Webstandards, die dein Browser nicht ausreichend unterstützt. Du kannst zwar auf alle Inhalte zugreifen, die Seite wird aber ohne <abbr title="Cascading Stylesheets">CSS</abbr> ausgeliefert.</p>' + links;
		}  else if (ua.indexOf('MSIE 7') != -1 ) {
			document.getElementById("wrapper").innerHTML='<div id="ie_warning"><h1 style="color:red;">&raquo;Redmond, wir haben ein Problem.&laquo;</h1><p style="color:red;">Du benutzt den veralteten <strong>Internet Explorer 7</strong>. Du kannst zwar auf alle Inhalte zugreifen, die Seite wird aber ohne <abbr title="Cascading Stylesheets">CSS</abbr> ausgeliefert.</p>' + links;
		} 
	
	
	
	}
	
	
}



/* Yellow Fading Technique */

function yellow_fading_technique() {
	var i=-2; /* -2 to 0 for comment form highlighting*/
	while ( document.getElementById("note-"+i) ) {
		if (window.addEventListener) {
			document.getElementById("note-"+i).addEventListener("click", yellowFade_moz, false); 
		} else {
			document.getElementById("note-"+i).attachEvent("onclick", yellowFade_ie); 
		}
		i++;
	}
}

function yellowFade_moz(el) {
	var b = 55;
  	function f_moz() {
		document.getElementById(el.target.getAttribute("href").replace("#","")).style.background = 'rgb(255,255,'+ (b+=4) +')';
		if (b < 255) {
	  		setTimeout(f_moz, 25);
		}
	};
  	f_moz();
}

var b = 55;
function f_ie(a) {
	document.getElementById(a.replace(/#/,"")).style.background = 'rgb(255,255,'+ (b+=4) +')';
	if (b < 255) {
		setTimeout(function(){f_ie(a)}, 25);
	} else {
		b=55;
	}
}

function yellowFade_ie(evt) {
	f_ie(evt.srcElement.getAttribute("href",2));
}