﻿ItemEditor = function (type, input, literal, user, item)
{
    this._type = type;
    this._user = user;
    this._item = item;
    this._input = input;
    this._literal = $("#" + literal);
};
ItemEditor.prototype = 
{
    ChangeItemAsynch: function ()
    {
        var inputs = $("input[@id$=tbItemName]");
       
        if (inputs[1].value != "" && inputs[1].value != this._literal.text())
        {
            $.post("../WebServices/Kitchen.ashx?ChangeItemName", {type: this._type, userId: this._user, itemId: this._item, name: inputs[1].value },
               function(data) {
                   eval("editor").ChangeCompleated(data);
               }, 'json');
        }
    },
    ChangeCompleated: function (data)
    {
        if (data.success)
        {
            var inputs = $("input[@id$=tbItemName]");
            inputs[0].value = inputs[1].value;
            
            this._literal.text(inputs[1].value);
            
            try
            {
                inputs[0].defaultValue = inputs[0].value;
            } catch (e)
            {
            }
        }
        $("#fancy_close").trigger('click');
    }
};

Ingredient = function (name, weight, id)
{
    this._name = $("<input type='text' id='name_" + id +"' name='name_" + id +"' maxlength='200' />");
    this._name[0].value = name;
    
    this._weight = $("<input type='text' id='weight_" + id +"' name='weight_" + id +"' maxlength='10' />");
    this._weight[0].value = weight;
    
    this._id = id;
};
Ingredient.prototype = 
{
    get_Name: function ()
    {
        return this._name;
    },
    get_Weight: function ()
    {
        return this._weight;
    },
    get_Id: function ()
    {
        return this._id;
    }
};

IngredientList = function (container)
{
    this._container = $("#" + container);
    this._ingredients = $.makeArray();
    
    this.Init();
};
IngredientList.prototype = 
{
    Init: function ()
    {
        if (this._ingredients.length == 0)
        {
            this._ingredients.push(new Ingredient("", "", this._ingredients.length));
        }
        this.Refresh();
    },
    Add: function (ingredient)
    {
        this._ingredients.push(ingredient);
        this.Refresh();
    },
    Remove: function (index)
    {
        var tmp = $.makeArray();
        for (indx in this._ingredients)
        {
            if (indx != index)
            {
                tmp.push(this._ingredients[indx]);
            }
        }
        this._ingredients = tmp;
        this.Refresh();
    },
    Count: function ()
    {
        return this._ingredients.length;
    },
    Refresh: function ()
    {
        this._container.empty();
        
        var bnadd = $("<a href='#'>Добавить к Списку покупок</a>");
        bnadd.bind("click", function () 
        {
            eval("ingredients").Add(new Ingredient("", "", eval("ingredients").Count()));
            return false;
        });
        var divAdd = $("<div class=\"add\"></div>") 
        divAdd.append(bnadd);
        this._container.append(divAdd);
             
        var tableAdd = $("<table cellpadding=\"0\" cellspacing=\"0\" class=\"add\"></table>");
        
        if (this._ingredients.length > 0)
        {
            tableAdd.append("<tr class=\"tr\"><td class=\"title\"></td><td class=\"quantity\">кол-во</td><td></td></tr>");
        }
        
        for (var indx in this._ingredients)
        {
            var tr = $("<tr></tr>");
            var tdTitle = $("<td class=\"title\"></td>");
            var tdQuantity = $("<td class=\"quantity\"></td>");
            var tdDelete = $("<td class=\"quantity\"></td>");
            

            tdTitle.append(this._ingredients[indx].get_Name());
            tdQuantity.append(this._ingredients[indx].get_Weight());
            var bndelete = $("<a href='#' id='delete_" + indx +"' title=\"удалить\"><img src=\"/i/icon-delete.png\" alt=\"удалить\" /></a>")
            bndelete.bind("click", function () 
            {
                eval("ingredients").Remove(this.id.replace(/delete_/g, ""));
                return false;
            });
            tdDelete.append(bndelete);
            
            tr.append(tdTitle);
            tr.append(tdQuantity);
            tr.append(tdDelete);
            
            tableAdd.append(tr);
        }
        
        if (this._ingredients.length > 0)
        {
             this._container.append(tableAdd);
        }
    }
};

