function docElemId(id)
{
	return document.getElementById(id);
}

function updateEvent(text)
{
	docElemId("preview_event").innerHTML = text+"!";
}

function updateFont(font)
{
	docElemId("preview").style.fontFamily = font;
}

function setEvent(text)
{
	docElemId("event").value = text;
	updateEvent(text);
}

function setDate(m, d, y)
{
	docElemId("date_month").value = m;
	docElemId("date_date").value = d;
	docElemId("date_year").value = y;
	
	getCountdown();
}

function setRecent(text)
{
	docElemId("recent").innerHTML = text;
}

function setBackgroundColor(color)
{
	if (color)
	{
		docElemId("preview").style.background = '#' + color;
		docElemId("backgroundColorbox").style.background = '#' + color;
		docElemId('hexbackgroundColor').value = color;
	}
}

function setTextColor(color)
{
	if (color)
	{
		docElemId("preview").style.color = '#' + color;
		docElemId("colorbox").style.background = '#' + color;
		docElemId('hexcolor').value = color;
	}
}

function setFont(font)
{
	docElemId("font").value = font;
	updateFont(font);
}
/*
function setSN(sn)
{
	docElemId("sn").value = sn;
}
*/
function setTime(hours, minutes)
{
	if (hours >= 12)
	{
		var ampmindex = 1;
		if (hours != 12)
		{
			hours -= 12;
		}
	}
	else
	{
		var ampmindex=0;
		if (hours == 0)
		{
			hours = 12;
		}
	}
	
	if (hours < 10)
	{
		hours = '0' + hours;
	}
	
	docElemId('hours').value = hours;
	docElemId('minutes').value = minutes;
	docElemId('ampm').selectedIndex = ampmindex;
}

function disableSN()
{
	docElemId('sn').disabled = true;
	docElemId('snwrap').ondblclick = function() {alert('Cannot change ScreenName.  Must create new countdown.');}
	
	// hide loading screen for the get screennames ajax, since it is completely unnecessary
	// disable it completely in the future?
	hideLoading('screennames');
}

function getDisplayOptions()
{
	var show = 0;
	
	var timeSections = Array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years');
	
	for (var i = 0; i < timeSections.length; i++)
	{
		if (docElemId('check' + timeSections[i]).checked)
		{
			show += Math.pow(2, i);
		}
	}
	
	return show;
}

function setDisplayOptions(show)
{
	var timeSections = Array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years');

	for (var i = timeSections.length - 1; i >= 0; i--)
	{
		if (show >= Math.pow(2, i))
		{
			docElemId('check' + timeSections[i]).checked = true;
			show -= Math.pow(2, i);
		}
	}
}

function getTime()
{
	var hours = parseInt(docElemId('hours').value);
	var minutes = docElemId('minutes').value;
	var ampm = docElemId('ampm').selectedIndex;
	
	if (hours == 12)
		hours -= 12;
	if (ampm == 1)
		hours += 12;
		
	return hours + ":" + minutes;
}


function showChooser(which)
{
	BGorFG = which;
	lastColor = docElemId('hex' + BGorFG).value;
	getMouse();
	chooser = docElemId("colorchooser");
	chooser.style.left = mouseX + "px";
	chooser.style.top = mouseY + "px";
	chooser.style.display = "block";
	chooser.onmouseout = setLastColor;
	window.oldEventListener = document.onmouseup;
	document.onmouseup = new Function ("hideChooser(); window.oldEventListener()");		// add event listener without removing other
	//opacity("preview", 100, 0, 1000);
}

function hideChooser()
{
	chooser.style.display = "none";
	document.onmouseup = oldEventListener;
}

function setLastColor()
{
	changeColor(lastColor);
	//docElemId("preview").style[BGorFG] = lastColor;
	//docElemId(BGorFG+"box").style.background = lastColor;
}

function setupMouse()
{
	IE = document.all?true:false;
	if (!IE)
	{
		//document.captureEvents(Event.MOUSEMOVE);
		document.onmousedown = function(e) { mouseX = e.pageX; mouseY = e.pageY; }
	}
}

function getMouse()
{
	if (IE)
	{
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	}
}

function previewType(type)
{
	var previewList = docElemId("preview_list");
	var previewCreate = docElemId("preview_create");
	
	var ownerButton = docElemId("viewtype_owner");
	var guestButton = docElemId("viewtype_guest");
	
	if (type == 'guest')
	{
		previewList.style.display = "none";
		previewCreate.style.display = "block";
		ownerButton.style.borderStyle = "outset";
		guestButton.style.borderStyle = "inset";
	}
	else if (type == 'owner')
	{
		previewList.style.display = "block";
		previewCreate.style.display = "none";
		ownerButton.style.borderStyle = "inset";
		guestButton.style.borderStyle = "outset";
	}
}

function initCalendar()
{
	cal11 = new CalendarPopup("calendar");
	cal11.showNavigationDropdowns();
	cal11.setReturnFunction("setMultipleValues3");
	cal11.setCssPrefix("CALENDAR");
}

function getDateToPass()
{
	var datetopass = docElemId("date_month").value + "/" + docElemId("date_date").value + "/" +docElemId("date_year").value;
	return datetopass;
}

function getCountdown()
{
	getAJAX('countdown', getDateToPass(), getTime(), getDisplayOptions());
}

