//Written by Josh Bavari
//May be used WITH my permission.

//On script load, currentPage becomes set to 1. Later on, it will be adjusted.
var currentPage = 0;
var currentVideo = 0;
var currentTopAd = 0;

function ajaxObject(layer, url)
{
	var current = this;
	var updating = false;
	var currLayer = layer;
	var layerID = document.getElementById(layer); //Get a reference to the layer that is constructed.
	var urlCall = url; //Maintain a reference to the url.
	this.callback = function(){}// Stub for now, can be overridden later.
	this.update = function(passData)
	{
		//Check if currently updating, if so, abort.
		if ( updating == true )
		{	
			return false;
		}
	
		//Set flag to say we are changing.
		updating = true;

		var xmlHttp = null;
	
	 	try
        {
			//Test for firefox, opera, safari
			xmlHttp = new XMLHttpRequest();
        }
        catch (e)
        {
	        //IE
	        try
	        {
      	    	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	        }
	        catch (e)
	        {
                try
                {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                        alert("Your browser does not support AJAX");
                        return false;
                }
	        }//closes inner catch
        }//closes outer catch
		if ( xmlHttp == null ) 
		{
			alert("Your browser doesn't support AJAX");
			return false;
		}
		//Define what happens when ready state changes.
		xmlHttp.onreadystatechange = function()
        {
			if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
			{
				//Make sure we have a reference to layerID, if not, assign one.
				if (layerID == null)
				{
					layerID = document.getElementById(currLayer);
				}
				layerID.innerHTML = xmlHttp.responseText;
				//alert(xmlHttp.responseText);
				delete xmlHttp;
				updating = false;
				current.callback();
			}//Closees if for readystate 4
       	}//Closes onreadystatechange

		//Define open and send actions.
                var uri = urlCall + '?' + passData;
		//alert('ajax call update uri: ' + uri);
                xmlHttp.open("GET", uri, true);
                xmlHttp.send(null);
                return true;

	}//Closes update method
}//Closes setUpXMLHttpRequest

//This function will get the next stories
function getNextStories()
{
	currentPage += 1;
	//alert(currentPage);
	var pageStr = 'p=' + currentPage;
	featuredStoriesAjax.update(pageStr);	
	//Get handle to prev button link.
	var prevStoryLink = document.getElementById('prevButton');
	//make sure its visible.
	prevStoryLink.style.visibility = 'visible';
	return true;
}//Closes getNextStories

//This function will get the previous stories
function getPrevStories()
{
	//Get handle to prev button.
	var prevStoryLink = document.getElementById('prevButton');
	
	currentPage -= 1;
	if ( currentPage == 1 )
	{
		prevStoryLink.style.visibility = 'hidden';
	}
	//alert(currentPage);
	var pageStr = 'p=' + currentPage;
	featuredStoriesAjax.update(pageStr);

}//Closes getPrevStories

function voteInPoll(currPoll, pollAjax)
{
//Here, we need to gather information about the form.
//We need to get what poll to vote for, then what option to cast a vote for.

	var getStr = "";
	
	var poll_id = currPoll.poll_id.value;
	var poll_option_id = -1;
	
	for(i = 0; i < currPoll.elements.length; i++ )
	{
		if ( currPoll.elements[i].type == "radio")
		{
			if ( currPoll.elements[i].checked )
			{
				poll_option_id = currPoll.elements[i].value;
				break;
			}
		}
	}
	
	if ( poll_option_id == -1 )
	{
		alert('You must pick an option to vote for!');
		return;
	}
	
	getStr = getStr + 'a=1&pid=' + poll_id + '&oid=' + poll_option_id ;
	
	pollAjax.update(getStr);
	
	//alert(getStr);

}//Closes voteInPoll

//This function makes use of a global variable currentVideo to tell what video it should display.
function getNextVideo()
{
	currentVideo = currentVideo + 1;
	
	var pageStr = 'a=1&p=' + currentVideo;
	//alert('page: ' + pageStr);
	//alert(pageStr);
	videoAjax.update(pageStr);
	
}//Closes getNextVideo

function getPrevVideo()
{
	currentVideo = currentVideo - 1;
	var pageStr = 'a=1&p=' + currentVideo;
	videoAjax.update(pageStr);
}//Closes getPrevVideo

function getNextVideos()
{
	currentVideo += 1;
	var pageStr = 'va=1&p=' + currentVideo;
	videoAjax.update(pageStr);
}//Closes getNextVideos

function getPrevVideos()
{
	currentVideo -= 1;
	var pageStr = 'va=1&p=' + currentVideo;
	videoAjax.update(pageStr);
}//Closes getPrevVideos

function getNextTopAd(adType)
{
	currentTopAd += 1;
	var pageStr = 'aj=1&p=' + currentTopAd + '&at=' + adType;
	topAdAjax.update(pageStr);
}//Closes getNextAd

function getPrevTopAd(adType)
{
	currentTopAd -= 1;
	var pageStr = 'aj=1&p=' + currentTopAd + '&at=' + adType;
	topAdAjax.update(pageStr);
}//Closes getPrevAd

function addNodeToList(node, list)
{
	list.appendChild(node);
}//Closes addNoteToList

function checkPostInformation(formId)
{
	var theForm = document.getElementById(formId);
	
	if(theForm.comment.value.length == 0 )
	{
		alert('You must supply at least some comment to post one!');
		return false;
	}
	if(theForm.captchaCode.value.length == 0)
	{
		alert('You must supply the security code to post a comment!');
		return false;
	}	
}

function checkMemberInfo(formId)
{
	var theForm = document.getElementById(formId);
	
	if(theForm.email.value.length == 0 )
	{
		alert('You must include an email address to sign up!');
		return;
	}
	if(theForm.pw.value.length == 0 )
	{
		alert('You must include a password to sign up!');
		return;
	}
}//closes checkMemberInfo

function showMenu(menuId)
{
	document.getElementById(menuId).style.visibility = "visible";
	document.getElementById(menuId).style.display = "block";
	//document.getElementById(menuId).className = "InnerMenu";	
}//closese showMenu

function hideMenu(menuId)
{
	document.getElementById(menuId).style.visibility = "hidden";
	document.getElementById(menuId).style.display = "none";
	//document.getElementById(menuId).className = "InnerMenuHidden";
}

function showBigView(viewId)
{
	var theView = document.getElementById(viewId);
	theView.style.visibility = "visible";
}//closes showBigView

var captchaNum = 0;

function refreshCaptcha(captchaId)
{
	var captchaImage = document.getElementById(captchaId);
	captchaNum += 1;
	captchaImage.src = 'CaptchaSecurityImages.php?width=100&height=40&characters=5&cn=' + captchaNum;
}//Closes refreshCaptcha



function setScreenSettings()
{
	if(screen.width < 1024)
	{
		document.getElementById('header').className = 'headerScroll';
		document.getElementById('topspacer').className = '';
	}
}

window.onload = setScreenSettings;