function _unrulyEC(a){
	var b="";
	for(i=a.length-1;i>=0;i--) {
		b+=a.charCodeAt(i)+1+"x";
	}
	return b;
};


/**
 * This file should have no external JS dependencies (not even unrulyInfo)
 */

var unrulyUtil = unrulyUtil || {};

(function() {
	unrulyUtil.getFixedUrl = function(url, protocol) {
		if (url.indexOf('http') != 0) {
			url = protocol + url;
		}
		
		var encProtocol = encodeURIComponent(protocol);
		var doubleEncProtocol = encodeURIComponent(encProtocol);
		
		if (protocol === 'https:') {
			url = url.replace(/cloud\.video\.unrulymedia\.com/g, 'video.unrulymedia.com');
		}
		
		return (url.replace(/https?:/g,     protocol)
				   .replace(/https?%3A/g,   encProtocol)
				   .replace(/https?%253A/g, doubleEncProtocol));
	};
		
})();

var UnrulyTrackingUtils;

////
if (typeof UnrulyTrackingUtils == 'undefined') {
	UnrulyTrackingUtils =  {
			
		parsePublisherMacros : function(url) {
			var localPublisherMacros = [];
			
			var publisherMacrosString = url.substring(url.indexOf('#'));
	
			if ( typeof publisherMacrosString == 'undefined' ||
				 publisherMacrosString.length < 2 ||
				 publisherMacrosString.indexOf('#') !== 0 || 
				 publisherMacrosString.indexOf('=') === -1) {
				return localPublisherMacros;
			}
			
			var macros = publisherMacrosString.substring(1).split('&');
			
			for (var i=0; i < macros.length; i++) {
				var macro_pair = macros[i].split('=');
				var macro_name = macro_pair[0];
				var macro_value = macro_pair[1];
				localPublisherMacros[macro_name] = decodeURIComponent(macro_value);
			}
			
			return localPublisherMacros;
		},
	
		encodePublisherMacros : function(publisherMacros) {
			var encodedMacroString = "";
			
			if (typeof publisherMacros == 'undefined') {
				return encodedMacroString;
			}
			
			for (var macroName in publisherMacros ) {
				
				if ( !publisherMacros.hasOwnProperty(macroName) ||
					  typeof publisherMacros[macroName] == 'undefined' || 
					  publisherMacros[macroName].indexOf("%%") == 0 || 
					  macroName==='uuid') {
					continue;
				}
				
				if (encodedMacroString.length > 0) {
					encodedMacroString +="&";
				}
				
				encodedMacroString += macroName + '=' + encodeURIComponent(publisherMacros[macroName]); 
			}
			
			return encodedMacroString;
		}
	
	};
}var unrulyHttpsUtil = unrulyHttpsUtil || {};

(function() {
	unrulyHttpsUtil.getFixedUrl = function(url) {
		var allowHttps = true;
					allowHttps = true;
				var protocol = (allowHttps ? document.location.protocol : 'http:');
		return unrulyUtil.getFixedUrl(url, protocol);
	};
})();


function _unrulyCollapseAd() {
	return false;
}

        	function _unrulyGetContentType(supportedFormats) {
            var listHasItem = function (formats, format) {
                for(i=0;i<formats.length;i++) {
                    if(formats[i] == format) {
                        return true;
                    }
                }
                return false;
            };
            
			if(listHasItem(supportedFormats, "html5")) {
                return ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("Mac") == -1 &&   navigator.appVersion.indexOf("3.1") == -1) 
                     || (navigator.plugins && navigator.plugins["Shockwave Flash"]) || navigator.plugins["Shockwave Flash 2.0"])
                    ? "flash" : "html5";
            }
            else {
                return "flash";
            }
    	}
	function _unrulyRefUrl() {
	if (top === self || typeof(document.referrer) == "undefined" || document.referrer.length < 1) { 
		return document.location.href;
	} else { 
		return document.referrer;
	}
}


