﻿function HoverWindow (listItemId, options) {
        this.listItem = document.getElementById(listItemId);
        this.options = options;
       
        var url = "json/"+listItemId+".json";
        this.ajaxHelper = new net.ContentLoader(this,url,"GET",options.requestParameters || []);
       
        this.initializeBehavior();
}

HoverWindow.prototype = {
        initializeBehavior: function() {
                var oThis = this;
                this.listItem.onmouseover = function(e) {
                        if (!e && window.event) { e = window.event; }
                        oThis.showHoverWindow(e);
                };
                this.listItem.onmouseout = function() {
                        oThis.removeHoverWindow();
                };
        },
       
        showHoverWindow: function(e) {
                if (this.listItem.className == 'liClosed') {
                        if (document.getElementById("hoverDiv") == null) {
                                curPos.setPos(e);
                                this.ajaxHelper.sendRequest();
                        }
                }
        },

        removeHoverWindow: function() {
                var bodyNode = document.getElementById("smithrock.com");
                var dragBarNode = document.getElementById("dragbar");
                if (dragBarNode) {
                        bodyNode.removeChild(dragBarNode);
                        dragBarNode = null;
                } 
                var closebuttonNode = document.getElementById("closebutton");
                if (closebuttonNode) {
                        bodyNode.removeChild(closebuttonNode);
                        closebuttonNode = null;
                } 

                var titleBarNode = document.getElementById("titlebar");
                if (titleBarNode) {
                        bodyNode.removeChild(titleBarNode);
                        titleBarNode = null;
                } 
                var bodNode = document.getElementById("hoverDiv");
                if (bodNode) {
                        bodyNode.removeChild(bodNode);
                        bodNode = null;
                } 

        },

       
        ajaxUpdate: function(request) {
                var name="";
                var descrip="";
                var image="";
                //alert(request.responseText);
                //top.removePopup();
                this.removeHoverWindow();

                var jsonTxt=request.responseText;
                var jsonObj=eval("("+jsonTxt+")");

                name=jsonObj.wall.name;
                var ptype=jsonObj.wall.type;

                if (ptype) {
                        descrip+="<h2 class='heading'>"+ptype+"</h2>";
                }
                var imageSrc=jsonObj.wall.image;

                if (imageSrc) { image+="<br /><div style='text-align:center'><img id='hoverimage' src='" + imageSrc + "' /><br />"; }

                var infos=jsonObj.wall.info;
                descrip+="<ul>";

                for (var i=0;i<infos.length;i++) {
                        descrip+="<li>"+infos[i]+"</li>\n";
                }
                descrip+="</ul>";
                
                //top.showPopup(name,descrip,image);
                viewport.setSize();
                if (viewport.w > 400 && viewport.h > 400) {
                    var w = viewport.w * .35;
                    var h = viewport.h * .4;
                    var m = .4;
                    while (h < 400 && m < .7)
                    {   m = m +.15;
                        h = viewport.h * m;
                    }
                    new this.showHover(name,descrip,image, w, h);
                } 
                //else probably a mobile device so don't show hover's.
        },

        showHover: function (name,description,image, w, h) {
                var bod=document.createElement("div");
                bod.id="hoverDiv";
                document.body.appendChild(bod);

                this.contentDiv=document.createElement("div");
                this.contentDiv.style.left = curPos.x;
                this.contentDiv.style.top = curPos.y;
                this.contentDiv.style.position = 'absolute';
                this.contentDiv.className="winContents";
                this.contentDiv.id="contentDiv";

                var imageDiv = document.createElement("div");
                imageDiv.innerHTML=image;
                this.contentDiv.appendChild(imageDiv);

                var descDiv = document.createElement("div");
                descDiv.innerHTML=description;
                this.contentDiv.appendChild(descDiv);
       
                bod.appendChild(this.contentDiv);

                //var nodeID = document.getElementById("newDiv");
                var winCursorPosX = curPos.x + 100;
                var winCursorPosY = (viewport.h - h)/2;
                if ((curPos.y + h) > document.body.clientHeight) {winCursorPosY = document.body.clientHeight-h; }
 
                this.win=new windows.Window(bod,name,winCursorPosX,winCursorPosY,w,h);
        },
       
        //If XMLHTTPRequest is unavailable, perform this...
        handleNoXMLHTTPRequest: function() {
            if (this.options.alternativeAction)
                this.options.alternativeAction();
        },
       
        handleError: function(request) {
                if (this.options.errorHandler)
                        this.options.errorHandler(request);
        }
}; 
