 /* Simon Willison's addLoadEvent function allows you to stack up 'window.onload' events 
without them stepping on each other's toes. 
It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578 */
//alert('jetzt aber!');
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
//	alert('addLoadEvent');
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


/* This is our current popup function - parts of the site still use it, so I need to keep it */

function acpopup(strURL,strType,strWidth,strHeight) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
window.open(strURL, '', strOptions);
}

/* new accessible, unobtrusive popup code */
function _utf8_decode(utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}

function windowLinks() {                      // create a new function called windowLink(); 
    if(!document.getElementsByTagName) {      // Only run the function on browsers that
         return;                              // understand 'getElementsByTagName' - new browsers
    }
	
    var anchors = document.getElementsByTagName("a"); // grab all links and pop them in an array called 'anchors'
    for (var i = 0; i < anchors.length; i++) {        // start a loop to work our way through 
         var anchor = anchors[i];                     // grab the next link & copy it to 'anchor' for working
         var relIndex = anchor.rel;                   // get the value of it's 'REL' attribute
         var targetIndex = anchor.target;				// get the target value
         
// (anchor.href.substring(0, 34) == 'https://shop.naturthermetemplin.de') ||
         // define onclick for linker
         if( unescape(anchor.href).match(/http:\/\/shop\.suralin\.de/gi) ) // shop.naturthermetemplin.de
//         if( unescape(anchor.href).match(/https:\/\/shop\.naturthermetemplin\.de/gi) ) // shop.naturthermetemplin.de
         {
        	 var link = anchor.href;

        	 // handle openx urls
//           	 var target_pattern = /__oadest=(https:\/\/shop\.naturthermetemplin\.de.*?)(?:__.*|)$/gi
//        	 if( target_pattern.exec(unescape(anchor.href)) != null )
//        	 {
//        		 link = RegExp.$1;
//            	 alert("found: " +  RegExp.$1 + "\n" + unescape(anchor.href));
//        	 }	
        	 
        	 var onclick = '';
        	 if( (anchor.onclick!=undefined) && (anchor.onclick!='') ) onclick = anchor.onclick;
        	 onclick = "_gaq.push(['_link', '" + link + "']); " + onclick + ' return false;';
//        	 onclick = "function onclick(event) {\n    _gaq.push([\"_link\", \"" + link + "\"]);\n    return false;\n}";
        	 anchor.onclick = onclick;
//        	 alert('onclick: ' + anchor.onclick);
         }
        	 
		 if( relIndex || (targetIndex=="_blank") ) {                               // does it have a value for REL?...
		 var relSplit = relIndex.split(" "); // changed "|" to " "
		 /* if it does, look for '|' to use to split the value into an 
		 array - relSplit[0], relSplit[1], relSplit[2], etc . If it doesn't 
		 find any '|', the whole value gets transferred to relSplit[0] */
		 
/* XHTML compliant target attribute */

		 if( (relSplit[0] == "external") || (targetIndex=="_blank") ) {       // If the relSplit[0] is 'external'...
            anchor.target = "_blank";           // then set it's 'target' attribute to '_blank'

            // nur wenn es sich nicht um einen ausschliesslichen Bildlink handelt
			var Ausdruck = /^<img[^>]+>$/i;
			if( !Ausdruck.test(anchor.innerHTML) )
			{
				anchor.className = "external";      // then attach a CSS class to it 
			}
			// #todo: title in verschiedenen Sprachen anzeigen (auswählen)
			anchor.title = "Im neuen Fenster öffnen: "+ anchor.href;  
			// And give it a new title attribute to warn users a new window is launching
			
/* Elegantly degrading window code */

   			} else if (relSplit[0] == "popup") {      // else if relSplit[0] is 'popup'...

            // nur wenn es sich nicht um einen ausschliesslichen Bildlink handelt
			var Ausdruck = /^<img[^>]+>$/i;
			if( !Ausdruck.test(anchor.innerHTML) )
			{
				anchor.className = "popup";               // attach a CSS class to it 
			}
			// #todo: title in verschiedenen Sprachen anzeigen (auswählen)
			anchor.title = "Öffnet im PopUp-Fenster"; // Give it a helpful title attribute
			anchor.popupWidth = relSplit[1];          // set the popup's width
			anchor.popupHeight = relSplit[2];          // set the popup's height
	        anchor.onclick = function() {acpopup(this.href,'console',this.popupWidth,this.popupHeight);return false;};
			// plug all this information into our origainl window launch function (acpopup) 
			}
		}
	  }
} 

function do_stuff() {
//	alert('do_stuff');
	windowLinks();	// run our new function as soon as the page loads. 
	//otherFunctions();
//	addInteractiveSearchInput();
//	roundedImages();
//	alert('hier: ');
}

addLoadEvent(function() {
//	roundedImages();
	windowLinks();
//	addInteractiveSearchInput();
});

function addInteractiveSearchInput() {
	if(!document.getElementById) {      // Only run the function on browsers that
		return;                         // understand 'getElementById' - new browsers
	}
	var searchfield = document.getElementById("ssquery");
	var searchphrase = document.getElementById("sphrase");
	searchfield.onfocus= function() { if(this.value==searchphrase.value) this.value=''; else this.select(); };
	searchfield.onblur= function() { if(this.value=='') this.value=searchphrase.value; };
}
