// JavaScript Document

$(document).ready(function(){
var total1 = 0;
						   
//CAMBIA LA IMAGEN DEL CUADRO CON EL "NAVEGADOR DE IMAGENES"
	$(".thumbnails a").click(function() {
	//	$('#picNormal').attr('src', $(this).attr('href'));
		img = $(this).attr('href');
		//alert(img);
		//upImg = img.replace('_thumb.jpg','.jpg');
		//alert(upImg);
		//title = $(this).attr('title');

		//$('#picNormal img').attr('title', title);
		//alert($('#picNormal img').attr('title', title));
		$('#picNormalLink').attr('href', img);
		$('#picNormal').attr('src', img); return false;
	
	});

//MUESTRA EL DIV OCULTO DE LA LISTA
	$('a.linkMore').click(function() {
        if($(this).hasClass("toggleLess")) {
             $(this).addClass("toggleMore");
			 $(this).removeClass("toggleLess");
        }else {
			$(this).addClass("toggleLess");
			$(this).removeClass("toggleMore");
			
        }                                                             
		$("#div_" + $(this).attr("id")).toggle(); return false;
		
	});
	
//MUESTRA EL DIV OCULTO DE LA LISTA  !!! NUEVA para los que tiene imagen al final
	$('a.linkMore2').click(function() {
	
        // src de la imagen que esta dentro del <a>
        imageSource = $('img', $(this) ).attr('src');
        
        // es el mas
        if(imageSource.indexOf('plus') != -1)$('img', $(this) ).attr('src', imageSource.replace('plus','minus') );
        else $('img', $(this) ).attr('src', imageSource.replace('minus','plus') );
        
        $("#div_" + $(this).attr("id")).toggle(); return false;
		
	});	
	
//MUESTRA LA "FINESTRA" OCULTA

	$('.bulletFinestra, .showPopup').click(function() {
         if($(this).attr("id") == "finestra_calculo2" && total1 == 0){       //lo agregue para q no me muestra la segunda finestra si todavia no se calculo la primera
            alert("Debe completar el primer formulario 1 para poder acceder a este.");
            return false; 
         }
         /*else{
            $('#total2_4').html(sub4.toFixed(2));   
         } */               
		$("#div_" + $(this).attr("id")).toggle(); return false;
		
	});
	
	$('.closeFinestra').click(function() {
        closeFinestra = $(this).attr("id").replace('close_','');                 
		$("#div_" + closeFinestra).toggle(); return false;
		
	});
	
	
//CALCULADORA1 RRHH SELECCIO 03
    /*
    $(".calculadora input[type=text]").focus(function() {
        $(this).val("");
    });
    */
    $(".costo, .horas").blur(function() {
        
        subtotal = 0;
        x=0;
        
      if($(this).val() != "") {  // por si no ponen un huevo, q ponga un huevo
            
        if (!isNaN($(this).val())) {   //compruebo q no ingresen verdura
        
            for(flag=1;flag<=8;flag++)  {    //loop para la sumatoria de los campos
            
                h = $('#horas_' + flag).val();     // traigo el valor del campo horas_x
                c = $('#costo_' + flag).val();     // traigo el valor del campo costo_x
                t = h * c;     // multiplico horas x costo y lo guardo en t
    
                if( t > 0 ){
                 $('#total_' + flag).html( number_format(t, 2, ',', '.') );
                }else{
                 $('#total_' + flag).html("0,0");
                }
                
                x+=h*1;
                subtotal += t*1;
            }
            
            $('#total_9').html( number_format(x, 2, ',', '.') );
            anunci = $('#total_10').val();
            total1 = subtotal*1 + anunci*1;

            $('#total_11').html( number_format(total1, 2, ',', '.') ); // seteo el resultado final  de la calculadora 1 (total1)
            $('#c1').html( number_format(total1, 2, ',', '.') ); // seteo el resultado final de la calculadora 1 en el span c1
            
        }else{ //si no ingresaron un numero:
             alert("Ingrese solamente numeros");
             $(this).val(0);
        }
     }else{
       $(this).val(0);  
     }
        
    });        

    //CALCULADORA2 RRHH SELECCIO 03
    /*
    $(".calculadora2 input[type=text]").focus(function() {
        $(this).val("");
    });
     */
    $(".costo2, .horas2").blur(function() {
        
      if($(this).val() != "") {   // por si no ponen un huevo, q ponga un huevo     
        if (!isNaN($(this).val())) {   //compruebo q no ingresen verdura
            
                h = $('#horas2_1').val();     // traigo el valor del campo horas_x
                c = $('#costo2_1').val();     // traigo el valor del campo costo_x
                t = h * c;     // multiplico horas x costo y lo guardo en t

                    if(t>0){
                     $('#total2_1').html( number_format(t, 2, ',', '.') );
                    }else{
                     $('#total2_1').html("0,0");
                    }

            sub2 = $('#total2_2').val();
            sub3 = $('#total2_3').val();
            sub4 = total1  * $('#porcentaje_primera').val() / 100;   // Cost repetició

            $('#total2_4').html( number_format(sub4, 2, ',', '.') );
            
            sub_seba = sub2*1 + sub3*1 + sub4*1;

            $('#c2').html( number_format(sub_seba, 2, ',', '.') ); // seteo el resultado de Cost repetició en el span c2

            sub5 = sub_seba + t;

            $('#total2_5').html( number_format(sub5, 2, ',', '.') );
            
            total2 = total1 + sub5;

            $('#total2_7').html( number_format(total2, 2, ',', '.') );
            $('#c3').html( number_format(total2, 2, ',', '.') ); // seteo el resultado total de ambas calculadoras en el span c3
            
        }else{ //si no ingresaron un numero:
             alert("Ingrese solamente numeros");
             $(this).val(0);
        }
     }else{
       $(this).val(0);
     }
    });
  
  $("#finestra_calculo2").click(function() {
        sub4 = total1  * $('#porcentaje_primera').val() / 100;   // Cost repetició
      $('#total2_4').html( number_format(sub4, 2, ',', '.') );
      $('#total2_6').html( number_format(total1, 2, ',', '.') );
  })
     
  

});

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
