var debug = true;
var isIE6 = false;
var useIE6Corners = true;
var logSlider;
var logSlideSpeed = 10;

$(window).load(function() {
	log("loaded");
	var logboxHideDateIndex = 0;
	var lastLogboxHideDate = 0;
	$(".logboxHideDate .item[date]").each(function (i) {
		var d = $(this).attr("date");
		if (d > lastLogboxHideDate) {
			lastLogboxHideDate = d;
			logboxHideDateIndex = i;
		}
	});
	if (logboxHideDateIndex > 0) {
		$(".logboxHideDate .logNavigation a:eq(" + logboxHideDateIndex + ")").click();
	}

	$(".imageWithOverlay").each(function () {
		$(this).attr("baseHeight", $(this).find(".overlay").height());
		$(this).attr("maxHeight", $(this).find("img").height() - 20);
		$(this).find(".overlay").css("height", $(this).attr("baseHeight"));
	});
	
	$(".imageWithOverlay").hoverIntent(function () {
		$(this).find(".mouseOver").removeClass("hidden");
		$(this).find(".default").addClass("hidden");
		$(this).find(".overlay").animate({
				height: $(this).attr("maxHeight")
			}, 400, function() {
			});
	}, function () {
		$(this).find(".mouseOver").addClass("hidden");
		$(this).find(".default").removeClass("hidden");
		$(this).find(".overlay").animate({
				height: $(this).attr("baseHeight")
			}, 200, function() {
			});
	}).click(function () {
		document.location = $(this).attr("url");
	});

});

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

	$("img.moveNextP").each(function () {
		$(this).closest("div").find("p:first").prepend(this);
	});

	$("#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);
		}
	);
	
	isIE6 = $.browser.msie && $.browser.version.substr(0, 1) == '6';
	
	if (!isIE6 || useIE6Corners)
	{
		$(".content > p > img, .column > p > img, .column > p > a:not(.package,.arrow) > img, .quoteImage img, img.roundMe, p.pink, p.roundMe, div.roundMe").each(function() {
			setImageCorners(this);
		});

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

	$("h6").addClass("rnd").prepend('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>');

	$("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;
	});
	$(".eventGroup:first").each(function () {
		$(this).find("a.button").addClass("open");
		$(this).find("div.events").removeClass("hidden");
	});
	$(".eventGroup a.button").click(function() {
		$eventGroup = $(this).parent();
		$events = $eventGroup.find("div.events");
		if ($events.hasClass("hidden")) {
			$(".eventGroup .events").addClass("hidden");
			$(".eventGroup a.button").removeClass("open");
			$events.removeClass("hidden");
			$(this).addClass("open");
		}
		else {
			$(this).removeClass("open");
			$events.addClass("hidden");
		}
		return false;
	});
	
	$(".logbox .logNavigation a").click(function () {
		if (!$(this).hasClass("active")) {
			log("clicked: " + $(this).text());
			var $nav = $(this).closest(".logNavigation");
			$nav.find("a").removeClass("active");
			$(this).addClass("active");
			var category = findBracketValue($(this).attr("class"), "category");
			var $items;
			if (category == '-1') {
				$items = $(this).closest(".logbox").find(".item");
				$items.each(function(i) {
					if (i < 10)
						$(this).removeClass("hidden");
					else
						$(this).addClass("hidden");
				});
			}
			else {
				$(this).closest(".logbox").find(".item").addClass("hidden");
				$items = $(this).closest(".logbox").find(".\\{category\\:" + category + "\\}");
				$items.each(function(i) {
					if (i <= 10) $(this).removeClass("hidden");
				});
			}
			log("items length: " + $items.length);
			if ($items.length < 10)
				$(this).closest(".logbox").find("a.bottomButton[href='#']").css("display", "none");
			else
				$(this).closest(".logbox").find("a.bottomButton[href='#']").css("display", "block");
			updateIEImageSizes();
		}
		else {
			log("already active");
		}
		return false;
	});
	
	$(".logbox a.bottomButton[href='#']").click(function () {
		var category = findBracketValue($(this).closest(".logbox").find(".logNavigation a.active").attr("class"), "category");
		log("show 10 more, category: " + category);

		var more = 10;
		if (category == '-1') {
			more = $(this).closest(".logbox").find(".item:not(.hidden)").length + 10;
			$items = $(this).closest(".logbox").find(".item");
		}
		else {
			more = $(this).closest(".logbox").find(".\\{category\\:" + category + "\\}:not(.hidden)").length + 11;
			$items = $(this).closest(".logbox").find(".\\{category\\:" + category + "\\}");
		}
		$(this).closest(".logbox").find(".item").addClass("hidden");
		$items.each(function(i) {
			if ($(this).hasClass("hidden")) {
				$(this).removeClass("hidden");
				--more;
				if (more == 0) return false;
			}
		});
		if (more > 0) $(this).closest(".logbox").find("a.bottomButton[href='#']").css("display", "none");
		return false;
	});
	$(".logbox:first").each(function () {
		var u = document.location.href;
		if (u.indexOf("#") > 0) {
			var c = u.split("#")[1];
			$("a.\\{category\\:" + c + "\\}").click();
		}
	});

	$("a[rel='lightbox']").colorbox();

	$("span.jqIframe").each(function () {
		var width = $(this).width();
		var height = $(this).height();
		var src = $(this).attr("src");
		$(this).css("display", "none");
		log("width: " + width);
		log("height: " + height);
		log("src: " + src);
		$(this).after('<iframe name="iframe" scrolling="no" frameborder="0" src="' + src + '" width="' + width + '" height="' + height + '">.</iframe>');
	});

	if ($.browser.msie && parseInt($.browser.version) == 7) {
		$(".navigationSlides a.item img").click(function () {
			document.location = $(this).closest("a").attr("href");
			return false;
		});
	}

	$(".navigationSliderContainer a.next").click(function () {
		var $s = $(this).closest(".navigationSliderContainer").find(".navigationSlides");
		var l = parseInt($s.css("left").replace("px", "")) - 425;
		$(".navigationSliderContainer a.prev").removeClass("disabled");
		if (l < (-1 * ($s.width() - 850)))
		{
			l = -1 * ($s.width() - 850);
			$(".navigationSliderContainer a.next").addClass("disabled");
		}
		$s.animate({ left: l }, 300);
		return false;
	});
	$(".navigationSliderContainer a.prev").click(function () {
		var $s = $(this).closest(".navigationSliderContainer").find(".navigationSlides");
		var l = parseInt($s.css("left").replace("px", "")) + 425;
		$(".navigationSliderContainer a.next").removeClass("disabled");
		if (l > 0) {
			$(".navigationSliderContainer a.prev").addClass("disabled");
			l = 0;
		}
		$s.animate({ left: l }, 300);
		return false;
	});

	

	$(".logTeaser .bottomSpace").each(function () {
		$(".logTeaser .item").each(function (i) {
			var a = "";
			if (i == 0) a = " active";
			$(".logTeaser .bottomSpace").append('<a href="#" class="dot' + a + '" index="' + i + '"></a>');
		});
		$(this).css("margin-left", 155 - ($(this).find(".dot").length * 22) / 2);
		$(this).find("a.dot").click(function () {
			$(".logTeaser .dot").removeClass("active");
			$(this).addClass("active");
			var idx = $(this).attr("index");
			var l = -idx * 680;
			var $s = $(this).closest(".logTeaser").find(".logSlideContainer");
			$s.animate({ left: l }, 300);
			clearTimeout(logSlider);
			logSlider = setTimeout(showNextLogTeaserItem, logSlideSpeed * 1000);
		});
		logSlider = setTimeout(showNextLogTeaserItem, logSlideSpeed * 1000);
	});
	

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

