// ====================================================
var dpExplorerTimer;
var dpWinTop;
var dpWinDiv;
var dpShowToolbar=0;
var dpImgHtml;
var dpFirstSSTick = false;
var dpErrorMargin = 10;
var dpImgCache = new Image();
var dpIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;


document.onkeypress=dpKeyCheck;
function dpKeyCheck(ev){
  ev=ev||event;
  if(ev.keyCode==27){
   dpRemovePopUp();
   return false
  }
 }

dpImgCache.onload = function() {
 // When the cache finishes loading, swap the main img with it
 //document.dpImg.src = dpImgCache.src;
 	document.getElementById('dpImage').innerHTML =  dpImgHtml;
 	var imgHeight = document.getElementById('dpImg').offsetHeight;
	var containerHeight = document.getElementById('dpImage').offsetHeight;

	var margin = parseInt((containerHeight  - imgHeight ) / 2);
	//alert( containerHeight + ':' +   imgHeight + ':' + margin);
	//document.getElementById('dpImage').style.marginTop = margin + 'px';
}

function domPopup(){

     //alert( navigator.appName + '\n' + navigator.appVersion);
	this.dpWinSize='IW';  	// FullScreen    FixedWidth   ImageWidth       MIW

    	// review
	this.winTop=0;
	this.winLeft=0;
	this.winHeight=0;
	this.winWidth=0;


     this.winWidthFactor = .8 ;
	this.winHeightFactor = .9;


 	this.view = dpView;

 	this.maxImgWidth=0;
 	this.maxImgHeight = 0;
 	this.winWidthAdjust = 0; //60
	this.winHeightAdjust = 0;    //100
	this.winMinFactor  = .4;

}
var dp = new domPopup;

// ====================================================
function dpView(imgURL,imgWidth,imgHeight,text ,imgID){
var scW; var scH;
var imgHtml;  var html;
var capHeight;

 	var arText = text.split('|');
     var title = arText[0];
     var caption = arText[1];

  	// Make sure the image width & height are ints!
	imgWidth = parseInt(imgWidth);
     imgHeight = parseInt(imgHeight);

 	// Get the available screen size
     scH = f_clientHeight();
     scW = f_clientWidth();

	// Figure out the max image height and width we can get in the popup window
	var maxH = parseInt(scH*.8);
	var maxW = parseInt(scW*.8);
	var factor;
	// and re-factor the image dimensions to fit
	if (imgHeight>maxH) {
       	factor = maxH/imgHeight;
       	imgHeight=parseInt(imgHeight*factor);;
       	imgWidth=parseInt(imgWidth*factor);;
 	}
	if (imgWidth>maxW) {
       	factor = maxW/imgWidth;
       	imgHeight=parseInt(imgHeight*factor);;
       	imgWidth=parseInt(imgWidth*factor);;
 	}

	var arScroll = libGetScrollXY();
	var scrollTop = arScroll[1];

	this.winWidth =  imgWidth  ;
    	this.winHeight = imgHeight   ;

	// Figure out the max image height and width we can get in the popup window
	var maxH = parseInt(this.winHeight*.8);
	var maxW = parseInt(this.winWidth*.8);
	var factor;


	// ERROR MARINSG
	this.winWidth += 2 * dpErrorMargin;
     this.winHeight +=2 * dpErrorMargin
     // WINDOW POS
	this.winLeft=Math.round((scW-this.winWidth)/2);
	this.winTop=Math.round(scH-this.winHeight) / 2;

	// Look for an existing popup div
	var dpWinIsOpen = document.getElementById('domPopup');
     // If popup doesnt exist, create it
	 if (! dpWinIsOpen) {
       	dpWinDiv = document.createElement('div');;
		dpWinDiv.setAttribute('id', 'domPopup');
		document.body.appendChild(dpWinDiv);
  	 }

//	 alert (  "Settings WS:" + this.dpWinSize + "\nImage W: " + imgWidth + " H:" + imgHeight + "\nPopup T:" +   this.winTop +" L:" + this.winLeft +" W:" + this.winWidth +" H:" + this.winHeight);

	// Positioning
	if( document.all  ) dpWinDiv.style.position = "absolute";  	// For IE we use absolute and adjust it via a timer
	else dpWinDiv.style.position = "fixed";   // or else we use 'proper' fixed positioning

	dpWinDiv.style.width = this.winWidth + 'px';
	dpWinDiv.style.height = this.winHeight + 'px';
	dpWinDiv.style.left = this.winLeft + 'px';
	dpWinDiv.style.top = this.winTop + 'px';
	dpWinTop =   this.winTop ;
	// Fix for IE Select elements showing through the 'popup'
	if( document.all  ) dpSelectToggle(false);

	// HTML
	// IMAGE  CONTAINER

     html = '<div id="dpImgContainer" > ';
 	// IMAGE
	html += '<div id="dpImage"></div>';
     html += '</div>';

	// IMAGE
	dpImgHtml = "<div class='dpShadow'> ";
    	dpImgHtml += '<a id="dpClose" href="#" onclick="dpRemovePopUp();return false;"></a><img id="dpImg" width="' + imgWidth + 'px" src="' + imgURL + '">';
	dpImgHtml += "</div> ";
	var ldMarginTop  =   imgHeight/ 2 - 16;
	dpImgLoadingHtml = '<div id="dpImgLoading" style="margin-top:' + ldMarginTop + 'px;"></div>';

	
	document.getElementById('domPopup').innerHTML = html;

	document.getElementById('dpImage').innerHTML =  dpImgLoadingHtml;

    	dpImgCache.src = imgURL;


	// Fix for IE Fixed Pos prob
	if( document.all  ) {

		dpExplorerTimer = window.setInterval(dpExplorerFix, 100);
 	}
	dpHideMain(true);
	return (false);
}


