// Javascript functions for Treasurer.jocogov.org v2.0
// The code below is maintained by Topher Stumph

// Function to handle selecting a tab from a text link
function setup_tab_link(link_id, tab_id, url)
{
	$(link_id).click(function() { // bind click event to link
		if (url)
		{
			window.location.href = url + '?tab=' + tab_id;
			return false;
		}
		else
		{
			$("#tab_container > ul").tabs('select', tab_id);
			return false;
		}
	});
}

// Function to handle the asynchronous submission of the "contact us" form.
// It grabs the appropriate elements from the document and submits them to
// the form in the appropriate format.
function contactus_mailer()
{
	// Get the selected department
	var to = document.getElementById('to').options[document.getElementById('to').selectedIndex].value;
	// Set the recipient's email address to the appropriate value, depending on the department selection.
	if (to == "tre") { var toAddy = "taxinfo@jocogov.org"; }
	else if (to == "dmv") { var toAddy = "DMV-CitizenQuestions@jocogov.org"; }
	else if (to == "ar") { var toAddy = "TRE-AccountsReceivable@jocogov.org"; }
	// If nothing was selected, no email will be set. This results in a sending error.
	else { var toAddy = ""; }
	// Get the sender's email address. No validation is performed currently.
	var fromAddy = document.getElementById("from").value;
	// Get the Message Subject.
	var msgSubject = document.getElementById("msgSubject").value;
	// Get the Message Body.
	var msgBody = document.getElementById("msgBody").value;
	// Using AJAX, submit the data to the formmailer. An alert will be displayed to the user with the return result.
	$.post("/formmailer.aspx", { to: toAddy, from: fromAddy, subject: msgSubject, body: msgBody }, function(data){ alert(data); });
}

// Overloaded form of the alert() function.
// It requests a query string, which will be displayed to the user
// along with an 'Ok' button.
function alert(query)
{
	$("#alert").modal({
		containerId: "smallmodalContainer",
		onOpen: function (dialog)
		{
			dialog.overlay.fadeIn('normal', function() { dialog.container.slideDown('fast', function() { dialog.data.fadeIn('fast'); }); });
		},
		onShow: function (dialog)
		{
			// Place the query string into the template
			dialog.data.find('.query').append(query);
		},
		onClose: function (dialog)
		{
			dialog.data.fadeOut('fast', function() { dialog.container.slideUp('fast', function() { dialog.overlay.fadeOut('fast', function() { $.modal.close(); }); }); });
		}
	});

}

// Overloaded form of the confirm() function.
// It requests a query string, a call back function, and an optional url string.
// The query string is typically "Do you agree?" or something similar.
// The callback function is performed if the user clicks the 'yes' button.
// The url is inserted into a scrollable <div>, if defined.
function confirm(query, callback, url)
{
	// Define the <div> to use as a template
	if (url)
	{
		var div = "#confirmdialog";
		var container = "modalContainer";
	}
	else
	{
		var div = "#confirm";
		var container = "smallmodalContainer";
	}
	$(div).modal({
		containerId: container,
		onOpen: function (dialog)
		{
			dialog.overlay.fadeIn('normal', function() { dialog.container.slideDown('fast', function() { dialog.data.fadeIn('fast'); }); });
		},
		onShow: function (dialog)
		{
			// Using AJAX, fetch the XML from the url if defined
			if (url)
			{
				$("#message").load(url);
			}
			// Place the query string into the template
			dialog.data.find('.query').append(query);
			// If the user clicks yes...
			dialog.data.find('.yes').click(function ()
			{
				// then perform the callback function...
				if ($.isFunction(callback))
				{
					callback.apply();
				}
				// and close the dialog.
				$.modal.close();
			});
		},
		onClose: function (dialog)
		{
			dialog.data.fadeOut('fast', function() { dialog.container.slideUp('fast', function() { dialog.overlay.fadeOut('fast', function() { $.modal.close(); }); }); });
		}
	});
}

