dojo.require("dojo.parser");
var showPreviewWindow = false;

function showMinMaxError() {
	alert('We did not understand the value(s) you entered for the minimum and maximum total cost field. Please modify your query and try again. ');
}

function previewPageHandler(response) {

	p = dojo.byId("previewpage_div");
	if (p != null) {
		if (showPreviewWindow) {
			p.innerHTML = response;
			p.style.display = "block";
		}
	}
}

function hidePreviewPage() {
	showPreviewWindow = false;
	p = dojo.byId("previewpage_div");
	if (p) {
		p.style.display = "none";
	}
}
function hideThumbnail() {
	p = dojo.byId("thumbnail_div");
	if (p) {
		p.style.display = "none";
	}
}

 
var large_image = null;

function showLargeImage(thumbnail) {
		var imgsrc = thumbnail.src.replace("/t2/","/o/");
		var x = findPosX(thumbnail);
		var y = findPosY(thumbnail);
		large_image = new Image();

		f = function () {

        p = dojo.byId("thumbnail_div");
        
        if (p) {
          var iw = '';
          if (large_image.width) {
             if (large_image.width > 250) {
                iw = ' width="250px" ';
             }
           } else if (large_image.height) {
             if (large_image.height > 250) {
                iw = ' height="250px" ';
             }
           }
          p.innerHTML = '<img style="border: solid 1px #aaaaaa;" ' + iw + ' align="left" src="' + large_image.src + '">';
          
              var myWidth = 0, myHeight = 0;
              if( typeof( window.innerWidth ) == 'number' ) {
                 myWidth = window.innerWidth;
                 myHeight = window.innerHeight;
                 }
              else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                 myWidth = document.documentElement.clientWidth;
                 myHeight = document.documentElement.clientHeight;
                 }
              else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                 myWidth = document.body.clientWidth;
                 myHeight = document.body.clientHeight;
                 }
              var scrOfX = 0, scrOfY = 0;
              if( typeof( window.pageYOffset ) == 'number' ) {
                 scrOfY = window.pageYOffset;
                 scrOfX = window.pageXOffset;
                 }
              else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                 scrOfY = document.body.scrollTop;
                 scrOfX = document.body.scrollLeft;
                 }
              else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
                 scrOfY = document.documentElement.scrollTop;
                 scrOfX = document.documentElement.scrollLeft;
                 }

              if ((y + p.clientHeight) > (myHeight + scrOfY)) {
                 y = y - ((y + p.clientHeight) - (myHeight + scrOfY)) ;
                 }
              if (p.clientHeight > myHeight ) {
                 y = scrOfY ;
                 }

              if (y < scrOfY)
                y = scrOfY;
          p.style.left = (x + 120) + 'px';
          p.style.top = y +'px';
          p.style.display = "block";
        }

		}
		
		if (dojo.isIE) {
		   large_image.onreadystatechange = f
		} else {
       large_image.onload = f
		}
		large_image.src = imgsrc;
		
}

function showPreviewPage(aE, link, page) {
	showPreviewWindow = true;
	e = dojo.byId(aE);
	if (e) {
		y = findPosY(e);
		
		p = dojo.byId("previewpage_div");
		if (p) {
	        var myWidth = 0, myHeight = 0;
	        if( typeof( window.innerWidth ) == 'number' ) {
	           myWidth = window.innerWidth;
	           myHeight = window.innerHeight;
	           }
	        else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	           myWidth = document.documentElement.clientWidth;
	           myHeight = document.documentElement.clientHeight;
	           }
	        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	           myWidth = document.body.clientWidth;
	           myHeight = document.body.clientHeight;
	           }
	        var scrOfX = 0, scrOfY = 0;
	        if( typeof( window.pageYOffset ) == 'number' ) {
	           scrOfY = window.pageYOffset;
	           scrOfX = window.pageXOffset;
	           }
	        else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	           scrOfY = document.body.scrollTop;
	           scrOfX = document.body.scrollLeft;
	           }
	        else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	           scrOfY = document.documentElement.scrollTop;
	           scrOfX = document.documentElement.scrollLeft;
	           }

	        if ((y + p.clientHeight) > (myHeight + scrOfY)) {
	           y = y - ((y + p.clientHeight) - (myHeight + scrOfY)) ;
	           }
	        if (p.clientHeight > myHeight ) {
	           y = scrOfY ;
	           }

                y = y + 100;
	        if (y < scrOfY)
	        	y = scrOfY + 100;

	        p.style.left = (findPosX(dojo.byId(aE)) - 580) + 'px';
			p.style.top = (scrOfY + 25) + 'px';
		}
		
		dojo.xhrGet( {
			url: page,
			sync : true,
			handle: previewPageHandler
		});
	}
}

function clear_default(e) {
	if ((e.value == '$min') || (e.value == '$max')) {
		e.value = '';
	}
}
function set_default(e, v) {
	if (e.value.length == 0) {
		e.value = v;
	}
}

function findPosX(obj) {
   var curleft = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curleft += obj.offsetLeft;
         obj = obj.offsetParent;
         }
      }
   else if (obj.x)curleft += obj.x;
   return curleft;
   }
function findPosY(obj) {
   var curtop = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curtop += obj.offsetTop;
         obj = obj.offsetParent;
         }
      }
   else if (obj.y)curtop += obj.y;
   return curtop;
   }
