var URL = getUrlVars();

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

$(function(){
	$("#gallery-list").each(function(){
		$(this).find(".gallery").click(showGallery);
		$(this).find(".gallery .thumb img").center();
		$(this).find(".gallery .thumb img").load(function(){
			$(this).center();
		});
		if (!isNaN(parseInt(URL["gallery"]))) {
			$(this).find(".gallery[rel="+URL["gallery"]+"]").click();
		}
	});
});

function showGallery() {
	var gid = parseInt($(this).attr("rel"));
	if ( !isNaN(gid)) {
		var gwindow = $("#gallery-window");
		if ( gwindow.length == 0 ) {
			gwindow = $("<div/>").attr("id","gallery-window");
			$("body").append(gwindow);
		}
		$(gwindow).load("/includes/box/gallery.cfm",{ id : gid });
	}
}


$.fn.center = function(parent) {
	if ( !parent )
		parent = $(this).parent();

	if ( $(this).css("position") != "absolute"
		&& $(this).css("position") != "fixed" )
		$(this).css("position","absolute");

	if ( ( parent != window ) && ( parent != document ) ) {
		if ( $(parent).css("position") == ""
			|| $(parent).css("position") == "static"
			|| $(parent).css("position") == "inherit" )
			$(parent).css("position","relative");
	}
		
	$(this).css("top", ( $(parent).height()-$(this).height() )/2 );
	$(this).css("left", ( $(parent).width()-$(this).width() )/2 );
}
