﻿///<reference path="../../../../Asserter.js"/>
///<reference path="../../../Element.js"/>
///<reference path="../../../Button.js"/>

var njs;
if (!njs) njs = {};
if (!njs.ui) njs.ui = {};
if (!njs.ui.widgets) njs.ui.widgets = {};
if (!njs.ui.widgets.buttons) njs.ui.widgets.buttons = {};

njs.ui.widgets.buttons.NineSliceButton = function (caption, onClickEvent, disableable) 
{
    njs.ui.widgets.buttons.NineSliceButton.Instance = this;
    
    this.base = njs.ui.Button;
    this.base(onClickEvent);
    
    this.IsDisableable = (njs.Asserter.IsNullOrEmpty(disableable)) ? false : true;
    
    this.SetType('njs.ui.widgets.buttons.NineSliceButton');
    this.SetClass('njs-buttons-NineSliceButton');
   
    var table =  new njs.ui.Element(document.createElement('table'));
    var buttonBody = new njs.ui.Element(document.createElement('tbody'));
    
    //Button Rows
    var topRow = new njs.ui.Element(document.createElement('tr'));
    var midRow = new njs.ui.Element(document.createElement('tr'));
    var bottomRow = new njs.ui.Element(document.createElement('tr'));
    
    //Button Cells
    var tlCell = new njs.ui.Element(document.createElement('td'));
    var tmCell = new njs.ui.Element(document.createElement('td'));
    var trCell = new njs.ui.Element(document.createElement('td'));
    
    var mlCell = new njs.ui.Element(document.createElement('td'));
    var mmCell = new njs.ui.Element(document.createElement('td'));
    var mrCell = new njs.ui.Element(document.createElement('td'));
    
    var blCell = new njs.ui.Element(document.createElement('td'));
    var bmCell = new njs.ui.Element(document.createElement('td'));
    var brCell = new njs.ui.Element(document.createElement('td'));

    //Set Class Attributes
    table.SetClass(this.GetChildClass('table'));
    tlCell.SetClass(this.GetChildClass('topLeftCornerCell'));
    trCell.SetClass(this.GetChildClass('topRightCornerCell'));
    blCell.SetClass(this.GetChildClass('bottomLeftCornerCell'));
    brCell.SetClass(this.GetChildClass('bottomRightCornerCell'));
    
    tmCell.SetClass(this.GetChildClass('topHorizontalCell'));
    bmCell.SetClass(this.GetChildClass('bottomHorizontalCell'));
    mlCell.SetClass(this.GetChildClass('leftVerticalCell'));
    mrCell.SetClass(this.GetChildClass('rightVerticalCell'));
    
    mmCell.SetClass(this.GetChildClass('textCell'));
    mmCell.AddText(caption);
    
    //Add Cells to rows
    topRow.Add(tlCell);
    topRow.Add(tmCell);
    topRow.Add(trCell);
    
    midRow.Add(mlCell);
    midRow.Add(mmCell);
    midRow.Add(mrCell);
    
    bottomRow.Add(blCell);
    bottomRow.Add(bmCell);
    bottomRow.Add(brCell);
    
    //Add Rows to Button
    buttonBody.Add(topRow);
    buttonBody.Add(midRow);
    buttonBody.Add(bottomRow);
    table.Add(buttonBody);
    
    table.SetAttribute('cellspacing','0');
    table.SetAttribute('cellpadding','0');
    
    //Add Events
    //if (!njs.Asserter.IsNullOrEmpty(clickEvent)) {
    this.AddEvent('mousedown', this.OnMouseDown);
    this.AddEvent('mouseover', this.OnMouseOver);
    this.AddEvent('mouseout', this.OnMouseOut);
    //}
    this.Add(table);
    this.SetColor();
}
njs.ui.widgets.buttons.NineSliceButton.prototype = new njs.ui.Button;  

njs.ui.widgets.buttons.NineSliceButton.prototype.OnClick = function(event) {
    event = event || window.event;
    var sourceElement = event.target || event.srcElement;
    if (sourceElement.tagName == 'TD') {
        var table = new njs.ui.Element(sourceElement.parentNode.parentNode.parentNode);
        table.SetClass('njs-buttons-NineSliceButton-tableMouseClick');
    }
}

njs.ui.widgets.buttons.NineSliceButton.prototype.OnMouseDown = function(event) {
    event = event || window.event;
    var sourceElement = event.target || event.srcElement;
    if (sourceElement.tagName == 'TD') {
        var table = new njs.ui.Element(sourceElement.parentNode.parentNode.parentNode);
        table.SetClass('njs-buttons-NineSliceButton-tableMouseDown');
    }
}

njs.ui.widgets.buttons.NineSliceButton.prototype.OnMouseOver = function(event) {
    event = event || window.event;
    var sourceElement = event.target || event.srcElement;
    if (sourceElement.tagName == 'TD') {
        var table = new njs.ui.Element(sourceElement.parentNode.parentNode.parentNode);
        table.SetClass('njs-buttons-NineSliceButton-tableMouseOver');
    }
}

njs.ui.widgets.buttons.NineSliceButton.prototype.OnMouseOut = function(event) {
    event = event || window.event;
    var sourceElement = event.target || event.srcElement;
    if (sourceElement.tagName == 'TD') {
        var table = new njs.ui.Element(sourceElement.parentNode.parentNode.parentNode);
        table.SetClass('njs-buttons-NineSliceButton-table');
    }
}
njs.ui.widgets.buttons.NineSliceButton.prototype.SetColor = function(color) {
    var buttonColor = (njs.Asserter.IsNullOrEmpty(color) || typeof color != 'string') ? 'default' : color;
    this.SetClass('njs-buttons-NineSliceButton-' + buttonColor);
}

njs.ui.widgets.buttons.NineSliceButton.prototype.Disabled = function() {
    
    njs.ui.widgets.buttons.NineSliceButton.Instance.SetClass('njs-buttons-NineSliceButton-disabled');
    this.RemoveEvent('mouseover', this.OnMouseOver);
    njs.ui.widgets.buttons.NineSliceButton.Instance.Disable();
}

njs.ui.widgets.buttons.NineSliceButton.prototype.Enabled = function() {
    
    njs.ui.widgets.buttons.NineSliceButton.Instance.SetClass('njs-buttons-NineSliceButton');
    this.AddEvent('mouseover', this.OnMouseOver);
    njs.ui.widgets.buttons.NineSliceButton.Instance.Enable();
}
