/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

function list(language) {
    window.open("/food/" +language+ "/book.do?method=viewCart", "cart");
   /* $.ajax({
        url:"/food/book.do",
        type:"post",
        data:({
            "method":"viewCart",
             "products":$.cookie('cart')
        }),
        success:function(data){
            $("body").append("<div id='order'></div>")
            $("#order").append(data);
            $("#order").modal({
                onClose:function(){
                    $.modal.close();
                    $("#order").remove();
                }
            });
        }
    });*/
}

function Product(id, num) {
    this.id = id;
    if (num != undefined && num != null && num!="") {
        this.num = num;
    } else {
        this.num = 1;
    }
}

function Cart() {
    this.cookieSet = { expires: 7, path: '/' };
}

Cart.prototype.addProduct = function(product) {
    if ($.cookie('cart') == null) {
        var products = new Array();
        products[0] = product;
        var jsonProduct = JSON.stringify(products);
        $.cookie('cart', jsonProduct , this.cookieSet);
    } else {
        var jsonObj = JSON.parse($.cookie('cart'));
        var findProduct = false;
        for (var obj in jsonObj) {
            if (jsonObj[obj].id == product.id) {
                jsonObj[obj].num = String(Number(jsonObj[obj].num) + Number(product.num));
                findProduct = true;
                break;
            }
        }
        if (findProduct == false) {
            jsonObj[jsonObj.length] = product;
        }

        $.cookie('cart', JSON.stringify(jsonObj), this.cookieSet);
        $.cookie('cart2',jsonObj,this.cookieSet);

    }
}

Cart.prototype.deleteProduct = function(id) {
    var jsonObj = JSON.parse($.cookie('cart'));
    for (var obj in jsonObj) {
        if (jsonObj[obj].id == id) {
            jsonObj.splice(obj, 1);
            break;
        }
    }

    if (jsonObj.length == 0){
        $.cookie('cart',null,{expires:-1, path: '/'} );
    } else {
        $.cookie('cart', JSON.stringify(jsonObj), this.cookieSet);
    }
}


Cart.prototype.deleteCart = function(){
    $.cookie('cart',null,{expires:-1, path: '/'});
}

Cart.prototype.listProduct = function() {
    if ($.cookie('cart') != null) {
        var jsonObj = JSON.parse($.cookie('cart'));
        return jsonObj;
    }
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



