is_initial = true;

function sel_slide(disable_animation) {
	var a = $("#images").find(".selected").offset().left;
	var w = $("#images").width();
	var ol = $("#images").offset().left;
	var l = parseInt($("#images div").css("left"));
	a = a - l - ol;

	if(disable_animation) $("#images").children("div").css({ "left" : (w/2 - a - 25) + "px" });
	else $("#images").children("div").animate({ "left" :  (w/2 - a - 25) + "px" }, "fast");
}
function next() {
	var tmp = $("#images").find(".selected").next();
	if(tmp.size()) tmp.mouseup();
	else { 
		if(is_slideshow) slideshow();
	}
}
function prev() {
	var tmp = $("#images").find(".selected").prev();
	if(tmp.size()) tmp.mouseup();
}

is_slideshow = false;
to_slideshow = false;
function slideshow() {
	if(is_slideshow) {
		is_slideshow = false;
		if(to_slideshow) clearTimeout(to_slideshow);
		$("#slideshow").text("Play");
	}
	else {
		$("#slideshow").text("Pause");
		is_slideshow = true;
		next();
	}
}

dragdrop = { on : false };

function do_height() {
	h = 520;
	w = 900;
	h = Math.max($(window).height() - 120, h);
	w = Math.max($("#container").width(), w);
	$("#image")
		.width(w)
		.height(h)
		.children(".image")
			.css({
				"maxWidth" : w - 20,
				"maxHeight" : h - 50,
			});
}


$(function () {
	do_height();
	$(window).bind("resize", function () { do_height(); });

	$("#slideshow").click(function (event) { this.blur(); slideshow(); event.preventDefault(); event.stopPropagation(); return false; });
	$("#galleries").css("opacity", 0.7)
		.bind("mouseover", function () {
			$(this).stop().animate({opacity : 1 }, "fast");
		})
		.bind("mouseout", function () {
			$(this).stop().animate({opacity : 0.7 }, "fast");
		});

	$(".item")
		.bind("mouseover", function () {
			if($(this).hasClass("selected")) return;
			$(this).stop().animate({opacity : 0.9 }, "fast");
		})
		.bind("mouseout", function () {
			if($(this).hasClass("selected")) return;
			$(this).stop().animate({opacity : 0.6 }, "fast");
		})
		.live("mouseup", function (event) {
			if(dragdrop.on == true && Math.abs(event.pageX - dragdrop.px) > 5) {
				event.preventDefault();
				event.stopPropagation();
				return false;
			}
			$this = $(this).blur();
			$.address.value("/" + $(this).attr("rel"));
			if(!$this.hasClass("selected") || is_initial) {
				is_initial = false;
				if(to_slideshow) clearTimeout(to_slideshow);
				$("#title").html($this.attr("title") + "&nbsp;");
				$("#image .image:visible:eq(0)").fadeOut("slow");


				if(ie6) $("#image .image:hidden:eq(0)").attr("src", "/images/640x480_" + $this.attr("rel"));
				else $("#image .image:hidden:eq(0)").attr("src", "/images/" + $this.attr("rel"));
				
				
				$this.parent().children(".selected").removeClass("selected").animate({opacity : 0.6 }, "fast").end().end().addClass("selected").animate({opacity : 1 });
				sel_slide();
			}
			event.preventDefault();
			event.stopPropagation();
			return false;
		})
		.live("click", function (event) {
			event.preventDefault();
			event.stopPropagation();
			return false;
		})
		.not(".selected").css("opacity", "0.6");
	$("#images").children("div").width($("#images").find("a").size()*90);
	sel_slide(true);

	$("#image .image")
		.bind("load", function () {
			$(this).css({ marginTop : "-" + ($(this).height()/2 + 6) + "px", marginLeft : "-" + ($(this).width()/2 + 6) + "px" }).fadeIn("slow");
			if(is_slideshow) to_slideshow = setTimeout(function () { next(); }, 4000);
		})
		.bind("click", function () {
			next();
		});

	$("#next").click(function () { next(); });
	$("#prev").click(function () { prev(); });

	$.address.change(function(event) {
		var addr = event.value.replace(/^\//,"");
		$("#images").find("[rel='" + addr + "']").mouseup();
	});

	if(!$.address.value() || $.address.value().length < 5) {
		$("#the_image_1").load();
	}

	$(".item")
		.bind("mousedown", function (event) {
			dragdrop.on = true;
			dragdrop.px = event.pageX;
			dragdrop.ix = parseInt($("#images div").css("left"));
			event.preventDefault();
			event.stopPropagation();
			return false;
		});
	$(document)
		.bind("mousemove", function (event) {
			if(dragdrop.on == true) {
				$("#images div").css("left", dragdrop.ix - (dragdrop.px - event.pageX) + "px");
				event.stopPropagation();
				event.preventDefault();
				return true;
			}
		})
		.bind("mouseup", function () {
			dragdrop.on = false;
		});

	$("#contact").click(function (event) {
		$("body").append("<div id='blend'></div><iframe id='ifr' src='" + $(this).attr("href") + "' frame-border='0'></iframe>");
		$("#blend").css({ height: $("#container").height() + 20, width : $(document).width(), opacity : 0.5, position: "absolute", left : "0px", top: "0px", background : "black" });
		$("#ifr").css({ width: "400px", height: "300px", border : "0", position: "absolute", left : "50%", top: "50%", marginTop: "-150px", marginLeft: "-200px", display : "none", border: "2px solid gray" }).load(function () { $(this).fadeIn(); });

		event.preventDefault();
		event.stopPropagation();
		return false;
	});
});
