$(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();

        });
    }


    if(jQuery('.show_product_options').length>0){

        change_prices();

        jQuery('.show_product_options').change(function(){

            change_prices();

            product_index = jQuery(this).children('option:selected').attr('rel');

            /*if(product_index==undefined || product_index.toString().toLowerCase()=='null'){
 jQuery('#index_powiazania').html('Nie wybrano powiązania');
 jQuery('#index').show();
 } else*/ if(product_index==undefined || product_index.toString().toLowerCase()=='null'){
 								product_index = jQuery(this).children('option:selected').attr('title');
 								if(product_index==undefined || product_index.toString().toLowerCase()=='null'){
	                jQuery('#index_powiazania').html('To powi±zanie nie ma przypisanego indeksu');
	                jQuery('#index').show();
                } else {
									jQuery('#index_powiazania').html(product_index);
                	jQuery('#index').show();
								}
            } else {
                jQuery('#index_powiazania').html(product_index);
                jQuery('#index').show();
            }
        });
    }

}); 


/*
*
*/

/*  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(/\+/);
    is_sub=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 if(is_sub>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());

    }
}
}
