﻿(function ()
{
	var contentDialog;

	jQuery(document).ready(function ()
	{
		InitializePrimaryNav();
		InitializeModalEmbedded();
		InitializeContentDialogs();
		InitializeTableTweaks();
		InitializeAnalytics();
	});

	function InitializePrimaryNav()
	{

	}

	function InitializeTableTweaks()
	{
		jQuery("table.basic-table").each(function ()
		{
			jQuery(this).find("tr:odd").addClass("odd");
		});
		jQuery("h2.table-head").each(function ()
		{
			jQuery("<a></a>").attr("name", jQuery(this).text().toLowerCase().replace(/\s|&nbsp;/g, "-")).appendTo(this);
		});
	}

	function InitializeContentDialogs()
	{
		if (contentDialog == null)
		{
			contentDialog = jQuery("<div id=\"ContentDialog\"></div>").appendTo("body");
		}

		jQuery("a.content-dialog").each(function ()
		{
			jQuery(this).click(function ()
			{
				var href = jQuery(this).attr("href").replace(/\s/g, "%20");
				jQuery(contentDialog).html("").load(href, function (r, s, x)
				{
					var title = jQuery(this).find("span.dialog-title").html();
					if (s == "error")
					{
						title = "Error: " + x.status + " " + x.statusText;
						jQuery(contentDialog).html("<div class=\"error\"><p>Sorry! There was a problem loading the resource: " + href + "</p></div>");
					}
					jQuery(contentDialog).dialog({ width: '550px', modal: true, title: title });
				});
				return false;
			});
		});
	}

	function InitializeModalEmbedded()
	{
		//is there a better selector to find all elements with a css class of 'modal-embedded-group-X' where 'X' is any numeral?
		jQuery('.video-modal-embedded,.modal-embedded,*[class*="modal-embedded-group"]').each(function ()
		{
			var elem = jQuery(this);
			if (!elem.is('a'))
				return;

			var href = elem.attr('href').split('?');

			var params = '';
			if (href.length > 1)
			{
				params = ParseQueryString(href[1]);
			}

			//extract the width and height parameters or use the defaults if the parameters aren't explicitly set
			var widthParam = params != '' && params["w"] != '' && params["w"] != undefined ? params["w"] : 480;
			var heightParam = params != '' && params["h"] != '' && params["h"] != undefined ? params["h"] : 360;

			var newQs = '';
			jQuery.each(params, function (index, val)
			{
				if (index == 'w' || index == 'h')
					return;

				if (newQs != '')
					newQs += "&";
				newQs += index + "=" + encodeURIComponent(val);
			});

			elem.attr('href', href[0] + (newQs != '' ? "?" + newQs : ''));

			var className = elem.attr('class');
			if (className.indexOf('modal-embedded-group') != -1)
			{
				//we can't assume that 'modal-embedded-group-X' will be the only class assigned to the element, so we need to retrieve all classes and find the 'modal-embedded-group-X' class
				var classes = className.split(' ');
				for (var i = 0; i < classes.length; i++)
				{
					var myClass = classes[i];
					if (myClass.indexOf('modal-embedded-group') == -1)
						continue;

					//users have the option of assigning the 'modal-embedded-group-X-hidden' class to items that are to be included in the group
					//the 'hidden' items should still be represented in the modal gallery, but should be hidden from the page
					if (myClass.indexOf('-hidden') != -1)
					{
						myClass = myClass.replace('-hidden', '');
						elem.hide();
					}

					//assign the first (hopefully only) instance of the 'modal-embedded-group-X' class to the 'rel' attribute of the colorbox element, this allows for colorbox grouping
					elem.attr('rel', myClass);
					break;
				}
				elem.colorbox({ iframe: true, width: widthParam, height: heightParam, current: "{current}/{total}" });
			}
			else
			{
				elem.colorbox({ iframe: true, width: widthParam, height: heightParam });
			}
		});
	}

	function InitializeAnalytics()
	{
		jQuery('a[href*="fstk="]').each(function (index)
		{
			var elem = jQuery(this);
			var href = elem.attr('href').split('?');

			var params = '';
			if (href.length > 1)
			{
				params = ParseQueryString(href[1]);
			}

			var modifiedParams = {};
			jQuery.each(params, function (index, val)
			{
				if (index != 'fstk')
					modifiedParams[index] = val;
			});

			var newQs = '';
			jQuery.each(modifiedParams, function (index, val)
			{
				if (newQs != '')
					newQs += "&";
				newQs += index + "=" + encodeURIComponent(val);
			});

			elem.attr('href', href[0] + (newQs != '' ? "?" + newQs : ''));
			elem.attr('onclick', "javascript:_gaq.push(['regionalTracker._trackPageview', '" + params['fstk'] + "']);");
		});
	}

	function ParseQueryString(qs)
	{
		var result = {}, queryString = qs, re = /([^&=]+)=([^&]*)/g, m;
		while (m = re.exec(queryString))
		{
			result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
		}
		return result;
	}
})();
