var bURL="/images/trans_40.png";
var dragObj=null;
var oX,oY;
function createCoverDiv()
{
	var coverDiv=document.createElement("div");
	coverDiv.id="coverDiv";
	coverDiv.className="coverDiv";
	setWH(coverDiv);
	if(isIE())
	{
		if(verIE()<7.0)
			coverDiv.style.backgroundImage="none";
	}
	document.body.appendChild(coverDiv);
	return coverDiv;
}

function showEventInput(defaultBlog)
{
	//Variables
	var imgUrl="/images/TA.png";
	//Width & Height
	var popupWidth=500;
	var popupHeight=400;
	if(window.innerHeight)
	{
		var wVal=(window.innerWidth/2)-(popupWidth/2);
		var hVal=((window.innerHeight/2)-(popupHeight/2))+document.body.scrollTop;
	}
	else
	{
		var wVal=(document.body.clientWidth/2)-(popupWidth/2);
		var hVal=(document.body.clientHeight/2)-(popupHeight/2)+document.body.scrollTop;
	}
	if(hVal<0)
		hVal=0;
	if(wVal<0)
		wVal=0;
	var coverDiv=createCoverDiv();
	var popupDiv=document.createElement("div");
	popupDiv.id="popupDiv";
	popupDiv.className="popupDiv";
	popupDiv.style.left=wVal + "px";
	popupDiv.style.top=hVal + "px";
	popupDiv.style.width=popupWidth + "px";
	popupDiv.style.height=popupHeight + "px";
	//HTML
	strHTML="<table width='100%' cellspacing='0px'>";
	strHTML=strHTML+"<tr class='titleBar'>";
	strHTML=strHTML+"<td colspan=2 class='noPad' id='titlebar'>";
	strHTML=strHTML+"<table width='100%' cellspacing='0px'><tr><td class='titleText'><font class='titleFont'>Event Submission</font></td>";
	strHTML=strHTML+"<td width='20px'><img src='/images/close_normal.png' id='cTB'></td></tr></table></td></tr>";
	strHTML=strHTML+"<tr><td width='50px' align='center'><img src='" + imgUrl + "' id='img'></td>"
	strHTML=strHTML+"<td class='descTD'>";
	//----------------------
	strHTML=strHTML+"<table width='100%'><tr><td width='125px' align='right' class='descTD'>Event Name / Title:</td><td><input type='text' id='eName' class='textinput' onkeyup='lengthCalc()'></td></tr><tr><td align='right' class='descTD'>Information:</td><td><textarea id='eInfo' class='textinput' onkeyup='lengthCalc()'></textarea></td></tr><tr><td align='right' class='descTD'>Quick Link:</td><td><input type='text' id='eLink' class='textinput' onkeyup='lengthCalc()'></td></tr><tr><td align='right' class='descTD'>Shorten With:</td><td align='center'><input type='button' value='TinyURL' class='btn' onClick='createTinyURL();' id='tinyURL'><input type='button' value='bit.ly' class='btn' onClick='createBitLY();' id='bitLY'></td></tr><tr><td align='right' class='descTD'>Post To:</td><td><select id='eBlog' onchange='blogChange()' onkeyup='blogChange()'><option value='uk'>UK Activism</option><option value='us'>US Activism</option><option value='canada'>Canada Activism</option><option value='london'>London Activism</option><option value='washington'>Washington Activism</option><option value='global'>Global Activism</option></td></tr>";
	strHTML=strHTML+"<tr><td align='right' class='descTD'>US State</td><td><select id='eState' disabled='true' onkeyup='lengthCalc()' onchange='lengthCalc()'><option value='Alabama'>Alabama</option><option value='Alaska'>Alaska</option><option value='Arizona'>Arizona</option><option value='Arkansas'>Arkansas</option><option value='California'>California</option><option value='Colorado'>Colorado</option><option value='Connecticut'>Connecticut</option><option value='Delaware'>Delaware</option><option value='Columbia'>District of Columbia</option><option value='Florida'>Florida</option><option value='Georgia'>Georgia</option><option value='Hawaii'>Hawaii</option><option value='Idaho'>Idaho</option><option value='Illinois'>Illinois</option><option value='Indiana'>Indiana</option><option value='Iowa'>Iowa</option><option value='Kansas'>Kansas</option><option value='Kentucky'>Kentucky</option><option value='Louisiana'>Louisiana</option><option value='Maine'>Maine</option><option value='Maryland'>Maryland</option><option value='Massachusetts'>Massachusetts</option><option value='Michigan'>Michigan</option><option value='Minnesota'>Minnesota</option><option value='Mississippi'>Mississippi</option><option value='Missouri'>Missouri</option><option value='Montana'>Montana</option><option value='Nebraska'>Nebraska</option><option value='Nevada'>Nevada</option><option value='New Hampshire'>New Hampshire</option><option value='New Jersey'>New Jersey</option><option value='New Mexico'>New Mexico</option><option value='New York'>New York</option><option value='North Carolina'>North Carolina</option><option value='North Dakota'>North Dakota</option><option value='Ohio'>Ohio</option><option value='Oklahoma'>Oklahoma</option><option value='Oregon'>Oregon</option><option value='Pennsylvania'>Pennsylvania</option><option value='Rhode Island'>Rhode Island</option><option value='South Carolina'>South Carolina</option><option value='South Dakota'>South Dakota</option><option value='Tennessee'>Tennessee</option><option value='Texas'>Texas</option><option value='Utah'>Utah</option><option value='Vermont'>Vermont</option><option value='Virginia'>Virginia</option><option value='Washington'>Washington</option><option value='West Virginia'>West Virginia</option><option value='Wisconsin'>Wisconsin</option><option value='Wyoming'>Wyoming</option></select></td></tr>";
	strHTML=strHTML+"<tr><td colspan=2 align='center' class='descTD'>Characters remaining: <span id='charsLeft'>134</span></td></tr></table>";
	//----------------------
	strHTML=strHTML+"<br><b>Give details about protests, gatherings, events for charity, workshops, camps, marches, festivals, online initiatives and cyberactivism...<br><br>ANYTHING with it's heart in making a positive difference that you would like to tell the Twitter world about!</b><br><br><small>Please try to keep entries relevant to the blog location you are posting them in! If the location is not covered yet or the cause effects all of us, feel free to use the GlobalActivism blog.</small></td></tr>";
	strHTML=strHTML+"<tr><td colspan=2 align='right'><input type='button' value='Cancel' onClick='closeEventInput(0)'> <input type='button' value='Post Event' onClick='closeEventInput(1)' id='postButton'></td></tr></table>";
	popupDiv.innerHTML=strHTML;
	coverDiv.appendChild(popupDiv);
	document.getElementById('titlebar').onmousedown=startDrag;
	document.getElementById('titlebar').onmouseup=function() { dragObj=null; }
	document.getElementById('coverDiv').onmousemove=doMouseMove;
	document.getElementById('cTB').onmouseover=function() { document.getElementById('cTB').src='/images/close_over.png'; setFilter(document.getElementById('cTB')); }
	document.getElementById('cTB').onmouseout=function() { document.getElementById('cTB').src='/images/close_normal.png'; setFilter(document.getElementById('cTB')); }
	document.getElementById('cTB').onclick=function() { closeEventInput(0); }
	setFilter(document.getElementById('cTB'));
	setFilter(document.getElementById('img'));
	//---
	var bO=document.getElementById("eBlog");
	for(i=0;i<bO.options.length;i++)
	{
		if(defaultBlog==bO.options[i].value)
			bO.options.selectedIndex=i;
	}
	lengthCalc();
	blogChange();
}

