function condor_init() {
	$(function () {
		Shadowbox.init({
			animSequence: "sync",
			continuous: true,
			counterType: "skip",
			skipSetup: true,
			onOpen: function() { $("#pager").cycle("pause"); },
			onClose: function() { $("#pager").cycle("resume"); }
		});

		$('#pager').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 10000, 
			pager:  '#pagerthumbnails',
			pagerAnchorBuilder: pagerAnchorBuilder,
			after: pagerFadeInCurrent
		});

		pagerFadeInCurrent();

		$("#language a").not(".active").fadeTo(1, 0.5);
		$("#language a").not(".active").hover(function () {
			$(this).fadeTo(100, 0.8);
		}, function () {
			$(this).fadeTo(100, 0.5);
		});

		$("#pagerthumbnails a.ptn").hover(function () {
			if(!$(this).hasClass("activeSlide"))
				$(this).fadeTo(100, 0.8);
		}, function () {
			if(!$(this).hasClass("activeSlide"))
				$(this).fadeTo(100, 0.5);
		});

		var hash = location.hash.split("/");

		if(location.hash.length > 0) {
			var current_page = "work";
			var params = { };
			if(hash.length > 0) {
				current_page = hash[0];
			}
			if(current_page.substr(0, 1) == "#") {
				current_page = current_page.substr(1);
			}
			if(current_page.length == 0) {
				current_page = "work";
			}

			if(hash.length >= 2) {
				if(current_page == "work") {
					current_page = "work_item";
					params = { "short": hash[1], "update_pager": true };
				} else {
					params = { "id": hash[1] };
				}
			}

			load_page(current_page, params);
		}
	});
}

function pagerFadeInCurrent(c,n,o,f) {
	$("#pagerthumbnails a.ptn").blur();
	$("#pagerthumbnails a.ptn").not(".activeSlide").fadeTo("fast", 0.5);
	$("#pagerthumbnails .activeSlide").fadeTo("fast", 1);
}

function pagerAnchorBuilder(i, el) {
	var src = $(el).children("img")[0].src;
	if(src.substr(src.length - 9) == "pager.jpg") {
		src = src.substr(0, src.length - 9) + "40.jpg";
	}
	return $('<a id="ptn_' + i + '" class="loader ptn" href="javascript:;"><img src="' + src + '"/></a>');
}

function load_page(page, params) {
	var pageslug = slug(page);
	if(pageslug == "workitem") pageslug = "work";
	$("#menu a").removeClass("active");
	$("#tab_" + pageslug).blur().addClass("active").addClass("loadersmall");
	$("#footer").fadeOut("fast");
	$("#page").fadeOut("fast", function () {
		$.get("page.php?page=" + page, params, function(data) {
			$("#page").html(data);	
			$("#footer").fadeIn("fast");
			$("#page").fadeIn("fast", function() {
				$("#tab_" + pageslug).removeClass("loadersmall");
			});
		}, "html");
	})
}

function slug(str) {
	var allowed = "abcdefghijklmnopqrstuvwxyz0123456789-";
	var out = "";
	var c = "";
	var l = "";
	for(i = 0; i < str.length; i++) {
		c = str[i];
		if(c == " ") c = "-";
		if(allowed.indexOf(c) !== -1) {
			if(!(c == "-" && l == "-")) {
				out = out + c;
			}
		}
		l = c;
	}
	return out;
}

function show_work(id, suppress_pager) {
	if(!suppress_pager) {
		for(var i = 0; i < _latest_work_ids.length; i++) {
			if(_latest_work_ids[i] == id) {
				load_pager_images(id, true);
				break;
			}
		}
	}
	var params = { id: id };
	load_page("work_item", params);
}

function show_video(id) {
	var filename = $("#video_" + id).attr("href");
	var title = $("#video_" + id).attr("title");
	Shadowbox.open({
		content: filename,
		player: 'flv',
		title: title,
		width: 720,
		height: 540
	});
	return false;
}

function show_image(id) {
	var filename = $("#image_" + id).attr("href");
	var title = $("#image_" + id).attr("title");
	Shadowbox.open({
		content: filename,
		player: 'img',
		title: title,
		width: 1000,
		height: 328
	});
	return false;
}

function load_pager_images(id, suppress_work) {
	if(!suppress_work)
		show_work(id, true);

	// Remove the pager and thumbnail images so the loader is shown
	for(i = 0; i < 5; i++) {
		$("#pager .loader").children().remove();
		$("#pagerthumbnails .loader").children().remove();
	}
	// Get the info about the latest work and show it
	$.get("work_get_pager_info.php", {id: id}, function (data) {
		$("#latestwork a").blur().removeClass("active");
		$("#latestwork #lw_" + id).addClass("active");
		for(i = 0; i < 5; i++) {
			preload_image($("#pager_" + i), "work/" + data.short + "/" + data.images[i] + ".pager.jpg", true);
			preload_image($("#ptn_" + i), "work/" + data.short + "/" + data.images[i] + ".40.jpg", false);
			$("#pager_" + i).unbind().click(function() { load_page("work_item", {id: id}); location = "#work/" + data.short; });
		}
		var onclick = function () {
			 location="#work/" + data.short;
			 load_page("work_item", {id: id});
		};
		$("#pagerthumbnails .title").attr("onclick", "").click(onclick).html(data.title);
		$("#pagerthumbnails .subtitle").attr("onclick", "").click(onclick).html(data.subtitle);
	}, "json");
}

function preload_image(el, src, fade) {
	var img = new Image();
	if(fade) $(img).hide();
	$(el).children().remove();
	$(img).load(function () {
		$(el).html(img);
		if(fade) $(img).fadeIn();
	})
	.attr('src', src);
}
