
var _ssDebugging = false;
var _ssDebugCounter = 1;
function ss_campaignDebug(msg){
	if (_ssDebugging){
		try {
			document.getElementById("adData").innerHTML = ('<hr/><h3>Message#' + (_ssDebugCounter++) + ': ' + msg + '</h3>') + document.getElementById("adData").innerHTML;
			return;
		} catch (error){alert('error in ss_campaignDebug: ' + error);}
	}
}
function ss_flashDebugging(msg){
	try {
		document.getElementById("ss_flashDebugging").innerHTML = (document.getElementById("ss_flashDebugging").innerHTML +  '<li><strong>' + msg + '</strong></li>');
		return;
	} catch (error){}
}



/***********************************************************************
 *
 * Copyright (c) 2008 ScanScout, Inc. All Rights Reserved.
 *
 */

// Special code to handle campaigns that require Javascdript (ex: widgets, surveys, etc)
// $Revision: $;
// @author: jburke



if (!self.ScanScout) ScanScout = {};

/******************************************************* START Utility Functions ********************************************/
	function getJsURL(theURL){
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = theURL;

		var head = document.getElementsByTagName('head').item(0);
		window.setTimeout(function() { head.appendChild(script) });
	}
	
	
	function _loadSurveyIFrame(iframeId, iframeUrl) {
		var surveyIframe = document.getElementById(iframeId);
		if (surveyIframe) return;  // Only one is used - the first one.
		surveyIframe = document.createElement('iframe');
		surveyIframe.name = surveyIframe.id = iframeId;
		surveyIframe.style.display = "none";
		surveyIframe.src = iframeUrl;
		document.body.appendChild(surveyIframe);
	}
	/**************************** START Insight Express Utility ****************************/
	function _loadInsightExpressSurveyIFrame(bannerId){
		_loadSurveyIFrame('ss_IXTagFrame', "http://core.insightexpressai.com/adServer/adServerESI.aspx?bannerID=" + bannerId + "&script=true");
	}		
	/**************************** END Insight Express Utility ****************************/
	
		
	/**************************** START QuestionMarket Utility ****************************/
	function _loadQuestionMarketSurveyIFrame(surveyId){
		getJsURL('http://amch.questionmarket.com/adsc/' + surveyId + '/randm.js');
	}		
	/**************************** END QuestionMarket Utility ****************************/	

/******************************************************* END Utility Functions ********************************************/




