// JavaScript Document

function findPos(obj) { // returns the x and y of any element on the page
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
	//alert(x+","+y);
    return [x, y];
}
           
function setScrollXY(x, y) {
    window.scrollTo(x, y);
}
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //alert(myWidth+","+myHeight);
  return [myWidth,myHeight];
}

bubbleImages = new Array();
function loadImage(imgSrc,afterFunc,args){
	//var i = bubbleImages.length;
	if(bubbleImages[imgSrc]) bubbleImages[imgSrc].onload();
	else {
		bubbleImages[imgSrc] = new Image();
		bubbleImages[imgSrc].src = imgSrc;
		bubbleImages[imgSrc].onload = function(){
			afterFunc(bubbleImages[imgSrc],args);	
		}
	}
}

bubbleTween = false;

function animateBubble(imgObj,argObj){
	
	var bubble = document.getElementById("bubble");
	
	if(bubbleTween) {
		bubbleTween.cancel();
		//bubbleTween.stop();
	}
	
	bubble.style.opacity="0";
	bubble.style.padding="0px";
	
	scrollPos = getScrollXY();
	//alert("scrollY = "+scrollPos[1]);
	windowSize = getWindowSize();
	bubble.style.top=scrollPos[1]+(windowSize[1]/2)+"px";;
	
	bubble.innerHTML = "<img src='"+argObj.src+"' />";
	
	// Requires Scriptaculous
	bubbleTween = new Effect.Morph('bubble',{style:'margin-left:-'+(imgObj.width/2-45)+'px;margin-top:-'+imgObj.height/2+'px;height:'+imgObj.height+'px; width:'+imgObj.width+'px; opacity:100;-moz-opacity:1;', duration:.5, transition:Effect.Transitions.EaseTo});
	
	
}

function showBubble(obj,type){
	// Show Bubble
	var pos = findPos(obj);
	var bubble = document.getElementById("bubble");
	var tnSrc = obj.childNodes[0].src;
	var lgSrc = tnSrc.replace("_tn","_lg");
	lgSrc = lgSrc.replace("/thumbnails/","/large/");	
	
	bubble.style.height="30px";
	bubble.style.width="30px";
	bubble.style.padding="10px";
	bubble.style.opacity="100";
	bubble.style.marginLeft="-25px";
	bubble.style.marginTop="-25px";
	
	bubble.style.visibility = "visible";
	bubble.style.display = "block";
	
	bubble.innerHTML = "<img src='/media/images/layout/loader.gif' />";
	
	loadImage(lgSrc,animateBubble,{src:lgSrc});
	
}
function hideBubble2(){
	var bubble = document.getElementById("bubble");
	bubble.style.visibility = "hidden";
	bubble.style.display = "";
}

function hideBubble(){
	// Animate Open
	if(bubbleTween) bubbleTween.cancel();
	
	/*// Requires Scriptaculous
	bubbleTween = new Effect.Morph('bubble',{style:'margin-left:-'+(width/2-45)+'px;margin-top:-'+height/2+'px;height:'+height+'px; width:'+width+'px; opacity:100;-moz-opacity:1;', duration:.5, transition:Effect.Transitions.EaseTo});*/
	bubbleTween = new Effect.Morph('bubble',{style:'margin-left:0px;margin-top:0px;height:0px; width:0px; opacity:0;-moz-opacity: 0;', duration:.5, afterFinish:hideBubble2, transition:Effect.Transitions.EaseTo });
}

function applyBubbles(){
	var doc = document.getElementsByTagName('div');
	for (var i = 0; i < doc.length; i++){
	   //Do Work on doc[i], this sets the border of the Div black
	   if(doc[i].className=="tn"){
		   doc[i].innerHTML = "<a href='javascript:;' onMouseOver='showBubble(this);' onMouseOut='hideBubble();'>"+doc[i].innerHTML+"</a>";
	   }
	   if(doc[i].className=="tnLarge"){
		   doc[i].innerHTML = "<a href='javascript:;' onMouseOver='showBubble(this,2);' onMouseOut='hideBubble();'>"+doc[i].innerHTML+"</a>";
	   }
	}
}
