Cufon.replace("p.tagline,body>.header h2,body>.header .two-columns h3,body>.content .three-columns h4,.projects h3,body>.content h2,body>.content h3");
Cufon.replace("body>.header ul.menu li", { hover: true });

$("a.contrast").click(function() { $("body").toggleClass("hc"); createCookie("style", $("body").hasClass("hc") ? "hc" : "", 365); return false; });
if (readCookie("style") == "hc" && !$("body").hasClass("hc"))
	$("a.contrast").click();


var
	intro = null
,	introTimer = null
,	introAuto = true
,	introPos = 0
,	introItems = 5
,	introSlideInTime = 2000
,	introSlideOutTime = 1000
,	introItemTime = 13000
,	introRulerStep = Math.round(930 / introItems)
,	introNavH = 0
,	introContentH = 0
,	introW = 0
,	pl = $("body").hasClass("pl")
;


function introStop() { if (introTimer) { clearTimeout(introTimer); introTimer = null; } }


function introPause(dir)
{
	introStop();
	introAuto = !introAuto;
	if (introAuto)
		introPlay();
}


function introPlay() { introStop(); introTimer = setTimeout("introChange()", introItemTime); }


function introChange(dir, manual)
{
	if (!dir)
		dir = 1;

	if (manual)
		introStop();

	var next = intro.find("> .content.current").stop(true).animate({ left: (-dir * introW) + "px" }, introSlideOutTime).removeClass("current");
	next = (dir < 0) ? next.prev(".content") : next.next(".content");
	if (!next.length)
	{
		next = (dir < 0) ? intro.find("> .content:last") : intro.find("> .content:first");
		introPos = (dir < 0) ? introItems - 1 : 0;
	}
	else
		introPos += dir;

	var ruler = intro.find(".ruler > a");
	ruler.stop(true).animate({ left: (introPos * introRulerStep) + "px"}, introSlideInTime);
	if (!next.is(":animated"))
		next.css("left", (dir * introW) + "px");
	next.addClass("current").show().stop(true).animate({ left: "0px" }, introSlideInTime);
	ruler.find("span").text(next.find(".info > span").text());
	if (introAuto)
		introPlay();
}


function introChangeTo(item)
{
	var current = intro.find("> .content.current");
	dir = (item > introPos) ? 1 : -1;
	if (introPos != item)
		current.stop(true).animate({ left: (-dir * introW) + "px" }, introSlideOutTime).removeClass("current");
	next = intro.find("> .content:eq(" + item + ")");
	var ruler = intro.find("> .ruler > a");
	ruler.stop(true).animate({ left: (item * introRulerStep) + "px"}, introSlideInTime);
	if (introPos != item)
		next.addClass("current").css("left", (dir * introW) + "px").show().stop(true).animate({ left: "0px" }, introSlideInTime);
	ruler.find("span").text(next.find(".info > span").text());
	introPos = item;
}


var tooltip = null;


function tooltipShow(element, text)
{
	tooltip.find("span").text(text);

	var IEOffset = { left: 0 };
	if ($.browser.msie)
		IEOffset = $("body").offset();
	var offset = element.offset();

	tooltip.css({ left: (offset.left + element.width() - IEOffset.left - 10) + "px", top: (offset.top + (element.outerHeight() - tooltip.outerHeight()) / 2) + "px" }).show();
}


var tooltipAlt = null;


function tooltipAltShow(element, text)
{
	var i = text.indexOf(":");
	tooltipAlt.find("span").empty().append("<strong>" + text.substring(0, i + 1) + "</strong> " + text.substring(i + 1));

	var IEOffset = { left: 0 };
	if ($.browser.msie)
		IEOffset = $("body").offset();
	var offset = element.offset();

	tooltipAlt.css({ left: (offset.left + element.width() - IEOffset.left) + "px", top: (offset.top + (element.outerHeight() - tooltipAlt.outerHeight()) / 2) + "px" }).show();
}


/*
** site map magix
*/

$("#site-map:first").clone().prependTo("body");
$(".site-map:last").remove();
var sitemap = $("#site-map");
var sitemapH = -sitemap.outerHeight();
sitemap.css("margin-top", sitemapH + "px").hide();

$("a[href=#site-map]").click(function()
{
	if (sitemap.is(":animated"))
		return false;
	if (sitemap.is(":visible"))
		sitemap.stop(true).animate({ marginTop: sitemapH + "px" }, 750, "swing", function() { $(this).hide(); });
	else
		sitemap.show().stop(true).animate({ marginTop: "0px" }, 750, "swing");
	return false;
});


/*
** tooltip
*/
if ($(".tooltip-alt").length)
{
	$("body").append("<p id=\"tooltip-alt\"><span></span></p>");
	tooltipAlt = $("#tooltip-alt").hide();
	$(".work .work img").hover(function() { tooltipAltShow($(this), $(this).attr("alt")); }, function() { tooltipAlt.hide(); });
}


