﻿// Register a new Namespace
Type.registerNamespace("MyServices");

// Define the constructor of the Location class
MyServices.Location = function (uiElement, uiBody, uiProgress) {
    // Call base class just for completeness
    MyServices.Location.initializeBase(this);
    
    this._uiElement = uiElement;
    this._uiBody = uiBody;
    this._uiProgress = uiProgress;
    this._xAxis = 0;
    this._yAxis = 0;
}

// Define the properties and methods of the Location class
// using the Prototype Design Pattern
MyServices.Location.prototype= {
    
    // Define UIElement Property
    get_uiElement:function() {
        // Get
        return this._uiElement;
    },
    set_uiElement:function(value) {
        // Set
        this._uiElement = value;
    },
    
    // Define UIBody Property
    get_uiBody:function() {
        // Get
        return this._uiBody;
    },
    set_uiBody:function(value) {
        // Set
        this._uiBody = value;
    },
    
    // Define UIProgress Property
    get_uiProgress:function() {
        // Get
        return this._uiProgress;
    },
    set_uiProgress:function(value) {
        // Set
        this._uiProgress = value;
    },
    
    // Define ShowPopupInfo method that is responsible
    // for calling the webservice, and setting the location
    // of the popup window.
    ShowPopupinfo:function(event, ID_Value, ID_Type) {
        // Call the AJAX Service
        MyServices.LocationService.GetAreaInfo(
            ID_Value, ID_Type,                                // Parameter
            Function.createDelegate(this, this.OnCompleted),  // Success Callback
            this.OnError,                                     // Failure Callback
            this.OnTimeOut);    // TimeOut Callback
        
        // Set the location of the popup window
        //this._xAxis = event.clientX;
        //this._yAxis = event.clientY;
        this._xAxis = mouseX(event);
        this._yAxis = mouseY(event);
        
        // Show the Progress Bar
        var progress = $get(this.get_uiProgress());
        if (progress != null) {
            progress.style.visibility = "visible";
            progress.style.display = "block";
        }
    },
    
    // Define HidePopupInfo method that is used to hide
    // the UI popup window
    HidePopupInfo:function() {
        var uiElement = $get(this.get_uiElement());
        if (uiElement != null) {
            uiElement.style.visibility = "hidden";
            uiElement.style.display = "none";
        }
        
        // Hide the progress bar
        //this.hideProgressBar();
    },
    
    // Define success callback function that will update the UI
    // with the data coming frmo the AJAX Service
    OnCompleted:function(result, userContext, methodName) {
        var uiElement = $get(this.get_uiElement());
        var uiBody = $get(this.get_uiBody());
        
        if (uiBody != null) {
            // Set the text inside the UI element
            // by creating a TextNode DOm object rather than
            // using innerHTML, innerText, or textContent, since
            // this is more standard compliant.
            //var textNode = uiBody.firstChild;
            //if(!textNode) {
            //    textNode = document.createHTMLNode(result);
            //    uiBody.appendChild(textNode);
            //    textNode.nodeValue = result;
            //} 
            //else {
            //    textNode.nodeValue = result;
            //}
            
            uiBody.innerHTML = result;
            
            // Show the UI element
            if (uiElement != null) {
                uiElement.style.visibility = "visible";
                uiElement.style.display = "inline";
                
                // Set the position below and just to the 
                // right of the cursor.
                var x_Axis = this._xAxis + 0;
                var y_Axis = this._yAxis + 7;
                                
                // Set the position of the popup.
                uiElement.style.left = x_Axis + "px";
                uiElement.style.top = y_Axis + "px";
            }
        }
        
        // Hide the progress bar
        //this.hideProgressBar();
    },
    
    // Error callback function to alert
    // the user that some exceptions were thrown
    // during the asynchronous processing
    onError:function(result, userContext, methodName) {
        // Hide the progress bar
        this.hideProgressBar();
        
        var msg = result.get_exceptionType() + "\r\n";
        msg += result.get_message() + "\r\n";
        msg += result.get_stackTrace();
        alert(msg);
    },
    
    // Timeout callabck function called
    // when the current asynchronous request
    // times out
    onTimeOut:function(result, userContext, methodName) {
        // Hide the progress bar
        this.hideProgressBar();
        
        alert("Request Timeout :" + result);
    },
    
    hideProgressBar:function() {      
        // Hide the Progress Bar
        var progress = $get(this.get_uiProgress());
        if (progress != null) {
            progress.style.visibility = "hidden";
            progress.style.display = "none";
        }
    }
}
    
// Register the class
MyServices.Location.registerClass("MyServices.Location");

var myLocation = null;
function pageLoad(sender, args) {
    // Create a new instance of the class
    var browserName=navigator.appName; 
    var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
        
    if (browserName=="Microsoft Internet Explorer")
    { 
        if (IE6 == true)
        {
            // Run the location script for IE 6 or earlier and use 
            // the simple popup box.
            myLocation = new MyServices.Location("modal_old", "modalBody_old", "updateProgress");
        }
        else
        {
            // This is for IE 7 or later
            // Use the fancy popup box.
            myLocation = new MyServices.Location("modal", "modalBody", "updateProgress");
        }        
    }
    else 
    { 
       // For all others use the fancy popup box.
       myLocation = new MyServices.Location("modal", "modalBody", "updateProgress");
    }     
    
    // Hide the popup div
    myLocation.HidePopupInfo();
}

// Method called by the page controls
// to update the UI with the information
// from the ajax service
function GetAreaInfo(event, args1, args2) 
{
    //document.getElementById('tempID').innerText = args1;
    myLocation.ShowPopupinfo(event, args1, args2);
}

function HidePopup() {
    // Hide the popup div
    myLocation.HidePopupInfo();
}

// Functions for finding the location of the mouse pointer...
function mouseX(evt) 
{
    // Grab the cursor x position.
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?
        document.documentElement.scrollLeft :
        document.body.scrollLeft);
    else return null;
}
function mouseY(evt) 
{
    // Grab the cursor y position.
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
        document.documentElement.scrollTop :
        document.body.scrollTop);
    else return null;
}

// Method called when the mouse is moved to move the popup with 
// the cursor.
function MovePopup(event, popUpID)
{
    if (document.getElementById(popUpID))
    {
        // If the element exists, get the cursor position.
        var leftOffset = mouseX(event) - 10;
        var topOffset = mouseY(event) + 10;  
        
        // Then, set the new location to the element.
        document.getElementById(popUpID).style.top = topOffset + "px";
        document.getElementById(popUpID).style.left = leftOffset + "px";      
    }
}

