/* ########################################################################
Titel: CrossBrowser PopUnder JavaScript

Description:
This code ensures the capability for opening popunder windows using  
javascript even if popup-blockers are present and active.
This code might be delivered by an adserver.

Tested Browsers:
    * Internet Explorer 6 (6.0.2900, WinXP Pro, SP2)
    * Internet Explorer 7 (7.0.5730.11, WinXP Pro, SP2)
	* Internet Explorer 8 (8.0.6001.17184, WinXP Pro, SP2)
	* Google Chrome 0.2 (0.2.149.30, WinXP Pro, SP2)
    * Firefox 2 (2.0.0.1)
    * Opera 8 (8.02)
    * Opera 9 (9.10)
    * Safari 2 (2.0.x)

Tested Toolbars:
    * Google Toolbar, Ver. 4.0.1020.2544-big/en (Internet Explorer 6.0.2900, WinXP Pro, SP2)
    * Yahoo Toolbar, Version 27.12.2006 (Internet Explorer 6.0.2900, WinXP Pro, SP2)


Date Created: 22.12.2006 - 17:08
Date Changed: 08.07.2008 - 16:15
Version: 1.9
Author: j.parree@invias.de
All rights reserved: INVIAS GmbH & Co. KG, www.invias.de
######################################################################## */

if (typeof Mirando === "undefined") {
	var Mirando = function() {};
}

Mirando.PopUnder = function() {

	/* CONFIGURATION PARAMETERS--------------------------------------- */
	this.windowName 		= false; /* the standard name of the new window or false if dynamic windownames are used */
	this.windowFeatures 	= 'scrollbars=yes,menubar=yes,location=yes,toolbar=yes,status=yes,resizable=yes'; /* you might want to also set 'width=800,height=600' */
	this.adTarget			= 'javascript:void(0);' //'http://www.mirando.de/?ad=popunder'; /* usualy set by Adserver */
	this.rewriteTags		= new Array('a', 'input', 'img', 'textarea', 'select'); /* Set all html tags which should be given an onclick-event during rewriting */
	this.triggerOnce		= true; /* Set this value to true, to only trigger the opening of a popunder window once per ad-webpage */
	this.uriBlacklist		= new Array('.googlesyndication.com', '.google.de', '.google.com', '.mirando.de'); /* Blacklist of URIs which must not be rewritten */
	this.elemIdBlacklist	= new Array('mirando'); /* Blacklist containing strings of Document-Element-IDs which must not be rewritten */
	this.instantOpen		= true; /* set to true, if you want to instantly try to open the popunder when it's loaded from the server */
	this.autoOpenDelay	= 2; /* set to a number of seconds you want to way before automaticaly open a popunder when 'this.instantOpen' is set to true */
	this.afterOpenCallback = null;
	/* --------------------------------------------------------------- */
	
	var actionTriggered = false;
	var now = false;
	
	var that = this; // to avoid dynamic this-binding problems
	
	
	function openAddLink()
	{
		
		var popUnderWindow = null;
		if(that.triggerOnce && actionTriggered) { return; }
		
		if(!that.adTarget && !that.afterOpenCallback) { 
			// neither content-url nor callback set, exiting.
			return; 
		}
		
		if(!that.windowName) {
			now = new Date();
			that.windowName = ''+(now.getTime());
		}

		var popUnderWindow = window.open(that.adTarget, that.windowName, that.windowFeatures);

		if(popUnderWindow) { 
			// special handling of blur() and focus() methods in chrome browser.
			if(navigator.userAgent.match(/chrome/i)) {
				popUnderWindow.focus();
				window.blur();
			} else {
				popUnderWindow.blur();
				window.focus();
			}
			actionTriggered = true;
			
			if (that.afterOpenCallback) {
				that.afterOpenCallback(popUnderWindow);
			}
		}
	}
	
	function inUriBlacklist(rewriteObj)
	{
		var k = 0;
		var matchString = '';
		var tagname = rewriteObj.tagName.toLowerCase();
		if(that.uriBlacklist.length>0) {
			if(tagname=='a') { matchString = rewriteObj.href.toLowerCase(); }
			if(tagname=='img') { matchString = rewriteObj.src.toLowerCase(); }
			if(matchString.length<1) return false;
			for(k=0;k<that.uriBlacklist.length;k++) {
				if(matchString.indexOf(that.uriBlacklist[k])!=-1) return true;
			}
		}
		return false;
	}
	
	function inElemIdBlacklist(rewriteObj)
	{
		var k = 0;
		var matchString = '';
		var tagname = rewriteObj.tagName.toLowerCase();
		if(that.elemIdBlacklist.length>0) {
			matchString = rewriteObj.id.toLowerCase();
			if(matchString.length<1) return false;
			for(k=0;k<that.elemIdBlacklist.length;k++) {
				if(matchString.indexOf(that.elemIdBlacklist[k])!=-1) return true;
			}
		}
		return false;
	}
	
	that.rewriteLinks = function()
	{
		var elementsFound = 0;
		var userAgent = navigator.userAgent;
		var i = 0;
		for(var j=0;j<that.rewriteTags.length;j++) {
			elementsFound = document.getElementsByTagName(that.rewriteTags[j]).length;
			if(elementsFound>0) {
				if(document.all && (!userAgent.match(/opera/i))) {
					for(i=0;i<elementsFound;i++) {
					
						var currentElement = document.getElementsByTagName(that.rewriteTags[j])[i];
					
						if(inUriBlacklist(currentElement)) continue;
						if(inElemIdBlacklist(currentElement)) continue;
						
						if(!currentElement.getAttribute('onclick')) {
							currentElement.attachEvent("onclick", openAddLink);
						}
					}
				} else {
					for(i=0;i<elementsFound;i++) {
						
						var currentElement = document.getElementsByTagName(that.rewriteTags[j])[i];
					
						if(inUriBlacklist(currentElement)) continue;
						if(inElemIdBlacklist(currentElement)) continue;
						
						if(!currentElement.onclick) {
							currentElement.onclick = function() {
								openAddLink();
							};
						}
					}
				}
			}
		}
		if(that.instantOpen) {
			// do not call this if client is chrome browser
			if(!navigator.userAgent.match(/chrome/i)) {
				window.setTimeout(function() {
					openAddLink();
				}, that.autoOpenDelay*1000 );
			}
		}
	}
}