// Function for displaying the Contact dialog.
// It requests a callback function, which will be performed
// after the user clicks the 'Send' button.  This function
// would normally be contactus_mailer().
function contact(callback)
{
	// Define the <div> to use as a template
	var div = "#contactdialog";
	var container = "modalContainer";
	$(div).modal({
		containerId: container,
		persist: false,
		onOpen: function (dialog)
		{
			dialog.overlay.fadeIn('normal', function() { dialog.container.slideDown('fast', function() { dialog.data.fadeIn('fast'); }); });
		},
		onShow: function (dialog)
		{
			// If the user clicks send...
			dialog.data.find('.send').click(function ()
			{
				// then perform the callback function...
				if ($.isFunction(callback))
				{
					callback.apply();
				}
				// and close the dialog.
				$.modal.close();
			});
		},
		onClose: function (dialog)
		{
			dialog.data.fadeOut('fast', function() { dialog.container.slideUp('fast', function() { dialog.overlay.fadeOut('fast', function() { $.modal.close(); }); }); });
		}
	});
}

// Function to grab parts of an RSS feed and insert them into a container
// It requires a valid url, aka "http://www.site.com/rss.xml";
// a valid container referenced by id, aka "#news";
// and provides optional start/end variables so that only a portion of the feed can be grabbed if desired.
function fetchRSS(url, container, start, end)
{
	$.getFeed(
	{
		url: url,
		success: function(feed)
		{
			// If start is undefined, set it to the start of the feed (0)
			if (start == undefined) { start = 0; }
			// If end is undefined, set it to the end of the feed
			if (end == undefined) { end = feed.items.length; }
			
			var html = '';

			for (var i = start; i < feed.items.length && i <= end; i++)
			{
				var item = feed.items[i];
				
				html += '<p class="header">'
				+ '<a href="'
				+ item.link
				+ '">'
				+ item.title
				+ '</a>'
				+ '</p>';
				
				html += '<p class="updated">'
				+ item.updated
				+ '</p>';
				
				html += '<p class="content">'
				+ item.description
				+ '</p>';
				
			}
			$(container).append(html);
		}
	});
}

// Function to set the title of each page.
function setTitle(title)
{
	document.title = "Johnson County, Kansas - Treasurer's Dept. - " + title;
}

function setTab(tab)
{
	$(tab).addClass("tabSelected");
}
// Function to style alternating elements with stripes.
function prepare_stripes()
{
	$(".striped:odd").css({background:"#cccccc"});
}

// Function to be performed during loading that prepares the various types of accordions used
function prepare_accordions()
{
/*	// Open the first accordion_header's corresponding accordion_content element prior to loading
	$(".accordion_header:first").css({backgroundImage:"url(/SpryAssets/SpryMenuBarDown.gif)"}).next(".accordion_content").css({display: "block"});

	// Handle the click event for accordion_header elements
	$(".accordion_header").click(function()
	{
		// Open the accordion_header that was clicked on while closing all of its siblings
		$(this).css({backgroundImage:'url(/SpryAssets/SpryMenuBarDown.gif)'}).next(".accordion_content").slideDown(500).siblings("div.accordion_content").slideUp("slow");
	    $(this).siblings(".accordion_header").css({backgroundImage:"url(/SpryAssets/SpryMenuBarRight.gif)"});
	});
*/
	// Handle the click event for accordion_subheader elements
	$(".accordion_subheader").click(function()
	{
		// Open the accordion_subheader that was clicked on while closing all of its siblings.
		// The only difference between the header and subheader is that the subheader has no background image to update,
		// and the first subheader doesn't open by default.
		$(this).next(".accordion_content").slideDown(500).siblings("div.accordion_content").slideUp("slow");
	});
}

// Function to get a Parameter from the query string
function getParameter(name) {
   var url = window.location.href;
   var paramsStart = url.indexOf("?");

   if(paramsStart != -1){

      var paramString = url.substr(paramsStart + 1);
      var tokenStart = paramString.indexOf(name);

      if(tokenStart != -1){

         paramToEnd = paramString.substr(tokenStart + name.length + 1);
         var delimiterPos = paramToEnd.indexOf("&");

         if(delimiterPos == -1){
            return paramToEnd;
         }
         else {
            return paramToEnd.substr(0, delimiterPos);
         }
      }
   }
}

// Function to handle selecting a tab as directed from another page
function open_tab(tab_id)
{
	$("#tab_container > ul").tabs('select', tab_id);
	return false;
}

// Perform functions when the HTML DOM document is ready, but before the page is displayed
$(document).ready(function()
{
	prepare_accordions();
	prepare_stripes();
});

// End of Document