if ( typeof (amplafi) == "undefined") var amplafi={};
amplafi.isDebug=false;amplafi.mep_lookup_prefix="ampmep_";amplafi.csUrlKey="url";amplafi.dataloadUrl="http://amplafi.net/scripts";amplafi.siteUrl="http://amplafi.net/";amplafi.baseAnchor="";amplafi.yahooMapId="fRhpGYrV34GK7jK4fUydUIo772jtpaCd118z7.Hmrr.0X1LwBluGv9Y4oLbOwST74PQ-";amplafi.pasteFlowLink="http://amplafi.net/flow/Paste";amplafi.publicUri="publicUri";amplafi.scripts=[];amplafi._pasteThis =(function() {

/*
 * TODO: If user clicks Preview, a page is refreshed and our PasteHere? javascript is lost.
 * The problem is if page reloaded / refreshed e.t.c. nothing can emulate user clicking the bookmarklet.
 * Possible solution is to rewrite preview button action - so page will not be reloaded/refreshed.
 */
	
	
function determineSite(){
	if(window.location.hostname.indexOf('dailykos.com')!=-1){
		return 'dailykos';
	}
	if(window.location.hostname.indexOf('blogger.com')!=-1){
		return 'blogspot';
	}
	//wordpress is considered as default
	return 'default';
}

var site = determineSite();

var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

var computeStyle = function(node, property) {
	if (!node.currentStyle) {
		var s = document.defaultView.getComputedStyle(node, null);
		return (s && s[property]) || "";
	} else {
		return node.currentStyle[property];
	}
}

var anyParentDisplayNone = function(node) {
	while (node && node!=document) {
		if (computeStyle(node, 'display')=='none') {
			return true;
		}
		node = node.parentNode;
	}
	return false;
}

var ifrmHeight=500;
var ifrmWidth=750;
var ifrmLeft=Math.round((myWidth-ifrmWidth)/2);
var ifrmTop=Math.round((myHeight-ifrmHeight)/2);

var child=window.open(amplafi.pasteFlowLink+"?"+amplafi.publicUri+"="+encodeURIComponent(window.location.href),'amplafi','height='+ifrmHeight+',width='+ifrmWidth+',scrollbars=1,left='+ifrmLeft+',top='+ifrmTop);
/*var floatingIframe=document.createElement('IFRAME');
floatingIframe.setAttribute('id', 'float');
floatingIframe.setAttribute('src', amplafi.siteUrl+'flow/Paste');
floatingIframe.setAttribute('width', ifrmWidth+'px');
floatingIframe.setAttribute('height', ifrmHeight+'px');
floatingIframe.setAttribute('marginwidth', '0');
floatingIframe.setAttribute('marginheight', '0');
floatingIframe.setAttribute('hspace', '0');
floatingIframe.setAttribute('vspace', '0');
floatingIframe.setAttribute('frameborder', '1');
floatingIframe.style.position='absolute';
floatingIframe.style.left=ifrmLeft+'px';
floatingIframe.style.top=ifrmTop+'px';
document.body.appendChild(floatingIframe);
floatingIframe.focus();*/

var textareas=document.getElementsByTagName('textarea');
for(i=0;i<textareas.length;i++){
	if(!anyParentDisplayNone(textareas[i])){
		txtarea=textareas[i];
		break;
	}
}
//for now this function is unneeded (#1564)
//function insertMessagesToTextarea(messages){
//	for(i=0;i<messages.length;i++){
//		alert(messages[i]);
//		title=messages[i][0];
//		link=messages[i][1];
//		text=messages[i][2];
//		txtarea.value+="<a href='"+link+"'>"+title+"</a> \n";
//		txtarea.value+='&nbsp;&nbsp;&quot;'+text+'&quot; \n\r';	
//	}
//}

ifrm = document.createElement('IFRAME');
ifrm.setAttribute('src', '#');
ifrm.style.width = 1+'px';
ifrm.style.height = 1+'px';
ifrm.style.visibility='hidden';
document.body.appendChild(ifrm);
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

var spans = document.getElementsByTagName("span");
var originalId;
for(i=0;i<spans.length;i++){
 span=spans[i];
 if(span.getAttribute('title')=='ampbe'){
    originalId = span.innerHTML;
    break;
 }

}

//This code may be useful for non-amplafi user case (to create message for some blog)

//messageTitle=document.title;
//
////do not send comment to Amplafi when user clicks Preview (for blogspot).
//doFormSubmit=true;
//if (site == 'blogspot') {
//	addEvent(elementToSubmit.preview, 'click', function() {
//		doFormSubmit = false;
//	}, false);
//}
//
//var eventToHandle = 'submit';
//
//if (site == 'dailykos') {
//	elementToSubmit = document.getElementById('ePostButton');
//	eventToHandle = 'click';
//}
//addEvent(elementToSubmit, eventToHandle, function() {
//	messageText = txtarea.value;
//	var path = amplafi.siteUrl + 'apiv1/SaveExternalPost?messageBody='
//			+ encodeURIComponent(messageText) + '&messageHeadline='
//			+ encodeURIComponent(messageTitle);
//	if (originalId != null) {
//		path = path + '&originalBroadcastEnvelope='
//				+ encodeURIComponent(originalId);
//	}
//	if (site == 'blogspot') {
//		if (doFormSubmit) {
//			ifrm.setAttribute('src', path);
//		}
//	} else {
//		ifrm.setAttribute('src', path);
//	}
//	doFormSubmit = true;
//}, false);

});

// uncomment for automatically triggering paste flow
// amplafi._pasteThis();