/*****************************************
Popunder
Author External
Last updated n/a

Use: creates popover windows

******************************************/

//window.onload = setDimensions();
        
        function setDimensions(mask, iFrame, Form)
        {
            // set the width/heights for the mask and the popunder position
            setMaskDimensions(document.getElementById(mask));
            setMaskDimensions(document.getElementById(iFrame));
            positionPopunderForm(document.getElementById(Form));
        }
        
        function setMaskDimensions(oElement)
        {
            // this sets the width and height for the 2 "page mask" elements - it calculates the width/height of the current browser viewport
            // and then sets the mask elements to those values
            if (oElement)
            {
                var iWidth = 0;
                var iHeight = 0;
                //all browser should understand this, getting scroll width, in ie it will get the max scrollable width, ff only get the width
                //adding the 20 which is a spacer
                if (document.body.scrollWidth)
                {
                    iWidth = document.body.scrollWidth + 20;
                }
                //therefore this is used only for ff to get the extra scrollable width
                if (window.scrollMaxX)
                {
                    iWidth += window.scrollMaxX;
                }
                //get page height
                if( window.innerHeight) // Firefox 
                {
                    //pageWidth = window.innerWidth + window.scrollMaxX;
                    iHeight = window.innerHeight + window.scrollMaxY;
                }
                else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
                {
                    //pageWidth = document.body.scrollWidth;
                    iHeight = document.body.scrollHeight;
                }
                else if(getViewportHeight() > document.body.offsetHeight )
                {
                    //pageWidth = document.body.scrollWidth;
                    iHeight = getViewportHeight();
                }
                else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
                { 
                    //pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
                    iHeight = document.body.offsetHeight + document.body.offsetTop; 
                }
                //adding the 15 which is a spacer and make iframe smaller than mask
                iHeight += 15
                ////alert('iHeight: ' + iHeight + ' | iWidth: ' + iWidth);
                if (oElement.className.indexOf('_iframe') != -1)
                {
                    iHeight -= 30;
                }
                oElement.style.width = iWidth + 'px';
				oElement.style.height = iHeight + 'px';
                /*oElement.style.width = '794px';*/
				
            }
        }
        
        function positionPopunderForm(oElement)
        {
            // this calculates the top and left co-ordinates to ensure that the popunder always appears centred (horizontally & vertically)
            // within the browser's viewport.
            
            if (oElement)
            {
               // alert('oElement: ' + oElement);
                
                // get the height and width of the visible area
                var iHeight = getViewportHeight();
                var iWidth = getViewportWidth();
                
                //alert('iHeight :' + iHeight + ' | iWidth: ' + iWidth);
                
                //get the height and width of our element
                var iElemHeight = oElement.offsetHeight;
                var iElemWidth = oElement.offsetWidth;
                
                //alert(oElement + ': iElemHeight: ' + iElemHeight + ' | iElemWidth: ' + iElemWidth);
                
                if (iElemHeight==0 && iElemWidth==0)
                {
                    //if we can't find the value, see what the style says
                    iElemHeight = oElement.style.height.replace('px','');
                    iElemWidth = oElement.style.width.replace('px','');
                }
                //alert(sElementId + ': iElemHeight: ' + iElemHeight + ' | iElemWidth: ' + iElemWidth);
                
                 /*var iTop = (iHeight / 2) - (iElemHeight / 2);*/
               var iLeft = (iWidth / 2) - (iElemWidth / 2);
				var iTop = 75;
				/*var iLeft = 160;*/
                
                // now get the scrolling position
                var iScrollX;
                var iScrollY
                
                if (window.pageXOffset) //firefox
                {
                    iScrollX = window.pageXOffset;
                    ////alert('1x');
                }
                else if (document.body.scrollLeft) //ie7?
                {
                    iScrollX = document.body.scrollLeft;
                    ////alert('2x');
                }
                else if (document.documentElement.scrollLeft) //ie6
                {
                    iScrollX = document.documentElement.scrollLeft;
                    ////alert('3x');
                }
                else //assume 0
                {
                    iScrollX = 0;
                    ////alert('4x');
                }
                
                if (window.pageYOffset) //firefox
                {
                    iScrollY = window.pageYOffset;
                    ////alert('1y');
                }
                else if (document.body.scrollTop) //ie7?
                {
                    iScrollY = document.body.scrollTop;
                    ////alert('2y');
                }
                else if (document.documentElement.scrollTop) //ie6
                {
                    iScrollY = document.documentElement.scrollTop;
                    ////alert('3y');
                }
                else //assume 0
                {
                    iScrollY = 0; 
                    ////alert('4y');
                }
                
                ////alert('iScrollX :' + iScrollX + ' | iScrollY: ' + iScrollY);
                
                //add the current Top/Left to current ScrollY/X
                //dont add scroll on fixed position
                if(sGetStyle(oElement, 'position') == 'fixed')
                {
                    iTop = iTop
                    iLeft = iLeft
                }
                else
                {
                   // iTop = iTop + iScrollY
				    iTop = iTop
                    iLeft = iLeft + iScrollX
                }
                
                if (iTop < 0) {iTop=0;}
                if (iLeft < 0) {iLeft=0;}
                
                ////alert(oElement.name + ': iTop :' + iTop + ' | iLeft: ' + iLeft);
                
                oElement.style.top = iTop + 'px';
                oElement.style.left = iLeft + 'px';
                
                oElement.style.visibility = 'visible';
                  
            }
        }
        function getViewportHeight()
        {
            if (window.innerHeight!=window.undefined) return window.innerHeight;
            if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
            if (document.body) return document.body.clientHeight; 
            return window.undefined; 
        }
        
        function getViewportWidth()
        {
            if (window.innerWidth!=window.undefined) return window.innerWidth; 
            if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
            if (document.body) return document.body.clientWidth; 
            return window.undefined; 
        }
        
        function sGetStyle(oElem, sStyleProp)
        {

            var sVal = '';
	        if (oElem.currentStyle)
	        {
		        sVal = oElem.currentStyle[sStyleProp];
            }
	        else if (window.getComputedStyle)
	        {
		        sVal = document.defaultView.getComputedStyle(oElem,null).getPropertyValue(sStyleProp);
	        }
        	
	        return sVal;
        }
        
        
        function doHideShow(element)

        {
            var oPopunder = document.getElementById(element);
			var theElement = element;
            if (oPopunder)
            {
                if(oPopunder.style.visibility == 'hidden')
                {
					////alert("theElement = " + theElement);
                    switch(theElement){
						case 'windows7KeyFeaturesChart':  setDimensions("pageMask","pageMask_iframe","popunderForm");
						break;
						case 'whichEdition': setDimensions("pageMask1","pageMask_iframe1","popunderForm1");
						break;
						case 'experienceWindows': setDimensions("pageMask2","pageMask_iframe2","popunderForm2");
						break;
						case 'office': setDimensions("pageMask3","pageMask_iframe3","popunderForm3");
						break;
						case 'whatToDoWhen': setDimensions("pageMask4","pageMask_iframe4","popunderForm4");
						break;
						case 'addNewFeatures': setDimensions("pageMask5","pageMask_iframe5","popunderForm5");
						break;
						case 'answerCentre': setDimensions("pageMask6","pageMask_iframe6","popunderForm6");
						break;
						case 'tradeIn': setDimensions("pageMask7","pageMask_iframe7","popunderForm7");
						break;
						case 'windows7UpgradeChart': setDimensions("pageMask8","pageMask_iframe8","popunderForm8");
						break;	
						case 'rightForMe': setDimensions("pageMask9","pageMask_iframe9","popunderForm9");
						break;
					}
					
					//setDimensions();
                    oPopunder.style.display = 'block';
                    oPopunder.style.visibility = 'visible';
                 }
                 else
                 {
                    oPopunder.style.display = 'none';
                    oPopunder.style.visibility = 'hidden';
                 }
                 
            }
        }
