﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 

//STEP 1: Replace Hyperlink w/ your controls' name 
//STEP  2: Move Constants.Hyperlink to shared.js 
//STEP 3: Modify your DefaultTemplate so it matches 
//STEP 4: Update manifest.xaml ... 
//STEP 5:  Add this JS file as a reference in default.html  (or your entry point) 


SLx.ControlFactory.Hyperlink = function () 
{ 
    var template = SLx.Application.Current.get_resources().findName ( SLx.Constants.Hyperlink); 
    if ( template  != "" ) 
    {     
        var Hyperlink= new SLx.Hyperlink ( null,  template , SLx.Hyperlink.DefaultTemplate );          
        return Hyperlink ; 
        
    } 
    Sys.Debug.assert( false , "this should not fail");     
    return null ; 
} 


SLx.Hyperlink = function ( visual , xamlTemplate , templateParts )
{    
    SLx.Hyperlink.initializeBase (this , [ visual , xamlTemplate , templateParts ] );   
    this._url = null ; 
    this._autoNavigate = true ; 
    this._clickListeners= [] ;     
    this._addHandler ( this._visualElement ,  "MouseLeftButtonUp" , $delegate ( this, this._onMouseUp ));

     
}, 


SLx.Hyperlink.prototype = 
{   
    set_Url : function ( value ) { this._url = value ; }, 
    get_Url : function ( ) { return this._url ; }, 
    
    set_autoNavigate : function ( value ) 
    { 
        this._autoNavigate = value ; 
    }, 
    
    get_autoNavigate : function ( value ) 
    {
        return this._autoNavigate ;  
    },  
    
   // overrides the mouse enter wired by Control ...
   _onMouseEnter : function ( sender, args ) 
   { 
        if (this._textblock ) this._textblock.TextDecorations = SLx.TextDecorations.Underline; 
        SLx.Label.callBaseMethod ( this , "_onMouseEnter", [sender, args] ); 
   }, 
   
   // overrides the mouse enter wired by Control ...
   _onMouseLeave : function ( sender, args ) 
   { 
        if (this._textblock ) this._textblock.TextDecorations = SLx.TextDecorations.None ; 
        SLx.Label.callBaseMethod ( this , "_onMouseLeave", [sender, args] ); 
   },
    
    add_Click : function SLx$Button$add_Click ( handler  ) 
    { 
        Array.add ( this._clickListeners, handler );          
    }, 
    
    
    remove_Click : function SLx$Button_removeClick ( handler ) 
    { 
        for ( var i = 0 ; i < this._clickListeners.length ; i++ ) 
        { 
            if ( this._clickListeners[i] == handler ) 
            { 
                Array.remove ( this._clickListeners, handler ); 
            } 
        } 
    },      
    
    _onMouseUp : function SLx$Button$_onMouseUp ( sender , args ) 
    {    
        if ( this.get_isEnabled ()) 
        {  
            this._onClick ( sender, args );         
            this._doNavigate ( ); 
        } 
    },  
    
    _doNavigate : function (  ) 
    { 
        if ( this._url != null && this._autoNavigate ) 
        { 
           var wnd=  window.open ( this._url );        
           if ( wnd == null ) 
           { 
                alert ('open window failed. Might need to disable popup blocker'); 
           } 
        }     
    }, 
    
    _onClick: function ( sender, args ) 
    { 
        for ( var x = 0 ; x < this._clickListeners.length ; x++  ) 
        { 
            this._clickListeners[x] ( this, new SLx.EventArgs ( sender ,args) ); 
        }
    }, 
    
    
    dispose: function () 
    { 
        if  ( this._content != null ) 
            this._content.dispose(); 
            
        SLx.Hyperlink.callBaseMethod( this, "dispose", [] );         
    }, 
    
    toString : function  () 
    { 
        return this._uniqueSystemId.toString ()  + "    " + Object.getTypeName(this) + "    " + this._id ; 
    }
}  


SLx.Hyperlink.registerClass('SLx.Hyperlink', SLx.Label ); 


SLx.Hyperlink.DefaultTemplate = [ 
{ name: "Hyperlink" , ref: "_visualElement" , type: "element" }, 
{ name: "PART_Text" , ref: "_textblock"  , type: "element" } 
]; 


