var links = new Object();

function showPicture( id, link ) {
	var pos = $(link).position();
	var lid = links[id];
	
	$("#float").html("<img src=\""+lid+"\" alt=\"\" style=\"floatingimage\" />");
	var lwid = $(link).width();
	var dwid = $("#float").width();
	var diff = Math.abs(lwid-dwid)/2;
	var docw = $(document).width();
	
	var left = pos["left"] + (lwid > dwid ? diff : -1*diff);
	if( left + dwid > docw ) {
        delta = (left+dwid - docw);
		left = left - (2*delta) - 3; }
	if( left < 0 ) {
		left = 3;}
	
	$("#float").css("left", left);

	var dhei = $("#float").height();
	var coef = dhei > 175 ? .64 : .47; //Portrait vs Landscape
	$("#float").css("top", pos["top"]-(dhei*coef));

	$("#float").show();
}

function hidePicture( ) {
	$("#float").hide();
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
    	do {
    		curleft += obj.offsetLeft;
    		curtop += obj.offsetTop;
    	} while (obj = obj.offsetParent);
    }
	return [curleft,curtop];
}


function loadLinks() {
	var xmldoc = $.parseXML($.ajax({
        url:"http://"+document.domain+"/docs/links.xml",
        cache: false,
        async: false
	}).responseText);

	$(xmldoc).find("link").each( function( i ) {
        links[""+$(this).attr("id")] = $(this).attr("href").replace("/s800/", "/s256/");
	} );
}

function updateMouseFunctions() {
    $("a").each( function ( k ) {

        var href = $(this).attr("href");
        var io = -1;
        if( href == undefined || (io = href.indexOf("&photo=")) < 0)
           	return;

		var id = href.substring(io+7);

		$(this).attr("onMouseOver", "showPicture(\""+id+"\", this);");
		$(this).attr("onMouseOut", "hidePicture();");
	});
}

function rewriteLinks( ) {
	var children = blogroll.getElementsByTagName("img");
	var height = window.innerHeight-100;
	var usewidth = Math.ceil(1.5 * height);
	for( var i = 0; i < children.length; i++ ) {
        children[i].setAttribute("onclick", "setNext()");
		children[i].src = children[i].src.replace("/s800/", "/s"+usewidth+"/");
		children[i].height = height;
		//children[i].width = usewidth;
	}

	/*children = blogroll.getElementsByTagName("li");
	for ( var j = 0; j < children.length; j++ ) {
        children[i].setAttribute("id", "photo"+(j+1));
   	}*/
}

loadLinks();
$(document).ready(updateMouseFunctions);
$("#float").hide();
