$(function(){

	$("#savedSearch").popup("<div id='savedSearchBox'><div id='searchTitleBar'><h3>Saved Searches</h3><a href='#'>[close]</a></div><div style='clear:both;'></div><p>Listed below are searches that have you have saved. You may view the results of the search by clicking on the title of the search.</p><ul class='listBox' id='savedSearchListing'></ul></div>", 175, loadSavedSearches)
	savedSearch.refreshSearchCountDisplay();
	
	if (cookiesEnabled())
	{	
		$("#saveSearch").popup("<div id='saveSearchBox'><div id='searchTitleBar'><h3>Save Search</h3><a href='#'>[close]</a></div><div style='clear:both;'></div><p>Please provide a title to describe your search. Once saved, you may access your saved search via the link labeled \"Saved Searches\" appearing in the upper right-hand corner of the College Results Online website.</p><input type='text' id='txtSearchTitle' name='txtSearchTitle' maxlength='100' /><input type='button' id='btSaveSearch' value='Save' onClick='saveSearch()' /><input type='button' id='btSaveSearchCancel' value='Cancel' onclick='cancelSearch()' /></div>", 275, loadSavedearchForm);
	}
	else
	{
		$("#saveSearch").hide();
	}

})

/* Search Form */
function cancelSearch()
{
	$("#saveSearchBox a").click();
}

function saveSearch()
{
	if (jQuery.trim($("#txtSearchTitle").val()) != "")
	{
		savedSearch.add(jQuery.trim($("#txtSearchTitle").val()),GetSearchURL());
		savedSearch.refreshSearchCountDisplay();
		$("#saveSearchBox a").click();
		$("#txtSearchTitle").val("")
	}
	else
	{
		alert("Please provide a title to describe your search.");
	}
}

/* Saved Search Listing */

var CRO_searches = null;
	
function loadSavedSearches(popup)
{
	savedSearch.refreshSearchDisplay();
	return true;
}

function loadSavedearchForm(popup)
{
	$("#txtSearchTitle").each(function(){this.focus();});
}

function removeSavedSearch(key)
{
	savedSearch.remove(key);
}

function savedSearch(key, value)
{
	this.key = key;
	this.value = value;
}

savedSearch.getKeyPrefix = function()
{
	return "CRO_SS_"
}

savedSearch.refreshSearchCountDisplay = function()
{
	$("#searchCount").text(savedSearch.list().length);
}

savedSearch.refreshSearchDisplay = function()
{

	if (cookiesEnabled())
	{
		searches = savedSearch.list();
		
		$("#savedSearchListing").empty();
		
		if (searches.length == 0)
		{
			$("#savedSearchListing").append("<li><strong>No searches have been saved.</strong></li>")
		}
		else
		{
			for (var i=0; i < searches.length; i++)
			{
				$("#savedSearchListing").append("<li><span class='search'><a href='" + searches[i].value + "'>" + searches[i].key + "</a></span><span class='action'><a href='javascript:void(0)' onclick='removeSavedSearch(" + i + ")'>remove</a></span></li>");
			}
		}
	}
	else
	{
		$("#savedSearchBox p").append("<br /><br /><strong style='color:red;'>This feature requires cookies to be enabled. You must enable cookies in order to be able to save your searches.</strong>")
	}
}

savedSearch.add = function(key, value)
{
	$.cookie("CRO_SS_" + key, value,{expires: 1000, path: '/'});
	savedSearch.refreshSearchCountDisplay();
	
}

savedSearch.remove = function(index)
{
	if (CRO_searches[index] && CRO_searches[index] != null)
	{
		$.cookie(savedSearch.getKeyPrefix() + CRO_searches[index].key, null,{path: '/'});
		CRO_searches[index] = null;
	}
	
	savedSearch.refreshSearchDisplay();
	savedSearch.refreshSearchCountDisplay();
	
}

savedSearch.list = function()
{

	CRO_searches = new Array();
	
	if (document.cookie && document.cookie != '')
	{
		
		var cookies = document.cookie.split(';');

		for (var i = 0; i < cookies.length; i++) {
	        
			var cookie = jQuery.trim(cookies[i]);
	        
			// Does this cookie string begin with the name we want?
			if (cookie.substring(0, savedSearch.getKeyPrefix().length) == savedSearch.getKeyPrefix()) {           
	        
				cookieKey = cookie.substring(savedSearch.getKeyPrefix().length, cookie.lastIndexOf("="));
				cookieValue = decodeURIComponent(cookie.substring(cookie.lastIndexOf("=") + 1));
	            
				CRO_searches[CRO_searches.length] = new savedSearch(cookieKey, cookieValue);
	            
			}
	        
		}
	
	}
    
    return CRO_searches
}