/******************************************************* START Surveys ********************************************/
	


	var surveyCampaignPubs = "|208|510|511|512|513|515|516|519|533|536|538|539|540|541|543|544|545|546|547|555|565|567|568|603|604|610|611|618|625|628|631|632|634|641|642|643|644|645|646|647|648|649|650|651|654|655|656|657|659|660|661|662|663|664|665|671|672|673|684|686|699|702|703|705|706|707|711|712|713|718|719|720|730|737|738|743|744|745|746|749|750|752|753|754|755|757|763|764|769|794|796|797|799|800|801|802|803|804|805|806|807|808|809|810|811|812|813|814|815|816|817|818|819|820|821|822|823|824|825|826|827|828|829|830|831|833|834|854|855|879|880|884|885|886|888|889|899|902|907|909|910|911|947|956|957|963|971|972|11002|13002|14019|17002|17003|17004|17005|20002|20003|20004|20005|24002|27002|";
	var surveyCampaignIds = ["1056751"]; //slingbox

	ScanScout.runCampaignFunctionality = function(publisherId, campaignId, adPosition){
		if (surveyCampaignPubs.indexOf("|" + publisherId + "|") > -1){
			ss_campaignDebug('surveyCampaignIds: ' + surveyCampaignIds);
			ss_campaignDebug('surveyCampaignIds.length: ' + surveyCampaignIds.length);
			for (var i = 0; i < surveyCampaignIds.length; i++){
			  var _currentCampaignId = surveyCampaignIds[i];
			  ss_campaignDebug('surveyCampaignIds[' + i + ']' + surveyCampaignIds[i]);
			  if (_currentCampaignId == campaignId){
				ScanScout.callTestCampaignSurvey(_currentCampaignId, adPosition);
			  } else {
			 	ss_campaignDebug('this campaign is not ' + _currentCampaignId + ' so call control');
				ScanScout.callControlCampaignSurvey(_currentCampaignId, adPosition);
			  }
			}

		}
	}
	ScanScout.callTestCampaignSurvey = function(campaignId, adPosition){	//Only call test function if the current campaign is a survey campaign
		if (adPosition == 0){  //for now, just run these on the first ad in the schedule
			ss_campaignDebug('call _callSsCampaignTestSurvey_' + campaignId);
			try {
				if(navigator.appName.indexOf("Microsoft") != -1) {
					window['_callSsCampaignTestSurvey_' + campaignId](adPosition);
				} else {
					document['_callSsCampaignTestSurvey_' + campaignId](adPosition);
				}	
			} catch (error){
				ss_campaignDebug('callTestCampaignSurvey: ' + error.message)
			}
		}
	};
	ScanScout.callControlCampaignSurvey = function(campaignId, adPosition){
		if (adPosition == 0){  //for now, just run these on the first ad in the schedule
			ss_campaignDebug('call _callSsCampaignControlSurvey_' + campaignId);
			try {
				if(navigator.appName.indexOf("Microsoft") != -1) {
					window['_callSsCampaignControlSurvey_' + campaignId](adPosition);
				} else {
					document['_callSsCampaignControlSurvey_' + campaignId](adPosition);
				}							
			} catch (error){
				ss_campaignDebug('callControlCampaignSurvey: ' + error.message)
			}
		}
	}

	//***********START SURVEY FUNCTIONS **************/
	


	//START PAMPERS
	var _hasCalledTestSurvey_1025366 = false;
	function _callSsCampaignTestSurvey_1025366(adPosition){
		if (!_hasCalledTestSurvey_1025366){
		  //_loadQuestionMarketSurveyIFrame('d440722/12/448406');
		  _hasCalledTestSurvey_1025366 = true;
		}
	}
	var _hasCalledControlSurvey_1025366 = false;
	function _callSsCampaignControlSurvey_1025366(adPosition){
		if (!_hasCalledControlSurvey_1025366){
		  //_loadQuestionMarketSurveyIFrame('d440722/12/453766');
		  _hasCalledControlSurvey_1025366 = true;
		}
	}
	//END PAMPERS
	
	//START SLINGBOX
	var _hasCalledTestSurvey_1056751 = false;
	function _callSsCampaignTestSurvey_1056751(adPosition){
		if (!_hasCalledTestSurvey_1056751){
		  _loadInsightExpressSurveyIFrame(38197); 
		  _hasCalledTestSurvey_1056751 = true;
		}
	}
	var _hasCalledControlSurvey_1056751 = false;
	function _callSsCampaignControlSurvey_1056751(adPosition){
		if (!_hasCalledControlSurvey_1056751){
		  _loadInsightExpressSurveyIFrame(37832);
		  _hasCalledControlSurvey_1056751 = true;
		}
	}
	//END SLINGBOX		
	
	//***********END SURVEY FUNCTIONS **************/




/******************************************************* END Surveys ********************************************/


/******************************************************* START ScanScoutLogger ********************************************/
if (!self.ScanScoutLogger) ScanScoutLogger = {};

ScanScoutLogger.logMsg = function(logEntry)
// Display log entry in popup.
{
	var now = new Date();
	var theTime = ScanScoutLogger.sprintf("%d:%'02d:%'02d:%'03d", now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());

	logEntry = theTime + ' | ' + logEntry;
	var debugElement = ScanScoutLogger.getElement('ss_debug');
	if (debugElement && debugElement.style.display != 'none')
	{
		debugElement.value = logEntry + '\n' + debugElement.value;
	}
	else if (ScanScoutLogger.debugging)
	{
		// Accumulate it for later, truncated to 5K chars.
		// TODO: append to array instead.
		ScanScoutLogger.initialLog = (ScanScoutLogger.initialLog || '').substring(0, 5000);
		ScanScoutLogger.initialLog = logEntry + '\n' + (ScanScoutLogger.initialLog || '');
	}
};

ScanScoutLogger.getElement = function (idOrName)
// Get an element identified by id or name attribute.
{
	var element = document.getElementById(idOrName);
	if (element) return element;
	var elements = document.getElementsByName(idOrName);
	if (elements && elements.length > 0) return elements[0];
};

