loadingImgFile = "wp-content/images/imgLoading.gif";
errorImgFile = "wp-content/images/imgError.gif";
var loadingImg = new Image();
// loadingImg.src = loadingImgFile;
var errorImg = new Image();
// errorImg.src = errorImgFile;



function calculateWinDims(oW, oH)
{
	var winDims = new Array();
	var imgRatio = oW / oH;

	// reduce popup size to fit the screen
	winDims["w"] = Math.min(oW, screen.availWidth - 100);
	winDims["h"] = Math.min(oH, screen.availHeight - 100);
		
	if ((oW != winDims["w"]) || (oH != winDims["h"])) { 
		// maintain image ratio
		if ((oW - winDims["w"])/imgRatio > (oH - winDims["h"]))
			winDims["h"] = Math.round(winDims["w"] / imgRatio);
		else
			winDims["w"] = Math.round(winDims["h"] * imgRatio);
	}
	
	// accomodate padding and border
	winDims["w"] += 20;
	winDims["h"] += 20;

	return winDims;
}	

function openImagePopup(imgTitle, imgFile) {
	var loading = 0, resize = 1;	
	var x;
	var winDims = new Array();
	var winPos = new Array();
	var img = new Image();
    
	// Set location of opened popup
	winPos["l"] = 20;
	winPos["t"] = 20;
		
    // Attempt to load the target image
    img.src = imgFile;
 
	if (!(img.width && img.height)) {
		// we'll need to resize after loading
		loading = 1;
	}

	winDims = calculateWinDims (img.width, img.height);
	winParams  = 'width=' + winDims["w"] + ' ,height=' + winDims["h"];
	winParams += ' ,resizable=' + resize;
	winParams += ', top=' + winPos["t"] + ' , left=' + winPos["l"];
	
	x = window.open('', '_blank', winParams);
	if( !x ) { 
		return true; 
	}
	
	docContent  = '<html><head>\n';
	docContent += '<title>'+imgTitle+'<\/title>\n'
	if (typeof(baseURL) != "undefined")
		docContent += '<base href="' + baseURL + '" />\n';
	docContent += '<\/head>\n';
	docContent += '<body style="padding:0px; margin:0px;">';
	docContent += '<div style="padding:8px;background:#d0d0d0;border:2px solid #999;" id="imgID">';
	docContent += '<img src="' + imgFile + '" alt="' + imgTitle + '" width="100%" ';
	docContent += 'onerror=\'this.onerror=null; this.src = "' + errorImgFile + '";\' ';
	docContent += '><\/div><\/body>\n<\/html>';
	    
	x.document.open();
	x.document.write(docContent);
	x.document.close();

	if (loading) {
		// resize only if properly loaded by now 
		// (unfortunaltely this never seem to work :( )
		if (img.width && img.height) {
			x.document.getElementById('imgID').src = imgFile;
			winDims = calculateWinDims (img.width, img.height);
			x.resizeTo(winDims["w"], winDims["h"]);
			x.document.write("iw = "+ img.width + "   ih = " + img.height + "\n");
			x.document.write("ww = "+ winDims["w"] + "   wh = " + winDims["h"] + "\n");
			loading = 0;
		}
	}

	if( x.focus ) {
		x.focus(); 
	}
	
	return false;
}



// Video Popup 


function openVidPopup(vidTitle,vidFile, vidW, vidH, download) {
	var resize=1; addControl = 45;
	var winDims = [0,0];
	var winPos = [0,0];
	winPos["t"] = 30;
	winPos["l"] = 30;


// calculate window size
	vidH += addControl;
	winDims["w"] = vidW + 36;
	winDims["h"] = vidH + 70 + (download ? 38: 0);



	winParams  = 'width=' + winDims["w"] + ' ,height=' + winDims["h"];
	winParams += ', top=' + winPos["t"] + ' , left=' + winPos["l"];
	winParams += ' ,resizable=' + resize;
	winParams += ' ,scrollbars=0, titlebar=0, toolbar=0, menubar=0';
	winParams += ', status=0, directories=0,personalbar=0';

	
	x = window.open('','_Blank', winParams);
	x.focus();

	docContent  = '<html><head><title>'+vidTitle+'<\/title>';
	docContent += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8;" />'
	docContent += '<\/head>';
	docContent += '<body style="padding:0px; margin:0px; background:#f0f0f0; direction:rtl;">';
	docContent += '<div style="padding:8px;" id="container">';
	docContent += '<h3 style="font:bold 14px verdana, arial; letter-spacing: 0.05em; ';
	docContent += 'color: #d50; padding:5px 0px 10px 0px; margin:0px; text-align: center">';
	docContent += '&raquo;' + vidTitle + "&laquo;</h3>";
	docContent += '<div style="padding:8px;background:#d0d0d0;border:2px solid #999;" id="vidID">'
	docContent += '<object width="' + vidW + '" height="' + vidH + '">';
	docContent += '<param name="src" value="' + vidFile + '">';
	docContent += '<param name="autoplay" value="true">';
	docContent += '<param name="controller" value="true">';
	docContent += '<param name="bgcolor" value="#999">';
	docContent += '<embed src="' + vidFile + '" autostart="true" loop="false" ';
	docContent += 'width="' + vidW + '" height="' + vidH + '" controller="true" bgcolor="#999">';
	docContent += '</embed></object></div>';
	if (download == 1) {
		docContent += '<p style="font:12px verdana, arial; text-align:right; ';
		docContent += 'padding:10px 0px 10px 0px; margin: 0px;">';
		docContent += '<a href="' + vidFile +'" style="color:#b50;">[Download this File]</a>';
		docContent += '<br><span style="font-size:10px;">(right-click + save as)</span></p>';
	}
	docContent += '<\/div><\/body><\/html>';

	x.document.open();
	x.document.write(docContent);
	x.document.close();

}
	
   	
function openPopup(w, h, textURL) {
	var resize=1;
	var winDims = [0,0];
	var winPos = [0,0];
	
	winPos["t"] = 30;
	winPos["l"] = 30;
	winDims["w"] = w;
	winDims["h"] = h;

	

	winParams  = 'width=' + winDims["w"] + ' ,height=' + winDims["h"];
	winParams += ', top=' + winPos["t"] + ' , left=' + winPos["l"];
	winParams += ' ,resizable=' + resize;
	winParams += ' , titlebar=0, toolbar=0, menubar=0';
	winParams += ', status=1, directories=0, personalbar=0';

	x = window.open(textURL,'_blank', winParams);
	x.focus();

}    


	

