var debug = true;

$(window).load(function() {
	log("loaded");
});

$().ready(function()
{
	log("js ready");

	$("#searchButton").click(function() {
		$(this).parent("form").submit();
	});
	
	$("p:empty").remove();
	$("p").each(function() {
		if ($(this).text() == '\u00A0') $(this).remove();
	})

	$("div.innerBox h1").wrapInner("<span></span>");
	
	jQuery.expr[':'].parents = function(a,i,m){
	    return jQuery(a).parents(m[3]).length < 1;
	};

	$("#floatContainer a").filter(":parents(#calendar)").each(
		function() {
			setIconIfAllowed(this);
			addPDFTracking(this);
		}
	);
	
	if (!($.browser.msie && $.browser.version.substr(0, 1) == '6'))
	{
		$(".column > p > img, .column > p > a:not(.package,.arrow) > img, .quoteImage img").each(function() {
			setImageCorners(this);
		});

		$("div.quoteImage .rnd").css("float", "left");
	}

	$("p span.rnd").each(function () {
		if ($(this).css("float") == "right") $(this).css("margin", "0px 0px 3px 8px");
		if ($(this).css("float") == "left") $(this).css("margin", "0px 8px 3px 0px");
	});

	$("div.pnav a").click(function() {
		$("div.pnav a").removeClass("selected");
		$(this).addClass("selected");
		var show = $(this).attr("class");
		if (show.indexOf("{") > -1)
		{
			show = show.substring(show.indexOf("{") + 1, show.indexOf("}"));
			$("div.packages div.packageIntro").addClass("hidden");
			$("div.packages div.\\{" + show + "\\}").removeClass("hidden");
			var l = $("div.packages a.package");
			for (var i = 0; i < l.length; i++)
			{
				if ($(l[i]).attr("class").indexOf(show) > 0 || show == "All")
				{
					$(l[i]).css("display", "block");
				}
				else
				{
					$(l[i]).css("display", "none");
				}
			}
		}
		return false;
	});

	$("#caseType, #caseArea").each(function() {
		var types = $("div.case");
		var pref = "{" + $(this).attr("id").substring(4).toLowerCase() + ":";
		for (var i = 0; i < types.length; i++)
		{
			var t = $(types[i]).attr("class");
			t = t.substring(t.indexOf(pref) + pref.length, t.indexOf("}", t.indexOf(pref) + pref.length));
			if ($(this).find("option[value='" + t + "']").length == 0) $(this).append("<option value=\"" + t + "\">" + t + "</option>");
		}

		var sortedVals = $.makeArray($(this).find("option")).sort(function(a,b)
		{
			return $(a).text() > $(b).text() ? 1: -1;
		});
		$(this).empty().html(sortedVals);
		$($(this).find("option")[0]).attr("selected", "selected");
	});
	
	$("#caseType, #caseArea").change(function() {
		filterCases();
	});

	$("#showAllCases").click(function () {
		$("#caseType, #caseArea").find("option").removeAttr("selected");
		$("#caseType, #caseArea").find("option:first").attr("selected", "selected");
		filterCases();
		return false;
	});

	log("js ready end");
});

function filterCases()
{
	var t = $("#caseType option:selected").val();
	var a = $("#caseArea option:selected").val();
	var c = $("div.case");
	for (var i = 0; i < c.length; i++)
	{
		if (t == '' && a == '')
		{
			$(c[i]).removeClass("hidden");
		}
		if (t != '' && a == '')
		{
			if ($(c[i]).hasClass("{type:" + t + "}"))
				$(c[i]).removeClass("hidden");
			else
				$(c[i]).addClass("hidden")
		}
		if (t == '' && a != '')
		{
			$(c[i]).addClass("hidden")
			if ($(c[i]).hasClass("{area:" + a + "}"))
				$(c[i]).removeClass("hidden");
			else
				$(c[i]).addClass("hidden")
		}
		if (t != '' && a != '')
		{
			if ($(c[i]).hasClass("{type:" + t + "}") && $(c[i]).hasClass("{area:" + a + "}"))
				$(c[i]).removeClass("hidden");
			else
				$(c[i]).addClass("hidden")
		}
	}
}

