///////////////////////////////////////////////////////////////////////////////
////////// Class xdDialog
///////////////////////////////////////////////////////////////////////////////
function xdDialog(float_id) {
    this.BACK_ID = 'HsBack';
    if (typeof(float_id)=="undefined") float_id = 'HsFloat';
	this.FLOAT_ID = float_id;
    this.CONTAINER_ID = 'dialogContainer';
    this.CLOSE_ID = 'closebuttonContainer';
    this.CONTENT_ID = 'hssurveys_full';
    this.LOADING_ID = 'loading';
    this.DIALOG_HTML_CODE = "";
    this.WAIT_HTML_CODE = "";
    this.Width = 0;
    this.Height = 800;
    this.HMargin = 100;
    this.VMargin = 50;
    
	// create a wait dialog for Loading
	this.createWait = function() {
	    var el = document.getElementById(this.LOADING_ID);
		if (!el) {
		      el = document.createElement("div");
		      $(el).attr("id", this.LOADING_ID);
		      $(el).html(this.WAIT_HTML_CODE);
		      $("BODY").append(el);
		}
	};

    // Create 2 divs: a background, and a panel to put content inside
    this.createDialog = function() {
		  // Create the background
		  var el = document.getElementById(this.BACK_ID);
		  if (!el) {
			  el = document.createElement("div");
			  $(el).attr("id", this.BACK_ID);
		  	  $("BODY").append(el);
		  };
		  // Create Float
		  el = document.getElementById(this.FLOAT_ID);
		  if (!el) {
			  el = document.createElement("div");
			  $(el).attr("id", this.FLOAT_ID);
		  	  $("BODY").append(el);
		  	  $(el).html(this.DIALOG_HTML_CODE);
		  	  // Create Survey Container
			  el = document.getElementById(this.CONTENT_ID);
		      if (!el) {  
			     el = document.createElement("div");
			     $(el).attr("id", this.CONTENT_ID);
			     $(el).attr("class", "my-container");
		         $(el).css({"overflow-y":"auto", "overflow-x":"hidden", "position":"relative" ,"height":"400px"});
				  var container = document.getElementById(this.CONTAINER_ID);
			      if (container) {
			         $(container).append(el);
			      } else {
				     $('#' +  this.FLOAT_ID).append(el);
			      };
		      };
		  };
		  this.hide();
	};	  
    
    this.hide = function() {
		$('#' + objDialog.BACK_ID).hide();
		$('#' + objDialog.FLOAT_ID).hide();
		$("select").each(function() {
		   $(this).show();
		});
		$("iframe").each(function() {
		   $(this).show();
		});
		
    };
    this.show = function(hideSelectsAndFrames) {
		if (typeof(hideSelectsAndFrames)=="undefined") hideSelectsAndFrames = true;
		$('#' + this.BACK_ID).show();
		$('#' + this.FLOAT_ID).show();
		$('#' + this.FLOAT_ID).css({position: "absolute"});
		$('#' + this.CONTENT_ID).css({position: "relative", top: "0", left: "0"});
		
		this.center(this.FLOAT_ID, this.HMargin, this.VMargin, this.Width, this.Height);
		this.maximize(this.BACK_ID);
		this.resizeContainer();
		$('#' + this.BACK_ID).bind("click", this.hide);
        $('#' + this.CLOSE_ID).bind("click", this.hide);
		if (hideSelectsAndFrames) {
			$("select").each(function(){
				$(this).hide();
			});
			$("iframe").each(function(){
				$(this).hide();
			});
		}
    };
    
    this.wait = function() {
		$('#' + this.LOADING_ID).show();
    };
    this.endwait = function() {
		$('#' + this.LOADING_ID).hide();
    };
	this.maximize= function(id) {
		var el = document.getElementById(id);
		if (!el) return;
		var vW = document.documentElement.clientWidth;
		var vH = document.documentElement.clientHeight;
		var wW = document.body.clientWidth;
		var wH = document.body.clientHeight;
		// var sT = document.documentElement.scrollTop || document.body.scrollTop;
		// var sL = document.documentElement.scrollLeft || document.body.scrollLeft;
		var w = ((vW > wW) ? vW : wW); 
		var h = ((vH > wH) ? vH : wH); 
		this.resize(el, 0, 0, w, h);
	};
	this.center = function(id, mL, mT, mW, mH) {
		// var visibleW = document.documentElement.clientWidth;
		// var visibleH = document.documentElement.clientHeight;
		var el = document.getElementById(id);
		if (!el) return;
		
		var wW = document.body.clientWidth;
		var wH = document.body.clientHeight;
		var sT = document.documentElement.scrollTop || document.body.scrollTop;
		var sL = document.documentElement.scrollLeft || document.body.scrollLeft;
		var w = parseInt(el['offsetWidth']);
		if (mL) {
			w = wW - (mL * 2);
			if (w < 0 ) w = wW;
		};
		if (mW && w > mW)
		  w = mW;
		var h = parseInt(el['offsetHeight']);
		var y = ((wH - h)/2);
		if (mT) {
		    y = mT;
			h = wH - mT;
			// h = wH - (mT * 2 );
			if (h < 0 ) h = wH;
		};
		if (mH && h > mH)
		  h = mH;
		var x = ((wW - w)/2);
		// var y = sT + ((wH - h)/2);
		this.resize(el, sL + x, sT+ y, w, h);
	};
	this.resize = function(el, x, y, w, h) {
	    el.style['left']= x + "px";
	    el.style['top']= y + "px";
	    el.style['width']= w + "px";
	    el.style['height']= h + "px";
	};
	this.resizeContainer = function() {
	    var elP = document.getElementById(this.FLOAT_ID);
	    if (!elP) return;
		var h = parseInt(elP['offsetHeight']);
	    var elC = document.getElementById(this.CONTENT_ID);
	    if (!elC) return;
	    elC.style['height'] = (h - 90) + "px";
	};
};


