var emba_randomnumber=Math.floor(Math.random()*10001);
var emba_host="http://www.embedarticle.com";
var emba_js_host="http://js.embedarticle.com";
var emba_widget_host = "http://widget.embedarticle.com";

if(typeof(embaURL) != 'undefined') {
	window[emba_randomnumber+"_url"] = embaURL;
}
if(typeof(pub) != 'undefined') {
	window[emba_randomnumber+"_pub"] = pub;
}
if(typeof(embaPub) != 'undefined') {
	window[emba_randomnumber+"_pub"] = embaPub;
}

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};



function embafindPos(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curtop];
	}
}

function embaGetURL(link_random) {
	if (typeof(window[link_random+"_url"]) == 'undefined') {
		return window.location.href;
	} else {
		return window[link_random+"_url"];
	}
}

function embaGetPub(link_random){
	if(typeof(window[link_random+"_pub"]) == 'undefined') {
		return "";
	} else {
		return "pub="+window[link_random+"_pub"]+"&";
	}
}

function embaGetStyle() {
	if (typeof(embaStyle) == 'undefined') {
		return "<a href=\"#\" name=\"embaLinks\" onclick=\"embaShowDialog(this);return false;\" rel=\""+emba_randomnumber+"\"><img src=\""+emba_widget_host+"/images/embed_article_button.gif\" border=\"0\" id=\"embaImg\" /></a>";
	} else if(embaStyle=='text') {
		return "<a href=\"#\" name=\"embaLinks\" onclick=\"embaShowDialog(this);return false;\" rel=\""+emba_randomnumber+"\"><img src=\""+emba_widget_host+"/images/embed_article_button_sm.gif\" border=\"0\" id=\"embaImg\" />&nbsp;Embed Article</a>";
	} else if(embaStyle=='counter') {
		return "<a href=\"#\" name=\"embaLinks\" onclick=\"embaShowDialog(this);return false;\" rel=\""+emba_randomnumber+"\"><span style=\"display:inline-block; width:50px;height:61px;background-image:url("+emba_widget_host+"/images/emba_counterbox.gif);\"><iframe src=\""+emba_js_host+'/article/site_count?'+ embaGetPub(emba_randomnumber) +'id=' + encodeURIComponent(embaGetURL(emba_randomnumber))+"\" style=\"margin:0px; width: 50px; height:20px; *height:18px;position:relative; top:10px;\" frameborder=\"0\" scrolling=\"no\"></iframe></span></a>";
	} else if(embaStyle=='custom' && embaWidget!='undefined'){
		return "<a href=\"#\" name=\"embaLinks\" onclick=\"embaShowDialog(this);return false;\" rel=\""+emba_randomnumber+"\"><img src=\""+embaWidget+"\" border=\"0\" id=\"embaImg\" /></a>";
	} else {
		return "<a href=\"#\" name=\"embaLinks\" onclick=\"embaShowDialog(this);return false;\" rel=\""+emba_randomnumber+"\"><img src=\""+emba_widget_host+"/images/embed_article_button.gif\" border=\"0\" id=\"embaImg\" /></a>";
	}
}

function embaShowDialog(link) {
	var link_random = link.rel //.gsub('link_','');
	if (document.getElementById('emba_light_'+link_random).style.display == 'none') {
		//hide all elements
		var embaElements = document.getElementsByName('emba_white_content');
		var i = 0;
        while (i < embaElements.length) {
        	embaElements[i].style.display = 'none';
        	i++;
        }
		embaElements = document.getElementsByName('emba_bg');
		var i = 0;
        while (i < embaElements.length) {
        	embaElements[i].style.display = 'none';
        	i++;
        }
		document.getElementById('emba_loading_'+link_random).style.display = 'block';
		document.getElementById('emba_article_content_'+link_random).src = emba_js_host+'/article/content?'+ embaGetPub(link_random) +'id=' + encodeURIComponent(embaGetURL(link_random))+"&title="+encodeURIComponent(document.title);
		document.getElementById('emba_light_'+link_random).style.display = 'block';
		document.getElementById('emba_bg_'+link_random).style.display = 'block';
	} else {
		embaHideDialog(link);
	}
}

