
var IE6 = false;

var browser_version = parseInt(navigator.appVersion); 
var browser_type = navigator.appName; 

if (browser_type == "Microsoft Internet Explorer" && (browser_version < 7)) 
{ 
    IE6 = true; 
} 
        
function ShowMagnifyLayer(imageID, closeLabel) { 
    var image = document.getElementById(imageID);    
    var imageLink = image.getAttribute("src");

    var pageField = getPageSize();   
    var pageWidth = pageField[0];
    var pageHeight = pageField[1];
    var windowWidth = pageField[2];
    var windowHeight = pageField[3];
        
    
    var body = document.getElementsByTagName("body").item(0);
    
    var layerDiv = document.getElementById("magnifyLayerDiv");
    layerDiv.setAttribute('id','magnifyLayerDiv'); 
    layerDiv.onclick = function(){hideMagnifyLayer(); }; 
    layerDiv.style.position = "absolute";
    layerDiv.style.display = "block";
    layerDiv.style.top = "0";
    layerDiv.style.left= "0";
    layerDiv.style.width= "100%";
    layerDiv.style.height= "100%";
    layerDiv.style.zIndex = "90";
     
    if(IE6){
        layerDiv.innerHTML = "<div id='layerContainer' style='height: " + pageHeight + "px; background-color: #a6d4ff; filter: alpha(opacity=90);'></div>";
    } else {
        layerDiv.innerHTML = "<div id='layerContainer' style='height: " + pageHeight + "px; background-image: url(templates/img/overlay.png);'></div>";
    }
    body.insertBefore(layerDiv, body.nextSibling);
     
   
    
    /*
    <div id="layerDiv">
        <div id="mainDiv">
            <div id="closeButtonDiv">
                <a onClick="hideLayer();return false;" href="#" />
            </div>
            <div id="layerImageDiv">
                <image>
            </div>
        </div>
    </div>
    */
    
    var layerWidth = image.style.width.replace('px', '') - 0 + 20;
    var layerHeight = image.style.height.replace('px', '') - 0 + 50;
    
    if(layerWidth == 20){
        layerWidth = 620;
    }
    if(layerHeight == 50){
        layerHeight = 500;
    }
    
    var pageScroll = getPageScroll();
    
    var layerPosLeft = (windowWidth - layerWidth) / 2;
    var layerPosTop = ((windowHeight - layerHeight) / 2) + pageScroll[1];
    
    var mainDiv = document.createElement("div");
    mainDiv.setAttribute('id','mainDiv');
    mainDiv.style.backgroundColor = "#FFFFFF";
    mainDiv.style.display = "block";
    mainDiv.style.position = "absolute";
    mainDiv.style.top = layerPosTop + "px";
    mainDiv.style.left= layerPosLeft + "px";
    mainDiv.style.width= layerWidth + "px";
    mainDiv.style.height= layerHeight + "px";
    mainDiv.style.zIndex = "100";
    
    var closeButtonDiv = document.createElement("div");
    closeButtonDiv.setAttribute('id','magnify_closeButtonDiv');
    closeButtonDiv.innerHTML = '<a onClick="hideMagnifyLayer();return false;" href="#">' + closeLabel + '<img src="templates/img/close_button.gif" border="0" align="middle" style="position: relative; top: -4px;" /></a>';
    closeButtonDiv.setAttribute('style', 'float:right');
            
    
    var layerImageDiv = document.createElement("div");
    layerImageDiv.setAttribute('id','layerImageDiv');    
    layerImageDiv.innerHTML = '<img src="' + imageLink + '" style="margin-top:10px;" />';
    layerImageDiv.setAttribute('style', 'text-align:center;');
    
    mainDiv.appendChild(closeButtonDiv);
    mainDiv.appendChild(layerImageDiv); 
    
    body.insertBefore(mainDiv, layerDiv.nextSibling);
}

function hideMagnifyLayer() { 
    document.getElementById('magnifyLayerDiv').style.display = "none";
    document.getElementById('mainDiv').style.display = "none";
    var mainDiv = document.getElementById('mainDiv');
    mainDiv.parentNode.removeChild(mainDiv); 
    var zoomLevelMeasure = document.getElementById('zoomLevelMeasure');
    zoomLevelMeasure.parentNode.removeChild(zoomLevelMeasure);  
}
    
    
    //
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