ButtonManaged = function (id, active, recipe)
{
    this._button = $("#" + id);
    this._active = active;
    this._recipe = recipe;
    this._image = $("<img />");
    
    this.Init();
};
ButtonManaged.prototype = 
{
    Init: function ()
    {
        this._button.append(this._image);
    },
    get_Active: function ()
    {
        return this._active;
    },
    set_Active: function (active)
    {
        this._active = active;
    },
    get_Recipe: function ()
    {
        return this._recipe;
    },
    get_Button: function ()
    {
        return this._button;
    },
    get_Image: function ()
    {
        return this._image;
    }
};

ButtonManager = function(application, img_act, img_dis, onclick_act, onclick_dis, title_act, title_dis, type, returnname, returnurl, item) {
    this._application = application;

    this._img_active = img_act;
    this._img_disabled = img_dis;

    this._onclick_active = onclick_act;
    this._onclick_disabled = onclick_dis;

    this._title_active = title_act;
    this._title_disabled = title_dis;

    this._type = type;
    this._item = item;

    this._returnname = returnname;
    this._returnurl = returnurl;

    this._buttons = $.makeArray();
}
ButtonManager.prototype =
{
    Register: function(button) {
        this._buttons.push(button);
    },
    Refresh: function() {
        for (var indx in this._buttons) {
            var button = this._buttons[indx];

            if (button.get_Active()) {
                button.get_Button().attr("title", this._title_active);
                button.get_Button().bind("click", function() {
                    eval("manager").ManageItemAsynch(this.id);
                    return false;
                }
                );

                button.get_Image().attr("alt", this._title_active);
                button.get_Image().attr("src", this._application + this._img_active);
            }
            else {
                button.get_Button().attr("title", this._title_disabled);
                button.get_Button().attr("onclick", "javascript:alert('" + this._onclick_disabled + "'); return false;");

                button.get_Image().attr("alt", this._title_disabled);
                button.get_Image().attr("src", this._application + this._img_disabled);
            }
        }
    },
    ManageItemAsynch: function(id) {
        for (var indx in this._buttons) {
            if (this._buttons[indx].get_Button()[0].id == id && this._buttons[indx].get_Active()) {
                $.post(this._application + "/WebServices/Kitchen.ashx?ManageMyKitchen", { type: this._type, itemId: this._item, recipeId: this._buttons[indx].get_Recipe() },
                   function(data) {
                       eval("manager").ManageCompleated(data);
                   }, 'json');
                break;
            }
        }
    },
    ManageCompleated: function(data) {
        if (data.success) {
            for (var indx in this._buttons) {
                if (this._buttons[indx].get_Recipe() == data.recipe && this._buttons[indx].get_Active()) {
                    var button = this._buttons[indx];

                    button.set_Active(false);
                    button.get_Button().attr("title", this._title_disabled);
                    button.get_Button().attr("onclick", "javascript:alert('" + this._onclick_disabled + "'); return false;");

                    button.get_Image().attr("alt", this._title_disabled);
                    button.get_Image().attr("src", this._application + this._img_disabled);

                    this.Message(this._onclick_active);
                    return;
                }
            }
        }
    },
    Message: function(message) {
        $("div#alertPopUpInner").empty();
        $("div#alertPopUpInner").append($("<div class='title'></div>").text(message));

        var ok = $("<a class='ok'>OK</a>");
        ok.attr("href", "javascript:CloseFancyPopup();");

        $("div#alertPopUpInner").append(ok);

        var link = $("<a class='link'></a>");
        link.text(this._returnname);
        link.attr("href", this._returnurl);

        $("div#alertPopUpInner").append(link);

        $("a#fancyboxPopupLink").trigger('click');
    }
};

function CloseFancyPopup() {
    $('#fancy_close').trigger('click');
}

function getStar(num) {
    for (var i = 1; i < num + 1; i++) {
        document.getElementById(i).src = "i/star-f.png";
    }
    for (var i = 5; i > num; i--) {
        document.getElementById(i).src = "i/star-e.png";
    }

    $("input[@id$=hfVote]").each(function() {
        this.value = num;
    });
    
    return true;
}

$(document).ready(function() {
    $("a.sl").click(function() {
        if ($(".smiles-box").is(":hidden")) {
            $(".smiles-box").slideDown("slow");
        } 
        else {
            $(".smiles-box").hide("slow");
        }
    });
});