function _unrulyTrackCollapsedAd() {
	var trackingCode = [
		'(function() {',
        '	var unrulyTracking = new UnrulyTracking(', 
        '		unrulyHttpsUtil.getFixedUrl("http://stats.unrulymedia.com/"), ', 
        '		unrulyHttpsUtil.getFixedUrl("http://stats2.unrulymedia.com/"), ', 
        '		unrulyHttpsUtil.getFixedUrl("http://stats3.unrulymedia.com/"), ', 
        '       5969,', 
        '       63061,', 
        '       15836,', 
        '       149,', 
        '       470,', 
        '       2837,', 
        '       "425x375",', 
        '       false,', 
        '       true,', 
        '       4778', 
        '	);',
		'	unrulyTracking.track("collapsed");',
		'}());'
	];

	document.write('<scr'+'ipt src="' + unrulyHttpsUtil.getFixedUrl("http://video.unrulymedia.com/") + 'tracking_v41.js" type="text/javascr'+'ipt"></scr'+'ipt>');
	document.write('<scr'+'ipt type="text/javascr'+'ipt">');
	document.write(trackingCode.join('\n'));
	document.write('</scr'+'ipt>');
}
	
			function UnrulyAP(sk, date, dom, apid, type, supportedFormats) {
	this.sk = sk;
	this.date = date;
	this.dom = dom;
	this.apid = "" + apid;
	this.type = type;
	this.supportedFormats = supportedFormats;
}
UnrulyAP.prototype.isViral = function() {
	return this.type == 'VIRAL';
};
UnrulyAP.prototype.url = function() {
	var url = this.dom;
	var ap = this.apid.length > 0 ? '_' + this.apid : '';
	if (this.isViral()){
		var cType = _unrulyGetContentType(this.supportedFormats);
		
		var macros = '';
		if (typeof unrulyMacros != 'undefined') {
			macros = UnrulyTrackingUtils.encodePublisherMacros(unrulyMacros);
			if (macros.length > 0) {
			   macros += '&';
			}
		}
		var fragment = '#' + macros + 'uuid=' + encodeURIComponent(_unrulyEC(_unrulyRefUrl()));
		
		if ("image" !== cType) {
			url += "iframe_" + this.sk +  "%s_" + cType + ".html?d=" + this.date +
				(typeof wildfireAddParams != "undefined" ? wildfireAddParams : '') + fragment;
		} else {
			url += "default_viral_fallback.html";
		}
	} else {
		url += 'wildfire_' + this.sk + '%s_eclipse.js';
	}
	return url.replace("%s", ap);
};		function UnrulyGeo(fback,noGeo,onHold,sk,time,dom) {
	this.fback = fback;
	this.noGeo = noGeo;
	this.onHold = onHold;
	this.map = {};
	this.sk = sk;
	this.time = time;
	this.dom = dom;
}
UnrulyGeo.prototype.add = function(countryCode, apid, unitType, supportedFormats) {
	this.map[countryCode.toLowerCase()] = new UnrulyAP(this.sk,this.time,this.dom,apid,unitType, supportedFormats);
};
UnrulyGeo.prototype.chooseAp = function(code) {
	if (this.onHold || typeof code == "undefined") {
		return this.noGeo;
	}
	if (code.toLowerCase()=="xx") {
		return this.noGeo;
	} else { 
		var ap = this.map[code.toLowerCase()];
		if (typeof ap == "undefined") {
			return this.fback;
		} else {
			return ap;
		}
	}
};		var writeViral = function(ap) {
			var i = document.getElementById("unruly1330001528163");
			i.style.display="block";
			i.src = ap.url();
		};
		unrulyGeoUrl = function() {
			function getQsParams(){
				var qsParams = {length:0, keys:new Array()};
				var usestr = _unrulyRefUrl();
				if(usestr.split("?").length == 2) {
					var paramInline = usestr.split("?")[1].split("&");
					for(var i = 0; i < paramInline.length; i++){
						qsParams.keys.push(paramInline[i].split("=")[0]);
						qsParams[paramInline[i].split("=")[0]] = paramInline[i].split("=")[1];
						qsParams.length++;
					}
				} 
				return qsParams;
			}
			var href = _unrulyRefUrl();
			if (href.indexOf("unrulyCountry") != -1) {
				var params = getQsParams();
				if (params["unrulyCountry"] != null) {
					unrulyGeoCountryCode = params["unrulyCountry"].toLowerCase();
				}
			}
			var fallback = new UnrulyAP('1836361','1330001528163', unrulyHttpsUtil.getFixedUrl("http://video.unrulymedia.com/"),'default','VIRAL', ["flash","html5"]);
			var geo = new UnrulyGeo(fallback, fallback, false,'1836361', '1330001528163', unrulyHttpsUtil.getFixedUrl("http://video.unrulymedia.com/"));
			var numTerritories = 2;
						geo.add('gb', '15836','VIRAL',["flash","html5"]);
						geo.add('au', '15795','VIRAL',["flash","html5"]);
				
			var ap = geo.chooseAp(unrulyGeoCountryCode);
			
			if (typeof UNRULY_DEFAULT == 'undefined') {
				UNRULY_DEFAULT = {};
			}
			UNRULY_DEFAULT['' + ap.sk] = ( ap.apid == 'default' );
			if ((numTerritories == 0 || ap.apid == "default") && 1 == 2) {
				
			} else if (ap.isViral()) {
				writeViral(ap);
			} else {
				document.write('<scr'+'ipt src="' + ap.url() + '" type="text/javascr'+'ipt"></scr'+'ipt>');
			}
		}

		if (_unrulyCollapseAd()) {
			_unrulyTrackCollapsedAd();
		} else {
    		document.write('<iframe id="unruly1330001528163" style="display:none;" frameborder="0" scrolling="no" width="425" height="375" src="javascript:\'\'"></iframe>');
    		document.write('<scr'+'ipt src="' + unrulyHttpsUtil.getFixedUrl('http://video.unrulymedia.com/') + 'country_code.js?version=5" type="text/javascr'+'ipt"></scr'+'ipt>');
		}
	