// -------------------------------------
function dpSetImage(){
      document.getElementById('dpImage').innerHTML = dpImgHtml;
}
// -------------------------------------
function dpExplorerFix(){
     var el = document.getElementById('domPopup');
     if (el){
 		var arScroll = libGetScrollXY();
	  	var scrollTop = arScroll[1];
	  	//alert(dpWinTop  + '-' + scrollTop)
        	el.style.top = (dpWinTop  + scrollTop ) + 'px';
     }
     else clearInterval(dpExplorerTimer)
}
// -------------------------------------
function dpHideMain(toggle){

	var el = document.getElementById('main');
	if (toggle) {
  		if (window.ActiveXObject){
            	el.style.filter= "alpha(opacity=33)";     // IE
 
		}
		else if (navigator.appName.indexOf("Netscape")!=-1) {
            	el.style.opacity = .33; // Gecko/Opera
   		}
 	}
	else {
  		if (window.ActiveXObject){
			el.style.filter= "";        // IE
		}
		else if (navigator.appName.indexOf("Netscape")!=-1) {
         		el.style.opacity = 1; // Gecko/Opera
   		}
	}

}
// -------------------------------------
function dpRemovePopUp( ) {
     dpCloseDiv();
     return;
}
// -------------------------------------
function dpCloseDiv(){
	if ( dp.showRunning )  dp.stopShow();
	if( document.all  ) dpSelectToggle(true);
  	if (dpExplorerTimer) clearInterval(dpExplorerTimer);
  	var el = document.getElementById('domPopup');
  	if(el) document.body.removeChild(el);
  	dpHideMain(false);
  	onkeypress=null;
}
// -------------------------------------
function dpSelectToggle(show){
	var theform =  document.forms[0];
	if (theform){
       	for (i=0; i<theform.elements.length; i++){
			if (theform.elements[i].type=="select-one" ){
	            	el = theform.elements[i];
	            	if (show) libElShow(el);
	            	else libElHide(el);
			}
		}
	}
}
// -------------------------------------
function libGetScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) { //Netscape compliant
		scrOfY = window.pageYOffset;
    		scrOfX = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { //DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
  	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	var myArray = [];
  	myArray[0]=   scrOfX;myArray[1]=   scrOfY;
  	return (myArray);    //return [ scrOfX, scrOfY ];
}
// ==============================
function libElHide(el){
if (document.getElementById || document.all) el.style.visibility = 'hidden';
else if (document.layers)  el.visibility = 'hidden';
}
// ==============================
function libElShow(el){
if (document.getElementById || document.all) {
	el.style.visibility = 'visible';
	//el.style.display = 'block';
}
else if (document.layers)  el.visibility = 'visible';
}

function getScrollerWidth () {
var inner = document.createElement('p');
inner.style.width = '100%';
inner.style.height = '200px';

var outer = document.createElement('div');
outer.style.position = 'absolute';
outer.style.top = '0px';
outer.style.left = '0px';
outer.style.visibility = 'hidden';
outer.style.width = '200px';
outer.style.height = '150px';
outer.style.overflow = 'hidden';
outer.appendChild (inner);

document.body.appendChild (outer);
var w1 = inner.offsetWidth;
outer.style.overflow = 'scroll';
var w2 = inner.offsetWidth;
if (w1 == w2) w2 = outer.clientWidth;

document.body.removeChild (outer);

return (w1 - w2);
};
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
