// hg
// provides httpget functionality (also could be known as ajax dialogs)
// Author: Jansen Price (sumpygump)
// Date: 2007-08-15

function createRequestObject() {
    var ro;
    if (window.XMLHttpRequest) {
        // If IE7, Mozilla, Safari, etc: Use native object
        ro = new XMLHttpRequest()
    } else {
        if (window.ActiveXObject){
            // ...otherwise, use the ActiveX control for IE5.x and IE6
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return ro;
}

var http = createRequestObject();
var type = "dialog";

function hg(url) {
    http.open('get', url, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}
function hg_l(url,id) {
    //id is the id of the element where we want to put the "loading" gif.
    if(id) {
        showDialog("<div class=\"loading\"><img src=\"img/loading.gif\" /></div>");
        //document.getElementById(id).innerHTML = "<div class=\"loading\"><img src=\"img/loading.gif\" /></div>";
    }
    http.open('get', url, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function hg_simple(url) {
	
    type = "simple";
    http.open('get', url, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}
function hg_simple_l(url,id) {
	//alert(url);
	//alert(id);
	lastOcc = url.lastIndexOf('/');
	var check_tmp = url.substring(0,url.lastIndexOf('/'))
	var check = check_tmp.substring(check_tmp.lastIndexOf('/')+1, url.lastIndexOf('/'));
	
    type = "simple";
	
    //id is the id of the element where we want to put the "loading" gif.
	
    if((id) && check == 'healthcare') {
        document.getElementById(id).innerHTML = "<span class=\"loading\"><img src=\"images/spark_loading_hcp.gif\" /> <span>Loading...</span></span>";
    }
	
    http.open('get', url, true);
	
    if (check == 'healthcare') {
		http.onreadystatechange = handleResponse;
		
	}else{
		//http.onreadystatechange = handleResponseVideos;
		var el = document.getElementById('swappable_contain_video');
		//alert(url);
		
	el.innerHTML = '<IFRAME id="frame_video" SRC="'+url+ '" frameborder="0" scrolling="no" class="frame_class" ALIGN=RIGHT ><A HREF="BeaverHabitat.html">Stuff about the Beaver</A></IFRAME>';
	el.innerHTML +='';
	document.getElementById('frame_video').src=url;
	}
	
    http.send(null);
}

function send_post_simple_l(url,id,parameters) {
    type = "simple";
    //id is the id of the element where we want to put the "loading" gif.
    if(id != "") {
        document.getElementById(id).innerHTML = "<span class=\"loading\"><img src=\"img/loading.gif\" /></span>";
    }
    http.open('POST', url, true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", parameters.length);
    http.setRequestHeader("Connection", "close");
    http.onreadystatechange = handleResponse;
    http.send(parameters);
}

//for backwards compatibility
function sndReq(url) {
    http.open('get', url, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4) {
        var response = http.responseText;
        var update = new Array();
	//alert('response here is : \n' + response);
        if(response.indexOf('|' != -1)) {
			
            update = response.split('|');
			
            if(update[0] != 'null') {
                if(type=="simple") {
                    var objhgElement = document.getElementById(update[0]);
                    objhgElement.style.display = 'none';
                    objhgElement.innerHTML = update[1];
                    if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
                        new Effect.SlideDown(
                            objhgElement,
                            {duration:.2,
                            afterFinish:function(){if(typeof(Modalbox) != 'undefined' && (Modalbox.MBwindow) && Modalbox.MBwindow.style.display != "none") {Modalbox.resizeToContent();}}
                            });
                    } 
                    else {
						//alert('the else statement worked : ' + update[0]);    
                        bytefx.alpha(objhgElement,0);
                        objhgElement.style.display = 'block';
                        //$(objhgElement).fadeIn('slow'); //with jquery
                        
                        if(typeof(Modalbox) != 'undefined' && (Modalbox.MBwindow) && Modalbox.MBwindow.style.display != "none") Modalbox.resizeToContent();
                        if(typeof sIFR == "function"){
                            sIFR.replaceElement("h2", baseUrl + "/swfs/HSTarzanaBold.swf", "#fc6509", null, null, "#FFFFFF", 0, 0, 0, 0, null, null, 'transparent');
                        };
                        function fadeInContent() {
                            
                            bytefx.alpha(objhgElement,100);
                        }
                        setTimeout(fadeInContent,100);
                    }
                } else {
                    showDialog(update[1]);
					
                }
            }
        }
		fixPng();
    }
}
function handleResponseVideos() {
    if(http.readyState == 4) {
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            if(update[0] != 'null') {
                if(type=="simple") {
                    var objhgElement = document.getElementById(update[0]);
                    objhgElement.style.display = 'none';
                    //objhgElement.innerHTML = update[1];
					var newIframe = document.createElement('iframe');
					newIframe.innerHTML = update[1];
					//objhgElement.appendChild(newIframe);
					objhgElement.innerHTML = '<iframe>'
                    if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
                        new Effect.SlideDown(
                            objhgElement,
                            {duration:.2,
                            afterFinish:function(){if(typeof(Modalbox) != 'undefined' && (Modalbox.MBwindow) && Modalbox.MBwindow.style.display != "none") {Modalbox.resizeToContent();}}
                            });
                    } 
                    else {    
                        bytefx.alpha(objhgElement,0);
                        objhgElement.style.display = 'block';
                        //$(objhgElement).fadeIn('slow'); //with jquery
                        
                        if(typeof(Modalbox) != 'undefined' && (Modalbox.MBwindow) && Modalbox.MBwindow.style.display != "none") Modalbox.resizeToContent();
                        if(typeof sIFR == "function"){
                            sIFR.replaceElement("h2", baseUrl + "/swfs/HSTarzanaBold.swf", "#fc6509", null, null, "#FFFFFF", 0, 0, 0, 0, null, null, 'transparent');
                        };

                        //I thought setting the timeout below would fix the flicker in the sIfr text, but it didn't :(
                        function fadeInContent() {
                            //bytefx.fade(objhgElement,0,100,20);
                            bytefx.alpha(objhgElement,100);
                        }
                        setTimeout(fadeInContent,100);
                        
                    }
                    //document.getElementById(update[0]).innerHTML = update[1];
                } else {
                    showDialog(update[1]);
					
                }
            }
        }
		fixPng();
    }
}

function showDialog(content) {
    // prep objects
    var objOverlay = document.getElementById('overlay');
    var objhgDialog = document.getElementById('hg_dialog');
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();
    var hwDivisor = 2;
    // set height of Overlay to take up whole page and show
    objOverlay.style.height = (arrayPageSize[1] + 'px');
    objOverlay.style.display = 'block';
    
    //show the dialog 
    objhgDialog.innerHTML = content;
    if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
        new Effect.SlideDown(objhgDialog,{duration:.2});
        hwDivisor = 3.28; //for some reason using these effects messes up the centering commands below.
    } 
    else {    
        objhgDialog.style.display = 'block';
    }
    
    //center the dialog
    var hgDialogTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objhgDialog.clientHeight) / hwDivisor);
    var hgDialogLeft = ((arrayPageSize[0] - 20 - objhgDialog.clientWidth) / hwDivisor);
    objhgDialog.style.top = (hgDialogTop < 0) ? "0px" : hgDialogTop + "px";
    objhgDialog.style.left = (hgDialogLeft < 0) ? "0px" : hgDialogLeft + "px";
    
    //if there is a form element with the id of "focus", set the focus to that element.
    if (document.getElementById('focus')) {
        document.forms[0].focus.focus();
    }
    
    //if there was nothing returned from the httpget, then hide the dialog.
    if(content=="") {
        hideDialog();
    }
}

function hideDialog() {
    // get objects
    objOverlay = document.getElementById('overlay');
    objhgDialog = document.getElementById('hg_dialog');
    // hide lightbox and overlay
    if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
        new Effect.SlideUp(objhgDialog,{duration:.4});
    }
    else {
        objhgDialog.style.display = 'none';
    }
    objOverlay.style.display = 'none';
}

//
// 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;
}

//
// 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;
}

function inithg() {
    var objBody = document.getElementsByTagName("body").item(0);
    
    // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id','overlay');
    objOverlay.onclick = function () {hideDialog(); return false;}
    objOverlay.style.display = 'none';
    objOverlay.style.position = 'absolute';
    objOverlay.style.top = '0';
    objOverlay.style.left = '0';
    objOverlay.style.zIndex = '90';
    objOverlay.style.width = '100%';
    objBody.insertBefore(objOverlay, objBody.firstChild);
    
    // create hg div, same note about styles as above
    var objhgDialog = document.createElement("div");
    objhgDialog.setAttribute('id','hg_dialog');
    objhgDialog.style.display = 'none';
    objhgDialog.style.position = 'absolute';
    objhgDialog.style.zIndex = '100';    
    objBody.insertBefore(objhgDialog, objOverlay.nextSibling);
}

addLoadEvent(inithg);    // run inithg onLoad