function closeEventInput(doPost)
{
	if(doPost==1)
	{
		var newBlog=document.getElementById("eBlog").value;
		if(newBlog=="us")
			iData="[" + document.getElementById("eState").value + "] ";
		else
			iData="";
		var ldata="eName=" + iData + document.getElementById("eName").value + "&eInfo=" + document.getElementById("eInfo").value + "&eLink=" + document.getElementById("eLink").value + "&eBlog=" + newBlog;
		ajaxRequest("/storeevent2.php",ldata,1,postResult);
	}
	var cD=document.getElementById("coverDiv");
	var pD=document.getElementById("popupDiv");
	cD.style.visibility="hidden";
	pD.style.visibility="hidden";
	cD.removeChild(pD);
	document.body.removeChild(cD);
}

function postResult(res)
{
	if(res=="OK")
		popup("Event Submission","Your post was stored successfully.<br>It is now awaiting approval by a moderator, and will be posted soon if it is approved.<br><br>Thanks for submitting to TwitterActivism!");
	else
		popup("Event Submission","Unfortunately, an error occurred while submitting your post.<br>Please try again in a few minutes.<br><br>If you have any unusual characters in your post, try removing those.");
}

//------------------------------------
function popup(pTitle,pMessage)
{
	//Variables
	var imgUrl="/images/information.png";
	//Width & Height
	var popupWidth=400;
	var popupHeight=250;
	var contentHeight=popupHeight-60;
	if(window.innerHeight)
	{
		var wVal=(window.innerWidth/2)-(popupWidth/2);
		var hVal=((window.innerHeight/2)-(popupHeight/2))+document.body.scrollTop;
	}
	else
	{
		var wVal=(document.body.clientWidth/2)-(popupWidth/2);
		var hVal=(document.body.clientHeight/2)-(popupHeight/2)+document.body.scrollTop;
	}
	if(hVal<0)
		hVal=0;
	if(wVal<0)
		wVal=0;
	//Cover
	var coverDiv=createCoverDiv();
	//Popup
	var popupDiv=document.createElement("div");
	popupDiv.id="popupDiv";
	popupDiv.className="popupDiv";
	popupDiv.style.left=wVal + "px";
	popupDiv.style.top=hVal + "px";
	popupDiv.style.width=popupWidth + "px";
	popupDiv.style.height=popupHeight + "px";
	//HTML
	strHTML="<table width='" + popupWidth + "px' cellspacing='0px'>";
	strHTML=strHTML+"<tr class='titleBar'>";
	strHTML=strHTML+"<td colspan=2 class='noPad' id='titlebar'>";
	strHTML=strHTML+"<table width='100%' cellspacing='0px'><tr><td class='titleText'><font class='titlefont'>" + pTitle + "</font></td>";
	strHTML=strHTML+"<td width='20px'><img src='/images/close_normal.png' id='cTB'></td></tr></table></td></tr>";
	strHTML=strHTML+"<tr height='" + contentHeight + "px'><td width='80px' align='center'><img src='" + imgUrl + "' id='img'></td>"
	strHTML=strHTML+"<td><font class='msgFont'>" + pMessage + "</font></td></tr><tr><td colspan=2 align='right'><input type='button' value='Close Window' onClick='closeMessage()'></td></tr></table>";
	popupDiv.innerHTML=strHTML;
	coverDiv.appendChild(popupDiv);
	document.getElementById('titlebar').onmousedown=startDrag;
	document.getElementById('titlebar').onmouseup=function() { dragObj=null; }
	document.getElementById('coverDiv').onmousemove=doMouseMove;
	document.getElementById('cTB').onmouseover=function() { document.getElementById('cTB').src='/images/close_over.png'; setFilter(document.getElementById('cTB')); }
	document.getElementById('cTB').onmouseout=function() { document.getElementById('cTB').src='/images/close_normal.png'; setFilter(document.getElementById('cTB')); }
	document.getElementById('cTB').onclick=function() { closeMessage(); }
	setFilter(document.getElementById('cTB'));
	setFilter(document.getElementById('img'));
}

