function getCities(el){
    var town = $(el).val();
    town = town*1;
    var obj = $("#city");
    obj.children("option").remove();
    if(town){
        $.get("/api/cities/citiesoftown/town/"+town, function(data){
            $.each(data.cities, function(i, val){
                obj.append('<option value="'+val.id+'">'+val.name+'</option>');
            });
        }, "json");
        obj.removeAttr("disabled");
    }
}

function addToCart(item){
    var amount = $("#amountOfProducts").val();
    $.get("/api/cart/addtocart", {"item": item, "amount": amount*1}, function(data){
        if(data.success){
            $("#totalCartProducts").html(data.products);
            $("#totalCartPrice").html(data.price);

            modalWindow.close();
            modalWindow.width = 360;
            modalWindow.height = 115;
            modalWindow.content = '<div style="text-align: center;"><p><b>Добавихте продуктите в пазарската количка успешно!</b></p><a style="margin: 0px 2px 0px 70px;" class=\"modal-button\" href="#" onclick="modalWindow.close(); return false;">Към магазина</a><a class=\"modal-button\" href="/order">Към касата</a></div>';
            modalWindow.open();
        }else{
            modalWindow.close();
            modalWindow.width = 360;
            modalWindow.height = 115;
            modalWindow.content = '<div style="text-align: center;"><p><b>ГРЕШКА! Моля, презаредете страницата и опитайте отново!</b></p></div>';
            modalWindow.open();
        }
    }, "json");
}

function windowAddToCart(item){
    modalWindow.width = 360;
    modalWindow.height = 115;
    modalWindow.content = '<div style="text-align: center;"><p><b>Посочете желания брой продукти.</b></p><input style="float: left; display: block; height: 20px; margin: 0px 3px 0px 115px;" type="text" id="amountOfProducts" name="amountOfProducts" value="1" size="4" /><a class=\"modal-button\" href="#" onclick="addToCart('+item+'); return false;">Добави</a></div>';
    modalWindow.open();
}

var modalWindow = {
     parent:"body",
     windowId:null,
     content:null,
     width:490,
     height:390,
     close:function()
     {
         $(".modal-window").remove();
         $(".modal-overlay").remove();
     },
     open:function()
     {
         var modal = "";
         modal += "<div class=\"modal-overlay\"></div>";
         modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\"><a id=\"close-window\" href=\"#\">Затвори</a>";

		 modal += this.content;
         modal += "</div>";

         $(this.parent).append(modal);

         //$(".modal-window").append("<a class=\"close-window\">Затвори</a>");
         $("#close-window").click(function(){modalWindow.close();});
         $(".modal-overlay").click(function(){modalWindow.close();});
     }
};

function roundVal(val){
	var dec = 2;
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function updateCartItem(i, el){
    $.get("/api/cart/updatecartitem", {item: i, val: $(el).val()}, function(data){
        if(data.success){
            $("#totalCartProducts").html(data.products);
            $("#totalCartPrice").html(data.price);
            $("#orderPrice").html(roundVal(data.price));
            $("#orderItemTotal"+i).html(($("#orderItemSinglePrice"+i).html()*1)*$(el).val());
        }

    }, "json");
}

function removeCartItem(i, el){
     $.get("/api/cart/removecartitem", {item: i}, function(data){
         if(data.success){
            $("#totalCartProducts").html(data.products);
            $("#totalCartPrice").html(data.price);
            $("#orderPrice").html(roundVal(data.price));
            $(el).parent("td").parent("tr").remove();
         }
     }, "json");
}

function updateOrderCart(i, el){
    $.get("/api/cart/updateorderitem", {item: i, val: $(el).val()}, function(data){
        if(data.success){
            $("#totalCartProducts").html(data.products);
            $("#totalCartPrice").html(data.price);
            $("#orderPrice").html(roundVal(data.price));
            $("#orderShipping").html(roundVal(data.shipping));
            $("#orderTotal").html(roundVal((data.price*1)+(data.shipping*1)));
            $("#orderItemTotal"+i).html(($("#orderItemSinglePrice"+i).html()*1)*$(el).val());
        }

    }, "json");
}

function removeOrderItem(i, el){
     $.get("/api/cart/removeorderitem", {item: i}, function(data){
         if(data.success){
            $("#totalCartProducts").html(data.products);
            $("#totalCartPrice").html(data.price);
            $("#orderPrice").html(roundVal(data.price));
            $("#orderShipping").html(roundVal(data.shipping));
            $("#orderTotal").html(roundVal((data.price*1)+(data.shipping*1)));
            $(el).parent("td").parent("tr").remove();
         }
     }, "json");
}

var currentIndex = 0

function moveRight(el){
    var list = $(el).parent("div").next("ul");

    var items = list.children("li");
    if(items.length > currentIndex+1){
        currentIndex=currentIndex+1;
        list.children("li").hide();
        $(items[currentIndex]).show();
        
    }else{
        currentIndex = 0;
        list.children("li").hide();
        $(items[currentIndex]).show();      
    }
}
function moveLeft(el){
    var list = $(el).parent("div").next("ul");

    var items = list.children("li");
    if(currentIndex-1 >= 0){
        currentIndex=currentIndex-1;
        list.children("li").hide();
        $(items[currentIndex]).show();

    }else{
        currentIndex = items.length - 1;
        list.children("li").hide();
        $(items[currentIndex]).show();
    }
}

var currentIndex2 = 0

function moveRight2(el){
    var list = $(el).parent("div").next("ul");

    var items = list.children("li");
    if(items.length > currentIndex2+1){
        currentIndex2=currentIndex2+1;
        list.children("li").hide();
        $(items[currentIndex2]).show();

    }else{
        currentIndex2 = 0;
        list.children("li").hide();
        $(items[currentIndex2]).show();
    }
}
function moveLeft2(el){
    var list = $(el).parent("div").next("ul");

    var items = list.children("li");
    if(currentIndex2-1 >= 0){
        currentIndex2=currentIndex2-1;
        list.children("li").hide();
        $(items[currentIndex2]).show();

    }else{
        currentIndex2 = items.length - 1;
        list.children("li").hide();
        $(items[currentIndex2]).show();
    }
}

function showProductTool(item){
    $(".products-tool-wrap").hide();
    $("#tool"+item).show();
}
