var CM_SITE_NAME="guardian",CM_SERVER="engine.cmmeglobal.com",CM_REQUEST_HTML_PATH="/cm_request.html",CM_ENABLE_FIREBUG_LOGGING=false,CM_RESPONSE_CHECK_PERIOD_MS=10,CM_RESPONSE_TIMEOUT_MS=3500,CM_EVENT_DELIM="|",CM_INCLUDE_PROFILE_ID=false,CM_PROFILE_COOKIE_NAME="",CM_PROFILE_ID_REGEX=CM_PROFILE_COOKIE_NAME+"=([^;]*)",CM_QS_SITE_NAME_PARAM="sitenm",CM_QS_CAPSULE_NAME_PARAM="capnm",CM_QS_MATCHING_AREA_ID_PARAM="maid",CM_QS_MATCHING_AREA_URL="murl",CM_QS_TIME_PARAM="t",CM_QS_TIME_ZONE_PARAM="tzo",CM_QS_PAGE_VIEW_ID_PARAM=
"pgid",CM_QS_IMPRESSION_ID="impid",CM_QS_NAVIGATOR_PLUGINS="nplg",CM_QS_WINDOW_HISTORY_LENGTH="hstl",CM_QS_WINDOW_SCREEN_HEIGHT="scrh",CM_QS_WINDOW_SCREEN_WIDTH="scrw",CM_QS_WINDOW_PIXEL_DEPTH="scrd",CM_QS_EXTERNAL_PARAMETERS="ep",CM_QS_REFERRER="ref",CM_QS_PAGE="pg",CM_QS_ERROR="err",CM_QS_PROFILE_ID="pfid",CM_QS_PROFILE_ID_ERR="pfiderr",CM_MAX_URL=1024,CM_URI_CLICK_LOG_PATH="/v1/click",CM_URI_REQUEST_LOG_PATH="/v1/request",CM_URI_PAGE_VIEW_PATH="/v1/page-view",CM_URI_VISITOR_EVENT_V1_PATH="/v1/visitor-event",
CM_URI_LOG_URL_REQUEST="/v1/log-url-request",CM_URI_REWARD_EVENT_V1_PATH="/v1/secure-click-reward",CM_COOKIE_NAME_CAPSULE_CLICK="click",CM_AREA_NAME_WILDCARD="*";function cm_log(a){CM_ENABLE_FIREBUG_LOGGING&&console.log(a)}function cm_trim(a){if(!a)return"";if(a.length<=CM_MAX_URL)return a;return a.substr(0,CM_MAX_URL)}function cm_format_reward(a){a=a.replace(/^\s+/,"");if(isNaN(a.charAt(0)))return a.substring(1,a.length);return a}
function cm_generate_page_view_id(){for(var a=[],c=0,b=0;c<36;c++)a[c]=Math.floor(Math.random()*16);a[14]=4;for(a[19]=a[19]&3|8;b<36;b++)a[b]="0123456789ABCDEF".charAt(a[b]);a[8]=a[13]=a[18]=a[23]="-";return a.join("")}function cm_encode(a){return encodeURIComponent(a)}function cm_is_ssl(){return document.location.protocol==="https"}function cm_defined(a){return typeof a!=="undefined"}
function cm_read_cookie(a){a=a+"=";for(var c=document.cookie.split(";"),b=0;b<c.length;b++){for(var d=c[b];d.charAt(0)===" ";)d=d.substring(1,d.length);if(d.indexOf(a)===0)return d.substring(a.length,d.length)}return null}function cm_get_window_location(){var a=window.location.hostname;if(cm_defined(window.location.port)&window.location.port!=="")a=a+":"+window.location.port;return a}
function cm_set_cookie(a,c,b){if(b){var d=new Date;d.setTime(d.getTime()+b*1E3);b="; expires="+d.toGMTString()}else b="";document.cookie=a+"="+c+b+"; path=/"}function cm_delete_cookie(a){cm_set_cookie(a,"",-1)}var CM_PAGE_VIEW_ID=cm_generate_page_view_id();function cm_build_profile_id_query_string(){var a=document.cookie.match(CM_PROFILE_ID_REGEX);return a===null?CM_QS_PROFILE_ID_ERR+"=cookie":a[1]===null||a[1].length===0?CM_QS_PROFILE_ID_ERR+"=regex":CM_QS_PROFILE_ID+"="+a[1]}
function cm_make_page_viewid_qs(){CM_PAGE_VIEW_ID||(CM_PAGE_VIEW_ID=generatePageViewId());return CM_QS_PAGE_VIEW_ID_PARAM+"="+CM_PAGE_VIEW_ID}function cm_make_time_qs(){var a=new Date;return CM_QS_TIME_PARAM+"="+a.getTime()+"&"+CM_QS_TIME_ZONE_PARAM+"="+a.getTimezoneOffset()}function cm_common_qs_params(){return CM_QS_SITE_NAME_PARAM+"="+cm_encode(CM_SITE_NAME)+"&"+cm_make_time_qs()+"&"+cm_make_page_viewid_qs()}
function cm_make_production_mode_global_qs_params(){var a="";if(cm_defined(navigator.plugins))a=a+"&"+CM_QS_NAVIGATOR_PLUGINS+"="+navigator.plugins.length;if(cm_defined(window.history))a=a+"&"+CM_QS_WINDOW_HISTORY_LENGTH+"="+window.history.length;if(cm_defined(window.screen.height))a=a+"&"+CM_QS_WINDOW_SCREEN_HEIGHT+"="+window.screen.height;if(cm_defined(window.screen.width))a=a+"&"+CM_QS_WINDOW_SCREEN_WIDTH+"="+window.screen.width;if(cm_defined(window.screen.pixelDepth))a=a+"&"+CM_QS_WINDOW_PIXEL_DEPTH+
"="+window.screen.pixelDepth;if(cm_is_ssl())a=a+"&"+CM_QS_PAGE+"="+cm_encode(cm_trim(document.location.href));if(cm_defined(document.referrer)&&document.referrer)a=a+"&"+CM_QS_REFERRER+"="+cm_encode(document.referrer);if(CM_INCLUDE_PROFILE_ID){visitorProfileParam=cm_build_profile_id_query_string();a=a+"&"+visitorProfileParam}return a}
function cm_build_matching_area_identifiers_qs(a){for(var c=cm_common_qs_params(),b=0;b<a.length;b++)c=c+"&=[&"+CM_QS_MATCHING_AREA_ID_PARAM+"="+cm_encode(a[b])+"&=]";c=c+"&"+CM_QS_MATCHING_AREA_URL+"="+cm_encode(document.URL);c+=cm_make_production_mode_global_qs_params();return c}
function cm_new_xmlhttprequest(){var a=false;if(window.XMLHttpRequest&&!window.ActiveXObject)try{a=new XMLHttpRequest}catch(c){a=false}else if(window.ActiveXObject)try{a=new ActiveXObject("Msxml2.XMLHTTP")}catch(b){try{a=new ActiveXObject("Microsoft.XMLHTTP")}catch(d){a=false}}return a}function cm_ajax_log_request(a){if(req=cm_new_xmlhttprequest()){req.open("GET",a,false);req.send("");return true}else return false}
function cm_ie_friendly_delay(){for(var a=0,c=(new Date).getTime()+150;(new Date).getTime()<c;)if(document.getElementById((new Date).getTime())!==null)a+=1;return a>0}function cm_image_log_click(a){cm_log("cm_image_log_click url="+a);(new Image).src=a;cm_ie_friendly_delay();return true}function cm_is_safari(){var a=navigator.userAgent.toLowerCase();return a.indexOf("safari/")!==-1&&a.indexOf("chrome")===-1?true:false}
function cm_is_chrome(){return navigator.userAgent.toLowerCase().indexOf("chrome")!==-1}function cm_log_click(a,c,b){var d=cm_common_qs_params();d=d+"&"+CM_QS_CAPSULE_NAME_PARAM+"="+cm_encode(a);d=d+"&"+CM_QS_IMPRESSION_ID+"="+cm_encode(c);a=CM_URI_CLICK_LOG_PATH;if(b){d=d+"&erw="+b;a=CM_URI_REWARD_EVENT_V1_PATH}b=document.location.protocol+"//"+CM_SERVER+a+"?"+d;cm_is_safari()||cm_is_chrome()?cm_ajax_log_request(b):cm_image_log_click(b);return true}
function cm_log_click_check(){var a=cm_read_cookie(CM_COOKIE_NAME_CAPSULE_CLICK);if(a!==null){(new Image).src=document.location.protocol+"//"+CM_SERVER+CM_URI_CLICK_LOG_PATH+"?"+a;cm_delete_cookie(CM_COOKIE_NAME_CAPSULE_CLICK)}}function cm_get_ie_ver(){var a=-1;if(navigator.appName==="Microsoft Internet Explorer")if(/MSIE ([0-9]{1,}[.0-9]{0,})/.exec(navigator.userAgent)!==null)a=parseFloat(RegExp.$1);return a}function MatchingEngine(){this.areas=[]}
MatchingEngine.prototype.setRequestPath=function(){if(arguments.length!==0){var a=arguments[0];if(a.charAt(a.length-1)==="/")a=a.substring(0,a.length-1);CM_REQUEST_HTML_PATH=a+CM_REQUEST_HTML_PATH}};
MatchingEngine.prototype.setup=function(a,c){if(cm_get_ie_ver()!==5){var b=0;this.areaNames=a.split(",");this.serverTimeout=false;for(this.timestamp=(new Date).getTime();b<this.areaNames.length;b++){var d={},e=this.areaNames[b];d.name=e;d.content=null;d.displayed=false;this.areas[e]=d}b=cm_build_matching_area_identifiers_qs(this.areaNames);b=document.location.protocol+"//"+cm_get_window_location()+CM_REQUEST_HTML_PATH+"?"+b;if(cm_defined(c))b=b+"&"+CM_QS_EXTERNAL_PARAMETERS+"="+cm_encode(c);document.write('<iframe id="cm_server_request" style="display:none" src="'+
b+'"></iframe>');setTimeout("MatchingEngine._responseChecker()",CM_RESPONSE_CHECK_PERIOD_MS)}};MatchingEngine.prototype.logEvent=function(a){var c=1;if(arguments.length!==0){for(var b=a=arguments[0];c<arguments.length;c++)b=b+CM_EVENT_DELIM+arguments[c];c="event="+cm_encode(b)+"&"+cm_common_qs_params()+cm_make_production_mode_global_qs_params();(new Image).src=document.location.protocol+"//"+CM_SERVER+CM_URI_VISITOR_EVENT_V1_PATH+"?"+c}};
MatchingEngine.prototype.logPageView=function(){var a=cm_common_qs_params()+cm_make_production_mode_global_qs_params();(new Image).src=document.location.protocol+"//"+CM_SERVER+CM_URI_PAGE_VIEW_PATH+"?"+a};MatchingEngine.prototype.logConversion=function(){var a=cm_read_cookie("cm_impid"),c=cm_read_cookie("cm_capsulename"),b=1;cm_log_click(c,a);if(arguments.length!==0){for(a=arguments[0];b<arguments.length;b++)a=a+CM_EVENT_DELIM+arguments[b]+"="+document.getElementById(arguments[b]).innerHTML;MatchingEngine.logEvent(a)}};
MatchingEngine.prototype.logReward=function(){var a=cm_read_cookie("cm_impid"),c=cm_read_cookie("cm_capsulename"),b=cm_format_reward(document.getElementById("reward").innerHTML);cm_log_click(c,a,b);return true};MatchingEngine.prototype.display=function(a){this._displayInternal(a,true)};
MatchingEngine.prototype._displayInternal=function(a,c){if(cm_get_ie_ver()===5)this._displayAreaDefault(a);else{var b=this.areas[a];if(cm_defined(b)){c&&document.write('<span id="'+a+'">');if(b.content!==null){cm_log("display:responseReceived = true "+a);if(c){document.write(b.content);b.displayed=true}else{var d=document.getElementById(a);if(d!==null){MatchingEngine._replaceWithScripts(d,b.content);b.displayed=true}}}c&&document.write("</span>")}}};
MatchingEngine.prototype._displayAll=function(){for(var a=true,c=0;c<this.areaNames.length;c++){var b=this.areas[this.areaNames[c]];cm_log("area "+b);if(b.displayed)cm_log("capsule.displayed capsule="+b.name);else if(b.content!==null){var d=document.getElementById(b.name);cm_log(b.name+" = "+d);if(d!==null){MatchingEngine._replaceWithScripts(d,b.content);b.displayed=true}else a=b.displayed=false}else a=b.displayed=false}return a};
MatchingEngine.prototype._replaceWithScripts=function(a,c){a.innerHTML=c.toLowerCase().indexOf("<script")>=0?"&nbsp;"+c:c;var b=a.getElementsByTagName("script");if(b&&b.length>0){for(var d=b.length,e=d-1,f=[];e>=0;e--){var g=b[e];f[e]=b[e].text;g.parentNode.removeChild(g)}for(e=0;e<d;e++){b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("language","JavaScript");b.text=f[e];a.appendChild(b)}}};
MatchingEngine.prototype._responseChecker=function(){if(!this.serverTimeout){var a=this._displayAll(),c=(new Date).getTime()-this.timestamp,b=c>=CM_RESPONSE_TIMEOUT_MS;cm_log("elapsed "+c);if(!a&&!b)setTimeout("MatchingEngine._responseChecker()",CM_RESPONSE_CHECK_PERIOD_MS);else if(!a&&b){cm_log("timeout allDisplayed="+a);this._handleTimeout()}}};
MatchingEngine.prototype._handleTimeout=function(){var a=0;cm_log("timeout");for(this.serverTimeout=true;a<this.areaNames.length;a++){var c=this.areaNames[a];this.areas[c].displayed=this._displayAreaDefault(c)}};MatchingEngine.prototype._displayAreaDefault=function(a){var c=document.getElementById(a+"_default");if(c!==null){a=document.getElementById(a);if(a!==null){a.innerHTML=c.innerHTML;return true}}return false};
MatchingEngine.prototype._callback=function(a,c){cm_log("_callback areaName="+a);var b=this.areas[a];if(cm_defined(b)){b.content=c;this._displayInternal(a,false)}};MatchingEngine.prototype._callback_display_default=function(a){var c=0;cm_log("_callback_display_default areaName="+a);if(a!==CM_AREA_NAME_WILDCARD)this._displayAreaDefault(a);else for(;c<this.areaNames.length;c++)this._displayAreaDefault(this.areaNames[c])};MatchingEngine=new MatchingEngine;