function embaHideDialog(link) {
	var embaElements = document.getElementsByName('emba_white_content');
	if(embaElements.length == 0) {
		embaElements = getElementsByName_iefix('div','emba_white_content');
	}
	var i = 0;
    while (i < embaElements.length) {
    	embaElements[i].style.display = 'none';
    	i++;
    }
	embaElements = document.getElementsByName('emba_bg');
	if(embaElements.length == 0) {
		embaElements = getElementsByName_iefix('div','emba_bg');
	}
	var i = 0;
    while (i < embaElements.length) {
    	embaElements[i].style.display = 'none';
    	i++;
    }
	
	document.getElementById('emba_bg_'+link.rel).style.display = 'none';
	document.getElementById('emba_light_'+link.rel).style.display = 'none';
}

function getElementsByName_iefix(tag, name) {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0, iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

document.write("<style type=\"text/css\">");
document.write(".embafacebox { position: absolute; top: 0; left: 0; z-index: 100; text-align: left; font: 12px Arial, Helvetica, sans-serif; } .embafacebox .emba_popup { position: relative; } .embafacebox table { border-collapse: collapse; } .embafacebox td { border-bottom: 0; padding: 0; } .embafacebox .emba_body { background: #E5E5E5; width: 390px; } .embafacebox .loading { text-align: center; } .embafacebox .image { text-align: center; } .embafacebox img { border: 0; margin: 0; } .embafacebox .embafooter { background-color:#E5E5E5; padding: 5px 5px 5px 0; margin-top: -7px; text-align: right; } .embafacebox .embatl, .embafacebox .embatr, .embafacebox .embabl, .embafacebox .embabr { height: 0; min-height:0; width: 0; overflow: hidden; padding: 0; } .facebox_hide { z-index:-100; } .embafacebox_overlayBG {position: fixed; top: 0px; left: 0px; height:100%; width:100%; background-color: #000; z-index: 1000; opacity:0.6; filter:alpha(opacity=60); } * html .embafacebox_overlayBG { /* ie6 hack */ position: absolute; height: expression(document.body.scrollHeight &gt; document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px'); }")

//positioning
document.write(".embafacebox { position: fixed; top:50%; left:50%; margin-left:-190px; margin-top:-120px; z-index:1001; width: 390px; }")
document.write(".embafacebox .embafooter a.emba_close{ color:#FFF; text-decoration: none; margin: 2px; margin-bottom: 5px; padding:2px; border-top:1px solid #8194BD; border-left:1px solid #8194BD; border-bottom:1px solid #0E1F5B; border-right:1px solid #0E1F5B; background-color: #6079AB;}")
document.write(".embafacebox .embafooter a.emba_close:visited{ color:#FFF; text-decoration: none;}")
document.write("* html body {height: 100%;}");
document.write("* html .emba_bg {position:absolute; top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );}");
document.write("* html .emba_white_content { position: absolute; _margin-left:-180px; _margin-top:-155px; _padding: 2px; _background-color: #E5E5E5; top: expression( ( 250 + ( ignoreMe2 = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );}");
document.write("</style>");


document.write(embaGetStyle()+"<div id=\"emba_bg_"+emba_randomnumber+"\" class=\"embafacebox_overlayBG emba_bg\" name=\"emba_bg\" style=\"display:none\"></div>");
document.write("<div class=\"embafacebox emba_white_content\" id=\"emba_light_"+emba_randomnumber+"\" name=\"emba_white_content\" style=\"display:none\">");
document.write("	<div class=\"emba_popup\">");
document.write("		<table>");
document.write("			<tbody>");
document.write("				<tr>");
document.write("					<td class=\"emba_body\">");
document.write("						<div class=\"emba_content\">");
document.write("							<img src=\""+emba_host+"/images/spinner.gif\" id=\"emba_loading_"+emba_randomnumber+"\" style=\"display:none;\">");
document.write("							<iframe id=\"emba_article_content_"+emba_randomnumber+"\" style=\"height:195px; width:100%;*width:100%;margin-bottom:-10px\" frameborder=\"0\" onload=\"document.getElementById('emba_loading_"+emba_randomnumber+"').style.display='none';\"></iframe>");
document.write("						</div>");
document.write("						<div class=\"embafooter\">");
document.write("							<strong>No?</strong> Then click here to <a href=\"#\" class=\"emba_close\" onclick=\"embaHideDialog(this);return false;\" rel=\""+emba_randomnumber+"\">Close</a>");
document.write("						</div>");
document.write("					</td>");
document.write("				</tr>");
document.write("			</tbody>");
document.write("		</table>");
document.write("	</div>");
document.write("</div>");
