// --------------------------------------------------------------------
// PopUp Image optimized for IE7

PositionX = 150;
PositionY = 100;
defaultWidth = 920;
defaultHeight = 530;
var AutoClose = true;

var bb1_popup_content_padding = 15;
var bb1_popup_closebtn_height = 30;

function popSimpleImage(path, popup){
 if (!popup) {
  bblImagePopup(path);
  return;
 }
 
	if (!path){ return true; }
	var imageTitle = 'Enlarged View';
	
	var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
	if( !imgWin ) { return true; } 									//popup blockers should not cause errors
	imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
	'function resizeWinTo() {\n'+
		'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
		'var oH = document.images[0].height, oW = document.images[0].width;\n'+
		'if( !oH || window.doneAlready ) { return; }\n'+			//in case images are disabled
		'window.doneAlready = true;\n'+ 							//for Safari and Opera
		'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
		'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
		'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
		'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
		'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
		'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
		'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
		'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
		'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
		'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
	'}\n'+
	'<\/script>'+
	'<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
	(document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
	'<img src='+path+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
	(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
	imgWin.document.close();
}

function bblImagePopup(url, width, height) {
 bb1AddImageViewerLayer();
 document.body.style.cursor = "wait";
 var img = new Image();
 img.onload = function() {
  width = (!isNaN(parseInt(width))) ? parseInt(width) : img.width; 
  height = (!isNaN(parseInt(height))) ? parseInt(height) : img.height;
  var transbackground = document.getElementById('bb1_transbackground');
  var screencenter = document.getElementById('bb1_screencenter');
  var image_container = document.getElementById('bb1_image_container');
  var image_div = document.getElementById('bb1_image_div');
  if (transbackground && image_container && image_div) {
   var position_top = (((height+(bb1_popup_content_padding*2)+bb1_popup_closebtn_height)/-2));
   position_top = (Math.abs(position_top) > screencenter.offsetTop) ? 0-screencenter.offsetTop : position_top;
   image_container.style.top = position_top + 'px';
   image_container.style.left = (((width+(bb1_popup_content_padding*2))/-2)) + 'px';
   image_container.style.width = width + (bb1_popup_content_padding*2) + 'px';
   image_container.style.height = height + (bb1_popup_content_padding*2)+bb1_popup_closebtn_height + 'px';
   var regex = new RegExp(/(.htm)|(.html)|(.pdf)$/i);
   if (regex.test(url))
    image_div.innerHTML = '<iframe src="'+url+'" height="'+height+'" width="'+width+'" frameborder="0" scrolling="auto">You need iframes enabled to view this page</iframe>';
   else if (url != '')
    image_div.innerHTML = '<img src="'+url+'" border="0">'; 
   image_div.style.width = width + 'px';
   image_div.style.height = height + 'px';
   image_div.align = 'center';
   if (window.innerWidth)
    screencenter.style.top = (window.pageYOffset + (window.innerHeight/2)) + "px";
   else if (document.all) {
    var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
    screencenter.style.top = (iebody.scrollTop + (iebody.clientHeight/2)) + "px";
   }
   transbackground.style.display = 'block';
   image_container.style.display = 'block';
  }
  document.body.style.cursor = "default";
 }
 img.src = url;
}

function bb1ClosePopup() {
 var transbackground = document.getElementById('bb1_transbackground');
 var image_container = document.getElementById('bb1_image_container');
 var image_div = document.getElementById('bb1_image_div');
 if (transbackground && image_container && image_div) {
  image_div.innerHTML = '&nbsp;'; 
  transbackground.style.display = 'none';
  image_container.style.display = 'none';
  return true;
 }
 else
  return false;
}

function bb1AddImageViewerLayer() {
 if (arguments.callee.done) return;
 arguments.callee.done = true;
 var translayer = document.createElement('div');
 translayer.id = 'bb1_transbackground';
 translayer.className = 'bb1_transbackground';
 translayer.onclick = function () {return false;};
 translayer.onmousedown = function () {return false;};
 translayer.onmousemove = function () {return false;};
 translayer.onmouseup = function () {return false;};
 translayer.ondblclick = function () {return false;};
 translayer.innerHTML = '&nbsp;';
 var screencentre = document.createElement('div');
 screencentre.id = 'bb1_screencenter';
 screencentre.className = 'bb1_screencentre';
 var image_container = document.createElement('div');
 image_container.id = 'bb1_image_container';
 image_container.className = 'bb1_image_container';
 var image_pad = document.createElement('div');
 image_pad.style.padding = bb1_popup_content_padding+'px';
 var image_div = document.createElement('div');
 image_div.id = 'bb1_image_div';
 image_div.className = 'bb1_image_div';
 image_div.innerHTML = '&nbsp;';
 image_pad.appendChild(image_div);
 var image_close_button = document.createElement('div');
 image_close_button.id = 'bb1_image_close_button';
 image_close_button.className = 'bb1_image_close_button';
 image_close_button.innerHTML = '<img width="47" height="15" border="0" align="right" alt="Close" src="http://www.westleyrichards.co.uk/site/templates/closebutton.gif" onclick="bb1ClosePopup();" />';
 image_pad.appendChild(image_close_button);
 image_container.appendChild(image_pad);
 screencentre.appendChild(image_container);
 document.getElementsByTagName("body")[0].appendChild(translayer);
 document.getElementsByTagName("body")[0].appendChild(screencentre);
}
