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

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


var
	intro = null
,	introTimer = null
,	introAuto = true
,	introPos = 0
,	introItems = 8
,	introSlideInTime = 2000
,	introSlideOutTime = 1000
,	introItemTime = 13000
,	introRulerStep = Math.round(930 / introItems)
,	introNavH = 0
,	introContentH = 0
,	introW = 0
,	pl = jQuery("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 (jQuery.browser.msie)
		IEOffset = jQuery("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 (jQuery.browser.msie)
		IEOffset = jQuery("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
*/

jQuery("#site-map:first").clone().prependTo("body");
jQuery(".site-map:last").remove();
var sitemap = jQuery("#site-map");
var sitemapH = -sitemap.outerHeight();
sitemap.css("margin-top", sitemapH + "px").hide();
 
jQuery("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() { jQuery(this).hide(); });
	else
		sitemap.show().stop(true).animate({ marginTop: "0px" }, 750, "swing");
	return false;
});


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


/*
** intro stuff
*/
intro = jQuery(".intro");
if (intro.length)
{
	jQuery.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ź" : "Visit");
			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() { jQuery(this).hide(); }); });

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

		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() { jQuery(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()
			{
				jQuery(this).stop(true).toggleClass("drag");
				introStop();
			})
			.bind("drag", function(event)
			{
				var offset = jQuery(this).parent().offset();
				var max_x = (introItems - 1) * introRulerStep;
				var x = Math.min(max_x, Math.max(0, event.offsetX - offset.left - 34));
				jQuery(this).css("left", x + "px");
				x = Math.round(x / introRulerStep);
				jQuery(this).find("span").text(intro.find("> .content:eq(" + x + ") .info > span:first").text());
			})
			.bind("dragend", function(event)
			{
				var offset = jQuery(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 (jQuery.browser.msie && jQuery.browser.version < 7)
			intro.find("> .ruler > a").attr("href", "#").click(function() { return false; });
	});
}


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

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

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

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

			return false;
		});
	});

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

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

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

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

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

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

		if (jQuery.browser.msie && jQuery.browser.version == 6)
			jQueryo.height(jQuery(document).height());

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

		return false;
	});

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


var testimonials = jQuery(".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, jQuery(this).outerHeight());
		jQuery(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 (jQuery(this).hasClass("selected"))
			return false;

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

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

		return false;
	});

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


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


/*
** Google map
*/
var googleMap = jQuery(".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());
	jQuery(window).unload(GUnload);
}


var jQuerytwitter = jQuery("ul.three-columns p.twitter");
if (jQuerytwitter.length)
{
	jQuery.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|jQuery)/gim, '<a href="jQuery&">jQuery&</a>');
			jQuerytwitter.html(data[0].text);
		}
		else
			jQuerytwitter.text("Error loading content.");
	});

}


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


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


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

