
$(document).ready(function(){
    //XDEBUG
    
    $('font>table').addClass('xdebug-error');
    $('font>table *').removeAttr('style').removeAttr('bgcolor');
    $('font>table tr:first-child').addClass('xdebug-error_description');
    $('font>table tr:nth-child(2)').addClass('xdebug-error_callStack');



    //fancy-box - powiększanie obrazków

    $('.przelicz').click(function() {
        $.getJSON('/order/coupon/format/json', {code: $("#code").val()}, function(data) {
            alert(data.msg);
        })
        return false;
    })

    //face-box
//    $('a[rel*=facebox]').facebox();

    /*$('a[rel*=fancybox]').fancybox({
        'titleShow' : false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic'
    });*/

    $('.close').click(function() {
        $('#messages').hide();
        $('#messages').removeClass();
        return false;
    });

    //menu glowne
//    $('#navTop li:first').addClass('no-padding-l');
//    $('#navTop li:last').addClass('no-border no-padding-r');

    //gallery
//    $('.gallery img:first').addClass('green');


    //wybór danych do wysyłki przy formularzu zamówienia
    $('label[for="delivery_choice-0"]').click(function(){
        $('.delivery_address').hide();
    });

    $('label[for="delivery_choice-1"]').click(function(){
        $('.delivery_address').show();
    });

    if($('#delivery_choice-1').is(':checked')){
        $('.delivery_address').show();
    }

    if($('#company').is(':checked')){
        $('#company_box').show();
    }
    $('#company').click(function(){
        if($(this).is(':checked'))
            $('#company_box').slideDown();
        else
            $('#company_box').slideUp();
    });


    //zmiana ilości wyświetlanych elementów na stronie
    $('.change-limit-on-page').change(function(){
        window.location = 'http://' + window.location.hostname + $(this).val();
    })

    //czyszczenie domyślnego tekstu inputa po rozpoczęciu wprowadzania treści
    $(".auto-clean").inputText();



    $('.category-list > li').css('margin-right','2px');
    $('.category-list > li').css('border-bottom','1px solid #c7c9cb');
    $('.category-list li:last').addClass('no-border');

    /* lista produktow w kategorii */
    //$('.products-list li:odd').addClass('no-margin-r');
    $('.products-list li:first').addClass('border-t-gray');
    $('.products-list li:last').addClass('no-border');

    $('.quantity').change(function(){
        if($(this).val()==''){
            $(this).val(1)
        }
        var key = $(this).attr('id').split('-')[1];
        countPrices(key);
        countAllSum();
        return false;
    });

    $('.breadcrumbs li').not('li:last').append(' &raquo;');

//    $('.footer div a').not('a:last').append('&nbsp;&nbsp;&nbsp;|');

    $('.delivery div.radios label').not('label.required').css('text-align','left');
    $('.delivery div.radios label').not('label.required').css('float','none');
    $('.delivery div.radios label').not('label.required').css('display','inline-block');
});

/* function countPrices(key){
    var price = fromPrice($('#oproducts-'+key+'-price').val());
    var quantity = parseInt($('#oproducts-'+key+'-quantity').val());
    $('#oproducts-'+key+'-part_sum').val(toPrice(price * quantity));
}

function countAllSum(){
    var allsum = 0.0
    $('.part_sum').each(function() {
        allsum += fromPrice($(this).val());
    });
    $('#sum_products').val(toPrice(allsum));
    $('#sum').val(toPrice(allsum));
} */

function fromPrice(str){
    if(str==null){
        return 0;
    }
    return parseFloat(str.replace(',','.'));
}

//rounds the input number to the desired precision
//and returns the rounded number
function roundToPrecision(inputNum, desiredPrecision){
    var precisionGuide = Math.pow(10, desiredPrecision);
    return(Math.round(inputNum * precisionGuide) / precisionGuide);
}

//converts the input number into a string and adds zeroes
//until the desired precision is reached and then
//returns the new string
function toPrice(inputNum, desiredPrecision, unit){
    if(desiredPrecision == undefined) desiredPrecision=2;
    if(unit == undefined){
        var unit = '';
    }
    else{
        var unit = ' '+unit;
    }

    var inputNumRounded = roundToPrecision(inputNum, desiredPrecision)

    var beforeComma;
    var afterComma;
    var numString = inputNumRounded + "";

    var comma = numString.search(/\./);
    if(comma == -1){
        beforeComma = numString;
        afterComma = "";
    }
    else{
        beforeComma = numString.substring(0,comma);
        afterComma = numString.substring(comma+1, numString.length);
    }
    while (afterComma.length < desiredPrecision) {
        afterComma += "0";
    }
    return(beforeComma+','+afterComma+unit);
}

function abbr(string){
    string = string.toLowerCase();
    string = string.replace(/ą/g,"a");
    string = string.replace(/ć/g,"c");
    string = string.replace(/ę/g,"e");
    string = string.replace(/ł/g,"l");
    string = string.replace(/ń/g,"n");
    string = string.replace(/ó/g,"o");
    string = string.replace(/ś/g,"s");
    string = string.replace(/ź/g,"z");
    string = string.replace(/ż/g,"z");
    string = string.replace(/ /g,"-");
    string = string.replace(/&/g,"-");
    string = string.replace(/\//g,"-");
    string = string.replace(/\\/g,"-");
    string = string.replace(/\"/g,"-");
    string = string.replace(/\'/g,"-");

    return string;
}

//otwiera strone o podanym linku w nowym oknie
function openPopup(href, width, height, left, top){
    var myRef = window.open(''+href, 'new_window', 'width='+width+', height='+height+', left='+left+', top='+top+', menubar=0, toolbar=0, location=0, directories=0, resizable=1, scrollbars=1, status=0, fullscreen=0');
    myRef.focus();
}
