window.onload = function ()
{
	checkBanners();
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var URLescape = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		var str = this._utf8_decode(unescape(string));
		return str.replace(/\+/g, ' ');
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (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 checkBanners()
{
	var banners = document.getElementsByTagName('a');
	var replace = [];
	for(var i = 0; i < banners.length; i++)
	{
		var banner = banners[i];
		if(banner.className.match(/^banner_image/))
		{
			var imgs = banner.childNodes;
			for(var j = 0; j < imgs.length; j++)
			{
				if(imgs[j].tagName == 'IMG') 
				{
					slot = imgs[j].src.split('?');
					slot = slot[1].split('&');
					for(var k = 0; k < slot.length; k++) if(slot[k] && slot[k].match(/^slot=/))
					{
						slot = slot[k].split('=');
						slot = slot[1];
						if(getCookie('scms_banners[' + slot + '][flash]')) replace.push({ elem: imgs[j], slot: slot});
						imgs[j].onclick = setNewWindow;
						var alt = URLescape.decode(getCookie('scms_banners[' + slot + '][alt_text]'));
						if(alt != 'undefined') imgs[j].alt = imgs[j].title = alt;
						if(!getCookie('scms_banners[' + slot + '][target]')) replace.push({ elem: imgs[j], target: 1});
					}
				}
			}
		}
	}
	
	for(var i = 0; i < replace.length; i++)
	{
		if(replace[i].slot) replaceBanner(replace[i].elem, replace[i].slot);
		if(replace[i].target) replace[i].elem.parentNode.parentNode.replaceChild(replace[i].elem, replace[i].elem.parentNode);
	}
}


function replaceBanner(elem, slot)
{
	if(slot && getCookie('scms_banners[' + slot + '][flash]'))
	{
		var flash = getCookie('scms_banners[' + slot + '][flash]');
		if (flash && flashinstalled == 2) 
		{
			var oeTags = '<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
			+ 'WIDTH="' + elem.width + '" HEIGHT="' + elem.height + '"'
			+ 'CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
			+ '<PARAM NAME="MOVIE" VALUE="' + flash + '?clickTAG=%2Fextensions%2Fsampo_banner_manager%2Fredirect.php%3Fslot%3D' + slot + '">'
			+ '<PARAM NAME="PLAY" VALUE="true">'
			+ '<PARAM NAME="LOOP" VALUE="true">'
			+ '<PARAM NAME="QUALITY" VALUE="high">'
			+ '<PARAM NAME="MENU" VALUE="false">'
			+ '<EMBED SRC="' + flash + '?clickTAG=%2Fextensions%2Fsampo_banner_manager%2Fredirect.php%3Fslot%3D' + slot + '"'
			+ 'WIDTH="' + elem.width + '" HEIGHT="' + elem.height + '"'
			+ 'PLAY="true"'
			+ 'LOOP="true"'
			+ 'QUALITY="high"'
			+ 'MENU="false"'
			+ 'TYPE="application/x-shockwave-flash"'
			+ 'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'
			+ '</EMBED>'
			+ '</OBJECT>';
			
			var span = document.createElement('span');
			span.innerHTML = oeTags;
			elem.parentNode.parentNode.replaceChild(span, elem.parentNode);
		}
	}
}

function setNewWindow()
{
	if(getCookie('scms_banners[' + slot + '][pop]') == 1)
	{
		this.parentNode.target = '_blank';
	}
}