function closeMessage()
{
	var cD=document.getElementById("coverDiv");
	var pD=document.getElementById("popupDiv");
	cD.style.visibility="hidden";
	pD.style.visibility="hidden";
	cD.removeChild(pD);
	document.body.removeChild(cD);
}
//------------------------------------

function blogChange()
{
	var newBlog=document.getElementById("eBlog").value;
	if(newBlog=="us")
		document.getElementById("eState").disabled=false;
	else
		document.getElementById("eState").disabled=true;
	lengthCalc();
}

function lengthCalc()
{
	var limit;
	var newBlog=document.getElementById("eBlog").value;
	if(newBlog!="us")
		limit=134;
	else
		limit=131-document.getElementById("eState").value.length;
	var cChars=0;
	cChars=cChars+document.getElementById("eName").value.length;
	cChars=cChars+document.getElementById("eInfo").value.length;
	cChars=cChars+document.getElementById("eLink").value.length;
	var cLeft=limit-cChars;
	document.getElementById("charsLeft").innerHTML=cLeft;
	if(cLeft<0)
	{
		document.getElementById("charsLeft").style.color="red";
		document.getElementById("postButton").disabled=true;
	}
	else
	{
		document.getElementById("charsLeft").style.color="#345678";
		document.getElementById("postButton").disabled=false;
	}
}

