var alertDialog = null;
var alertX = -1; // centers dialog in window!
var alertY = -1;

function getCookie(name) {
	name = name + '=';
	var cookies = document.cookie.split(';');
	for ( var i = 0; i < cookies.length; i++ ) {
		var cookie = cookies[i];
		while ( cookie.charAt(0) == ' ' )
			cookie = cookie.substring(1, cookie.length);
		if ( cookie.indexOf(name) == 0 )
			return cookie.substring(name.length, cookie.length);
	}
	return null;
}

function ViralPluginAlertInit(title, icon) {
	if (alertDialog == null) {
		alertDialog = new jt_AppAlert(null);
		alertDialog.setTitle(title);
		}
	else {
		if (title) alertDialog.setTitle(title);
		if (icon) alertDialog.setIcon(icon);
		}
}

function encodeStr(str) {
	if (encodeURIComponent)
		return encodeURIComponent(str);
	else
		return escape(str).replace(/&/, "%26");
}

function getHTTPRequester()
{
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        return new window.ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

function submit_post(id, dialog) {
	var div = document.getElementById("post-submit-"+id);
	if ( !div )
		return;
	var inputs = div.getElementsByTagName("input");
	params = 'id=' + id;
	for ( i in inputs ) {
		if ( !inputs[i] )
			continue;
		if ( !inputs[i].name )
			continue;
		if ( inputs[i].type == 'checkbox' || inputs[i].type == 'radio' )
			if ( !inputs[i].checked )
				continue;
		if ( params.length )
			params += '&';
		params += encodeStr(inputs[i].name) + '=' + encodeStr(inputs[i].value);
	}
	// send with ajax
	sendPost(params, id, dialog);
	// switch to results div
	div.style.display = 'none';
	sbdiv = document.getElementById("post-submitting-"+id);
	sbdiv.innerHTML = 'Sending data...';
	sbdiv.style.display = '';
}

function attachButtons(id, dialog) {
	var div = document.getElementById("post-submit-"+id);
	if ( !div )
		return;	
	
	var inputs = div.getElementsByTagName("input");
	for ( i in inputs ) {
		if ( !inputs[i].name )
			continue;
		if ( inputs[i].type == 'button' ) {
			if ( inputs[i].name == 'ok' )
				inputs[i].onclick = function() {
					submit_post(id, dialog);
				}
			else if ( inputs[i].name == 'cancel' )
				inputs[i].onclick = function() {
					dialog.hide();
				}
				
		} else if ( inputs[i].type == 'text' || inputs[i].type == 'password' ) {
			var stored = getCookie(inputs[i].name.replace('password', 'pass'));
			if ( stored != null && stored.length )
				inputs[i].value = stored;
		}
	}
}

function sendPost(params, id, dialog)
{
	var oncomplete = function(text) {
		sbdiv = document.getElementById("post-submitting-"+id);
		if ( sbdiv )
			sbdiv.style.display = 'none';
		var div = document.getElementById("post-submit-"+id);
		if ( div ) {
			div.innerHTML = text;
			div.style.display = '';
			attachButtons(id, dialog);
		}
	};
	
	var onfail = function() {
		var div = document.getElementById("post-submitting-"+id);
		if ( div )
			div.innerHTML = "Error; bookmarking failed!";
	};
	
    req = getHTTPRequester();
    if (req == null) {
        onfail();
		return false;
    }
    
    var url = blogaddr + '/wp-content/plugins/viralplugin/ajax.php';
    
    try {
        req.open("POST", url, true);
		try{
			req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		} catch(e){
		}
		req.onreadystatechange = function() {
			if (req.readyState == 4 || req.readyState == "complete") {
				if ( req.status == 200 )
					oncomplete(req.responseText);
				else
					onfail();
			}
		};
        req.send(params);
		return true;
    }
    catch (e) {
        delete req.onreadystatechange();
        onfail();
    }
	return false;
}

function set_event_pos(ev, link) {
	var e = jt_fixE(ev);
	alertX = e.clientX;
	///alertY = e.clientY + document.body.scrollTop;
	// use window.pageYOffset in firefox
	//alertX = link.offsetLeft + 50;
	alertY = link.offsetTop;
	return true;
}

function show_dialog(id) {
	msg = get_form_html(id);
	title = "Share Post";

	ViralPluginAlertInit(title, null);
	alertDialog.post_id = id;
	alertDialog.setContent(msg);
	attachButtons(id, alertDialog);
	alertDialog.moveTo(alertX, alertY);
	alertDialog.show();
	if ((alertX == -1) || (alertY == -1)) alertDialog.moveTo(alertX, alertY);
	alertDialog.setWidth(350);
}

/***********************************************
* Show Hint script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
		
var horizontal_offset="9px" //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
	if ((ie||ns6) && document.getElementById("hintbox")){
		dropmenuobj=document.getElementById("hintbox")
		dropmenuobj.innerHTML=menucontents
		dropmenuobj.style.left=dropmenuobj.style.top=-500
		if (tipwidth!=""){
			dropmenuobj.widthobj=dropmenuobj.style
			dropmenuobj.widthobj.width=tipwidth
		}
		dropmenuobj.x=getposOffset(obj, "left")
		dropmenuobj.y=getposOffset(obj, "top")
		dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
		dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
		dropmenuobj.style.visibility="visible"
		dropmenuobj.style.zIndex = 1000;
		obj.onmouseout=hidetip
	}
}

function hidetip(e){
dropmenuobj.style.visibility="hidden"
dropmenuobj.style.left="-500px"
}

function createhintbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "hintbox")
document.body.appendChild(divblock)
}

if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)
window.onload=createhintbox