ScanScoutLogger.init = function (e)
{
	// Append a new DIV to the body. 
	var debugWrapper = document.createElement('div');
	debugWrapper.id = 'ss_debug_wrapper';
	debugWrapper.style.position = 'absolute';
	debugWrapper.style.top = '100px';
	debugWrapper.style.left = '100px';
	debugWrapper.style.width = '900px';
	debugWrapper.style.border = '2px outset';
	debugWrapper.style.backgroundColor = 'lightblue'; 
	debugWrapper.style.padding = '2px';
	debugWrapper.style.fontSize = 'small';

	var body = document.getElementsByTagName('body').item(0);
	body.appendChild(debugWrapper);

	var debugHTML =
		'<div id="ss_debug_titlebar" style="padding:2px" title="Drag slowly"><nobr>' +
		'ScanScout Debugger' +
		'&nbsp;&nbsp;&nbsp;' +
		'<button onClick="ScanScoutLogger.toggleShowDebug(); return false;" title="Hide or Show the log">Hide/Show</button>' +
		'<button onClick="ScanScoutLogger.clearDebug(); return false;" title="Clear the log">Clear</button>' +
		'&nbsp;&nbsp;&nbsp;' +

		'</nobr></div>' +
		'<textarea id="ss_debug" cols=120 rows=20 style="width:100%">' + 
		(ScanScoutLogger.initialLog || '') + 
		'</textarea>';
	debugWrapper.innerHTML = debugHTML;

	var debugTitlebar = document.getElementById('ss_debug_titlebar');
	debugTitlebar.style.cursor = 'move';
	debugTitlebar.onmousedown = ScanScoutLogger.debugDragStart;
	debugTitlebar.onmousemove=ScanScoutLogger.debugDrag;
	debugTitlebar.onmouseup=ScanScoutLogger.debugDragEnd;

}

ScanScoutLogger.toggleShowDebug = function ()
{
	var log = document.getElementById('ss_debug');
	var display = log.style.display;
	log.style.display = (display == 'none' ? 'block' : 'none');
}


ScanScoutLogger.clearDebug = function ()
{
	document.getElementById('ss_debug').value = '';
}

ScanScoutLogger.debugDragStart = function (e)
{
	ScanScoutLogger.debugDrag.dragging = true;
	if (document.body.attachEvent)
		document.body.attachEvent("move", ScanScoutLogger.debugDrag)
	else
		document.body.addEventListener("move", ScanScoutLogger.debugDrag, false);

	e = e || window.event;	
	ScanScoutLogger.getMouseXY(e);

	var debugWrapper = document.getElementById('ss_debug_wrapper');
	if (!debugWrapper) return;
	
	debugWrapper.style.top;
	ScanScoutLogger.debugDrag.xOffset = ScanScoutLogger.debugDrag.x - debugWrapper.offsetLeft;
	ScanScoutLogger.debugDrag.yOffset = ScanScoutLogger.debugDrag.y - debugWrapper.offsetTop;
}

ScanScoutLogger.getMouseXY = function (e)
{
  	// grab the x-y pos.s if browser is NS
	ScanScoutLogger.debugDrag.x = e.pageX;
	ScanScoutLogger.debugDrag.y = e.pageY;
	if (ScanScoutLogger.getBrowserType() == 0)
 	{ 
		// grab the x-y pos.s if browser is IE
		ScanScoutLogger.debugDrag.x = e.clientX + document.body.scrollLeft;
		ScanScoutLogger.debugDrag.y = e.clientY + document.body.scrollTop;
 	}
}

ScanScoutLogger.debugDrag = function (e)
{
	if (!ScanScoutLogger.debugDrag.dragging) return;
	e = e || window.event;	
	ScanScoutLogger.getMouseXY(e);
	
	var debugWrapper = document.getElementById('ss_debug_wrapper');
	if (!debugWrapper) return;
	
	debugWrapper.style.left = (ScanScoutLogger.debugDrag.x - ScanScoutLogger.debugDrag.xOffset) + 'px';
	debugWrapper.style.top = (ScanScoutLogger.debugDrag.y - ScanScoutLogger.debugDrag.yOffset) + 'px';
};
ScanScoutLogger.debugDrag.dragging = false;

ScanScoutLogger.debugDragEnd = function ()
{
	ScanScoutLogger.debugDrag.dragging = false;
	if (document.body.detachEvent)
		document.body.detachEvent("move", ScanScoutLogger.debugDrag)
	else
		document.body.removeEventListener("move", ScanScoutLogger.debugDrag, true);
};



ScanScoutLogger.getBrowserType = function()
// Return browser type:  MSIE: 0,  Firefox: 1,  Opera: 2,  Others: 3
{
	var ua = window.navigator.userAgent;
	if (ua.indexOf("MSIE") >=0) {
		return 0;
	} else if (ua.indexOf("Firefox/") >=0) {
		return 1;
	} else if (ua.indexOf("Opera/") >=0) {
		return 2;
	} else {
		return 3;	// Other
	}
}


ScanScoutLogger.str_repeat = function (i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); };

ScanScoutLogger.sprintf = function () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? ScanScoutLogger.str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
};

/******************************************************* END ScanScoutLogger ********************************************/








