var caltime;

function htmlEntityDecode(s)
{
	var t = document.createElement("textarea");
	t.innerHTML = s;
	return t.value;
}

function test_menu(direction,menu){
	var main_cal = document.getElementById('container_'+menu);
	var themenu = document.getElementById('pop_'+menu);
	
	if(direction == 'on'){
		themenu.style.display = 'block';
		main_cal.style.overflow = 'visible';
		//alert('te');
	} else {
		themenu.style.display = 'none';
		main_cal.style.overflow = 'hidden';
	}
}

function popcal(number)
{
	var main_cal = document.getElementById('container_'+number);
	var pop_cal = document.getElementById('pop_'+number);
	
	main_cal.style.overflow = 'visible';
	pop_cal.style.display = 'block';
}


function clearcaltime(number)
{
	caltime = setTimeout("clearcal('"+number+"');", 1000);
}

function clearcal(number)
{
	var thisp = '<?php echo $thispagename; ?>';
	var main_cal = document.getElementById('container_'+number);
	var pop_cal = document.getElementById('pop_'+number);
	
	main_cal.style.overflow = 'hidden';
	pop_cal.style.display = 'none';

	//MM_swapImgRestore();
		/*
		if(thisp == number){
			document.getElementById(number).src = homedir+"images/header_menu_"+number+"_s.jpg";
		} else {
			document.getElementById(number).src = homedir+"images/header_menu_"+number+".jpg";
		} */
}
	
function clearallcal()
{
	var thisp = '<?php echo $thispagename; ?>';
	var menus = [
		"explore",
		"education",
		"lodging",
		"homeschool",
		"store"
	];
	
	/*
	var menus = new Array();
	menus[0] = 'explore';
	menus[1] = 'education';
	menus[2] = 'lodging';
	menus[3] = 'store';
	*/
	
	for (i = 0; i < menus.length; i++)
	{
		var main_cal = document.getElementById('container_'+menus[i]);
		var pop_cal = document.getElementById('pop_'+menus[i]);

		if(main_cal)
		{
			main_cal.style.overflow = 'hidden';
			pop_cal.style.display = 'none';
			/*
			if(thisp == menus[i]){
				document.getElementById(menus[i]).src = homedir+"images/header_menu_"+menus[i]+"_s.jpg";
			} else {
				document.getElementById(menus[i]).src = homedir+"images/header_menu_"+menus[i]+".jpg";
			}
			*/
		}
	}

}

function popupEvent(ev, w, h) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	win = window.open("/calendar/functions/popup.php?ev=" + ev + "&showCat=&oc=1","EasyPHPCalendar","scrollbars=yes,status=no,location=no,toolbar=no,menubar=no,directories=no,resizable=yes,width=" + w + ",height=" + h + ",top=" + wint + ",left=" + winl + "");
	if (window.parseInt(navigator.appVersion) >= 4) 
	{ 
		win.window.focus(); 
	}
}

var FossilRimSidebar = function()
{
	this.initialize = function()
	{
		var __this = this;

		/*
		 * Move the hover-over div at the beginning of the body
		 * so it shows up over other items.
		 */
		$("#overDiv").prependTo("body");
		$(".event").click(function(e) {
			$.ajax({
				url: "ajaxProxy.php",
				data: {
					af: "calendar",
					am: "getJulianDate",
					eventDate: $(this).children("span").attr("date")
				},
				dataType: "json",
				type: "POST",
				success: __this.launchEventWindow
			});
		});
		
	};
	
	this.launchEventWindow = function(o, textStatus)
	{
		popupEvent(o.julianDate, 350, 400);
	};
	
	this.__config = {
		hover: {
			dateBackgroundColor: "#663300",
			dateForegroundColor: "white",
			timeBackgroundColor: "#f4f4f4",
			timeForegroundColor: "black",
			hoverBackgroundColor: "white",
			descriptionForegroundColor: "black"
		}
	};
	
	this.initialize();
};

/**
 * Object for handling Animal Info page events.
 * @author Adam Presley
 */
