$(document).ready(function(){
	var checkLogin = RunCGI("/checkLogin.php");
	if(checkLogin == "true"){
		$("#login").hide();
		$("#loginregister").hide();
		$("#memberbar").show();
		$("#usercp").show();
	}
	else {
		$("#memberbar").hide();
		$("#usercp").hide();
	}
	$("#archive-menu").hide();
	$("#fu").show();
	$("#rp").hide();
	$("#ms").hide();
	$("#pf").hide();
	$("#nf").hide();
	$("#rc").show();
	$("#ng").show();
	$("#register").hide();
	$("#forgot").hide();

	$("#followus").click( function() { showDrops('fu'); });
	$("#recentposts").click( function() { showDrops('rp'); });
	$("#morningskate").click( function() { showDrops('ms'); });
	$("#pensfeed").click( function() { showDrops('pf'); });
	$("#nhlfeed").click( function() { showDrops('nf'); });
	$("#blogroll").click( function() { showDrops('rc'); });
	$("#loginregister").click( function() { showDrops('login'); });
	$("#regbutton").click( function() { showDrops('login'); showDrops('register'); });
	$("#forgotpass").click( function() { showDrops('login'); showDrops('forgot'); });
	$("#nextgame").click( function() { showDrops('ng'); });
	mainmenu();

	$('.imageContainer').cycle({
		fx: 'fade',
		speed: 4000,
		timeout: 2000
	});
});

function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
                $(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
                },function(){
                $(this).find('ul:first').css({visibility: "hidden"});
                });
}
function showMenu(which){
	if($("#" + which).attr('state') == 'hide'){
                $("#" + which).slideDown(300);
		$("#" + which).attr('state', 'show');
	}
	else{
		$("#" + which).slideUp(300);
		$("#" + which).attr('state', 'hide');
	}
}

function showDrops(which){
	if($("#" + which).attr('state') == 'hide'){
	        $("#" + which).slideDown(300);
		$("#" + which).attr('state', 'show');
	}
	else{
		$("#" + which).slideUp(300);
		$("#" + which).attr('state', 'hide');
	}
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.style.color = '#000';
		thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.style.color = '#9b9b9b';
		thisfield.value = defaulttext;
	}
}

function getXMLObject(){
	var xmlHttp = false;
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
	}
	catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
		}
		catch (e2) {
			xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
		}
	}
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
	}
	return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject();
var xmlhttp2 = new getXMLObject();

function login(){
	var username = document.login.username.value;
	var password = document.login.password.value;
	var remember = true;

	var params = "";
	params += "remember=" + remember + "&";
	params += "username=" + username + "&";
	params += "password=" + password;

	xmlhttp.open("POST", "/login.php", true); 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(params);

	//wait for the script to come back
	xmlhttp.onreadystatechange = function(){ 
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText == 'success'){
				window.location.reload();
				//$("#login").slideUp(300);
				//$("#loginregister").slideUp(300);
				//$("#memberbar").slideDown(300);
				//$("#usercp").slideDown(300);
			}
			else{
				document.getElementById('loginform').style.height = "100px";
				document.getElementById('loginerror').innerHTML = "<p><b>The username/password entered is incorrect.</b></p>";
			}
		}
	}
}

function register(){
	var username = document.register.username.value;
	var password = document.register.password.value;
	var confirm_pass = document.register.password_confirmed.value;
	var email = document.register.email.value;

	if(username == ''){
		alert("Please enter a username.");
		return(false);
	}
	if(email == ''){
		alert("Please enter an email address.");
		return(false);
	}
	if(password == ''){
		alert("Please enter a password.");
		return(false);
	}
	if(password != confirm_pass){
		alert("Passwords do not match.");
		return(false);
	}

	var params = "";
	params += "username=" + username + "&";
	params += "password=" + password + "&";
	params += "email=" + email;

	xmlhttp.open("POST", "/register.php", true); 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(params);

	//wait for the script to come back
	xmlhttp.onreadystatechange = function(){ 
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText == 'success'){
				document.getElementById('registerform').style.height = "250px";
				document.getElementById('registererror').innerHTML = "<a class=\"fakelink\" onClick=\"showDrops('register'); showDrops('login');\">Click Here to Login</a>";
				//$("#login").slideUp(300);
				//$("#loginregister").slideUp(300);
				//$("#memberbar").slideDown(300);
				//$("#usercp").slideDown(300);
			}
			else{
				document.getElementById('registerform').style.height = "250px";
				document.getElementById('registererror').innerHTML = xmlhttp.responseText;
			}
		}
	}
}
function forgotPassword(){
	var email = document.forgotPass.email.value;

	var params = "";
	params += "email=" + email;

	xmlhttp.open("POST", "/forgotpass.php", true); 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(params);

	//wait for the script to come back
	xmlhttp.onreadystatechange = function(){ 
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText == 'success'){
				document.getElementById('forgotform').style.height = "70px";
				document.getElementById('forgoterrors').innerHTML = "<p>New password sent. Please check your email for more details.</p>";
			}
			else {
				document.getElementById('forgotform').style.height = "70px";
				document.getElementById('forgoterrors').innerHTML = xmlhttp.responseText;
			}
		}
	}
}

