function pagination (url) {
    $("div#all_divs").load(
        url+" #all_divs div#all_divs_sphere", function(){
        reloadJS('product_3');
        $("#tooltip").hide();
    });
}
function edit_product(id, symbol, name, capacity, price, category) {
    $.ajax({
        url: "/admin_panel/edit_product/"+id,
        type: "POST",
        data: "symbol="+symbol+"&name="+name+"&capacity="+capacity+"&price="+price+"&product_category_id="+category,
        dataType: "json",
        success: function(result){
            $("#tooltip").hide();
            if(result == 1) {
                $("div#all_divs").load(
                "http://www.monopolowy6-24.pl/admin_panel/index.html #all_divs div", function(){
                    reloadJS('product_3');
                    alert('produkt zapisany pomyślnie');
                });
            }
            else {
                alert('produkt nie został zapisany, sprawdź jeszcze raz wpisane pola');
            }
        }
    });
}
function remove_product(id) {
    $.ajax({
        url: "/admin_panel/remove_product/"+id,
        type: "POST",
        dataType: "json",
        success: function(status){
            $("#tooltip").hide();
            if(status == 1) {
                $("div#all_divs").load(
                "http://www.monopolowy6-24.pl/admin_panel/index.html #all_divs div", function(){
                    reloadJS('product_3');
                    alert('produkt usunięty pomyślnie');
                });
            }
            else {
                alert('błąd przy usuwaniu, spróbuj jeszcze raz');
            }
        }
    });
}
function add_product(symbol, name, capacity, price, category) {
    $.ajax({
        url: "/admin_panel/add_product",
        type: "POST",
        data: "symbol="+symbol+"&name="+name+"&capacity="+capacity+"&price="+price+"&product_category_id="+category,
        dataType: "json",
        success: function(result){
            $("#tooltip").hide();
            if(result == 1) {
                $("div#all_divs").load(
                "http://www.monopolowy6-24.pl/admin_panel/index.html #all_divs div", function(){
                    showJqueryForm();
                    reloadJS('product_3');
                    alert('produkt zapisany pomyślnie');
                });
            }
            else {
                alert('produkt nie został zapisany, sprawdź jeszcze raz wpisane pola');
            }
        }
    });
}
function add_waiting_product(symbol, name, capacity, price, category) {
    $.ajax({
        url: "/admin_panel/add_product",
        type: "POST",
        data: "symbol="+symbol+"&name="+name+"&capacity="+capacity+"&price="+price+"&product_category_id="+category,
        dataType: "json",
        success: function(result){
            
            if(result == 1) {
                $("div#all_divs").load(
                "http://www.monopolowy6-24.pl/admin_panel/waiting_products.html #all_divs div", function(){
                    reloadJS('product_3');
                    $("#tooltip").hide();
                    alert('produkt zapisany pomyślnie');
                });
            }
            else {
                alert('produkt nie został zapisany, sprawdź jeszcze raz wpisane pola');
            }
        }
    });
}
function add_content(id) {
    $("#jquery_form_load").load(
    "http://www.monopolowy6-24.pl/admin_panel/add_content/"+id+" #product_content", function(){
        showJqueryForm();
        reloadJS('product_3');
        $("#tooltip").hide();
    });
}
function submit_content(url, content) {
    $.ajax({
        url: url,
        type: "POST",
        data: "content="+content,
        dataType: "json",
        success: function(status){
            $("#tooltip").hide();
            if(status == 1) {
                alert('zapisano pomyślnie');
                hideJqueryForm();
            }
            else {
                alert('błąd przy zapisie');
            }
            reloadJS('product_3');
        }
    });
}
$(document).ready(function(){
    //po załadowaniu dokumentu
    $("button#new_product").click(function(e){
        //zmienne
        var symbol = $("input#symbol").val();
        var name = $("input#name").val();
        var capacity = $("input#capacity").val()+$("select#input_dropdown").val();
        var price = $("input#price").val();
        var category = $("select#categories_dropdown").val();
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", e.pageY + "px")
            .text('TRWA ZAPISYWANIE, PROSZĘ CZEKAJ.')
            .show();
        add_product(symbol, name, capacity, price, category);
        return false;
    });
    $("a.new_waiting").click(function(){
    	var symbol = $(this).text();
    	var name = $(this).parent().next("td").text();
    	$("input#symbol").val(symbol);
    	$("input#name").val(name);
    });
    $("button#new_waiting_product").click(function(e){
        //zmienne
        var symbol = $("input#symbol").val();
        var name = $("input#name").val();
        var capacity = $("input#capacity").val()+$("select#input_dropdown").val();
        var price = 'undefined';
        var category = $("select#categories_dropdown").val();
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", e.pageY + "px")
            .text('TRWA ZAPISYWANIE, PROSZĘ CZEKAJ.')
            .show();
        add_waiting_product(symbol, name, capacity, price, category);
        return false;
    });
    $(".delete_product").click(function(e){
        //usuwanie produktu
        var id = $(this).parent().siblings("td.product_id").text();
        if(confirm('Czy na pewno usunąć produkt?')){
            $("#tooltip")
                .css("left", e.pageX + "px")
                .css("top", e.pageY + "px")
                .text('TRWA USUWANIE, PROSZĘ CZEKAJ.')
                .show();
            remove_product(id);
        }
        else {
            alert('Produkt nie został usunięty.');
        }
        return false;
    });
    $(".edit_product").click(function(e){
        //zmienne
        var id = $(this).parent().siblings("td.product_id").text();
        var symbol = $(this).parent().siblings("td.product_symbol").children("input").val();
        var name = $(this).parent().siblings("td.product_name").children("input").val();
        var capacity = $(this).parent().siblings("td.product_capacity").children("input").val();
        var price = $(this).parent().siblings("td.product_price").children("input").val();
        var category = $(this).parent().siblings("td.product_category").children("select").val();
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", e.pageY + "px")
            .text('TRWA ZAPIS, PROSZĘ CZEKAJ.')
            .show();
        edit_product(id, symbol, name, capacity, price, category);
        return false;
    });
    $("div.random_product img") .mousemove(function(e){
        $("#tooltip")
            .css("left", (e.pageX + 10) + "px")
            .css("top", (e.pageY + 20) + "px");
    });
    $("#edit_products p.pagination a").click(function(e){
        var linkText = $(this).attr("href");
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", (e.pageY + 20) + "px")
            .text('TRWA ŁADOWANIE...')
            .show();
        pagination(linkText);
        return false;
    });
    //dodawanie treści do produktu
    $("a.content_product").click(function(e){
        var product_id = $(this).parent().siblings("td.product_id").text();
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", (e.pageY + 20) + "px")
            .text('TRWA ŁADOWANIE...')
            .show();
        add_content(product_id);
        return false;
    });
    //wysyłanie treści
    $("#jquery_form_load form.product_content_ajax").submit(function(e){
        var content = $("form.product_content_ajax textarea#content").val();
        var url = $("form.product_content_ajax").attr('action');
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", (e.pageY + 20) + "px")
            .text('TRWA ZAPIS...')
            .show();
        submit_content(url, content);
        return false;
    });
    //wysyłanie zdjęcia
    $("div.upload_button form").submit(function(e){
        $("#tooltip")
            .css("left", e.pageX + "px")
            .css("top", (e.pageY + 20) + "px")
            .text('TRWA WYSYŁANIE PLIKU, PROSZĘ CZEKAĆ...')
            .show();
    });
});
