/**
 * crafted by www.blazsak.pl
 */

  $(document).ready(function(){ 
    $('#logo a').pngFix();
    $('#createdby a').pngFix();
    $('.pngfix').pngFix();
    
    $('#acclist').accordion({header: "h4", autoHeight: false ,active:false, alwaysopen:false}); 
    //$("#galeria_lista  a").fancybox({'overlayOpacity':0.7, 'zoomSpeedIn': 0, 'zoomSpeedOut': 0, 'overlayShow': true }); 
    $("#galeria_lista  a").fancybox(); 
    $("a.preview").fancybox(); 
    
    $.swapImage(".swapImage");
    
    
// CALCULATIONS    
   $.Calculation.setDefaults({
	// regular expression used to detect numbers, if you want to force the field to contain
	// numbers, you can add a ^ to the beginning or $ to the end of the regex to force the
	// the regex to match the entire string: /^(-|-\$)?(\d+(,\d{3})*(\.\d{1,})?|\.\d{1,})$/g
	reNumbers: /(-|-\$)?(\d+(,\d{3})*(\.\d{1,})?|\.\d{1,})?/g
	// should the Field plug-in be used for getting values of :input elements?
	, useFieldPlugin: true
	// a callback function to run when an parsing error occurs
	, onParseError: null
	// a callback function to run once a parsing error has cleared
	, onParseClear: null
}); 

		//	$('#obliczenia').change(correct_price);
			$("select[@id^=price_item_]").change(change_price2);
      $("[@id^=qty_item_]").keyup(function(){$("select[@id^=price_item_]").change()});

      
      // bind the recalc function to the quantity fields
			//$("input[@name^=qty_item_]").bind("change", recalc);
      //$("select[@id^=price_item_]").bind("change", recalc);

      // run the calculation function now
			//recalc();

			// automatically update the "#totalSum" field every time
			// the values are changes via the keyup event
		//	$("input[@name^=sum]").sum("keyup", "#totalSum");
function change_price2() {
 var listacen = ($(this).getValue()).split('-');
  var id = listacen[0];
  $('#total_item_'+id).html(' Ceny zostały zmienione.<br/> Zapisz zmiany.');
}


function correct_price() {
          var oblicz = $('#obliczenia').getValue();
          //alert(oblicz);
          if(!oblicz) return false;       

        
          var ilosc = parseInt($("[@id^=qty_item_]").sum());
          
          $('#photosTotal').text(ilosc);
          var listacen = ($(this).getValue()).split('-');
          var cena;
          var id = listacen[0];

                  if(ilosc >= 400) cena = listacen[8] ;
                  if(ilosc < 400) cena = listacen[7];
                  if(ilosc < 300) cena = listacen[6];
                  if(ilosc < 250) cena = listacen[5];
                  if(ilosc < 200) cena = listacen[4];
                  if(ilosc < 150) cena = listacen[3];
                  if(ilosc < 100) cena = listacen[2];
                  if(ilosc < 70) cena = listacen[1];

      $('#current_price_'+id).setValue(cena);
      
      var ilosc_id = $('#qty_item_'+id).getValue();
      
      $('#total_item_'+id).text(cena * ilosc_id + ' zł');
      $('#total_item_val_'+id).setValue(cena * ilosc_id);
     // $("#grandTotal").text("<input type='submit' value='Zapisz zmiany' />");
      //var suma = $("[@id^=total_item_val_]").sum();
      //$("#grandTotal").text(suma.toFixed(2) + ' zł');
      //recalc();
      
}



function recalc(){
  


  $("[@id^=total_item]").calc(  // the equation to use for the calculation
  "qty * current",  // define the variables used in the equation, these can be a jQuery object
  {
    qty: $("[@id^=qty_item_]"),
   // price: getprice($("[@id^=price_item_]")),
    current: $("[@id^=current_price_]")
   
  },  
 // pierwsze obliczenia
  function(s){     return s.toFixed(2) + ' zł';  },  
 // po kalkulajach
  function($this){
    // sum the total of the $("[@id^=total_item]") selector
    //alert($this.getValue());
   // alert($this.val());
    var sum = $this.sum();
    $("#grandTotal").text(sum.toFixed(2)+ ' zł');
  });
 // alert('nop');
  $('#photosTotal').text($("[@id^=qty_item_]").sum());
$.unblockUI();
}
    
    
    
    $('#c_kod').mask('99-999');
   // $('#c_tel').mask('(999) 999-99-99');
    
    $(".help-size").cluetip({ shadow: true, width: 400, local: true, cursor: 'pointer', hideLocal: true });
    $(".help-kadr").cluetip({ shadow: true, width: 400, local: true, cursor: 'pointer', hideLocal: true });
        

  }); 
    




function orderform(ver){
  if(!ver) {alert('ERROR formularza'); return false;}
  var form = $('#order-form');
  form.attr('action','/zdjecia-online/zamow/'+ver);
  // alert(form.attr('action'));
 
  form.submit();
}





function photodel(id,ver){
  if(!id || !ver) {alert('init error'); return false;}
   if (confirm("Czy na pewno usunąć wybrane zdjęcie ?")) {
     $("#tr-" + id).html("<td class='borbott' style='text-align:center;' > <img src='/gfx/site/loading.gif' alt='przetwarzanie...' /> </td>");
     $.post("/ajax", {
       route: 'ajax',
       action: 'delete_order_photo',
       id: id,
       part: ver
     }, function(data){
       //alert(data);
      $("#tr-" + id).remove();
    });
  }  
}





function cancelOrder(order,id,ver){
 
 if(!order || !id || !ver ){alert('Błąd wywołania funkcji'); return false;}
 if (confirm("Czy na pewno anulować zamówienie "+order+" ?\nWszystkie zdjęcia zostaną usunięte z serwera!")) {
   $("#order").html("<div style='text-align:center;' > <img src='/gfx/site/loading.gif' alt='przetwarzanie...' /> </div>");
   $.post("/ajax", {
     route: 'ajax',action:'delete_order', id: id, part: ver   }, function(data){
     //alert(data);
     $("#order").html(data);
   });
   
 }
}


    
  