var AnimalInfoPage = function(config)
{
	/**
	 * Initializes the object, sets up events.
	 * @author Adam Presley
	 */
	this.initialize = function()
	{
		var __this = this;
		
		$("#animalGroupScroller").scrollable({
			size: 6,
			activeClass: "active",
			nextPage: ".nextPage1",
			prevPage: ".prevPage1"
		});
		
		/*
		 * Attach the click event to the group images.
		 */
		$(".animalGroupItem").click(function(e) {
			__this.onClick_animalGroupItem.call(__this, $(this).attr("data-groupId"));
		});
		
		$("#animalGroupScroller").fadeIn("medium");
		$(".animalImage").cycle({ fx: "fade" });
		
		this.setAnimalTooltips();
		this.setAnimalClickEvent();
	};
	
	this.setAnimalTooltips = function()
	{
		$("#animalListing span a").tooltip({ 
			showURL: false,
			opacity: 1,
			bodyHandler: function() {
				return $("<img />").attr({ src: $(this).attr("src") });
			}
		});
	};
	
	this.setAnimalClickEvent = function()
	{
		var __this = this;
		
		$(".animalItem").click(function(e) {
			__this.onClick_animalItem.call(__this, $(this).attr("data-animalId"));
		});
	};
	
	/**
	 * Fired when an animal group is clicked. The in turn fires an AJAX
	 * call to get all animals in this category. Upon success calls
	 * populateAnimalsScroller.
	 * @author Adam Presley
	 * @param groupId The group ID to load animals for
	 */
	this.onClick_animalGroupItem = function(groupId)
	{
		var __this = this;
		
		$.ajax({
			url: "ajaxProxy.php",
			data: {
				af: "animals",
				am: "getAnimalsByGroup",
				groupId: groupId
			},
			dataType: "json",
			type: "POST",
			success: function(o) {
				if (o.success)
				{
					__this.populateAnimals.call(__this, o);
				}
				else
				{
					alert(o.message);
				}
			}
		});
	};
	
	/**
	 * Creates the animals scroller HTML.
	 * @author Adam Presley
	 * @param data Array containing animal data
	 */
	this.populateAnimals = function(o)
	{
		var __this = this;
		var html = "";
		var data = o.data;
		
		for (var i = 0; i < data.length; i++)
		{
			html += "<span style=\"margin-right: 3px; text-transform: uppercase; font-size: 12pt;\"> &bull; ";
			html += "<a class=\"animalItem\" href=\"javascript:void(0);\" data-animalId=\"" + data[i].animalId + "\" style=\"text-decoration: none; margin-bottom: 15px;\" src=\"/" + data[i].image + "\" title=\"" + data[i].animalName + "\">" + data[i].animalName + "</a>";
			html += "</span>";
		}
		
		$("#animalTitle").html("");

		$("<img />").attr({ src: "/animal_info/images/animal_info_category.png" }).appendTo("#animalTitle");
		$("<img />").attr({ src: "/" + o.imageHeading }).appendTo("#animalTitle");
		$("#animalListing").html(html);

		this.setAnimalTooltips();
		this.setAnimalClickEvent();
		
		for (var i = 0; i < data.length; i++)
		{
			if (data[i].defaultAnimal == 1)
			{
				this.onClick_animalItem(data[i].animalId);
				break;
			}
		}
	};
	
	/**
	 * Fired when an animal is clicked. The in turn fires an AJAX
	 * call to get this animal's information. Upon success calls
	 * populateAnimalInfo.
	 * @author Adam Presley
	 * @param animalId The animal ID to load information for
	 */
	this.onClick_animalItem = function(animalId)
	{
		var __this = this;
		
//		$(".animalInfo").slideUp("medium");
		
		$.ajax({
			url: "ajaxProxy.php",
			data: {
				af: "animals",
				am: "getAnimal",
				animalId: animalId
			},
			dataType: "json",
			type: "POST",
			success: function(o) {
				if (o.success)
				{
					__this.populateAnimalInfo.call(__this, o.data);
				}
				else
				{
					alert(o.message);
				}
			}
		});
	};
	
	/**
	 * Populates the HTML with the information for a given animal. This is
	 * fired from an AJAX success call from onClick_animalItem.
	 * @author Adam Presley
	 * @param data Array containing animal data
	 */
	this.populateAnimalInfo = function(data)
	{
		var i = 0;
		
		$(".animalInfoTitle").html(data.animalName);
		$(".scientificName").html(data.scientificName);
		$(".ssp").html("<span>Species Survival Plan? - </span>" + data.sspStatus);
		$(".status").html("<span>Status - </span>" + data.endStatus);
		$(".habitat").html("<span>Habitat - </span>" + data.habitat);
		$(".predators").html("<span>Predators - </span>" + data.predators);
		$(".food").html("<span>Food - </span>" + data.food);
		
		$(".animalImage").html("");
		
		$.each(data.images, function(index, item) {
			$("<img />").attr("src", "/" + item.imagePath).css({ width: "320px", height: "240px" }).appendTo($(".animalImage"));
		});
		
		$(".animalImage").cycle({ fx: "fade" });
		
		$(".middleColumn").html(data.descriptions);
		
		$(".nativeTo").html("<span>Originally Native To</span>" + data.origin);
		$(".height").html("<span>Height</span>" + data.overallSize);
		$(".weight").html("<span>Weight</span>Male: " + data.mWeight);
		$(".weightFemale").html("Female: " + data.fWeight);
		$(".characteristics").html("<span>Characteristics</span>" + data.characteristics);
		$(".gestationPeriod").html("<span>Gestation Period</span>" + data.gestation);
		$(".numberOfOffspring").html("<span>Offspring</span>" + data.offspring);
		$(".birthSeason").html("<span>Birth Season</span>" + data.birthdays);
		$(".lifespanWild").html("<span>Lifespan</span>Wild: " + data.lifeSpanWild);
		$(".lifespanCaptivity").html("Captivity: " + data.lifeSpanCaptivity);
		$(".socialBehavior").html("<span>Social Behavior</span>" + data.social);
		
		$(".animalInfo").slideDown("fast");
	};
	
	this.__config = $.extend({ selectedGroup: 1}, config);
	this.initialize();
};

