/** * Adverticum Helper class * * This class helps to work with the Adverticum AdServer. * * - makes it easy to use the Click-through URLs * - provides close function to hide layers * * author: KARASZI Istvan * copyright: Adverticum Zrt. */ package { import flash.display.DisplayObject; import flash.display.InteractiveObject; import flash.events.MouseEvent; import flash.external.ExternalInterface; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.net.URLVariables; public class AdverticumHelper { /** * Utility function to wrap up changing pages. Avoids over-aggressive popup blockers. * * @param url * the URL to change to. Either a String or a URLRequest * @param window * the target browser window/tab, generally _self, _top, or _blank * @usage AdverticumHelper.changePage("http://www.google.com", "_blank"); */ public static function changePage( url:*, window:String = "_blank" ):void { var req:URLRequest = url is String ? new URLRequest(url) : url; if (!ExternalInterface.available) { navigateToURL(req, window); } else { var strUserAgent:String = String(ExternalInterface.call("function() {return navigator.userAgent;}")).toLowerCase(); if ((strUserAgent.indexOf("msie") != -1 && uint(strUserAgent.substr(strUserAgent.indexOf("msie") + 5, 3)) >= 7)) { ExternalInterface.call("window.open", req.url, window); } else { navigateToURL(req, window); } } } /** * Binds the close event to the specified object * * @param obj * the object to bind event on */ public static function bindCloseEvent( obj:InteractiveObject ):void { obj.addEventListener(MouseEvent.MOUSE_UP, function( e:MouseEvent ) { closeLayer(obj.root); }); } /** * Closes the layer * * @param root * the root Object */ public static function closeLayer( root:DisplayObject ):void { if (ExternalInterface.available) { if (root.loaderInfo.parameters.closeFUNCTION) { var cf:String = "window.".concat(root.loaderInfo.parameters.closeFUNCTION); try { ExternalInterface.call(cf); } catch( e:Error ) { trace("Could not execute ExternalInterface.call()"); } } } else { trace("ExternalInterface is not available"); } } /** * Binds the click event to an object with the specified CT link. * * @param obj * the object to bind event on * @param ct * the click tag * @param vars * the variables to append to click tag */ public static function bindCTEvent( obj:InteractiveObject, ct:String, vars:URLVariables=null ):void { obj.addEventListener(MouseEvent.MOUSE_UP, function( e:MouseEvent ) { changePage(getURLRequest(obj.root, ct, vars), getClickTarget(obj.root)); }); } /** * Gets the click target ("_blank", "_top", etc.). * * @param root * the root object */ public static function getClickTarget( root:DisplayObject ):String { var target:String = null; if (root.loaderInfo.parameters.clickTARGET) { target = root.loaderInfo.parameters.clickTARGET; } else { target = "_blank"; } return target; } /** * Gets the URL request for the specified CT link. * * @param root * the root object * @param ct * the click tag * @param vars * the variables to append to click tag */ public static function getURLRequest( root:DisplayObject, ct:String, vars:URLVariables=null ):URLRequest { var link:String = _getClickURL(root, ct); // modify URL only if we have variables if (vars != null) { var fragment:String = ""; var fragmentPos = link.indexOf('#'); // cut out fragment if (fragmentPos != -1) { fragment = link.substr(fragmentPos); link = link.substr(0, fragmentPos); } link = link.concat((link.indexOf("?") == -1) ? "?" : "&", _encodeData(vars), fragment); } trace("getURLRequest(): ".concat(link)); return new URLRequest(link); } /** * Gets the click URL for the specified CT link. * * @param root * the root object * @param ct * the click tag */ private static function _getClickURL( root:DisplayObject, ct:String ):String { var url:String = null; if (ExternalInterface.available && root.loaderInfo.parameters.clickFUNCTION) { var cf:String = "window.".concat(root.loaderInfo.parameters.clickFUNCTION); try { url = ExternalInterface.call(cf, ct); } catch( e:Error ) { url = ct; } } else { url = ct; } return url; } /** * Converts the passed variables to URL encoed form. * * @param vars * the variables */ private static function _encodeData( vars:URLVariables ):String { var variables:URLVariables = new URLVariables(); variables.fwd = vars.toString(); return variables.toString(); } } } // vim: ai ts=4 sw=4 noet ft=actionscript: