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;
				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/"), ', 
        '       2793,', 
        '       432011,', 
        '       3555,', 
        '       957,', 
        '       1686,', 
        '       1623,', 
        '       "300x250",', 
        '       false,', 
        '       false,', 
        '       2438', 
        '	);',
		'	unrulyTracking.track("collapsed");',
		'}());'
	];

	document.write('<scr'+'ipt src="' + unrulyHttpsUtil.getFixedUrl("http://video.unrulymedia.com/") + 'tracking_v37.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(){
			if (_unrulyCollapseAd()) {
				_unrulyTrackCollapsedAd();
				return;
			}
		
			    		var contentType = _unrulyGetContentType(["flash","html5"]);
			
			var macros = '';
			if (typeof unrulyMacros != 'undefined') {
				macros = UnrulyTrackingUtils.encodePublisherMacros(unrulyMacros);
    			if (macros.length > 0) {
    			   macros += '&';
    			}
			}
			var fragment = '#' + macros + 'uuid=' + encodeURIComponent(_unrulyEC(_unrulyRefUrl()));
			
			if ("image" !== contentType) {
				var url = unrulyHttpsUtil.getFixedUrl("http://video.unrulymedia.com/") + 'iframe_12535911_' + contentType + '.html?d=1326402558475' + 
					(typeof wildfireAddParams != "undefined" ? wildfireAddParams : '') + fragment;
					
			} else {
				var url = unrulyHttpsUtil.getFixedUrl("http://video.unrulymedia.com/") + "default_viral_fallback.html";
			}
			
			document.write(
			'<iframe ' + 
			'src="' + url + '" ' +
			
			' width="' + 300 + '" ' + 
			' height="' + 250 +'" ' + 
			' frameborder="0" ' + 
			' scrolling="no"></iframe>');
			
					}());
	
