function DrinkMakerControl(chart)
{
    this.chart = chart;

    this.lister = document.getElementById('ingredientList');

    this.ingredientList = [];

    this.ingredientChoice = function(id,unit,amount,specname, element)
    {
        this.id = id;
        this.unit = unit;
        this.amount = amount;
        this.specname = specname;
        this.element = element;
    };

    this.ingredientExists = function(id)
    {
        var I;
        for(I = 0; I < this.ingredientList.length; I++)
        {
            if (this.ingredientList[I].id == id)
            {
                return I;
            }
        }

        return false;
    };

    this.redrawChart = function()
    {
        var query = "./ajax/builderChartDat.php"+this.getQueryString();

        this.chart.setDataURL(query);
    };

    this.getQueryString = function()
    {
        var query = "?";

        var I = 0;
        for (I = 0; I < this.ingredientList.length; I++)
        {
            query += "ing"+I+"="+encodeURIComponent(this.ingredientList[I].id)+"&";
            query += "unit"+I+"="+encodeURIComponent(this.ingredientList[I].unit)+"&";
            query += "amnt"+I+"="+encodeURIComponent(this.ingredientList[I].amount)+"&";
            query += "name"+I+"="+encodeURIComponent(this.ingredientList[I].specname)+"&";
        }

        return query;
    };

    this.getQueryStringKeyForm = function()
    {
        var query = {};

        var I = 0;
        for (I = 0; I < this.ingredientList.length; I++)
        {
            query["ing"+I] = this.ingredientList[I].id;
            query["unit"+I] = this.ingredientList[I].unit;
            query["amnt"+I] = this.ingredientList[I].amount;
            query["name"+I] = this.ingredientList[I].specname;
        }

        return query;
    };

    this.loadNutritionLabel = function()
    {
        $.ajax({
              url: "./ajax/builderNutritionLabel.php" + this.getQueryString(),
              data: null,
              success: function (a,b,c){
                  $("#nutDiv").html(a);
              },
              dataType: "HTML"
              });
    };

    this.blinker = function(count, element, timeout)
    {
        if (count % 2 == 1)
        {
            element.className = '';
        }
        else
        {
            element.className='attention';
        }

        if (count > 1)
        {
            var par = this;

            setTimeout( function () {
            par.blinker(count - 1, element, timeout);
            }, timeout);
        }
        else
        {
            element.className='';
        }
    }

    this.doAddIngredient = function()
    {
        var genericPicker = document.getElementById('ingredientSelect');
        var amountField = document.getElementById('ingredientAmount');
        var unitPicker = document.getElementById('ingredientUnit');

        var selectID = genericPicker.options[genericPicker.selectedIndex].value;
        var selectName = genericPicker.options[genericPicker.selectedIndex].text;
        var selectAmount = amountField.value;
        var selectUnitID = unitPicker.options[unitPicker.selectedIndex].value;
        var selectUnitName = unitPicker.options[unitPicker.selectedIndex].text;

        //Check for input issues
        if (this.ingredientExists(selectID) !== false) //Check name
        {
            $(this.ingredientList[this.ingredientExists(selectID)].element).addClass('attention');

            var item = this.ingredientList[this.ingredientExists(selectID)].element;
            var item2 = document.getElementById('ingredientLabel');


            this.blinker(6, item, 400);
            this.blinker(6, item2, 400);
        }
        else if (isNaN(selectAmount) || selectAmount <= 0) //Check for valid amount
        {
            var item3 = document.getElementById('amountLabel');

            this.blinker(6, item3, 400);
            amountField.select();
        }
        else
        {
            var NewLI = document.createElement("li");
            NewLI.innerHTML = "<a onclick='javascript:drinkMaker.removeItem("+selectID+")' class='deleteIngredient'><img src='./img/smallacts/cross.png' alt='X' /></a>&nbsp;"+selectAmount+" "+selectUnitName+" of "+selectName;
            this.lister.appendChild(NewLI);
            this.ingredientList.push(new this.ingredientChoice(selectID,selectUnitID,selectAmount,selectName,NewLI));

            this.redrawChart();
            this.loadNutritionLabel();
        }
    };



    this.clearIngredients = function()
    {
        this.lister.innerHTML = '';
        this.ingredientList = [];
        this.redrawChart();
        this.loadNutritionLabel();
    };

    this.refresh = function()
    {
        this.loadNutritionLabel();
        this.redrawChart();
    };

    this.init = function()
    {
        this.loadNutritionLabel();
    };

    this.submitRecipe = function()
    {
        var nameEle = document.getElementById('submitName');
        var descEle = document.getElementById('submitDesc');

        var query = this.getQueryStringKeyForm();

        query["spec0"] = nameEle.value;
        query["spec1"] = descEle.value;

        var par = this;

        $.post("./ajax/submitDrink.php", query,
          function(data){
            if (data.success == true)
            {
                javascript:drinkMaker.showMessage(false, "Drink submitted to database. (Not currently viewable)");
                nameEle.value = '';
                descEle.value = '';
                par.clearIngredients();
            }
            else
            {
                javascript:drinkMaker.showMessage(true, "Error: "+data.message);
            }
          }, "json");
    };

    this.removeItem = function(id)
    {
        var exist = this.ingredientExists(id);
        if (exist !== false)
        {
            var list = document.getElementById('ingredientList');
            list.removeChild(this.ingredientList[exist].element);

            //Cut the value out of the internal list
            this.ingredientList.splice(exist, 1);

            //Refresh
            this.refresh();
        }
    };

    this.showMessage = function(error, message)
    {
        var box = document.getElementById('msgDisplay');
        var msg = document.getElementById('message');
        if (error === true)
        {
            box.style.backgroundColor = '#dc2727';
        }
        else
        {
            box.style.backgroundColor = '#CCCCCC';
        }

        var text = document.createTextNode(message);
        msg.innerHTML = '';
        msg.appendChild(text);

        //when the close button at right corner of the message box is clicked
        $('#close_message').click(function()
        {
          //the messagebox gets scrool down with top property and gets hidden with zero opacity
          box.style.visibility = 'hidden';
        });

        box.style.visibility = 'visible';
    }

}