/**
 * @author Admin
 */
$(document).ready(function()
{

    roundP = function(number, precision)
    {
            precision = Math.abs(parseInt(precision)) || 0;
            var coefficient = Math.pow(10, precision);
            return Math.round(number*coefficient)/coefficient;
    }


    // -------------- oprava vyrendrovaneho stromu kde jsou prazdne li
    //najdeme je a vymazeme
    $(".leftmenu ul li").each(
        function (i)
        {
            if ( $.trim($(this).text()) == "")
            {
                //console.log('mazu node');
                //console.log($(this));

                $(this).remove();
            }
            else
            {

        //console.log($(this).text());
        //console.log($.trim($(this).text()));
        }
        }
        );

    /********** autocomplete **************/
    
    $("form#myportal_search input#search_string").focus(
        function()
        {
            if ($(this).val() == 'hledej na stránkách')
            {
                $(this).val('');
            }
        }
        );

    $("form#myportal_search input#search_string").blur(
        function()
        {
            if ($(this).val() == '')
            {
                $(this).val('hledej na stránkách');
            }
        }

        );
     
                    

    /*
    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

    $("input#search_test").autocomplete(months, {});
    */

    //inside an LI element in the results list.
    function format(row) {
        return '<strong>' + row.title + '</strong> <p> ' + row.segment + " </p> "; // <small> id:" + row.id_page + "</small>"
    }

    //inside an LI element in the results list.
    function formatFullWord(row) {
        return row.fullword + ' - ' +  '<small>' +  row.title + '</small>';
    }


    $("input#search_string").autocomplete('/pages/fullTextSuggest/', {
        minChars: 5,
        width: 183,
        scrollHeight: 300,
        multiple: false,
        mustMatch: false,
        //matchContains: true,
        max: 100,
        dataType: "json",
        selectFirst: false,
        parse: function(data) {

            console.log(data);
            
            return $.map(data, function(row) {
                return {
                    data: row,
                    //value: row.name,
                    value: row.id_page,
                    //result: row.name + " <" + row.to + ">"
                    //result: '<strong>' + row.title + '</strong> <p> ' + row.segment + " </p> <small> id:" + row.id_page + "</small>"

                    //jde to select boxu po vybrání
                    result: row.fullword
                }
            });
        },
        //Data, position (starting with one) and total number of data.
        //inside an LI element in the results list.
        xformatItem: function(item) {
            return format(item);
        }
        ,
        formatItem: function(item) {
            return formatFullWord(item);
        }
        ,
        //to input field
        //Data, position (starting with one) and total number of data.
        xxformatResult: function(item) {
            return format(item);
        }

    });




    /********** VYPOCET CENY **************/

    $("#cenik_form").submit(function()
    {

       if ($("#cenik_form #kod").val() == '')
           {
               alert('Zadajte kód výrobku.');
               return false;
           }

        var info =
        {
            kod: $("#cenik_form #kod").val(),
            kusu: $("#cenik_form #kusu").val(),
            //baleni_varianta: $("#cenik_form #baleni_varianta").val(),
            //baleni: $("#cenik_form #baleni").val(),
            id_imported: $("#cenik_form #id_imported").val()
            ,sleva : $("#cenik_form #sleva").val()

        };


        //http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
        $.ajax(
        {
            url: '/imported/searchPrice/',
            data: info,
            dataType: "json",
            success : function(html)
            {
                    //alert('heslo ok');

                    //var data = eval(html);

                    //console.log(data);
                    console.log(html);
                    
                    console.log('zaznamu:' + html.length);

                    //dej baleni do volby
                    /*
                    $("#cenik_form #baleni_varianta").removeOption(/./);

                      //pokud jen jedno tak jedna volba
                
                    for(i=0;i<html.length;i++)
                        {
                            //aktualizuj select
                            $("#cenik_form #baleni_varianta").addOption(html[i].info_1, html[i].info_1);

                        }
                    */


                   //ajax cena dle kodu

                    //var cena_ks = 50;

                    var imported = html[0];

                    //var cena_1_ks = parseFloat(imported.price) / parseFloat(imported.info_1);

                    var cena_za_baleni =  roundP ( parseFloat(imported.price) * parseFloat(imported.info_1) , 2 );

                    var cena_1_ks = parseFloat(imported.price);

                    console.log('cena 1 ks:' + cena_1_ks);
                    
                                       
                    var sleva = (100 - parseFloat((info.sleva == '') ? 0 : info.sleva )) / 100;

                    var cena_celkem = roundP ( (parseInt(info.kusu) * cena_1_ks * sleva) , 2 );

                    console.log('cena celkem :' + cena_celkem);

                    //$("#cenik_form #cenik_response").html('Celková cena je: ' + cena_celkem);
                    $("#cenik_form #cena_baleni").html( (cena_za_baleni) + ' Kč' );
                    $("#cenik_form #cena_ks").html( (cena_1_ks) + ' Kč' );
                    $("#cenik_form #cena_celkem").html( cena_celkem + ' Kč' );
                   
            }
        }
        );

        

        return false;

    });

    /********** ROUNDED **************/
		
    //zaobleni

    var zaoblit = "12px";

    $("#right h2").corner(zaoblit);
    $("#right .box").corner(zaoblit);
    $("#article h3.redBox,#article_full h3.redBox").corner(zaoblit);
    $("#article .box").corner(zaoblit);

    $("#article .urlHere").corner(zaoblit);
    $("#article_full .urlHere").corner(zaoblit);

    $("#article .prevNextMenu").corner(zaoblit);
    $("#article_full .prevNextMenu").corner(zaoblit);

    $(".pre_form").corner(zaoblit);


		
//$("div.nabidka").corner("round bl tr 7px");
//$("div.obrazek").corner("round bl tr 7px");
		
		
//$("#vyhledavani_zadavani").corner("round bl br 10px");
		
		
/********** search pozicovani **************/
					
//$("#vyhledavani_zadavani").hide();
				
/* nastaveni pozice */
	
}
);