function setImageCorners(e)
{
	var img = $(e);

	var width = img.width();
	if (img.attr("width") != '') width = img.attr("width");
	var height = img.height();
	if (img.attr("height") != '') height = img.attr("height");
 
	var wrapper = $('<span class="rnd"></span>');
	wrapper.width(width);
	wrapper.height(height);
 
	wrapper.css('float', img.css('float'));
	img.css('float', 'none')
 
	wrapper.css('margin-right', img.css('margin-right'));
	img.css('margin-right', '0')
 
	wrapper.css('margin-left', img.css('margin-left'));
	img.css('margin-left', '0')
 
	wrapper.css('margin-bottom', img.css('margin-bottom'));
	img.css('margin-bottom', '0')
 
	wrapper.css('margin-top', img.css('margin-top'));
	img.css('margin-top', '0')
 
	if (!($.browser.msie && ($.browser.version.substr(0, 1) == '6' || $.browser.version.substr(0, 1) == '7')))
	{
		wrapper.css('display', 'block');
		img.css('display', 'block')
	}

	img.wrap(wrapper);

	img.before('<span class="tl"></span>');
	img.before('<span class="tr"></span>');
	img.before('<span class="bl"></span>');
	img.before('<span class="br"></span>');
}


function setIconIfAllowed(e)
{
	var icons = ",.pdf,.ppt,/rss,";
	var classString = "arrow";
	if (icons.indexOf(',' + e.href.substr(e.href.length - 4).toLowerCase() + ',') > -1) classString += (" " + e.href.substr(e.href.length - 3).toLowerCase());
	if ($(e).children("img").length > 0)
	{
	}
	else if ($(e).html() == '')
	{
	}
	else if ($(e).parent().is("p") && ($.trim($(e).parent().html()).substr(0,2).toLowerCase() == "<a") && ($.trim($(e).parent().html().substr($(e).parent().html().length - 3,3).toLowerCase()) == "/a>"))
	{
		$(e).addClass(classString);
	}
	else if ($(e).parent().is("p") && ($.trim($(e).parent().html().substr(0,2)).toLowerCase() == "<a"))
	{
	}
	else if ($(e).parent().is("p") && ($.trim($(e).parent().html().substr($(e).parent().html().length - 3,3).toLowerCase()) == "/a>"))
	{
	}
	else if ($(e).parent().is("p") && !($.trim($(e).parent().html()).substr(0,2).toLowerCase() == "<a"))
	{
	}
	else if ($(e).parent().is("li") && !($.trim($(e).parent().html()).substr(0,2).toLowerCase() == "<a") && ($.trim($(e).parent().html().substr($(e).parent().html().length - 3,3).toLowerCase()) == "/a>"))
	{
		$(e).addClass(classString);
	}
	else
	{
		$(e).addClass(classString);
	}
}

function addPDFTracking(e)
{
	var track = ",.pdf,.ppt,/rss,";
	if (track.indexOf(',' + e.href.substr(e.href.length - 4).toLowerCase() + ',') > -1)
	{
		$(e).click(function() {
			var url = this.href.substr(this.href.lastIndexOf("/media"));
			pageTracker._trackPageview(url);
		})
	}
}

function recordOutboundLink(link, category, action) {
	try {
		var pageTracker=_gat._getTracker("UA-16231587-1");
		pageTracker._trackEvent(category, action);
		setTimeout('document.location = "' + link.href + '"', 100)
	}
	catch(err){}
}






function replaceAll(text, strA, strB)
{
    return text.replace( new RegExp(strA,"g"), strB );    
}

function log(s)
{
	if (debug && self.console) console.log(s);
}