/**
 * Object for handling the Article page..
 * @author Adam Presley
 */
var ArticlePage = function()
{
	this.initialize = function()
	{
		$("#imageContainer").cycle({ fx: "fade" });
	};
	
	this.initialize();	
};

/**
 * Object for handling the employeeEditProfile page.
 * @author Adam Presley
 */
var EmployeeEditProfilePage = function(config)
{
	var __initialize = function()
	{
		$("#btnAddEmail").click(__onClick_btnAddEmail);
		$("#btnRemoveEmail").click(__onClick_btnRemoveEmail);
		$("#btnAddPhone").click(__onClick_btnAddPhone);
		$("#btnRemovePhone").click(__onClick_btnRemovePhone);
		$("#btnAddEec").click(__onClick_btnAddEec);
		$("#btnRemoveEec").click(__onClick_btnRemoveEec);
		
		$("#frmUpdate").submit(__onClick_btnSubmit);
		
		/*
		 * Define masks
		 */
		$("#dob").mask("99/99/9999");
		$("#postalCode").mask("99999");
		$("#phone").mask("999-999-9999");
		$("#eecPhoneNumber").mask("999-999-9999");
	};
	
	var __onClick_btnSubmit = function(e)
	{
		$("#emailAddresses *").attr("selected", "selected");
		$("#phoneNumbers *").attr("selected", "selected");
		$("#eecs *").attr("selected", "selected");
	};
	
	var __onClick_btnAddEmail = function(e)
	{
		var email = $("#email").val();
		var type = $("#emailType :selected").val();
		var typeName = $("#emailType :selected").text();
		
		if (email !== "")
		{
			var opts = $("#emailAddresses").get(0).options;
			opts[opts.length] = new Option(email + " (" + typeName + ")", email + "~~~" + type);
		}
	};
	
	var __onClick_btnRemoveEmail = function(e)
	{
		var selected = $("#emailAddresses :selected");
		
		if (selected.length)
		{
			$(selected).remove();
		}
	};
	
	var __onClick_btnAddPhone = function(e)
	{
		var phone = $("#phone").val();
		var type = $("#phoneType :selected").val();
		var typeName = $("#phoneType :selected").text();
		
		if (phone !== "")
		{
			var opts = $("#phoneNumbers").get(0).options;
			opts[opts.length] = new Option(phone + " (" + typeName + ")", phone + "~~~" + type);
		}
	};
	
	var __onClick_btnRemovePhone = function(e)
	{
		var selected = $("#phoneNumbers :selected");
		
		if (selected.length)
		{
			$(selected).remove();
		}
	};
	
	var __onClick_btnAddEec = function(e)
	{
		var name = $("#eecName").val();
		var relationship = $("#eecRelationship").val();
		var phone = $("#eecPhoneNumber").val();
		
		if (name !== "" && relationship !== "" && phone !== "")
		{
			var opts = $("#eecs").get(0).options;
			var newoptDisplay = name + " - " + relationship + " (" + phone + ")";
			var newoptValue = name + "~~~" + relationship + "~~~" + phone;
			opts[opts.length] = new Option(newoptDisplay, newoptValue);
		}
	};
	
	var __onClick_btnRemoveEec = function(e)
	{
		var selected = $("#eecs :selected");
		
		if (selected.length)
		{
			$(selected).remove();
		}
	};
	
	var __config = $.extend({
		employeeId: 0
	}, config);
	var __this = this;
	__initialize();
};


