/* ==========================================================
JAVASCRIPT FOR LROC VIDEO QUESTIONS
By: Andrew Gaddis (2009)
========================================================== */


/*%%% SETTINGS & VARIABLES %%% */
currentVideo = null;
currentMode = 'startup';
color_img_selected = '#0066FF';
color_selected = '#0000AA';
color_over = '#000000';
color_default = '';

loadinitialbg = false;

sel_mission = 'true';
sel_general = 'true';
sel_science = 'true';
sel_school = 'true';

par = '';

hashp = new Object();


/* %%% FUNCTIONS %%% */
//-----------------------------------------------------------
function loadDefault()
{
	//...Process...
	var initialHash = document.location.hash;
	if (initialHash=='')
	{
		//Load Standard Default/New
			tmpOverride = false;
			/*currentCategory = 'moon';*/
		loadVidDefault();
		currentMode = 'default';
	}
	else
	{
			tmpOverride = false;
		//Load 'state' from URL Hash
		currentMode = 'hashstate';
		var hash = initialHash;
		hash = hash.replace('#','');
		var tokens = hash.split('&');
		for (i=0;i<tokens.length;i++)
		{
			var thistoken = tokens[i];
			var pair = thistoken.split('=');
			if (pair.length>=2)
			{
				var thiskey = pair[0];
				var thisval = pair[1];
				window['ui_'+thiskey] = thisval;
			}
		}
		//Parse URL Values
		currentCategory = window['ui_category'];
		currentVideo = window['ui_vid'];
		sel_mission = window['ui_mission'];
		sel_general = window['ui_general'];
		sel_science = window['ui_science'];
		sel_school = window['ui_school'];
		//Catch Blank/Empty Values
		if (currentCategory==null) { currentCategory = 'atlas'; }
		if (currentVideo==null) { currentVideo = ''; }
		if (sel_mission==null) { sel_mission = 'true'; }
		if (sel_general==null) { sel_general = 'true'; }
		if (sel_science==null) { sel_science = 'true'; }
		if (sel_school==null) { sel_school = 'true'; }
		//Set People Form Check Boxes
		setPeopleForm();
		//Set Video BG (if video selected/loaded)
		if (currentVideo!='') { loadinitialbg = true; }
		//Load into Hash Parameter Object
		hashp['category'] = currentCategory;
		hashp['vid'] = currentVideo;
		hashp['mission'] = sel_mission;
		hashp['general'] = sel_general;
		hashp['science'] = sel_science;
		hashp['school'] = sel_school;
		//Load Pane /w URL Input
		loadVidPane(currentCategory);
	}
}
//-----------------------------------------------------------
function loadVidDefault()
{
	//...Variables...
	var catID = 'img' + currentCategory;
	//...Load Default Pane...
	loadVidPane(currentCategory);
	//...Force Reload of Default Choices...
	setPeopleForm();
	//...Output...
	return false;
}
//-----------------------------------------------------------
function loadVidPane(category)
{
	//...Variables...
	var resultID = 'vidpane';
	var url = 'vid_pane.php';
	var imgOldID = 'img'+currentCategory;
	var imgOldobj = document.getElementById(imgOldID);
	var imgNewID = 'img'+category;
	var imgNewobj = document.getElementById(imgNewID);
	//...Swap Category Image Border...
	imgOldobj.style.borderColor = '';
	imgNewobj.style.borderColor = color_img_selected;
	currentCategory = category;
	//...Clear 'currentVideo'...
	if (currentMode!='hashstate') { currentVideo = ''; }
	//...Parse Parameters...
	par = 'category='+currentCategory+'&vid='+currentVideo+'&people_mission='+sel_mission+'&people_general='+sel_general+'&people_science='+sel_science+'&people_school='+sel_school;
	//...URL Handling...
	hashp['loc'] = 'pane';
	hashp['category'] = currentCategory;
	if (currentMode!='hashstate')
	{
		hashp['vid'] = '';
		delete hashp['vid'];
	}
	setHash();
	//...Process...
	loadAJAX('500',resultID,url,par);
		if (tmpOverride) { /*TEMP TO DO */ setTimeout("loadVideo('02');",1000); }
	currentMode = 'loadpane';
	//...Output...
	return false;
}
//-----------------------------------------------------------
function loadVideo(vid)
{
	//...Variables...
	var resultID = 'video';
	var url = 'vid_embed.php';
	//...Flip Select Video BG...
	if (currentVideo!=null)
	{
		var oldVidStr = 'vid'+currentVideo;
		var oldVidObj = document.getElementById(oldVidStr);
		if (oldVidObj!=null)
		{
			oldVidObj.style.backgroundColor = color_default;
		}
	}
	var newVidStr = 'vid'+vid;
	var newVidObj = document.getElementById(newVidStr);
	if (newVidObj!=null)
	{
		newVidObj.style.backgroundColor = color_selected;
		currentVideo = vid;
	}
	//...Parse Parameters...
	par = 'category='+currentCategory+'&vid='+currentVideo+'&people_mission='+sel_mission+'&people_general='+sel_general+'&people_science='+sel_science+'&people_school='+sel_school;
	//...Set URL Handling...
	hashp['loc'] = 'video';
	hashp['vid'] = currentVideo;
	setHash();
	//...Process...
	loadAJAX('500',resultID,url,par);
	//...Output...
	return false;
}
//-----------------------------------------------------------
function flipVidColor(vid,action)
{
	//...Process...
	if (vid!=currentVideo)
	{
		var str = 'vid'+vid;
		var obj = document.getElementById(str);
		if (obj!=null)
		{
			if (action=='over') { obj.style.backgroundColor = color_over; }
			if (action=='out') { obj.style.backgroundColor = color_default; }
		}
	}
	//...Output...
	return false;
}
//-----------------------------------------------------------
function loadPeopleForm()
{
	//...Process...
		//Initials
		this_mission = '';
		this_general = '';
		this_science = '';
		this_school = '';
		//Grab Current Values
		var formobj = document.getElementById('peopleForm');
		var fieldobj = formobj.people;
		var numfields = fieldobj.length;
		for (i=0;i<numfields;i++)
		{
			var thisobj = fieldobj[i];
			var thisValue = thisobj.value;
			var thisChecked = thisobj.checked;
			//Setup New Values
			var varname = 'this_'+thisValue;
			window[varname] = thisChecked;
		}
		//Reset Master Values
		sel_mission = this_mission;
		sel_general = this_general;
		sel_science = this_science;
		sel_school = this_school;
		//Set URL Handling
		hashp['mission'] = sel_mission;
		hashp['general'] = sel_general;
		hashp['science'] = sel_science;
		hashp['school'] = sel_school;
		setHash();
		//Force Pane Reload
		loadVidPane(currentCategory);
	//...Output...
	return false;
}
//-----------------------------------------------------------
function setHash()
{
	//...Process...
		//Initials
		var hashstr = '';
		//Build Hash String
		for (thiskey in hashp)
		{
			var thisvalue = hashp[thiskey];
			if (hashstr=='') { hashstr = thiskey+'='+thisvalue; }
			else { hashstr = hashstr + '&'+thiskey+'='+thisvalue; }
		}
		//Set the Hash
		document.location.hash = hashstr;
	//...Output...
	return hashstr;
}
//-----------------------------------------------------------
function setPeopleForm()
{
	//...Process...
		//Variables
		var formobj = document.getElementById('peopleForm');
		var fieldobj = formobj.people;
		var numfields = fieldobj.length;
		//Grab Current Check Box Values
		var cbox = [];
		if (sel_mission=='true') { sel_mX = true; } else { sel_mX = false; }
		if (sel_general=='true') { sel_gX = true; } else { sel_gX = false; }
		if (sel_science=='true') { sel_sX = true; } else { sel_sX = false; }
		if (sel_school=='true') { sel_scX = true; } else { sel_scX = false; }
		cbox[0] = sel_mX;
		cbox[1] = sel_gX;
		cbox[2] = sel_sX;
		cbox[3] = sel_scX;
		//Hit Each Check Box
		for (i=0;i<numfields;i++)
		{
			var thisobj = fieldobj[i];
			thisobj.checked = cbox[i];
		}
	//...Output...
	return false;
}
//-----------------------------------------------------------
function setInitialVidBG(thisvid)
{
	var newVidStr = 'vid'+thisvid;
	var newVidObj = document.getElementById(newVidStr);
	if (newVidObj!=null)
	{
		newVidObj.style.backgroundColor = color_selected;
	}
}
//-----------------------------------------------------------


/* %%% END %%% */

