// JavaScript Document

document.loggedInHTML = "<span class=\"headlineLink\" style=\"float:right; margin-right:15px;\">Logged in as [THEIKRUSER] | <a href=\"http://www.iveknownrivers.org/community/\" class=\"headlineLink\">my settings</a> | <a href=\"#\" onclick=\"javascript:logout();\" id=\logoutbutton\" class=\"headlineLink\">logout</a></span>";

document.loggedOutHTML = "<span class=\"headlineLink\" style=\"float:right; margin-right:15px;\"><a href=\"#\" id=\"showlogin\" class=\"headlineLink\">login</a><span id=\"ajaxlogin\">: <input name=\"userlogin\" type=\"text\" class=\"loginfield\" id=\"userlogin\" size=\"10\" /> password: <input name=\"userpassword\" type=\"password\" class=\"loginfield\" id=\"userpassword\" size=\"10\" /> <span class=\"formerror\" id=\"loginerror\"></span> <a href=\"#\" onclick=\"javascript:login();\" id=\"loginbutton\" class=\"headlineLink\">go</a> | <a href=\"#\" id=\"hidelogin\" class=\"headlineLink\">cancel</a></span> | <a href=\"#\"id=\"joinikrlink\" class=\"headlineLink\">join IKR</a></span></span>";


// some global variables to keep track of exactly what the user is doing with the page 

document.sending = false;
document.loggedIn = false;
document.commenting = false;
document.joining = false;


// called when read-2.0.php is loaded... binds events for various buttons and links on the page

function storyEventsInit(logged) {
	
	// how the comments panel is handled depends on whether the user is already logged in.
	if (logged == "1") {
		document.loggedIn = true;
		document.getElementById("holdcommentiframe").style.display = "block";
		document.getElementById("commentiframe").src = "http://www.iveknownrivers.org/comments/add-comment.php";
	}
	else {
		document.loggedIn = false;	
	}
	
	
	if (document.getElementById("showsharelink")) { document.getElementById("showsharelink").onclick = showsharepanel; }
	if (document.getElementById("hidesharelink")) { document.getElementById("hidesharelink").onclick = hidesharepanel; }
								 
	if (document.getElementById("showauthorlink")) { document.getElementById("showauthorlink").onclick = showauthorpanel; }
	if (document.getElementById("hideauthorlink")) { document.getElementById("hideauthorlink").onclick = hideauthorpanel; }
	
	if (document.getElementById("sendemail")) { document.getElementById("sendemail").onclick = sendemail; }
	
	if (document.getElementById("addcomment")) { document.getElementById("addcomment").onclick = addcomments; }
	
	if (document.loggedIn == false) {
		document.getElementById("joinikrlink").onclick = joinIKR;
		document.getElementById("incommentjoinikrlink").onclick = joinIKR;
		document.getElementById("showlogin").onclick = showlogin;	
		document.getElementById("incommentshowlogin").onclick = showlogin;
		document.getElementById("hidelogin").onclick = hidelogin;
	}
	
	document.getElementById("motherheader").onmouseover = hiliteheadlineon;
	document.getElementById("motherheader").onmouseout = hiliteheadlineoff;
	document.getElementById("motherheader").style.cursor = "pointer";
	
	document.getElementById("homebutton").onmouseover = hilitehomeon;
	document.getElementById("homebutton").onmouseout = hilitehomeoff;
	document.getElementById("homebutton").style.cursor = "pointer";
	document.getElementById("homebutton").onclick = gohome;
	
}


function showsharepanel(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here		
		this.blur();
		document.getElementById("share").style.display = "block";
    }
	return false;
}

