﻿designerCalc = function() {
    this.n7 = null;
    this.buttonBG = null;
    this.n8 = null;
    this.buttonBG1 = null;
    this.fequals = null;
    this.buttonBG2 = null;
    this.fplus = null;
    this.buttonBG3 = null;
    this.n0 = null;
    this.buttonBG4 = null;
    this.n3 = null;
    this.buttonBG5 = null;
    this.n2 = null;
    this.buttonBG6 = null;
    this.n1 = null;
    this.buttonBG7 = null;
    this.n6 = null;
    this.buttonBG8 = null;
    this.n5 = null;
    this.buttonBG9 = null;
    this.n4 = null;
    this.buttonBG10 = null;
    this.n9 = null;
    this.buttonBG11 = null;
    this.fminus = null;
    this.buttonBG12 = null;
    this.fdivide = null;
    this.buttonBG13 = null;
    this.fmultiply = null;
    this.buttonBG14 = null;
    this.result = null;
    this.tb1 = null;
} 
designerCalc.prototype = {
    initializeComponent: function(slhost) {
        var host = slhost.content;
        
        this.n0 = host.findName("n0");
        this.n1 = host.findName("n1");
        this.n2 = host.findName("n2");
        this.n3 = host.findName("n3");
        this.n4 = host.findName("n4");
        this.n5 = host.findName("n5");
        this.n6 = host.findName("n6");
        this.n7 = host.findName("n7");
        this.n8 = host.findName("n8");
        this.n9 = host.findName("n9");
        this.buttonBG = host.findName("buttonBG");
        this.buttonBG1 = host.findName("buttonBG1");
        this.buttonBG2 = host.findName("buttonBG2");
        this.buttonBG3 = host.findName("buttonBG3");
        this.buttonBG4 = host.findName("buttonBG4");
        this.buttonBG5 = host.findName("buttonBG5");
        this.buttonBG6 = host.findName("buttonBG6");
        this.buttonBG7 = host.findName("buttonBG7");
        this.buttonBG8 = host.findName("buttonBG8");
        this.buttonBG9 = host.findName("buttonBG9");
        this.buttonBG10 = host.findName("buttonBG10");
        this.buttonBG11 = host.findName("buttonBG11");
        this.buttonBG12 = host.findName("buttonBG12");
        this.buttonBG13 = host.findName("buttonBG13");
        this.buttonBG14 = host.findName("buttonBG14");
        this.fequals = host.findName("fequals");
        this.fplus = host.findName("fplus");
        this.fminus = host.findName("fminus");
        this.fdivide = host.findName("fdivide");
        this.fmultiply = host.findName("fmultiply");
        this.result = host.findName("result"); 
  }
}
Type.registerNamespace("Custom");

Custom.Calculator = function(element) {
	Custom.Calculator.initializeBase(this, [element]);
	
	this._designer = new designerCalc();
	this._value1 = "";
    this._operator = "=";
    this._recentOperator = true;
}

Custom.Calculator.prototype = {
    _events: null,
	xamlInitialize : function() {
		Custom.Calculator.callBaseMethod(this, 'xamlInitialize');

        // 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 f = Function.createDelegate(this, this._numClick);
		
		this.addEventListener(this._designer.n0, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n1, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n2, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n3, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n4, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n5, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n6, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n7, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n8, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.n9, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.fplus, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.fminus, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.fmultiply, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.fequals, "mouseLeftButtonUp", f);
		this.addEventListener(this._designer.fdivide, "mouseLeftButtonUp", f);
				
		// Set initial value
		this._designer.result.Text = "0";
	},
	_numClick : function(sender, e) {
        var s = sender.children.getItem(2).Text;

        if ("=-+/*".indexOf(s) !== -1) {
            if (!this._recentOperator) {
                if (this._operator === "=") {
                    this._value1 = this._designer.result.Text;
                }
                else {
                    if ((this._operator === "/") && (parseFloat(this._designer.result.Text) === 0)) {
                        this._designer.result.Text = "Error";
                        this._value1 = "0";
                        s = "=";
                    }
                    else {
                        var value = eval("parseFloat(this._value1) " + this._operator + " parseFloat(this._designer.result.Text);");
                        value = String(value).substr(0, 12);
                        this._value1 = value;
                        this._designer.result.Text = this._value1;
                    }
                }
            }

            this._operator = s;
            this._recentOperator = true;
            return;
        }

        if (this._recentOperator) {            
            this._designer.result.Text = s;
            this._recentOperator = false;
        }
        else {
            this._designer.result.Text += s;
        }
	}
}
Custom.Calculator.registerClass('Custom.Calculator', Sys.Preview.UI.Xaml.Control);

