﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//

Type.registerNamespace('GenX.Common.Ajax');

GenX.Common.Ajax.HyperlinkPopupControlBehavior = function(element) {

    GenX.Common.Ajax.HyperlinkPopupControlBehavior.initializeBase(this, [element]);

    // TODO : (Step 1) Add your property variables here
    //
     this._targetPanelID = null;  //Panel ID containing popup
    this._targetPanelElement= null; //Panel Element containing popup
    this._targetPanelControl= null; //Panel Element containing popup
  this._popupBehavior= null; // behavior on panel
    this._hoverBehavior= null; //behavior on link for activating popup
    this._hoverLinkID= null;
    this._hoverElement= null;
    this._hoverControl= null;
    this._closeLinkID= null; //link id for closing pop-up panel
    this._closeLinkElement= null; //link element for closing popop
    this._closeLinkControl= null; //controls for closing popup
    this._closeClickBehavior= null; //
    this._positioningMode = AjaxControlToolkit.PositioningMode.BottomLeft;// position
    this._offsetX= 0;// Position X offset
    this._offsetY= 0;//   Position Y Offset
    this._allowHover= null; //Maybe we want them to click to popup
    this._isIE = true;
    
   
    this._hoverShow= null; //hover show

    this._popupBehaviorId = null; 
    //Click handler to delate function for passing in THIS
    this._clickHandler =  Function.createDelegate(this, this._onClick);   
    this._closeClickBehavior = null;
    this._popped = false;
    
}

GenX.Common.Ajax.HyperlinkPopupControlBehavior.prototype = {

    initialize : function() {
        GenX.Common.Ajax.HyperlinkPopupControlBehavior.callBaseMethod(this, 'initialize');

        // TODO: add your initalization code here
        var e = this.get_element();
        
        //alert(e.id);
        this._targetPanelElement = $get(this._targetPanelID);
        
         //debug.assert(this._targetPanelElement, "Couldn't find HTML element '" + this._targetPanelID  + "'");
        if(navigator.userAgent.toLowerCase().indexOf("msie") != -1) { this._isIE = true; } else { this._isIE = false; }

         //Create a control wrapper and behavior for the popup control
        if (this._targetPanelElement) {
            this._popupBehaviorId =this.get_id()+'PopupBehavior';
                this._popupBehavior =  $create(AjaxControlToolkit.PopupBehavior, { 'id':this._popupBehaviorId, 'parentElement':e }, null, null, this._targetPanelElement);
                
//            this._popupBehavior.set_x(this._offsetX);
//            this._popupBehavior.set_y(this._offsetY);
                   
                   
                    this._closeLinkElement = $get(this._closeLinkID)
                    
                   //Add click handlers               
                   $addHandler(e, 'click', this._clickHandler);
                   $addHandler(this._closeLinkElement, 'click', this._clickHandler);
                   

          }
    
    },

    dispose : function() {
        // TODO: add your cleanup code here
        var e = this.get_element();
        GenX.Common.Ajax.HyperlinkPopupControlBehavior.callBaseMethod(this, 'dispose');
          if (this._clickHandler) {
            $removeHandler(e, 'click', this._clickHandler);
            this._focusHandler = null;
        }
    },
    
    _popupHide : function() {
        this._popupBehavior.hide();
    },
    
    _popupShow : function() {
        if (this._isIE) {
            var offset = this._browserOffset();
            //Sys.Debug.traceDump(offset, "ie offset");
            this._popupBehavior.set_x(offset[0] + this._offsetX);
            this._popupBehavior.set_y(offset[1] + this._offsetY);
        } else {
            //Sys.Debug.traceDump(offset, "not an ie offset");
            this._popupBehavior.set_x(this._offsetX);
            this._popupBehavior.set_y(this._offsetY);
        }
        
        this._popupBehavior.set_positioningMode(this._positioningMode);
        
        this._popupBehavior.show();
    },

    _onClick : function(e) {
        e.stopPropagation();
        if(this._popped) {
            this._popupHide();
            this._popped = false;
        } else {
            this._popupShow();
            this._popped = true;
        }
    },
    
    _browserOffset : function() {
	    var width = 0;
	    var height = 0;
    	//Sys.Debug.traceDump( Sys.Browser, "Browser Detect");
    	//Sys.Debug.traceDump( Sys.Browser.agent, "Browser Detect agent");
	    if (typeof(window.pageYOffset) == 'number') {
		    //Netscape compliant
		    //Sys.Debug.traceDump(window.pageYOffset, "y offset netscape");
		    width = window.pageXOffset;
		    height = window.pageYOffset;
	    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		    //DOM compliant
		    //Sys.Debug.traceDump( document.body.scrollTop, "scrollTop offset Dom");
		    width = document.body.scrollLeft;
		    height = document.body.scrollTop;
	    } 
	     else if (Sys.Browser.name =="Microsoft Internet Explorer" ) {
		    //IE compliant
		    //Sys.Debug.traceDump( document.body.scrollTop, "scrollTop offset IE");
		    width = document.body.scrollLeft;
		    height = document.body.scrollTop;
	    }	    
	    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		    //IE6 standards compliant mode--- issue here if ever gets into this block
		    //Sys.Debug.traceDump( document.documentElement.scrollTop, "scrollTop offset IE6");
		    width = document.documentElement.scrollLeft;
		    height = document.documentElement.scrollTop;
	    }
    	
	    return [ width, height ];
    },
  
//   _onCloseClick: function(e) {
//        this._popupHide();
//   },

    // TODO: (Step 2) Add your property accessors here
    //
    get_TargetPanelID : function() {
            return this._targetPanelID;
        },
        
        set_TargetPanelID : function(value) {
            this._targetPanelID = value;
        },
        
        get_CloseLinkID : function() {
            return this._closeLinkID;
        },
        
        set_CloseLinkID : function(value) {
           this._closeLinkID = value;
        },
        
        get_OffsetX : function() {
            return this._offsetX;
        },
        
        set_OffsetX : function(value) {
            this._offsetX = value;
           
        },
        
        get_OffsetY : function() {
            return this._offsetY;
        },
        
        set_OffsetY : function(value) {
            this._offsetY = value;
            
        },
        
        get_PositioningMode : function() {
            return this._positioningMode;
        },

        set_PositioningMode : function(value) {
            if (this._positioningMode != value) {
                this._positioningMode = value;
                this.raisePropertyChanged('Position');
            }
        },
        
         get_AllowHover : function() {
            return this._allowHover;
        },
        
        set_AllowHover : function(value) {
            this._allowHover = value;  
        }
}

GenX.Common.Ajax.HyperlinkPopupControlBehavior.registerClass('GenX.Common.Ajax.HyperlinkPopupControlBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();