/*
** intro stuff
*/
intro = $(".intro");
if (intro.length)
{
	$.getJSON(pl ? "data/intro_pl.js" : "data/intro.js", function(json)
	{
		intro.addClass("intro-scripted");
		var introNav = intro.find("> .navigation > ul");

		// data loaded from JSON, now inject it to the HTML
		for (var i = 0; i < introItems; i++)
		{
			var item = intro.find("> .content:first").clone().insertBefore(intro.find("> .ruler"));
			item.find("> .photo img").attr("src", "data/intro/" + json.items[i].file + ".jpg").attr("alt", json.items[i].info);
			item.find("> h2").html(json.items[i].title);
			var info = item.find("> .info");
			info.find("> span").text(json.items[i].info);
			if (json.items[i].visit)
				info.find("> a").attr("href", json.items[i].visit).text(pl ? "Odwiedź" : "Selengkapnya");
			else
				info.find("> a").attr("href", json.items[i].work).text(pl ? "Zobacz projekt" : "See project");
			info.find("~ p").remove();
			for (t in json.items[i].text)
				item.append("<p>" + json.items[i].text[t] + "</p>");
		}

		Cufon.replace(".intro h2");

		intro.find("> .content:first").remove();

		// make navigation work
		introNav.find("> li:first").after("<li><a class=\"pause\" href=\"#\">|| / [&gt;</a></li>");
		introNavH = -introNav.outerHeight() - intro.find("> .navigation > p").outerHeight();
		introNav.css("margin-top", introNavH + "px").hide();
		intro.find("> .navigation")
			.bind("mouseenter", function() { introNav.show().stop(true).animate({ marginTop: "0px" }, 350, "swing"); })
			.bind("mouseleave", function() { introNav.stop(true).animate({ marginTop: introNavH + "px" }, 150, "swing", function() { $(this).hide(); }); });

		// content sliding
		introContentH = 0;
		introW = intro.find("> .content:first").addClass("current").outerWidth();
		intro.find("> .content").each(function() { introContentH = Math.max($(this).outerHeight(), introContentH); }).not(":first").hide();
		// intro.height(introContentH + 30);
		intro.height(220);

		intro.append("<div class=\"fade-left\"></div><div class=\"fade-right\"></div>");
		intro.find("> .fade-left, > .fade-right").height(intro.find("> .content").outerHeight());

		// handle clicking on previous and next buttons
		intro.find("> .navigation a.previous").click(function() { introChange(-1, true); return false; });
		intro.find("> .navigation a.next").click(function() { introChange(1, true); return false; });
		intro.find("> .navigation a.pause").click(function() { $(this).toggleClass("play"); introPause(); return false; });

		intro.find("> .ruler").css("padding-top", (introContentH + 10) + "px").find("a").css("left", (introPos * introRulerStep) + "px")
			.bind("dragstart dragend", function()
			{
				$(this).stop(true).toggleClass("drag");
				introStop();
			})
			.bind("drag", function(event)
			{
				var offset = $(this).parent().offset();
				var max_x = (introItems - 1) * introRulerStep;
				var x = Math.min(max_x, Math.max(0, event.offsetX - offset.left - 34));
				$(this).css("left", x + "px");
				x = Math.round(x / introRulerStep);
				$(this).find("span").text(intro.find("> .content:eq(" + x + ") .info > span:first").text());
			})
			.bind("dragend", function(event)
			{
				var offset = $(this).parent().offset();
				var max_x = (introItems - 1) * introRulerStep;
				var x = Math.round(Math.min(max_x, Math.max(0, event.offsetX - offset.left - 34)) / introRulerStep);
				introChangeTo(x);
				if (introAuto)
					setTimeout("introPlay()", introSlideInTime + 1000);
			});

		introPlay();

		if ($.browser.msie && $.browser.version < 7)
			intro.find("> .ruler > a").attr("href", "#").click(function() { return false; });
	});
}


