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

window["emba_image_random"] = emba_randomnumber;

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

function embafindImages() {
	var emba_doc_images = document.getElementsByTagName("img");
	for(var x=0;x<emba_doc_images.length;x++){
		embaImg = emba_doc_images[x];
		var regex  =  new RegExp("emba_no_img");
		var embaClass = embaImg.className;;
		if(embaClass == null) {
			embaClass="";
		}	
		if(embaImg.width >= 250 && embaImg.width < 801 && embaClass.search(regex)==-1) {
			embaAddButton(embaImg,x);
		}
	}
}

var addLoadEvent;
if(addLoadEvent!=='function'){
  addLoadEvent = function (func) {
    var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
	    window.onload = func;
	  } else {
	    window.onload = function() {
	      if (oldonload) {
	        oldonload();
	      }
	      func();
	    }
	  }
  };
}
addLoadEvent(embafindImages);

function getStyle(el, cssprop){
 if (el.currentStyle) //IE
  return el.currentStyle[cssprop]
 else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
  return document.defaultView.getComputedStyle(el, "")[cssprop]
 else //try and get inline style
  return el.style[cssprop]
}

function embaAddButton(img_target,x) {
    var div = document.createElement('div');
	div.innerHTML = "";
	window[emba_randomnumber+"_"+x+"_image"] = img_target.src;
	window[emba_randomnumber+"_"+x+"_alt"] = img_target.alt;
	window[emba_randomnumber+"_"+x+"_title"] = img_target.title;
	div.id = emba_randomnumber+"_"+x;
	//div.style.width = img_target.width+"px";
 	//div.style.height = img_target.height+"px";
	div.style.display = "inline-block";
	div.style.cssFloat = getStyle(img_target, 'cssFloat');
 	img_target.name = "done";
	
	div.className = "embaImage";
	
	var parent = img_target.parentNode;
	var child = img_target;
	if(img_target.parentNode.nodeName == "A") {
		parent = img_target.parentNode.parentNode;
		child = img_target.parentNode;
	}
	//div.innerHTML += '<style type="text/css">.emba_image_widget{top:15px;_top:30px;}</style>';
	div.innerHTML = '<div style="position:relative;height:0;left:15px;top:15px;color:#FFF;width:90px;z-index:99998;display:none;" class="emba_image_widget" name="emba_image_widget"><a href="#" onclick="embaShowDialog(this,this.rel); return false;" style="background-image:url(http://widget.embedanything.com/images/embed_image_button.gif);width:90px; height:18px;background-repeat:no-repeat;text-indent:-9999px;display:block;">Embed Image</a></div>';
	div.appendChild(child.cloneNode(true));
	div.innerHTML += '<div style="clear:both"></div>';

	parent.replaceChild(div, child);
	
	if (parent.addEventListener) {
		parent.addEventListener('mouseover',function (e) {
		  	embashowButtons();
		},false);
		parent.addEventListener('mouseout',function (e) {
		  	embashowButtons(true);
		},false);
	} else if (parent.attachEvent) {
		parent.attachEvent('onmouseover',embashowButtons);
		parent.attachEvent('onmouseout',function (e) {
		  	embashowButtons(true);
		});
	} else {
		parent.onmouseover = embashowButtons;
		parent.onmouseout = function (e) {
		  	embashowButtons(true);
		};
	}
}

function embashowButtons(hide) {
	if(hide==true) {
		embaButton = document.getElementsByName('emba_image_widget');
		if(embaButton.length == 0) {
			embaButton = getElementsByName_iefix('div','emba_image_widget');
		}
		var i = 0;
		while (i < embaButton.length) {
	    	embaButton[i].style.display = 'none';
	    	i++;
	    }
	} else {
		embaButton = document.getElementsByName('emba_image_widget');
		if(embaButton.length == 0) {
			embaButton = getElementsByName_iefix('div','emba_image_widget');
		}
		var i = 0;
		while (i < embaButton.length) {
	    	embaButton[i].style.display = 'block';
	    	i++;
	    }
	}
}

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

function embaShowDialog(link, url) {
	var link_random = window["emba_image_random"];
	var emba_image_url = window[link.parentNode.parentNode.id+"_image"];
	var emba_alt = window[link.parentNode.parentNode.id+"_alt"];
	var emba_title = window[link.parentNode.parentNode.id+"_title"];
	
	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+'/image/simplesubmit?'+ embaGetPub(link_random) +'site=' + encodeURIComponent(window.location.href)+"&image_url="+encodeURIComponent(emba_image_url)+"&alt="+encodeURIComponent(emba_alt)+"&title="+encodeURIComponent(emba_title);
		document.getElementById('emba_light_'+link_random).style.display = 'block';
		document.getElementById('emba_bg_'+link_random).style.display = 'block';
		
		//			document.getElementById('emba_article_content_'+link_random).src = emba_js_host+'/image/show_embed_box?'+ embaGetUrlId(link_random)+embaGetCode(link_random);
	} 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("<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>");