$(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();

        });
    }

    $('#invoice').change(function() {
        $('#invoice_table').slideToggle(500);

    });
    
    $('#shipment2').change(function() {
        $('#shipment2_table').slideToggle(500);

    });

    $('#faktura').change(function() {
        $('#invoice_table').slideToggle(500);

    });


});


/*
  *
  */

/*  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());

        }
    }
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
