Event.observe(window, "load", initialize, false);

function initialize(){
  if($('dealerResults')){
  	var anchors = $('dealerResults').getElementsByTagName('a');
	  for(var i = 0; i<anchors.length; i++ ){
		  if(anchors[i].className == 'dealerImg'){
		  	anchors[i].onmouseover = function(){createBigImg(this)};
		  	anchors[i].onmouseout = function(){removeBigImg(this)};
	  	}
  	}
  }
}


function findPosition(obj) {
	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 removeBigImg(thisAnchor){
	var thisNode = "";
	for(var n=document.body.childNodes.length - 1;n>=0;n--){
		thisNode = document.body.childNodes[n];
		if(thisNode.className == 'bigImage_popup' || thisNode.className == 'bigImage_shim'){
			document.body.removeChild(thisNode);
		}
	}
}

function createBigImg(thisAnchor){
	var path = thisAnchor.href;
	var thumbImage = thisAnchor.getElementsByTagName("img")[0];
	var thisImage = new Image();
	
	thisAnchor.onclick = function(){return false;}; 
	thisImage.className = "bigImage_popup";
	thisImage.thumbNail = thumbImage;
	thisImage.style.margin = '0px 0px 0px 0px';
	thisImage.style.padding = '0px 0px 0px 0px';
	thisImage.style.position = 'absolute';
	thisImage.style.zIndex = "501";
	thisImage.style.border = '1px solid #000000';
	thisImage.style.display = 'none';
	thisImage.onload = function(){showBigImg(this)};
	thisImage.src=thisAnchor.href;
	document.body.appendChild(thisImage);
}

function showBigImg(thisImage){
	var pageSize = getWindowHeight();
	var pageHeight = pageSize[0];
	var pageOffset = pageSize[1];
	var thumbImage = thisImage.thumbNail;
	var thumbTop = findPosition(thumbImage)[1];
	//var pixelsFromLeft = (findPosition(thumbImage)[0] - 20 - thisImage.width);
	var pixelsFromLeft = findPosition(thumbImage)[0] + 20 + thumbImage.width;
	var pixelsFromtop = thumbTop;
	thisImage.style.left = pixelsFromLeft + "px";

	if((pageHeight - thumbTop) < thisImage.height){
		//theres not enough room under the thumbnail - put it on top
		pixelsFromtop = (thumbTop + thumbImage.height - thisImage.height);
		if((pageOffset + 15) > pixelsFromtop){
			//there is not enough room on top so move it down 15 px from the top
			pixelsFromtop = (pageOffset + 15);
		}
	}
	thisImage.style.top = pixelsFromtop + "px";
	
	//this adds an Iframe shim under the image so it goes over dropdown menus 
	if (document.all){
		var iframeShim = document.createElement("IFRAME");
		iframeShim.className = "bigImage_shim";
		iframeShim.style.position = 'absolute';
		iframeShim.style.left = pixelsFromLeft + "px";
		iframeShim.style.height = thisImage.height;
		iframeShim.style.width = thisImage.width;
		iframeShim.style.zIndex = "500";
		iframeShim.style.top = pixelsFromtop + "px";
		document.body.appendChild(iframeShim);
	}
	thisImage.style.display = '';
}

function getWindowHeight(){
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myHeight = window.innerHeight;
	}else if( document.documentElement && document.documentElement.clientHeight ) {
	//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	}else if( document.body && document.body.clientHeight ) {
	//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	  
	var scrollOffset = 0;
	if(typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
		scrollOffset = window.pageYOffset;
	}else if( document.body && document.body.scrollTop ) {
	//DOM compliant
		scrollOffset = document.body.scrollTop;
	}else if( document.documentElement && document.documentElement.scrollTop ) {
	//IE6 standards compliant mode
		scrollOffset = document.documentElement.scrollTop;
	}
	return [myHeight + scrollOffset, scrollOffset];
}