//Submit stuff
function createTinyURL()
{
	var url=document.getElementById("eLink").value;
	if(url.indexOf("http://")!=0)
	url="http://" + url;
	var ldata="url=" + url;
	ajaxRequest("tinyurl.php",ldata,1,doLink);
	document.getElementById("tinyURL").disabled=true;
	document.getElementById("bitLY").disabled=true;
}

function createBitLY()
{
	var url=document.getElementById("eLink").value;
	if(url.indexOf("http://")!=0)
	url="http://" + url;
	var ldata="url=" + url;
	ajaxRequest("bitly.php",ldata,1,doLink);
	document.getElementById("tinyURL").disabled=true;
	document.getElementById("bitLY").disabled=true;
}

function doLink(val)
{
	document.getElementById("eLink").value=val;
	lengthCalc();
}

//Drag stuff
function doMouseMove(evt)
{
	evt=evt || window.event;
	if(dragObj!=null)
	{
		var mP=findPos(evt);
		dragObj.style.left = mP.x-oX;
		dragObj.style.top = mP.y-oY;
	}
}

function findPos(e)
{
	var left=e.offsetLeft;
	var top=e.offsetTop;
	if(e.pageX)
	{
		left=e.pageX;
		top=e.pageY;
	}
	else
	{
		left=e.clientX + document.body.scrollLeft - document.body.clientLeft;
		top=e.clientY + document.body.scrollTop - document.body.clientTop;
	}
	return { x:left, y:top };
}

function startDrag(e)
{
	e=e || window.event;
	if( (e.button==0 && !isIE()) || (e.button==1 && isIE()) )
	{
		var mP=findPos(e);
		oX=mP.x-parseInt(document.getElementById('popupDiv').style.left);
		oY=mP.y-parseInt(document.getElementById('popupDiv').style.top);
		dragObj=document.getElementById("popupDiv");
	}
}

function isIE()
{
	var ua=navigator.userAgent;
	if(ua.indexOf("MSIE") > 0)
		return true;
	else
		return false;
}

function verIE()
{
	var ua=navigator.userAgent;
	var re  = new RegExp("MSIE ([0-9]+[\.0-9]+)");
	var iv=re.exec(ua);
    return parseFloat(RegExp.$1);
}

function setWH(elem)
{
	if(isIE())
	{
		var mW=(document.body.clientWidth>document.body.scrollWidth) ? document.body.clientWidth : document.body.scrollWidth;
		var mH=(document.body.clientHeight>document.body.scrollHeight) ? document.body.clientHeight : document.body.scrollHeight;
		elem.style.width=mW + "px";
		elem.style.height=mH + "px";
	}
	else
	{
		elem.style.width=(window.innerWidth + window.scrollMaxX) + "px";
		elem.style.height=(window.innerHeight + window.scrollMaxY) + "px";
	}
}

function setFilter(elem,newImage)
{
	if(!isIE())
		return;
	if(verIE()>=7.0)
		return;
	var iSrc=elem.src;
	var iW=elem.offsetWidth;
	var iH=elem.offsetHeight;
	elem.src="/images/transparent.gif";
	elem.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iSrc + "',sizingMethod='scale')";
	elem.style.width=iW + "px";
	elem.style.height=iH + "px";
}