var minPostWidth = 100;
var minPostHeight = 50;
var maxPostArea = 90000;

function stripHtml(s)
{
	return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}

function YouTubeParse(str)
{
    valpos = str.indexOf('value');
    if(valpos > -1)
    {
        sstr = str.substr(valpos+7);
        quotpos = sstr.indexOf('>');
        if(quotpos > -1)
        {
            str = sstr.substr(0, quotpos - 2);
        }
    }
    
    return str;
}

function HideContent(d)
{
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}

function ShowContent(d)
{
if(d.length < 1) { return; }
document.getElementById(d).style.display = "block";
}

function ReverseContentDisplay(d)
{
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function ShowRoomAdd(dir)
{
document.getElementById("modal").style.display = "block";
document.getElementById("roomadddir").value = dir;
document.getElementById("roomadd").style.display = "block";
}

function CancelRoomAdd()
{
document.getElementById("roomadd").style.display = "none";
document.getElementById("roomaddform").reset();
document.getElementById("modal").style.display = "none";
}

function ShowRoomLink(dir)
{
document.getElementById("modal").style.display = "block";
document.getElementById("roomlinkdir").value = dir;
document.getElementById("roomlink").style.display = "block";
}

function CancelRoomLink()
{
document.getElementById("roomlink").style.display = "none";
document.getElementById("roomlinkform").reset();
document.getElementById("modal").style.display = "none";
}

function ShowPostAdd()
{
document.getElementById("modal").style.display = "block";
document.getElementById("postadd").style.display = "block";
}

function CancelPostAdd()
{
document.getElementById("postadd").style.display = "none";
document.getElementById("postaddform").reset();
document.getElementById("modal").style.display = "none";
}

function CancelLogin()
{
document.getElementById("login").style.display = "none";
document.getElementById("loginform").reset();
document.getElementById("loginmodal").style.display = "none";
}

function CancelUserReg()
{
document.getElementById("userreg").style.display = "none";
document.getElementById("userregform").reset();
document.getElementById("loginmodal").style.display = "none";
}

function CancelPopup()
{
document.getElementById("popupmodal").style.display = "none";
document.getElementById("popup").style.display = "none";
}

function ShowPost()
{
	var posttype = stripHtml(document.getElementById("postaddtype").value);
	var posttypeval = parseInt(posttype);
	
	var posttext;
	if(posttypeval == 2)
	{
		posttext = stripHtml(YouTubeParse(document.getElementById("postaddtext").value));
	}
	else
	{
		posttext = stripHtml(document.getElementById("postaddtext").value);
	}
	
	document.getElementById("modal").style.display = "none";
	document.getElementById("postadd").style.display = "none";
	document.getElementById("postaddform").reset();
	
	var content = "";
	if(posttypeval == 1)
	{
		content = '<img id="newpostimage" style="border-style:none;width:100%;height:100%" src="' + posttext + '">';
	}
	else if(posttypeval == 2)
	{
		content = '<object width="100%" height="100%"><param name="movie" value="' + posttext + '"></param><param name="wmode" value="transparent"></param><embed src="' + posttext + '" type="application/x-shockwave-flash" wmode="transparent" width="100%" height="100%"></embed></object>';
	}
	else
	{
		content = posttext.replace(/\n/g, "<br/>");
	}
	
	document.getElementById("wallcontent").innerHTML +=
		"<div id=\"newpost\" style=\"font-size:x-small;z-index:1000; display:block; overflow: auto; position:relative; left:0px; top:0px; width:100px; height:100px; border-style: solid; border-width: 1px; background-color: yellow;\">" +
		"<div style=\"height:10px;border-bottom-width:1px;border-bottom-style:solid;\"></div><div id=\"newpostcontent\" style=\"height:89px;\">" +
		content + "</div></div>";
	
	document.getElementById("page").innerHTML +=
	"<div id=\"newpostform\">" +
		"Use the mouse to position your new tag. Shift-click the tag to change the size.<br/><br/>" +
		"<form method=\"POST\" id=\"postsubmitform\" action=\"/\"><div>" +
		"<input type=\"hidden\" value=\"" + posttype + "\" name=\"posttype\" id=\"posttype\"/>" +
		"<input type=\"hidden\" value=\"" + posttext + "\" name=\"posttext\" id=\"posttext\"/>" +
		"<input type=\"hidden\" value=\"0\" name=\"xpos\" id=\"xpos\" />" +
		"<input type=\"hidden\" value=\"0\" name=\"ypos\" id=\"ypos\" />" +
		"<input type=\"hidden\" value=\"100\" name=\"width\" id=\"pwidth\" />" +
		"<input type=\"hidden\" value=\"100\" name=\"height\" id=\"pheight\" />" +
		"<input type=\"submit\" id=\"postsubmit\" class=\"ok\" value=\"Ok\" />" +
		"<input type=\"button\" id=\"postcancel\" class=\"cancel\" value=\"Cancel\" onClick=\"window.location='http://meffiti.com'\" />" +
		"</div></form>" +
	"</div>";
}

var ob;
var wc;
var wcleft
var wctop;
var shiftPressed = 0;
var curX = 0;
var curY = 0;
var browser = Browser();

document.onselectstart = function() {return false;} // ie

function NS()
{
    if(browser == "Firefox" || browser == "Netscape" || browser == "Mozilla")
        return true;
    else
        return false;
}

function IE()
{
    if(browser == "IE")
        return true;
    else
        return false;
       
}

function MD(e)
{    
    if(IE())
    {
        document.selection.empty()
    }  
    
        var srcElement;
        if(NS())
        {
            shiftPressed = e.shiftKey;
            srcElement = e.target;
        }
        else        
        {
            shiftPressed = event.shiftKey;
            srcElement = event.srcElement;
        }
		
		wc = document.getElementById("wallcontent");        
        		
		//is this inside the "wallcontent" div?
		var t = srcElement;        
		var newpost = null;
		var topel = null;
		while(t != null)
		{
			if(t.id == "newpost")
			{
				newpost = t;
			}
			else if(t.id == wc.id)
			{
				break;
			}
			topel = t;
            
            if(NS())
            {
                t = t.parentNode;
            }
            else
            {
                t = t.parentElement;
            }
            
		}
        
		if(t != null)
		{
			if(newpost != null)
			{
				wcleft = wc.offsetLeft;
				var el = wc;
				while((el=el.offsetParent) != null)
				{
					wcleft += el.offsetLeft;
				}
				
				wctop = wc.offsetTop;
				var el = wc;
				while((el=el.offsetParent) != null)
				{
					wctop += el.offsetTop;
				}
				
				ob = newpost;
                if(NS())
                {
                    curX = e.clientX;
                    curY = e.clientY;                    
                }
                else
                {
                    curX = event.clientX;
                    curY = event.clientY;
                }
				return false;
			}
			else if(topel)
			{
				var post = wc.firstChild;
				while(post != null)
				{
					if(topel.id == post.id)
					{
						post.style.zIndex = 11;
					}
					else
					{
						post.style.zIndex = 10;
					}
	
					post = post.nextSibling;
				}
			}
		}
}

function MM(e)
{
	if (ob)
	{
        if(NS())
        {
            event = null;
        }
        
        borderWidth = 2;
        if(IE())
        {
            borderWidth = 0;
        }
        
        if(NS())
        {
            x = e.clientX;
            y = e.clientY;
        }
        else
        {
            x = event.clientX;
            y = event.clientY;
        }
		
		if(shiftPressed)
		{
            newwidth = parseInt(ob.style.width) + (x - curX);
            newheight =  parseInt(ob.style.height) + (y - curY);
               				
			if(newwidth < minPostWidth) newwidth = minPostWidth;
			if(newheight < minPostHeight) newheight = minPostHeight;
			if(parseInt(ob.style.left) + newwidth > wc.offsetWidth - borderWidth)
			{
				newwidth = wc.offsetWidth - parseInt(ob.style.left) - borderWidth;
			}

			if(parseInt(ob.style.top) + newheight > wc.offsetHeight - borderWidth)
			{
				newheight = wc.offsetHeight - parseInt(ob.style.top) - borderWidth;
			}
			
			if(newwidth * newheight <= maxPostArea)
			{			
				document.getElementById("pwidth").value = newwidth;
				document.getElementById("pheight").value = newheight;
				
				mod = 11;
				if(IE())
				{
					mod = 7;
				}
				
				contentHeight = newheight - mod;
				
				document.getElementById("newpostcontent").style.height = contentHeight + "px";
				//document.getElementById("newpostimage").style.height = contentHeight + "px";
	               
	            ob.style.width = newwidth + "px";
	            ob.style.height = newheight + "px";
			}
		}
		else
		{
            newleft = x - wcleft;
            newtop = y - wctop;
               
			if(newleft < 0)
			{
				newleft = 0;
			}
			
			if(newtop < 0)
			{
				newtop = 0;
			}
			
			if(newleft + parseInt(ob.style.width) > wc.offsetWidth - borderWidth)
			{
				newleft = wc.offsetWidth - parseInt(ob.style.width) - borderWidth;
			}
			
			if(newtop + parseInt(ob.style.height) > wc.offsetHeight - borderWidth)
			{
				newtop = wc.offsetHeight - parseInt(ob.style.height) - borderWidth;
			}
			
			document.getElementById("xpos").value = newleft;
			document.getElementById("ypos").value = newtop;

               ob.style.left = newleft + "px";
               ob.style.top = newtop + "px";
		}

		curX = x;
		curY = y;

		return false;
	}
}

function MU()
{
	ob = null;
}

document.onmousedown = MD;
document.onmousemove = MM;
document.onmouseup = MU;

function Browser() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'IE';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}