function hidesharepanel(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here	
		this.blur();
		document.getElementById("share").style.display = "none";
		
		document.getElementById("e1").style.display = "none";
		document.getElementById("e2").style.display = "none";
		document.getElementById("e3").style.display = "none";
		document.getElementById("e4").style.display = "none";
		document.getElementById("e5").style.display = "none";
		document.getElementById("e6").style.display = "none";
		
		document.getElementById("e1").innerHTML = "";
		document.getElementById("e2").innerHTML = "";
		document.getElementById("e3").innerHTML = "";
		document.getElementById("e4").innerHTML = "";
		document.getElementById("e5").innerHTML = "";
		
		document.forms[1].namefrom.value = "";
		document.forms[1].emailfrom.value = "";
		document.forms[1].emailto.value = "";
		document.forms[1].nameto.value = "";
		document.forms[1].message.value = "";
		document.forms[1].code.value = "";
    }
	return false;
}

function showauthorpanel(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here	
		this.blur();
		document.getElementById("author").style.display = "block";
    }
	return false;
}

function hideauthorpanel(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here		
		this.blur();
		document.getElementById("author").style.display = "none";
    }
	return false;
}

function sendemail(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here	
		this.blur();
		if (document.sending == false) {
			// show the delay
			document.getElementById("e6").innerHTML = "<img src=\"images/interface-2.0/delay.gif\" width=\"77\" height=\"40\" /><br /><span class=\"smallgreentext\">Sending mail</span>";
			document.getElementById("e6").style.display = "block";
			document.forms[1].submit();
			document.sending = true;
		}
    }
	return false;
}


function hilitehomeon(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here
		this.blur();
		document.images.homebutton.src = "images/interface-2.0/moad-home-ov.png";
    }
}

function hilitehomeoff(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here	
		this.blur();
		document.images.homebutton.src = "images/interface-2.0/moad-home-up.png";
    }
}



function hiliteheadlineon(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here
		this.blur();
		document.images.headlineimage.src = "images/interface-2.0/headline-ikr-ov.png";
    }
}

function hiliteheadlineoff(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here	
		this.blur();
		document.images.headlineimage.src = "images/interface-2.0/headline-ikr.png";
    }
}

function gohome(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here	
		document.location.href = "http://www.moadsf.org/";
    }
}

function showlogin(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
        // process event here
		this.blur();
		if (document.joining == true) {
			document.getElementById("joinpanel").style.display = "none";
			document.joining = false;
		}
		
		if (this.id == "incommentshowlogin") {
			document.location.href = "#joinanchor";
			document.commenting = true;
			document.getElementById("addacomment").style.display = "none";
		}
		document.getElementById("ajaxlogin").style.display = "inline";
    }
	return false;
}

function hidelogin(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
		// process event here	
		this.blur();
		document.getElementById("ajaxlogin").style.display = "none";
		if (document.commenting == true) { document.commenting = false; }
		document.forms[0].userlogin.value = "";
		document.forms[0].userpassword.value = "";
    }
	return false;
}


function addcomments(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
		// process event here	
		this.blur();
		document.commenting = true;
		document.getElementById("addacomment").style.display = "block";
		if (document.loggedIn == true) {
			// make the iframe visible, hide the warning
			document.getElementById("commentloginwarning").style.display = "none";
			document.getElementById("holdcommentiframe").style.display = "block";
		}
		else {
			document.getElementById("commentloginwarning").style.display = "block";
		}
		document.location.href = "#addcommentanchor";
    }
	return false;
}

function joinIKR(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) {
		// process event here	
		this.blur();
		document.joining = true;
		if (this.id = "incommentjoinikrlink") {
			document.location.href = "#joinanchor";
			document.getElementById("addacomment").style.display = "none";
		}
		document.getElementById("joinpanel").style.display = "block";
		document.getElementById("thelogin").innerHTML = document.loggedOutHTML;
		document.getElementById("showlogin").onclick = showlogin;
		document.getElementById("hidelogin").onclick = hidelogin;
		document.getElementById("joinikrlink").onclick = joinIKR;
    }
	return false;
}


function login() {
	document.forms[0].action = "javascript:sendData('lib/login-user.php',0);";
	document.forms[0].submit();
	return false;
}

function logout() {
	document.forms[0].action = "javascript:sendData('lib/logout-user.php',1);";
	document.forms[0].submit();
	return false;
}

function commentReset() {
	window.location.reload();
}
