// Footer positioning
$(function() {
	var footer = $("#footer");
	var win = $(window);
	var footerHeight = footer.height();
	var fix = function() {
		var footerEnd = footerHeight+footer.position()['top'];
		var winHeight = win.height();
		if(footerEnd <= winHeight) {
			footer.css("padding-top", winHeight - footerEnd);
		}
	}
	$(window).resize(function() {
		fix();
	}).resize();
	$(window).load(function() {
		fix();
	})
})
// News scrolling
window.news = {
	interval: 2000,
	index: 0,
	last: -1,
	init: function() {
		news.container = $(".homepage .news");
		news.container.css({
			"overflow": "hidden",
			"position": "relative"
		});
		news.container.find(".item:not(:first)").hide();
		news.container.find(".item").css({"position":'absolute'}).each(function(i) {
			$(this).attr("rel", i);
		});
		news.container.css({"height":news.container.children(":first").outerHeight()});
		news.show(false);
		$("<img src='/design/molonglo/graphics/news-down.gif' />").appendTo("#newscontrol").click(function() {
			news.index++;
			news.show();
		})
		$("<img src='/design/molonglo/graphics/news-up.gif' />").appendTo("#newscontrol").click(function() {
			news.index--;
			news.show();
		})
	},
	show: function(scrolling) {
		var item = news.container.children("[rel="+news.index+"]");
		if(!item.size() && news.index > 0) {
			news.index = 0;
			return news.show();
		} else if(!item.size() && news.index < 0) {
			news.index = news.container.children(":last").attr("rel");
			return news.show();
		}
		var active = news.container.find(".active");
		
		// resize the container
		news.container.animate({"height":item.outerHeight()});
		
		// moving forward (older)
		if(scrolling == undefined) {
			if(news.last < news.index) {
				// move new item up, from the bottom
				item.show().css("top", active.height()+30).animate({"top":0},function() {
					$(this).addClass("active");
				});
				
				// move active up
				active.animate({top: -active.height()-30},function() {
					$(this).removeClass("active");
					$(this).hide();
				});
			} else {
				// move new item down, from above the top
				item.show().css("top", -item.height()-20).animate({"top":0},function() {
					$(this).addClass("active");
				});
				
				// move active up
				active.animate({top: item.height()+30},function() {
					$(this).removeClass("active");
					$(this).hide();
				});
			}
		} else {
			// no animation, just position the item correctly
			// move new item up, from the bottom
			item.show().css({"top":0}).addClass("active");
			
			// move active up
			active.hide();
		}
		news.last = news.index;
	}
}
window.gal = {
	"init":function() {
		$(document.body).bind("resize.preview",gal.reposition).bind("scroll.preview",gal.reposition);
		$(window).bind("resize.preview",gal.reposition).bind("scroll.preview",gal.reposition);
		
		$(".gallery a img").parent().each(function() {
			$("<img>").attr("src",this.href);
		}).click(function(e) {
		    if($(this).attr("size")) {
		        var size = $(this).attr("size").split("x");
		    } else {
		        var size = null;
		    }
		    gal.show({
		        "title":$(this).children("img").attr("title"),
		        "medium":$(this).attr("href"),
		        "original":$(this).attr("rel"),
		        "size":size
		    },e,this);
		    return false;
		});
	},
	"show":function(photo, e, obj) {
		if($("#galleryZoom").size()) var exists = true;
		gal.hide();
		$(document.body).append("<div style=\"display:none;\" id=\"galleryZoom\"><div class=\"caption\"></div><a class=\"close\" title=\"Close\"></a><img /><div class=\"controls\"><a class=\"prev\">&nbsp;</a><a class=\"next\"></a></div></div>");
		$("#galleryZoom img").attr("src",photo.medium)
		if(photo.size) {
			$("#galleryZoom img").attr("height",photo.size[0]).attr("width",photo.size[1]).attr("title",photo.title);
			$("#galleryZoom").css("width",photo.size[1]);
		}
		if(obj.title) {
			$("#galleryZoom .caption").append("<span>"+obj.title+"</span>");
			if(photo.size) {
				$("#galleryZoom .caption").css("top",photo.size[0]*1).css("top",photo.size[0]*1);
			}
		}
		$("#galleryZoom img, #galleryZoom .close").click(gal.hide);
		gal.reposition(e);
		if(exists) {
			$("#galleryZoom").show();
		} else {
			$("#galleryZoom").fadeIn("fast");
		}
		// next and prev buttons
		if($(obj).next("a").size()) {
			$("#galleryZoom .next").click(function() {
				$(obj).next("a").click();
			});
		} else {
			$("#galleryZoom .next").click(function() {
				$(obj).siblings("a:first").click();
			});
		}
		if($(obj).prev("a").size()) {
			$("#galleryZoom .prev").click(function() {
				$(obj).prev("a").click();
			});
		} else {
			$("#galleryZoom .prev").click(function() {
				$(obj).siblings("a:last").click();
			});
		}
		if(photo.controls === false) {
			$("#galleryZoom .controls").hide();
			$("#galleryZoom .caption").hide();
		}
	},
	"hide":function(e) {
		if(e) {
			$("#galleryZoom").fadeOut("fast",function() {$(this).remove()} );
		} else {
			$("#galleryZoom").remove();
		}
	},
	"reposition":function(event) {
		var obj = $("#galleryZoom");
		if(obj.size() == 0) return true;
		obj.css({
			"top": $(window).scrollTop() + $(window).height()/2 - $("#galleryZoom").height()/2,
			"left": $(window).scrollLeft() + $(window).width()/2 - $("#galleryZoom").width()/2
		});
	}
}
$(function() {
	news.init();
});
$(function() {
	gal.init();
})

