// $Id: haps.js,v 1.2 2011-03-24 10:01:37 MatthiasK Exp $

var user_agent = navigator.userAgent.toLowerCase();

function HAPSCtrl() {
	
	var self = this;
	var yPos;
	var isIE = false;
	var elements_visibility;
	
	this.showBox = function(url, title, button_text) {
		if (!title.length) title = '\u00A0';
		this.getContent(url, title);
	},
	
	this.hideBox = function() {
		var haps_box = document.getElementById('haps_box');
		if (haps_box != undefined) document.body.removeChild(haps_box);
		var haps_overlay = document.getElementById('haps_overlay');
		if (haps_overlay != undefined) document.body.removeChild(haps_overlay);
		var haps_helper = document.getElementById('haps_helper');
		document.body.removeChild(haps_helper);
		this.IEfixesHide();
		this.hide_show_elements('iframe', true);
	},
	
	this.getContent = function(url, title) {
		var req = new ClientHttpRequest();
    req.onLoaded = function (sender) {
      self.loadBox(sender, title);
    }
    req.Get(url);
	},
	
	this.loadBox = function(sender, title) {
		if (sender.Status != 200 || !sender.responseText) return;
		var haps_helper = document.createElement('div');
		haps_helper.id = 'haps_helper';
		haps_helper.style.display = 'none';
		document.body.appendChild(haps_helper);
		haps_helper.innerHTML = sender.responseText;
		var haps_title = document.getElementById('haps_box_title');
		var haps_overlay = document.getElementById('haps_overlay');
		var haps_box = document.getElementById('haps_box');
		if (!haps_title || !haps_overlay || !haps_box) {
			document.body.removeChild(haps_helper);
			return;
		}
		this.IEfixesShow();
		this.hide_show_elements('iframe', false);
		haps_title.firstChild.data = title;
		if (haps_overlay != undefined) {
			document.body.appendChild(haps_overlay);
			haps_overlay.style.display = 'block';
		}
		if (haps_box != undefined) document.body.appendChild(haps_box);
	},
	
	
	
	// IE fixes
	
	this.IEfixesShow = function() {
	  if (this.isIE){
	  	this.saveYPosition();
	  	this.prepareIE('100%', 'hidden');
	  	this.scrollTo(0,0);
	  	this.hide_show_elements('select', false);
	  }
	},
	
	this.IEfixesHide = function() {
	  if (this.isIE){
	  	this.prepareIE('auto', 'auto');
	  	this.scrollTo(0, this.yPos);
	  	this.hide_show_elements('select', true);
	  }
	},
	
	//hide all iframes for they can occlude the dialog
	this.hide_show_elements = function(name, show) {
	 var elements = document.getElementsByTagName(name);
	 for(var i = 0; i < elements.length; i++) {
		 if (!show) {
			 this.elements_visibility = new Array();
			 this.elements_visibility[i] = elements[i].style.visibility;
		 }
		 elements[i].style.visibility = show ? this.elements_visibility[i] : 'hidden';
	 }
	}
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	this.saveYPosition = function() {
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop;
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},

	this.scrollTo = function(x, y) {
		window.scrollTo(x, y);
	},

	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	this.prepareIE = function(height, overflow) {
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;

		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow;
	}
	
}

if (HAPSCtrlInst == undefined) var HAPSCtrlInst = new HAPSCtrl();
HAPSCtrlInst.isIE = (user_agent.indexOf('msie') + 1);
