//////////////////////////////////////////////////////////////////////////////////////////////
// FRAME FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////
// init all basic variables
if (typeof(__saf_global) == "undefined") {var __saf_global = this;};
if (typeof(__saf_global.__saf_init) == "undefined") {__saf_global.__saf_init = false;};
if (typeof(__saf_global.__saf_bs) == "undefined") {__saf_global.__saf_bs = new Object();};
if (typeof(__saf_global.__saf_ex) == "undefined") {__saf_global.__saf_ex = new Object();};
if (typeof(__saf_global.__saf_ex_init) == "undefined") {__saf_global.__saf_ex_init = new Object();};
if (typeof(__saf_global.__saf_fv) == "undefined") {__saf_global.__saf_fv = 7;};
if (typeof(__saf) == "undefined") {var __saf = new Object();};

// Append window onload event.
__saf.addLoadListener = function(fn){
    if (typeof(window.addEventListener) != "undefined") {window.addEventListener("load", fn, false);} else if (typeof(document.addEventListener) != "undefined") {document.addEventListener("load", fn, false);} else if (typeof(window.attachEvent) != "undefined") {window.attachEvent("onload", fn);} else {var oldfn = window.onload; if (typeof(window.onload) != "function") {window.onload = fn;} else {window.onload = function() {oldfn(); fn();}}};
};
// get HTML Element & return its coordinates, return false if not found.
__saf.$ = function(el) {
	var obj;
	if (el == null) {obj = false;} else if (typeof(el) == "object" && typeof(el.tagName) != "undefined") {obj = el;} else if (typeof(el) == "string") {obj = (document.getElementById(el) != null) ? document.getElementById(el) : false;} else {obj = false;}
	if (obj != false) {
		obj._width = this.fGetCoordinates(obj, "_width");
		obj._height = this.fGetCoordinates(obj, "_height");
		obj._top = this.fGetCoordinates(obj, "_top");
		obj._left = this.fGetCoordinates(obj, "_left");
		obj.setHTML = function(htmlText) {obj.innerHTML = htmlText; return obj;};
		obj.setStyle = function (styleName, styleValue){obj.style[styleName] = styleValue; return obj;};
		obj.setStyles = function (styleObject){for (var key in styleObject) {obj.style[key] = styleObject[key];}; return obj;};
	};
	return obj;
};
// trim string
__saf.trim = function(s){
	var re = new RegExp("^\\s+|\\s+$", "g");
	return s.replace(re,"");
};
// parse number
__saf.parseNumber = function(n) {
	if (typeof(n) == "undefined" || n == null) {return 0;};
	return parseFloat(__saf.trim(n));
};
// get HTML Element's coordinates
__saf.fGetCoordinates = function(el, param) {
	if (!el || el == null) {
		return null;
	};
	//
	var position = this.fGetPosition(el);
	switch (param) {
		case "_width": return el.offsetWidth; break;
		case "_height": return el.offsetHeight; break;
		case "_top": return position._y; break;
		case "_left": return position._x; break;
	};
};
// get HTML Element's position
__saf.fGetPosition = function(el) {
	var e = el, l = 0, t = 0;
	do {
		try {
			l += e.offsetLeft || 0;
			t += e.offsetTop || 0;
			e = e.offsetParent;
		} catch(error) {
			e = false;
		};
	} while (e);
	return {"_x": l, "_y": t};
};
// show HTML Element
__saf.fShow = function(el) {
	__saf.fShowHideObject(el, true);
};
// hide HTML Element
__saf.fHide = function(el) {
	__saf.fShowHideObject(el, false);
};
// show/hide HTML Element
__saf.fShowHideObject = function(el, visible) {
	if (this.$(el)) {
		this.$(el).setStyle("visibility", (visible && visible == true) ? "visible" : "hidden");
	};
};
// create HTML Element
__saf.Element = function(sTagName) {
	var el = document.createElement(sTagName);
	el.appendTo = function(parentObj){
		if (parentObj == "body" || parentObj == document.body) {
			document.body.appendChild(el);
		} else if (__saf.$(parentObj)) {
			__saf.$(parentObj).appendChild(el);
		};
	};
	return __saf.$(el);
};
// detect Flash PLayer version
__saf.PlayerVersion = function(arrVersion){
	var parseVersion = (typeof(arrVersion).toString().toLowerCase() != "array") ? arrVersion.toString().split(".") : arrVersion;
	var fv = new Object();
	fv.major = parseVersion[0] != null ? parseInt(parseVersion[0]) : 0;
	fv.minor = parseVersion[1] != null ? parseInt(parseVersion[1]) : 0;
	fv.rev = parseVersion[2] != null ? parseInt(parseVersion[2]) : 0;
	return fv;
};
// detect Flash PLayer version
__saf.getPlayerVersion = function(){
	var PlayerVersion = new __saf.PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			var re1 = new RegExp("([a-zA-Z]|\\s)+");
			var re2 = new RegExp("(\\s+r|\\s+b[0-9]+)");
			PlayerVersion = new __saf.PlayerVersion(x.description.replace(re1, "").replace(re2, ".").split("."));
		};
	} else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){ // if Windows CE
		var axo = 1;
		var counter = 3;
		while(axo) {
			try {
				counter++;
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+ counter);
				PlayerVersion = new __saf.PlayerVersion([counter,0,0]);
			} catch (e) {
				axo = null;
			};
		};
	} else { // Win IE (non mobile)
		try {
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		} catch(e) {
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				PlayerVersion = new __saf.PlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always";
			} catch(e) {
				if (PlayerVersion.major == 6) {
					return PlayerVersion;
				}
			};
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {};
		};
		if (axo != null) {
			PlayerVersion = new __saf.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		};
	};
	return PlayerVersion;
};
//
__saf.versionIsValid = function(fv){
	var installVer = this.getPlayerVersion();
	if(installVer.major < fv.major) return false;
	if(installVer.major > fv.major) return true;
	if(installVer.minor < fv.minor) return false;
	if(installVer.minor > fv.minor) return true;
	if(installVer.rev < fv.rev) return false;
	return true;
};
// create SWF object
__saf.SWF = function(moviePath, movieWidth, movieHeight, movieBackground, alternateHTML, movieContainer, containerOffset){
	this.__movie = moviePath;
	this.__id = movieContainer+"_saf_swf";
	this.__width = movieWidth;
	this.__height = movieHeight;
	this.__background = movieBackground;
	this.__parameters = new Object();
	this.__variables = new Object();
	this.__alternateHTML = alternateHTML;
	this.__container = movieContainer;
	this.__offset = containerOffset;
	this.__initialized = false;
	this.setParam = this.fSetParam;
	this.setVariable = this.fSetVariable;
	this.isValidVer = __saf.versionIsValid(__saf.PlayerVersion(__saf_global.__saf_fv));
	this.initialize = this.fInitialize;
};
//
__saf.SWF.prototype = {
	// parse movie Id from movie Path
	fSwfParseId : function(moviePath) {
		var sPath = moviePath.toString().toLowerCase();
		return sPath.substring(0, sPath.lastIndexOf("."));
	},
	// set parameter for the flash movie (add to _parameters object)
	fSetParam : function(paramName, paramValue){
		this.__parameters[paramName] = paramValue;
	},
	// set variables for the flash movie (add to __variables object)
	fSetVariable : function(variableName, variableValue){
		this.__variables[variableName] = variableValue;
		// if the __saf_SWF has been created, we will pass the variable directly to the movie
		if (__saf.$(this.__id)) {
			__saf.$(this.__id).SetVariable(variableName, variableValue);
		};
	},
	// create SWF into an passed object
	fInitialize : function() {
		var swfHTML = "";
		if (this.isValidVer == true) {
			var variablePairs = new Array();
			for (var fv in this.__variables) {variablePairs[variablePairs.length] = fv+"="+this.__variables[fv];}
			var vars = variablePairs.join("&");
			//
			swfHTML += "\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\""+this.__width+"\" height=\""+this.__height+"\" name=\""+this.__id+"\" id=\""+this.__id+"\">\n";
			// object tag
			swfHTML += "\t<param name=\"movie\" value=\""+this.__movie+"\" />\n";
			swfHTML += "\t<param name=\"quality\" value=\"high\" />\n";
			swfHTML += "\t<param name=\"menu\" value=\"false\" />\n";
			swfHTML += "\t<param name=\"bgcolor\" value=\""+this.__background+"\" />\n";
			for (var k1 in this.__parameters) { swfHTML += "\t<param name=\""+k1+"\" value=\""+this.__parameters[k1]+"\" />\n";}
			if (vars.length > 0) {swfHTML += "\t<param name=\"flashvars\" value=\""+vars+"\" />\n";}
			// embed tag
			swfHTML += "\t<embed src=\""+this.__movie+"\" name=\""+this.__id+"\" id=\""+this.__id+"\" quality=\"high\" menu=\"false\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\""+this.__width+"\" height=\""+this.__height+"\" bgcolor=\""+this.__background+"\" ";
			for(var k2 in this.__parameters){swfHTML += [k2]+"=\""+this.__parameters[k2]+"\" ";}
			if (vars.length > 0) {swfHTML += "\"flashvars=\""+vars+"\"";}
			swfHTML += "></embed>\n";
			swfHTML += "</object>";
		} else {
			swfHTML += this.__alternateHTML;
		};
		//
		if (swfHTML != "") {
			if (__saf.$(this.__container)) {
				__saf.$(this.__container).setHTML(swfHTML);
				if (this.__offset) {
					var aRef = this.__offset.split(",");
					var l = __saf.parseNumber(aRef[0]);
					var t = __saf.parseNumber(aRef[1]);
					if (l != 0 && t != 0) {
						__saf.$(this.__container).setStyles({
							position: "absolute",
							left: l+"px",
							top: t+"px"
						});
					}
				}
			} else if (this.__offset) {
				var aRef = this.__offset.split(",");
				var oRef = __saf.$(__saf.trim(aRef[2]));
				var l = __saf.parseNumber(aRef[0]);
				var t = __saf.parseNumber(aRef[1]);
				if (oRef) {l += oRef._left; t += oRef._top;}
				var oDiv = new __saf.Element("div");
				oDiv.id = this.__container;
				oDiv.className = "indexTop"
				oDiv.setStyles({
					position: "absolute",
					width: this.__width+"px",
					height: this.__height+"px",
					left: l+"px",
					top: t+"px"
				}).setHTML(swfHTML).appendTo("body");
			};
		};
		//
		this.__initialized = true;
	}
};
//////////////////////////////////////////////////////////////////////////////////////////////
// ADVERTISING FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////
// Create banner
__saf.fInitBanner = function(bannerObject){
	var __o = bannerObject;
	var __aSize = __o.size.split("x");
	if (__aSize.length == 2 && typeof(__saf_global.__saf_bs[__o.content]) == "undefined") {
		__saf_global.__saf_bs[__o.content] = new this.SWF(__o.movie, __aSize[0], __aSize[1], (typeof(__o.background) != "undefined") ? __o.background : "#FFFFFF", __o.alternateHTML,__o.content, __o.offset);
		if (typeof(__o.wmode) != "undefined") {__saf_global.__saf_bs[__o.content].setParam("wmode", __o.wmode);}
		if (typeof(__o.clickTag) != "undefined") {
			var tag = __o.clickTag.split("=");
			__saf_global.__saf_bs[__o.content].setVariable(tag[0], tag[1]);
		};
		__saf_global.__saf_bs[__o.content].initialize();
	};
};
// create current banner & save extend banner content to create later.
__saf.fInitBaseBanner = function() {
	if (typeof(__saf_base) != "undefined") {
		// create base Flash Ad
		this.fInitBanner(__saf_base);
		// store expand Flash Ad
		var __count = 1;
		while (__saf_global["__saf_extend"+__count ]){
			var __o = __saf_global["__saf_extend"+__count ];
			if (typeof(__saf_global.__saf_ex[__o.content]) == "undefined") {
				__saf_global.__saf_ex[__o.content] = __o;
			};
			__count++;
		};
	};
};
// create extend banners (if have)
__saf.fInitExtendBanners = function() {
	if (__saf_global.__saf_init == true) {return;};
	for (var i in __saf_global.__saf_ex) {__saf.fInitBanner(__saf_global.__saf_ex[i]);};
	__saf_global.__saf_init = true;
};
// create extend banner (if have)
__saf.fInitExtendBanner = function(i) {
	if (__saf_global.__saf_ex_init[i] == true) {return;};
	__saf.fInitBanner(__saf_global.__saf_ex[i]);
	__saf_global.__saf_ex_init[i] == true;
};
//
__saf.fInitBaseBanner();
//__saf.addLoadListener(__saf.fInitExtendBanners);