/// <reference path="util.js" />
var shownick, newnick;
var showvb, showubb, showhtml, showtext, showpreview, selecthandtext;
var handtext, handtextubb, handtextonly, handtexthtml, preview, scriptoptions;
var ASPNETCONST = "ctl00_maincontentcp_";

function convInit()	{
	shownick = $("ctl00_maincontentcp_DisplayNickname");
	newnick = $("ctl00_maincontentcp_HerosName");

    //hand display buttons
	showvb = $("ctl00_maincontentcp_vbformat");
	showubb = $("ctl00_maincontentcp_ubbformat");
	showhtml = $("ctl00_maincontentcp_htmlformat");
	showtext = $("ctl00_maincontentcp_textformat");
	showpreview = $("ctl00_maincontentcp_previewhandtext");
		
	//hand boxes
	handtext = $("ctl00_maincontentcp_handtext");
	handtextubb = $("ctl00_maincontentcp_handtextubb");
	handtexthtml = $("ctl00_maincontentcp_handtexthtml");
	handtextonly = $("ctl00_maincontentcp_handtextonly");
	preview = $("ctl00_maincontentcp_preview");
	scriptoptions = $("scriptoptions");
	
	if (shownick)	{
		shownick.onclick=shownickClicked;
		newnick.onkeyup=newnickChanged;
	    newnick.onclick=function() {this.select();};
	}
	
	if (scriptoptions)  {
	    selecthandtext = document.createElement("a");
	    //selecthandtext.type = "button";
	    selecthandtext.innerHTML = "Select Text";
	    selecthandtext.className = "linkbutton";
	    selecthandtext.href = "#";
	    selecthandtext.onclick=function() {
			if (handtext.className=="previewbox")	{
				handtext.select();	
			} else if (handtextonly.className=="previewbox")	{
				handtextonly.select();				
			} else if (handtextubb.className=="previewbox") {
			    handtextubb.select();
			} else if (handtexthtml.className=="previewbox") {
			    handtexthtml.select();
			}
			return false;
		};
		scriptoptions.append(selecthandtext);
	}
    if (showvb) {
        showvb.onclick = ChangeFormat;
    }
    if (showubb) {
        showubb.onclick = ChangeFormat;
    }
    if (showhtml) {
        showhtml.onclick = ChangeFormat;
    }
    if (showtext) {
        showtext.onclick = ChangeFormat;
    }    
	if (showpreview)	{
		showpreview.onclick = showpreviewClicked;
	}
}
function ChangeFormat() {
    //alert("starts anyway!");
    //undo preview if necessary
    showvb.className = "linkbutton";
    showubb.className = "linkbutton";
    showhtml.className = "linkbutton";
    showtext.className = "linkbutton";
    if (showpreview.innerHTML=="Preview Off")	{
        preview.className="invisible";
		selecthandtext.className="";
		if (showvb.className == "linkbuttonsel")  {
		    handtext.className="previewbox";
		} else if (showubb.className == "linkbuttonsel")   {
		    handtextubb.className="previewbox";
		} else if (showhtml.className == "linkbuttonsel")   {
		    handtexthtml.className="previewbox";
		}
		showpreview.innerHTML = "Preview";
		showpreview.className = "linkbutton";
    }
    var btnid = this.id.replace(ASPNETCONST, "");
    if (btnid=="vbformat")  {
        handtext.className="previewbox";
        showvb.className = "linkbuttonsel";
        handtextubb.className="invisible";
        handtexthtml.className="invisible";
        handtextonly.className="invisible";
        showpreview.className="linkbutton";
	} else if (btnid=="ubbformat")   {
        handtext.className="invisible";
        handtextubb.className="previewbox";
        showubb.className = "linkbuttonsel";
        handtexthtml.className="invisible";
        handtextonly.className="invisible";
        showpreview.className="linkbutton";
    } else if (btnid=="htmlformat")   {
        handtext.className="invisible";
        handtextubb.className="invisible";
        handtexthtml.className="previewbox";
        showhtml.className = "linkbuttonsel";
        handtextonly.className="invisible";
        showpreview.className="linkbutton";
	} else  {
        handtext.className="invisible";
        handtextubb.className="invisible";
        handtexthtml.className="invisible";
        handtextonly.className="previewbox";
        showtext.className = "linkbuttonsel";
        showpreview.className="invisible";	
	}	
	return false;
}
function shownickClicked()	{
	if (!this.checked)	{
		if (newnick.value.length==0)	{
			newnick.value = "Hero";
		}
		newnick.select();		
	} else	{
		if (newnick.value=="Hero"){
			newnick.value = "";
		}
	}
}
function newnickChanged()	{
	if (newnick.value.length==0)	{
		shownick.checked=true;
	} else	{
		shownick.checked = false;
	}
}
function showpreviewClicked()	{
	if (!preview)	{
		alert("no preview object");
		return false;
	}
	if (this.innerHTML=="Preview")	{
		preview.className="previewbox";
		selecthandtext.className="invisible";
		handtext.className="invisible";
		handtextubb.className="invisible";
		handtexthtml.className="invisible";
    	this.innerHTML = "Preview Off";
		this.className = "linkbuttonsel";
	} else {
		preview.className="invisible";
		selecthandtext.className="linkbutton";
		if (showvb.className == "linkbuttonsel")  {
		    handtext.className="previewbox";
		} else if (showubb.className == "linkbuttonsel")   {
		    handtextubb.className="previewbox";
		} else if (showhtml.className == "linkbuttonsel")   {
		    handtexthtml.className="previewbox";
		}
		this.innerHTML = "Preview";
		this.className = "linkbutton";
	}	
	return false;
}

function sitenamelistOnChange() {
    sitename.value = sitenamelist.options[sitenamelist.selectedIndex].text;
    return false;
}