function showNextLogTeaserItem() {
	var idx = parseInt($(".logTeaser:first .active").attr("index"));
	var c = $(".logTeaser:first .dot").length - 1;
	var n = idx + 1;
	log("n: " + n);
	log("c: " + c);
	if (n > c) n = 0;
	$(".logTeaser:first .dot:eq(" + n + ")").click();
	clearTimeout(logSlider);
	logSlider = setTimeout(showNextLogTeaserItem, logSlideSpeed * 1000);
}

function updateIEImageSizes() {
	$("span.rnd:has(img)").each(function () {
		if ($(this).css("width") == "0px") $(this).css("width", $(this).find("img").width() + 'px');
		if ($(this).css("height") == "0px") $(this).css("height", $(this).find("img").height() + 'px');
	});
}

function findBracketValue(s, f)
{
	var start = s.indexOf("{" + f + ":");
	if (start >= 0) {
		var end = s.indexOf("}", start);
		return s.substring(start + f.length + 2, end);
	}
	else
	{
		return "";
	}
}

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")
		}
	}
}

var imageCornersRetryCount = 0;
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");

	if (height == 0 && imageCornersRetryCount < 50) {
		imageCornersRetryCount++;
		setTimeout(function () {setImageCorners(e)}, 300);
		log("image height was reported as 0, retrying in 300ms");
		return true;
	}
 
	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')
	}

	if (isIE6)
	{
		var ieWrap = $('<span class="outerRnd"></span>').width(width).height(height);
		ieWrap.css('float', wrapper.css('float'));
		wrapper.css('float', 'none')

		ieWrap.css('margin-right', wrapper.css('margin-right'));
		wrapper.css('margin-right', '0')

		ieWrap.css('margin-left', wrapper.css('margin-left'));
		wrapper.css('margin-left', '0')

		ieWrap.css('margin-bottom', wrapper.css('margin-bottom'));
		wrapper.css('margin-bottom', '0')

		ieWrap.css('margin-top', wrapper.css('margin-top'));
		wrapper.css('margin-top', '0')
		img.wrap(ieWrap);
	}

	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
	{
		if ($(e).parents().find(".path, .nav").length == 0) $(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);
}

