$(document).ready(function() {
    var lightboxArr = new Array()
    $('a[rel^=lightbox]').each(function(){
        if(jQuery.inArray($(this).attr('rel'),lightboxArr)<0) {
            lightboxArr.push($(this).attr('rel'));
        }
    });
	
    jQuery.each(lightboxArr,function(){
        $('a[rel^='+this+']').lightBox();
    });


    if(jQuery('.show_product_options').length>0){
            
        change_prices();

        jQuery('.show_product_options').change(function(){

            change_prices();

        });
    }

    $('#faktura').change(function() {
        $td_invoice = $('.td_invoice');

        if($td_invoice.css('display')=='none')
            $td_invoice.show();
        else
            $td_invoice.hide();
    });


});


/*
  *
  */

/*  przykładowe dane w szablonie
             *  <span id="price" >[%price%]</span>
                <span id="price_net" >[%price_net%]</span>
                <span id="price_formated">[%price_formated%]</span>
                <span id="price_formated_net">[%price_formated_net%]</span>
                <span id="prom_price">[%prom_price%]</span>
                <span id="prom_price_net">[%prom_price_net%]</span>
                <span id="prom_price_formated">[%prom_price_formated%]</span>
                <span id="prom_price_formated_net">[%prom_price_formated_net%]</span>
             */
function change_prices() {

    var tmp_price_add=0;
    var tmp_price_set=0;

    jQuery('select.show_product_options option:selected').each(function(){

        str=jQuery(this).html().replace(/\s|&nbsp;/g,'').match(/\(.*\)/g)+'';

        if(str=='null') return null;

        is_add=str.search(/\+/);
        str=str.match(/\d+\.?\d*/g)+'';
        number=parseFloat(str.replace(/,/g,'.'));

        if(number>0){
            if(is_add>0)
                tmp_price_add+=number;
            else
                tmp_price_set=number;

        } else return null;
    })



    //jeżeli ustawiono cene w cechach powiązanych
    if(tmp_price_set>0){

        //jezeli produkt ma cenę promocyjną
        if(prom_price>0) {
            vat_value=((show_product_tax*(tmp_price_set+tmp_price_add))/(show_product_tax+100)).toFixed(2);
            netto=(tmp_price_set+tmp_price_add-vat_value);
            netto=parseFloat(netto.toFixed(2));

            jQuery('#prom_price').html((tmp_price_set+tmp_price_add).toString().replace(/\./g,','));
            jQuery('#prom_price_net').html(netto.toString().replace(/\./g,','));
            jQuery('#prom_price_formated').html((tmp_price_set+tmp_price_add).number_format());
            jQuery('#prom_price_formated_net').html(netto.number_format());

        } else {

            vat_value=((show_product_tax*(tmp_price_set+tmp_price_add))/(show_product_tax+100)).toFixed(2);
            netto=(tmp_price_set+tmp_price_add-vat_value);
            netto=parseFloat(netto.toFixed(2));

            jQuery('#price').html((tmp_price_set+tmp_price_add).toString().replace(/\./g,','));
            jQuery('#price_net').html(netto.toString().replace(/\./g,','));
            jQuery('#price_formated').html((tmp_price_set+tmp_price_add).number_format());
            jQuery('#price_formated_net').html(netto.number_format());
        }

    } else if(tmp_price_set==0) {

        //jezeli produkt ma cenę promocyjną
        if(prom_price>0){

            vat_value=((show_product_tax*(prom_price+tmp_price_add))/(show_product_tax+100)).toFixed(2);
            netto=(prom_price+tmp_price_add-vat_value);
            netto=parseFloat(netto.toFixed(2));
            jQuery('#prom_price').html((prom_price+tmp_price_add).toString().replace(/\./g,','));
            jQuery('#prom_price_net').html(netto.toString().replace(/\./g,','));
            jQuery('#prom_price_formated').html((prom_price+tmp_price_add).number_format());
            jQuery('#prom_price_formated_net').html(netto.number_format());

        } else {

            vat_value=((show_product_tax*(price+tmp_price_add))/(show_product_tax+100)).toFixed(2);
            netto=(price+tmp_price_add-vat_value);
            netto=parseFloat(netto.toFixed(2));

            jQuery('#price').html((price+tmp_price_add).toString().replace(/\./g,','));
            jQuery('#price_net').html(netto.toString().replace(/\./g,','));
            jQuery('#price_formated').html((price+tmp_price_add).number_format());
            jQuery('#price_formated_net').html(netto.number_format());

        }
    }
}