﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 

//STEP 1: Replace ComboBox w/ your controls' name 
//STEP  2: Move Constants.ComboBox 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.ComboBox = function () 
{ 
    var template = SLx.Application.Current.get_resources().findName ( SLx.Constants.ComboBox ); 
    if ( template  != "" ) 
    {     
        var ComboBox= new SLx.ComboBox ( null,  template , SLx.ComboBox.DefaultTemplate );          
        return ComboBox ; 
        
    } 
    Sys.Debug.assert( false , "this should not fail");     
    return null ; 
} 


SLx.ComboBox = function ( visual , xamlTemplate , templateParts )
{    
    SLx.ComboBox.initializeBase (this , [ visual , xamlTemplate , templateParts ] );   
    
   //  this._initListBox () ; 
    
}, 

 
SLx.ComboBox.prototype = 
{   
    
    _initListBox : function ( ) 
    { 
    
        Sys.Debug.assert ( this._content.get_hasContent() ); 
        this._listBox = this._content.get_content(); 
        
        Sys.Debug.assert ( SLx.VisualTreeHelper.isContainer( this._listBox )); 
        
        
        this._clickDelegate = $delegate ( this, this._selectionChanged ); 
        var childrencollection = this._listBox.get_children(); 
        for ( var i = 0 , len = childrencollection .get_length() ; i < len  ; i++  ) 
        { 
            var item = childrencollection .getItem(i); 
            // Sys.Debug.assert ( Object.getTypeName ( item ) == "SLx.ToggleButton") ;             
            item.add_Checked ( this._clickDelegate );         
        } 
    }, 
    
    
    _selectionChanged : function ( sender, args ) 
    { 
        if ( this._lastItemSelected != null ) 
        { 
            this._lastItemSelected.set_isChecked ( false ); 
        }             
        sender.set_isChecked ( true ); 
        this._lastItemSelected = sender; 
        
        Sys.Debug.assert ( sender.get_content().get_hasContent() == false ) ; 
        Sys.Debug.assert ( sender.get_content().get_visual().Children.Count == 1 ) ; 
        
        var textBlock = sender.get_content().get_visual().Children.GetItem(0) ; 
        var text = textBlock.Text; 
        this._toggleHeader.set_content(text); 
        
        this.set_isExpanded ( false );                     
    }, 
    
    set_listBox : function ( value ) 
    { 
        this._content.set_content ( value ); 
        this._initListBox (); 
    } , 
    
    _bindParts : function SLx$Expander$_bindParts ( templateParts ) 
    {    
       var tag ;                          
         for ( var i = 0 ; i < templateParts .length ; i++ ) 
        { 
            var item = templateParts[i];              
              var element; 
                if ( this._visualElement != null ) 
                    element = this._visualElement.findName ( item.name ) ; 
                
                if ( element == null )  
                    element = SLx.Application.Current.get_host().content.findName ( item.name ) ;  
                
                if ( element != null && element.Tag != "" ) 
                { 
                    tag = eval ( "({" + element.Tag + "})" ) ; 
                } 
                else 
                    tag = null ; 
                            
            if ( item.type == "element" ) 
            { 
                this[item.ref] = element ;                             
            } 
            else if  ( item.type == "BasicDockPanel" || item.type == "StackPanel" ) 
            { 
                
                if ( item.type == "StackPanel" ) 
                {
                    var panel = new SLx.StackPanel ( element , true ); 
                } 
                else 
                    var panel = new SLx.BasicDockPanel ( element , false ); 
                        
                this[item.ref] = panel ; 
                               
            } 
            else if ( item.type  == "Checkbox" ) 
            { 
                //TODO: how to pass these parameters in..?  
                 var check = new SLx.ToggleButton ( element , null, 
                [ {name : "ComboBoxToggle"  , ref : "_visualElement", type : "element" },
                  {name : "PART_ComboBoxToggle_Content"   , ref : "_content", type : "ContentPresenter" }]);               
                  this[item.ref] = check ;                                                              
            }            
            else if ( item.type == "ContentPresenter") 
            { 
                var cp = new SLx.ContentPresenter ( element ) ;                                 
                this[item.ref] = cp ;                                
            }
            else 
            { 
                Sys.Debug.assert(false, "we skipped a type"); 
            }                   
        }                  
    } , 

    
    toString : function () 
    { 
    }
}  


SLx.ComboBox.DefaultTemplate = [ 
{ name: "ComboBox" , ref: "_visualElement", type: "element"  } , 
{ name: "ComboBox" , ref: "_panel", type: "StackPanel" , instantiate:true   } , 
{ name: "ComboBoxToggle" , ref: "_toggleHeader", type: "Checkbox" , instantiate:true   } , 
{ name: "PART_ContentSite" , ref: "_content", type: "ContentPresenter" }  
]; 


SLx.ComboBox.registerClass('SLx.ComboBox', SLx.Expander  ); 




