/// <reference name="MicrosoftAjax.js"/>
/// <reference name="SilverlightControl.js"/>

personElements = function() {
    this.leftarm = null;
    this.rightarm = null;
    this.head = null;
    this.leftleg = null;
    this.rightleg = null;
    this.torso = null;
} 
personElements.prototype = {
    initializeComponent: function(slhost) {
        var host = slhost.content;

        this.leftarm = host.findName("leftarm");
        this.rightarm = host.findName("rightarm");
        this.head = host.findName("head");
        this.leftleg = host.findName("leftleg");
        this.rightleg = host.findName("rightleg");
        this.torso = host.findName("torso");
    }
}

Type.registerNamespace("Custom");

Custom.SLPerson = function(element) {
    Custom.SLPerson.initializeBase(this, [element]);

    this._designer = new personElements();
}

Custom.SLPerson.prototype = {
    initialize : function() {
        Custom.SLPerson.callBaseMethod(this, 'initialize');

        // Call on the component initialized to get the specific component's XAML element fields.
        this._designer.initializeComponent(this.get_element());

        // Hookup event handlers as required in this custom type.
        var onClick = Function.createDelegate(this, this._onPersonClick);

        this.addEventListener(this._designer.leftarm, "mouseLeftButtonUp", onClick);
        this.addEventListener(this._designer.rightarm, "mouseLeftButtonUp", onClick);
        this.addEventListener(this._designer.head, "mouseLeftButtonUp", onClick);
        this.addEventListener(this._designer.leftleg, "mouseLeftButtonUp", onClick);
        this.addEventListener(this._designer.rightleg, "mouseLeftButtonUp", onClick);
        this.addEventListener(this._designer.torso, "mouseLeftButtonUp", onClick);
    },

    _onPersonClick : function(sender, e) {
        var region = sender.Name;
        this.toggleVisibility(region);

        var elem = $get(region + "CheckBox");
        elem.checked = !elem.checked;
    },

    toggleVisibility : function(region) {
        var elem = this._designer[region];
        var wasVisible = (elem.children.getItem(1).Visibility == "Visible");
        elem.children.getItem(1).Visibility = wasVisible ? "Collapsed" : "Visible";
    }
}
Custom.SLPerson.registerClass('Custom.SLPerson', Sys.UI.Silverlight.Control);


//var foo = new Sys.UI.Silverlight.Control();
//foo.initialize