/**
 * Dynamic function name to create a seperated enviroment.
 */
if (typeof document["__showOxynadeAd" + window.location + (new Date())] != "function") {
	document["__showOxynadeAd" + window.location + (new Date())] = function(container, position, width, height, language, visitor_country, country, region, town, google, image, imageLink) {
		var host;

		try {
			// Try to get the host where this script is running on
			var scripts = document.getElementsByTagName("script");

			var pattern = new RegExp("^(https?://ads\\.((local|dev)\\.)?oxynade\\.com)", "i");
			var matches = pattern.exec(scripts[scripts.length - 1].src);

				host = matches[1];
		} catch (e) {
			// No catching, fallback follows
		} finally {
			if (typeof host != "string" || host.length < 3) {
				host = "http://ads.oxynade.com";
			}
		}

		// $_GET parameter names
		var names = new Array("", "position", "width", "height", "language", "visitorCountry", "country", "region", "town", "google", "image", "link");

		// Function to serialize data
		var serialize = function(data) {
			switch (typeof data) {
				case "object":
					var result = new Array();

					// Loop over elements and add them as json string
					for (var k in data) {
						if (typeof data[n] == "integer" || typeof data[n] == "string") {
							result.push('"' + k + '":"' + data[n] + '"');
						}

					}

					data = "{" + result.join(",") + "}";
					// No break here
				case "string":
					// Decode entities
					var div = document.createElement("div");
					div.innerHTML = data;
					data = (div.textContent || div.innerText);

					// URL encode
					data = encodeURIComponent(data);
					break;
			}

			return data;
		};

		// Construct the iframe link
		var link = host + "/showAd.php?host=" + window.location.host;

		for (var i = 1; i < arguments.length; i++) {
			if (arguments[i] != null) {
				switch (typeof arguments[i]) {
					case "string":
						if (arguments[i].length < 1) {
							continue;
						}
						break;
					case "object":
						for (var key in arguments[i]) {
							if (arguments[i].hasOwnProperty(key)) {
								break;
							}
						}

						continue;
						break;
					case "number":
						if (arguments[i] < 1) {
							continue;
						}
						break;
					default:
						continue;
						break;
				}

				link += "&amp;" + names[i] + "=" + serialize(arguments[i]);
			}
		}

		// Iframe code
		var iframe = '<iframe src="' + link + '" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0" style="width: ' + parseInt(width) + 'px; height: ' +  parseInt(height) + 'px; overflow: hidden;" allowtransparency="true"></iframe>';

		// Container provided?
		if (container.length < 0) {
			container = "eventad_" + position;
		}

		container = document.getElementById(container);

		// Insert the ad
		if (container != null) {
			container.innerHTML = iframe;
		} else {
			document.write(iframe);
		}
	}
}

if (typeof ad_container != "string") {
	var ad_container = "";
}

if (typeof ad_language != "string") {
	var ad_language = "";
}

if (typeof ad_visitor_country != "string") {
	var ad_visitor_country = "";
}

if (typeof ad_fallback_google != "object") {
	var ad_fallback_google = {};
}

if (typeof ad_fallback_image != "string") {
	var ad_fallback_image = "";
}

if (typeof ad_fallback_link != "string") {
	var ad_fallback_link = "";
}

document["__showOxynadeAd" + window.location + (new Date())](ad_container, ad_position, ad_width, ad_height, ad_language, ad_visitor_country, ad_country, ad_region, ad_town, ad_fallback_google, ad_fallback_image, ad_fallback_link);
