﻿var xmlhttp = null;
var lnk = null;
var productName = null;
var ssImage = null;

function ProcessResponse()
{
    if (xmlhttp.readyState == 4) { // completed
        if (xmlhttp.status == 200) { // ok
            if (xmlhttp.responseText != 'ERROR') {
                var obj = eval('(' + xmlhttp.responseText + ')');
               
                document.getElementById('MatInfo_Title').innerHTML = obj.Material.Title;
                document.getElementById('MatInfo_PopUses').innerHTML = obj.Material.PopularUses + '<div style="width:1px; height: 4em;"></div>';
               
                var divMatInfo = document.getElementById('divMatInfo');

				if (FindPosY(lnk) > FindWindowVertMiddle()+getScrollY() ) {
					divMatInfo.style.top = FindPosY(lnk) - 200 + 'px';
				}
				else {
					divMatInfo.style.top = FindPosY(lnk) + 25 + 'px';
				}

                divMatInfo.style.left = '120px';
                divMatInfo.style.display = 'block';

                ssImage = obj.Material.SuperSizeImage;
                                        
                document.getElementById('MatInfo_Image').src = '/images/materials/thumb_smaller/' + obj.Material.Thumbnail;
                document.getElementById('MatInfoArea').onclick = ShowSuperSizeImage;
                document.getElementById('selectMaterial').href = '/Products/' + productName + '.htm';
                
                if (navigator.appName.indexOf('Microsoft') >= 0) {
                    var iframeMatInfo = document.getElementById('iframeMatInfo');
                    iframeMatInfo.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
                    iframeMatInfo.style.display = 'block'
                    iframeMatInfo.style.height = divMatInfo.offsetHeight;
                }
            }
        }
    }
}


function ProcessResponseMat()
{
    if (xmlhttp.readyState == 4) { // completed
        if (xmlhttp.status == 200) { // ok
            if (xmlhttp.responseText != 'ERROR') {
                var obj = eval('(' + xmlhttp.responseText + ')');
               
                document.getElementById('MatInfo_Title').innerHTML = obj.Material.Title;
                document.getElementById('MatInfo_PopUses').innerHTML = obj.Material.PopularUses + '<div style="width:1px; height: 4em;"></div>';
               
                var divMatInfo = document.getElementById('divMatInfo');
				
				//Set Vertical Position
				if (FindPosY(lnk) > FindWindowVertMiddle()+getScrollY() ) {
					divMatInfo.style.top = FindPosY(lnk) - 200 + 'px';
				}
				else {
					divMatInfo.style.top = FindPosY(lnk) + 50 + 'px';
				}
				
				//Set Horizonal Position
				divMatInfo.style.left = FindWindowHorizMiddle() - 165 + 'px';
				
                divMatInfo.style.display = 'block';

                ssImage = obj.Material.SuperSizeImage;
                                        
                document.getElementById('MatInfo_Image').src = '/images/materials/thumb_smaller/' + obj.Material.Thumbnail;
                document.getElementById('MatInfoArea').onclick = ShowSuperSizeImage;
//              document.getElementById('selectMaterial').href = productName + '.htm';  <--Define extension on the page in fucntion call instead
				document.getElementById('selectMaterial').href = productName;

                if (navigator.appName.indexOf('Microsoft') >= 0) {
                    var iframeMatInfo = document.getElementById('iframeMatInfo');
                    iframeMatInfo.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
                    iframeMatInfo.style.display = 'block'
                    iframeMatInfo.style.height = divMatInfo.offsetHeight;
                }
            }
        }
    }
}

function ShowSuperSizeImage()
{
   window.open('/images/super_size/' + ssImage,'SuperSizeImage','toolbar=no,menubar=no,location=no,height=480,width=640,left=100,top=150');
}

function SetXmlHttp()
{
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
       
        if (xmlhttp.overrideMimeType)
            xmlhttp.overrideMimeType('text/xml');
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function ShowMatInfo(matCode, curProductName, curLnk)
{
    productName = curProductName;
    lnk = curLnk;
   
    var url = '/Products/GetMaterialInfo.aspx';
    url += '?MatCode=' + matCode;
   
    SetXmlHttp();
   
    //if (window.XMLHttpRequest)
    //    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   
    xmlhttp.onreadystatechange = ProcessResponse;
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}

function ShowMatPageInfo(matCode, curProductName, curLnk)
{
    productName = curProductName;
    lnk = curLnk;
   
    var url = '/Products/GetMaterialInfo.aspx';
    url += '?MatCode=' + matCode;
   
    SetXmlHttp();
   
    //if (window.XMLHttpRequest)
    //    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   
    xmlhttp.onreadystatechange = ProcessResponseMat;
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}

function HideMatInfo()
{
    document.getElementById('divMatInfo').style.display = 'none';
}

function FindPosX(obj)
{
    var curleft = 0;
   
    if (obj.offsetParent) {
        while(true) {
            curleft += obj.offsetLeft;
     
            if(!obj.offsetParent)
                break;
               
            obj = obj.offsetParent;
        }
    }
    else if(obj.x) {
        curleft += obj.x;
    }
   
    return curleft;
}

function FindPosY(obj)
{
    var curtop = 0;
   
    if (obj.offsetParent) {
        while(true) {
            curtop += obj.offsetTop;
           
            if (!obj.offsetParent)
                break;
               
            obj = obj.offsetParent;
        }
    }
    else if(obj.y) {
        curtop += obj.y;
    }
   
    return curtop;
}
function FindWindowMiddle()
{
	var winH = 0;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winH = window.innerHeight;
		 }
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winH = document.body.clientHeight;
		}
	}
	return winH /2;
}

function FindWindowVertMiddle()
{
	var winH = 0;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winH = window.innerHeight;
		 }
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winH = document.documentElement.clientHeight;
		}
	}
	return winH /2;
}


function FindWindowHorizMiddle()
{
	var winH = 0;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winH = window.innerWidth;
		 }
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winH = document.body.clientWidth;
		}
	}
	return winH /2;
}

function getScrollY()
{
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//NS
		scrOfY = window.pageYOffset;
	} 
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
	} 
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY;
}
