//----------------------------------------------------------------
// Written by Ray Wilson
// Copyright Music From Outer Space LLC 2010.
// http://www.musicfromouterspace.com/mfosweb/home.action
//----------------------------------------------------------------
var GLOBALSHAREWINDOWOPEN = false;
var GLOBALEMAILESCAPEDBOOKMARKURI = null;
var GLOBALSHAREBOOKMARKURI = null;
var GLOBALBOOKMARKURI = null;
var GLOBALTITLE = null;

function showShareWindow(obj)
{
	closeSearchWindow();
	if (GLOBALSHAREWINDOWOPEN)
	{
		return;
	}
	
	var html = generateShareHTML();
	var pt = getUpperLeftPoint(obj);
	
	var sw = document.getElementById("shareWindowDiv");
	sw.style.left = (pt.x - 120) + "px";
	sw.style.top = (pt.y + 55) + "px";
	sw.style.padding = "0px";
	sw.style.zIndex = "500";
	sw.style.width = "250";
	sw.style.height = "120";
	sw.style.display = "block";
	sw.innerHTML = html;
	
	addEvent(document.getElementById("emailaddress"),"keypress", emailAddressKeyPress)
	document.getElementById("emailaddress").focus();
	
	GLOBALSHAREWINDOWOPEN = true;
	setTimeout("delayedReaction()", 100);
}

function delayedReaction()
{
	addEvent(document.body, "click", closeAllDialogs);
	if(window.frames['maincontentiframe'] && window.frames['maincontentiframe'].document)
	{
		addEvent(window.frames['maincontentiframe'].document.body, "click", closeAllDialogs);
	}
}


function closeShareWindow(b)
{
	var sw = document.getElementById("shareWindowDiv");
	sw.style.display = "none";
	sw.style.left = "-1000px";
	sw.style.top = "-1000px";
	sw.style.zIndex = "-500";
	GLOBALSHAREWINDOWOPEN = b;
	removeEvent(document.body, "click", closeAllDialogs, false);
}

function mailmenow()
{
	var text = document.getElementById("emailaddress").value;
	if(text.length > 5)
	{
		document.location.href = "mailto:" + text + "?subject=Cool MFOS Link&body=Check out this cool MFOS link:%0A%0A" + GLOBALEMAILESCAPEDBOOKMARKURI;
		closeShareWindow(false);
	}
}

function emailAddressKeyPress(e)
{
	e = getEvent(e);
    var target = getEventSource(e);
    if(target.id == "emailaddress")
    {
    	if(e.keyCode == 13)
    	{
    		mailmenow();
    	}
    }
}

function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function emailEscapeSpaces(s)
{
	if(s.indexOf(' ') > -1)
	{
		s = s.replace(/\s/g,'%20');
	}
	if(s.indexOf("%20") > -1)
	{
		s = s.replace(/%20/g,'%3720');
	}
	return s;
}

function urlSpaceEscape(s)
{
	if(s.indexOf(' '))
	{
		s = s.replace(/\s/g,"%20");
	}
	return s;
}

function generateCurrentURI()
{
	GLOBALTITLE = "MFOS Website";
	
	var tab = "HOME";
	var prefix    = top.GLOBALURLPREFIX;
	var maintab   = top.GLOBALCURRENTTAB;
	var projarg   = top.GLOBALPROJECTURI;
	var catpartno = top.GLOBALPARTNO;
	
	var sb = new StringBuffer();
	sb.append(prefix);
	sb.append("index.php?MAINTAB=");
	sb.append(maintab);
	sb.append("&PROJARG=");
	sb.append(projarg);
	sb.append("&CATPARTNO=");
	sb.append(catpartno);
	
	var url = sb.toString().trim();
	GLOBALSHAREBOOKMARKURI = escape(url);
	GLOBALEMAILESCAPEDBOOKMARKURI = emailEscapeSpaces(GLOBALSHAREBOOKMARKURI);
	GLOBALBOOKMARKURI = urlSpaceEscape(url); 
}

function generateShareHTML()
{
	generateCurrentURI();
	
	var sb = new StringBuffer();
	sb.append("<table cellpadding='0' style='background-color:#DDDDDD;' cellspacing='0' border='0' width='100%'>");
	sb.append("<tr class='sharehead'>");
	sb.append("<td class='sharehead'>Link Sharing</td>");
	sb.append("<td class='shareheadcloser'><img onclick='closeShareWindow(false);' style='cursor:pointer;' src='images/closewindow.gif'></td></tr>");
	sb.append("</tr>");
	sb.append("<tr><td colspan='2' style='padding:2px;'>");
	sb.append("<table cellpadding='2' cellspacing='0' border='0' style='margin-top:5px;'>");
	sb.append("<tr><td colspan=2 class='bmtext'>Send link to an email address.</td></tr>");
	sb.append("<tr><td valign=top><input maxlength=200 class='sharetextinput' style='width:165px;' type='text' id='emailaddress' name='emailaddress'></td>");
	sb.append("<td><input title='Send email' class='sharetextinput' type=button value='send' onclick='mailmenow();'></td>");
	sb.append("</tr>");
	sb.append("<tr><td colspan=2 class='bmtext'>Cut &amp; paste link into your own email.</td></tr>");
	sb.append("<tr><td colspan=2><input type=text class='sharetextinput' style='width:220px;' onclick='SelectAll(this.id);' id='linktext' value=\"");
	sb.append(GLOBALBOOKMARKURI);
	sb.append("\" /></td></tr></table>");
	sb.append("</td></tr>");
	sb.append("</table>");
	
	return sb.toString();
}