function GetHTTP(){
	var httpRequest;
	if(document.all) {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		httpRequest = new XMLHttpRequest();
	}
	return(httpRequest);
}
function RunCGI(Command){
	var httpRequest = GetHTTP();
	var SpecString=Command;
	httpRequest.open('GET', SpecString, false);
	httpRequest.send(null);
	return(httpRequest.responseText);
}
function xxMakeComment(){
	var rand = Math.floor(Math.random() * 10);
	document.getElementById('errors').style.display = "none";

	var params = "";
	params += "captcha=" + document.makeComment.captcha.value + "&";
	params += "slug=" + document.makeComment.slug.value + "&";
	params += "username=" + document.makeComment.username.value + "&";
	params += "comment=" + tinyMCE.activeEditor.getContent();

	var xhrComment = new getXMLObject();
	xhrComment.open("POST", "/post-comment.php", true);
	xhrComment.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhrComment.send(params);
	xhrComment.onreadystatechange = function(){
		if(xhrComment.readyState == 4 && xhrComment.status == 200){
			if(xhrComment.responseText == 'bad captcha'){
				document.getElementById('errors').innerHTML = "<p>The captcha code was incorrect.</p>";
				document.getElementById('captchaImage').src = "/image"+rand+".php";
				document.getElementById('errors').style.display = "block";
				document.makeComment.captcha.value = '';
			}
			else if(xhrComment.responseText == 'success'){
				document.getElementById('loading').style.display="block";
				document.getElementById('postComments').innerHTML = '';
				$("#postComments").slideUp();
				loadComments();
				tinyMCE.getInstanceById('comment_box').execCommand('mceSetContent', false, '');
				document.makeComment.captcha.value = '';
				document.getElementById('captchaImage').src = "/image"+rand+".php";
			}
			else {
				//Something bad happened
				document.getElementById('errors').innerHTML = "<p>" + xhrComment.responseText + "</p>";
			}
		}
	}
}
var commentRowClass = "comment-box1";
function loadComments(){
	var browserName = navigator.appName;
	var post = document.makeComment.slug.value;
	var getComments = eval('(' + RunCGI("/get-comments.php?post=" + post) + ')');
	var allcomments = document.getElementById('postComments');
	allcomments.innerHTML = '';
	for(var i = 0; i < getComments.comments.length; i++){
		var commentHolder = document.createElement('div');
		/*if(i%2 == 0){
			commentHolder.className = "comment-box1";
		}
		else {
			commentHolder.className = "comment-box2";
		}*/

		commentHolder.className = commentRowClass;

		if(i == 0){
			commentHolder.style.borderTop = "0px";
		}

		var userColumn = document.createElement('div');
		userColumn.className = "commentuser-info";
		var userImage = document.createElement('img');
		userImage.className = "commentImage";
		if(!getComments.comments[i].AVATAR){ userImage.src = "/images/no-avatar.jpg"; }
		else { userImage.src = getComments.comments[i].AVATAR; }
	
		var userName = document.createElement('p');
		userName.appendChild(document.createTextNode(getComments.comments[i].USER));

		userColumn.appendChild(userName);
		userColumn.appendChild(userImage);

		var commentBox = document.createElement('div');
		commentBox.className = "userComments";

		var commentDate = document.createElement('p');
		commentDate.className = "comment-date";
		commentDate.appendChild(document.createTextNode("Posted: " + getComments.comments[i].DATE));

		var userComment = document.createElement('p');
		userComment.className = "commentText";

		//This is why IE sucks
		if(browserName == "Microsoft Internet Explorer"){ 
			userComment.innerText = getComments.comments[i].COMMENT.replace('<p>','').replace('</p>',''); 
		}
		else { userComment.innerHTML = getComments.comments[i].COMMENT; }

		var clear = document.createElement('div');
		clear.className = "clear";

		commentBox.appendChild(commentDate);
		commentBox.appendChild(userComment);

		commentHolder.appendChild(userColumn);
		commentHolder.appendChild(commentBox);
		commentHolder.appendChild(clear)

		allcomments.appendChild(commentHolder);
		if(commentRowClass == 'comment-box1'){ commentRowClass = 'comment-box2'; }
		else { commentRowClass = 'comment-box1'; }
	}
	if(getComments.comments.length == 0){ 
		document.getElementById('postComments').style.padding = "5px";
		document.getElementById('postComments').innerHTML = "<p>There are currently no comments for this topic yet...</p>"; 
	}
	document.getElementById('loading').style.display="none";
	$("#postComments").slideDown();
	setTimeout(loadComments, 50000);
}
function goPensRadio(){
	window.open('http://downloads.penguins.nhl.com/radio/Player/Penguins_Radio.htm','radio','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=540,height=380,status');
}