function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
		{
			return pair[1];
		}
	} 
}

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
		
	return true;
}

function getFonts()
{
	var fonts = "Arial, Comic Sans MS, Courier New, Garamond, Lucida Sans, Tahoma, Times New Roman, Verdana";
	var fontoptions = fonts.split(", ");
	
	var fontbox = docElemId('font');
	
	for (var i = 0; i < fontoptions.length; i++)
	{
		fontbox.options[i] = new Option(fontoptions[i], fontoptions[i]);
		fontbox.options[i].style.fontFamily = fontoptions[i];
	}
}

function toggleAdvanced(toggle)
{
	var displayMode;
	var inlinedisplayMode;
	var activeButton;
	var inactiveButton;
	//var inlinedisplayMode;
	
	if (toggle == 1)
	{
		displayMode = 'block';
		inlinedisplayMode = 'inline';
		activeButton = 'show_advanced';
		inactiveButton = 'hide_advanced';
//		var hexcodewidth = '4em';
//		var hexcodeborderwidth = '2px';
	}
	else if (toggle == 0)
	{
		displayMode = inlinedisplayMode = 'none';
		//inlinedisplayMode = 'none';
		activeButton = 'hide_advanced';
		inactiveButton = 'show_advanced';
//		var hexcodewidth = '0em';
//		var hexcodeborderwidth = '0px';
	}
	
	
	docElemId('displayoptions').style.display = displayMode;
	docElemId('displayoptionslabel').style.display = displayMode;
	docElemId('timeline').style.display = displayMode;
	docElemId('timelabel').style.display = displayMode;
	docElemId('hexcolor').style.display = inlinedisplayMode;
	docElemId('hexbackgroundColor').style.display = inlinedisplayMode;
//	docElemId('hexcolor').style.width = hexcodewidth;
//	docElemId('hexbackgroundColor').style.width = hexcodewidth;
//	docElemId('hexcolor').style.borderWidth = hexcodeborderwidth;
//	docElemId('hexbackgroundColor').style.borderWidth = hexcodeborderwidth;
	
	
	docElemId(activeButton).style.borderStyle = 'inset';
	docElemId(inactiveButton).style.borderStyle = 'outset';
	
}

function storeCookie()
{
	var SN = docElemId('sn').value;
	var event = docElemId('event').value;
	var date = getDateToPass();
	var time = getTime();
	var font = docElemId('font').value;
	var background = docElemId('hexbackgroundColor').value;
	var textcolor = docElemId('hexcolor').value;
	var showint = getDisplayOptions();
	
	//alert(date);
	
	getAJAX('storecookie', SN, event, date, time, font, background, textcolor, showint);
}

function saveCountdown()
{
	var SN = docElemId('sn').value;
	var event = docElemId('event').value;
	var date = getDateToPass();
	var time = getTime();
	var font = docElemId('font').value;
	var background = docElemId('hexbackgroundColor').value;
	var textcolor = docElemId('hexcolor').value;
	var showint = getDisplayOptions();
	
	//alert(date);
	
	getAJAX('save', SN, event, date, time, font, background, textcolor, showint);
}

function showLink(href)
{
	this.href = href;
	
	docElemId('link').style.display = "block";
	updateLink(docElemId('linktext').value);
}

function updateLink(text)
{
	// var text = escape(text);
	
	linkcode = '<a href="' + this.href + '" target="_self">' + text + '</a>';

	docElemId('textlinktextarea').value = linkcode;
	docElemId('htmllinkdiv').innerHTML = linkcode;

	
	var ta = docElemId('textlinktextarea');
	
	
	if (!document.all && ta.scrollHeight == ta.clientHeight)
	{
		ta.style.height = "0px";
	}

	if (ta.scrollHeight != ta.clientHeight)
	{
		ta.style.padding = "0";
		ta.style.height = ta.scrollHeight + "px";
		ta.style.padding = "1em";
	}
	
		
}


function showTextLink()
{
	docElemId('linkframe').style.display = "block";
	docElemId('textlink').style.display = "block";
	docElemId('htmllink').style.display = "none";
	docElemId('linkorcode').innerHTML = "code";
	
	updateLink(docElemId('linktext').value);
		
	//docElemId('linkcontainer').style.border = "none";
	//docElemId('linkcontainer').style.padding = "0";
}

function showHTMLLink()
{
	docElemId('linkframe').style.display = "block";
	docElemId('textlink').style.display = "none";
	docElemId('htmllink').style.display = "block";
	docElemId('linkorcode').innerHTML = "link";
	//updateLink(docElemId('linktext').value);
	//docElemId('linkcontainer').style.border = "1px solid";
	//docElemId('linkcontainer').style.padding = "1em";
}

function imageLabel(id)
{
	docElemId(id).checked = true;
}

function loadCookie()
{
	getAJAX('loadcookie');
}

function defaultSettings()
{
	setBackgroundColor('FFFFFF');
	setTextColor('000000');
	
	getAJAX('randomevent', 'next');
}

function populate()
{
	//getAJAX('screennames');
	
	setDisplayOptions(14);
	setFont('Tahoma');
	
	getAJAX('countdowndata', getQueryVariable('s'));
}

function initialize()
{
	//initAJAX();
	//getAJAX('recent');
	getFonts();
	setupMouse();
	initCalendar();
	populate();
	hideLoading();
}