var work = $(".work");
if (work.length)
{
	var file = $(".content > .location > strong").attr("class");
	work.find(".screenshot .right a").removeClass("selected").each(function(i)
	{
		$(this).attr("href", "#screenshot-" + i);
		$(this).parents(".screenshot").prepend("<p id=\"screenshot-" + i + "\"><img src=\"data/work/" + file + "_" + i + ".jpg\" alt=\"\"></p>");

		$(this).click(function(event, fast)
		{
			if ($(this).hasClass("selected"))
				return false;

			$(this).parent().parent().find("a").removeClass("selected");
			$(this).addClass("selected");

			if (fast)
			{
				$(this).parents(".screenshot").children("p:visible").hide();
				$($(this).hrefId()).show();
			}
			else
			{
				$(this).parents(".screenshot").children("p:visible").fadeOut(500);
				$($(this).hrefId()).fadeIn(500);
			}

			return false;
		});
	});

	var loadingText = $("body").hasClass("pl") ? "Pobieranie danych, proszę czekać…" : "Loading image, please be patient…";

	function positionWorkOverlay()
	{
		var $o = $(".overlay");
		var $w = $(".work-big");
		$w.children("img").removeAttr("style");
		$w.width($w.children("img").width()).css({ marginLeft: -($w.outerWidth() / 2) + 'px' });

		if ($w.outerHeight() < $(document).height())
			$w.css("top", (($(document).height() - $w.outerHeight()) / 2) + "px");

		if ($.browser.msie && $.browser.version == 6)
			$('.overlay').height($(document).height())
	}

	work.find(".screenshot>div>p>a:first").click(function()
	{
		$("body").prepend("<div class=\"overlay\"></div><p class=\"work-big\"><span>" + loadingText + "</span> <img style=\"position: absolute; left: -10000px\" onload=\"positionWorkOverlay();\" src=\"" + $(this).attr("href") + "\" alt=\"\"></p>");

		var $o = $(".overlay").css({ opacity: 0 }).fadeTo(500, .75);

		if ($.browser.msie && $.browser.version == 6)
			$o.height($(document).height());

		var $w = $(".work-big").css({ opacity: 0 }).animate({ opacity: 1 }, 500);
		$o.add($w).click(function() { $o.add($w).unbind("click").fadeOut(500, function() { $(this).remove(); }); });

		return false;
	});

	work.find(".screenshot > p").hide();
	work.find(".screenshot .right a:first").trigger("click", true);
}


var testimonials = $(".testimonials");
if (testimonials.length)
{
	testimonials.append("<ul class=\"numbers\"></ul>");
	var h = 0;
	testimonials.find("> ul:not(.numbers) > li").each(function(i)
	{
		h = Math.max(h, $(this).outerHeight());
		$(this).attr("id", "testimonial-" + i);
		testimonials.find(".numbers").append("<li><a href=\"#testimonial-" + i + "\">" + (i + 1) + "</a></li>");
	});
	testimonials.find("> ul:not(.numbers)").height(h);

	testimonials.find(".numbers a").click(function(event, fast)
	{
		if ($(this).hasClass("selected"))
			return false;

		$(this).parents(".numbers").find("a.selected").removeClass("selected");
		$(this).addClass("selected");

		if (fast)
		{
			$(this).parents(".testimonials").find("> ul:not(.numbers) > li:visible").hide();
			$($(this).hrefId()).show();
		}
		else
		{
			$(this).parents(".testimonials").find("> ul:not(.numbers) > li:visible").fadeOut(500);
			$($(this).hrefId()).animate({ opacity: "show" }, 500, "linear", function() { if ($.browser.msie && $.browser.version == 7) this.style.removeAttribute("filter"); });
		}

		return false;
	});

	testimonials.find(".numbers a:first").trigger("click", true);
}


$(".welcome p:first-child").append("<a class=\"close\">X</a>");
$(".welcome a.close").click(function() { $(this).parent().height($(this).parent().height()).animate({ opacity: "hide" }, 1000, "linear", function() { $(this).css({ display: "block", visibility: "hidden" }); $(this).parent().animate({ height: 0, paddingTop: 0, paddingBottom: 0 }, 1000, "swing", function() { $(this).remove(); }); }); return false; });


/*
** Google map
*/
var googleMap = $(".google-map");
var map;
if (googleMap.length)
{
	map = new GMap2(googleMap.empty()[0]);
	var center = new GLatLng(52.396567, 16.923923);
	map.setCenter(center, 15);
	map.addOverlay(new GMarker(center));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());
	$(window).unload(GUnload);
}


var $twitter = $("ul.three-columns p.twitter");
if ($twitter.length)
{
	$.getJSON("http://www.twitter.com/statuses/user_timeline/merix_studio.json?count=1&callback=?", function(data)
	{
		if (data[0] && data[0].text && data[0].text.length)
		{
			data[0].text = data[0].text.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '<a href="$&">$&</a>');
			$twitter.html(data[0].text);
		}
		else
			$twitter.text("Error loading content.");
	});

}


if ($.isFunction($("input.date").DatePicker))
	$("input.date").DatePicker
	({
		eventName: "focus"
	,	date: ""
	,	onBeforeShow: function() { $("input.date").DatePickerSetDate($("input.date").val(), true); }
	,	onChange: function(formated, dates) { $("input.date").val(formated); }
	});


$("div.more").each(function() { $(this).prev().append(" <a class=\"show-more\">rozwiń</a>").find("a.show-more").click(function() { $(this).parent().next(".more").show(); $(this).fadeOut(2000, function() { $(this).remove(); }); return false; }); });


if ($.browser.msie)
	$("a:not([href])").attr("href", "#");
