var ModalWindow = function(windowId){
    this.windowId = windowId;
    this.title = "";
    this.parent = "body";
    this.hide_fields = "";
    this.content = null;
    this.width = null;
    this.height = null;
}

ModalWindow.prototype.getIEVersion = function() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
    }
    
    return rv;
}

ModalWindow.prototype.close = function(hide_fields) {
    /*
    if (ModalWindow.prototype.getIEVersion()==6 && hide_fields != "")
    {
        $('#'+hide_fields+' select').show();
    }
    */
    
    if (ModalWindow.prototype.getIEVersion()==6) {
        $('select[modal_hidden=true]').show();
        $('select[modal_hidden=true]').each(function() {this.removeAttribute('modal_hidden', 'true') });
    }
    
    $(".modal-window").remove();  
    $(".modal-overlay").remove();  
};

ModalWindow.prototype.open = function() {

    if (ModalWindow.prototype.getIEVersion()==6) {
        $('select').each(function() {if (this.style.display!='none' && this.style.visibility!='hidden') this.setAttribute('modal_hidden', 'true') });
        $('select').hide();
    }

    var modal = "";  
    modal += "<div id=\"" + this.windowId + "_overlay\" class=\"modal-overlay\"></div>";  
    modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";  
    modal += "<table width=\"" + this.width + "\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border:1px solid black\"><tr><td>";
    //modal += "<div class=\"popup\" style=\"width: " + this.width + "px;\">";
    modal += "<div class=\"popup\" style=\"width: " + this.width + "px;\">";
    //modal += "<table class=\"popup_header\" width=\"" + this.width + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
    //modal += "<td><strong class=\"title\">"+this.title+"</strong></td>";
    //modal += "<td align=\"right\"><a href=\"#\" title=\"CLOSE\" class=\"close-window\"></a></td>";
    //modal += "</tr></table>";
    //modal += "<a href=\"#\" class=\"close-window\" title=\"CLOSE\"><img src=\"/admin/images/survey/tabs/close.gif\" width=\"17\" height=\"20\" border=\"0\" align=\"right\" /></a><br /><br />";  
    modal += "<div id=\"" + this.windowId + "_content\">";  
    modal += this.content;  
    modal += "</div></div></td></tr></table></div>"; 
    /*
    if (ModalWindow.prototype.getIEVersion()==6 && this.hide_fields != "")
    {
        $('#'+this.hide_fields+' select').hide();
    }
     */
    
    
    $(this.parent).append(modal);  
    
    var scrollTop = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
    var scrollLeft = self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
    
    var height = (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
    var width = (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
    
    var centerWidth = parseInt(width/2)+scrollLeft;
    var centerHeight = parseInt(height/2)+scrollTop;
    
    $(".modal-overlay").css("height", height+"px");
    /*$(".modal-window").css("left", centerWidth+"px");*/
    $(".modal-window").css("top", (scrollTop+300)+"px");
    /*$(".modal-window").append("<a class=\"close-window\">Close</a>");  */
    $(".close-window").click(function(){ ModalWindow.prototype.close(); return false; });  
    /*$(".modal-overlay").click(function(){modalWindow.close();});  */
}
ModalWindow.prototype.updateContentFromURL = function(url) {
        $("#"+this.windowId+"_content").load(url);
}
