$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
};







function men(fnt) {
    $('#menu a').css("font-size", fnt + "px");
    $('#menu a').css("padding", "0 " + (fnt + 2) + "px");
	$('#menu ul li ul a').css("font-size", fnt - 1 + "px");

	
	
    var w = 0;
	
    $('#menu>ul>li').each(function () {
        w += $(this).outerWidth(true);
    });
    if ($('#menu').width() < w) {
        men(fnt - 0.5);
    } else {
        var separator = Math.floor(($('#menu').width() - w) / ($('#menu>ul>li').length-1));
        $('#menu>ul>li:not(:last , .limbalink)').css("margin-right", separator);
    };
    $('#menu>ul>li').each(function () {
        $("ul" , this).css("left", $(this).position().left + "px");
        var w2 = $(this).width();
        $("ul" , this).css("min-width", w2);
    });
    $('#menu>ul>li>ul>li').each(function () {
        $("ul" , this).css("left", $(this).position().left + $(this).width() + "px");
    });
	
	$('#menu').css("visibility", "visible");

};



$(window).bind("resize", function() {
  $('#content').css("min-height", ($(window).height()- $('#footer').outerHeight(true) - $('#header').outerHeight(true) - 27) +"px");
});

$.fn.fontfit = function(max) {
	var max_size = 18;
	if (typeof(max) == "undefined")
		max = max_size;
	$(this).wrapInner('<div id="fontfit"></div>');
	var dheight = $(this).height();
	var cheight = $("#fontfit").height();
	var fsize = (($(this).css("font-size")).slice(0,-2))*1;
	while(cheight<dheight && fsize<max) {
		fsize+=1;
		$(this).css("font-size",fsize+"px");
		cheight = $("#fontfit").height();
	}
	while(cheight>dheight || fsize>max) {
		fsize-=1;
		$(this).css("font-size",fsize+"px");
		cheight = $("#fontfit").height();
	}
	$("#fontfit").replaceWith($("#fontfit").html());
	return this;
}








$(document).ready(function () {

	var primg = ['/gfx/hoversubmeniu.png','/gfx/fondsubmeniu.png','/gfx/ordernowhvr.png','/gfx/ordernow2hvr.png','/gfx/lefthvr.png','/gfx/righthvr.png','/gfx/bulinadark.png','/gfx/blank.gif','/gfx/pdfhvr.png','/gfx/calcraspunsgasit.png'];



	$('.culori img').each(function() {
		primg.push('/gfx/poze/galerii/mari/' + $(this).attr('jjj'));
	});

	
	$(primg).preload();




	$('#menu').prepend('<div id="hvr"><div id="righthvr"></div></div>');   
	
	$('#menu a').each(function () {
		var s = $(this).text();
		$(this).text(s.replace(' ',"\u00A0"));
    });	
	
	$('#menu>ul>li').each(function () {
	if ($(">ul" , this).length) {
		$(">a" , this).append('<span>&nbsp;&nbsp;&nbsp;&nbsp;▼</span>');
		};	
    });	

	$('#menu ul li li').each(function () {
	if ($(">ul" , this).length) {
		$(">a" , this).append('<span>&nbsp;&nbsp;&nbsp;&nbsp;►</span>');
		};	
    });	


	
	men(15);
 
	
	if (!$('.current').length) {
		$('#menu').prepend('<div id="dummy" class="current"></div>');
	};

	
	$('#scrollnws .nwsitem:last').css("border-bottom","none");
	$('#scrollnws .nwsitem:last').css("margin-bottom","0px");
	
	

    $('#hvr').css("width", $('.current').outerWidth(true) + "px");
    $('#hvr').css("left", $('.current').position().left + "px");



    $('.current').css("color", "#fff");
	
	
	
    $('#menu>ul>li:not(.limbalink)').hoverIntent(function () {
        $('.current').stop(true,true).animate({
            color: "#335D7F"
        }, 500, function () {
            // Animation complete.
        });
        $(">a" , this).stop().animate({
            color: "#fff"
        }, 500, function () {
            // Animation complete.
        });
        $('#hvr').stop(true,true).animate({
            left: $(">a" , this).position().left + "px",
            width: $(">a" , this).outerWidth(true) + "px"
        }, 300, function () {
            // Animation complete.
        });
		$(">ul" , this).css("visibility", "visible");

    }, function () {
        $(">a" , this).stop(true,true).animate({
            color: "#335D7F"
        }, 500, function () {
            // Animation complete.
        });


    });
		
	
	
	
	
	
	    $('#menu ul li').hover(function () {
			}, function () {
				$("ul" , this).css("visibility", "hidden");
    });
	



	
	    $('#menu ul li ul li').hover(function () {
		$(">ul" , this).css("visibility", "visible");
		
			}, function () {
    });


	
	

    $('#menu').hoverIntent(function () {}, function () {
		$('.current').stop().animate({
            color: "#fff"
        }, 500, function () {
            // Animation complete.
        });
        $('#hvr').stop().animate({
            left: $('.current').position().left + "px",
            width: $('.current').outerWidth(true) + "px"
        }, 500, function () {
            // Animation complete.
        });
    });

var valsus = $('#textsearch').val();
$('#textsearch').focus(function() {
    if(this.value == valsus) {
    this.value = '';
    }
    });
$('#textsearch').blur(function() {
    if(this.value == '') {
    this.value = valsus;
    }
    });
var valjos = $('#textnws').val();
$('#textnws').focus(function() {
    if(this.value == valjos) {
    this.value = '';
    }
    });
$('#textnws').blur(function() {
    if(this.value == '') {
    this.value = valjos;
    }
    });

$('.accesoriu').each(function() {
 	$('a:first', this).css("display","block");   
});
$('.stire').each(function() {
 	$('.lnkstire:first', this).css("display","block");   
});






$('.culori img').click(function() {
	$(this).parents('.dispens').css('background-image','url(/gfx/poze/galerii/mari/'+$(this).attr("jjj")+')');
});






$('#pozajud img').attr('src','/gfx/judete/'+$("#seljud option:selected").attr("pz"));
$.post("/ajax/adrese.php", { j: $('#seljud').val() },
   function(data) {
     $('#adrjud').html(data);
});

$('#seljud').change(function() {
	$('#pozajud img').attr('src','/gfx/blank.gif');
	$.post("/ajax/adrese.php", { j: $(this).val() },
	   function(data) {
		 $('#adrjud').html(data);
		 $('#pozajud img').attr('src','/gfx/judete/'+$("#seljud option:selected").attr("pz"));
	   });
    
});



$('.aratarasp').click(function() {
	$(this).hide(0);
    $(this).parent().siblings('.raspuns').slideDown(200);
});

$('.closerasp').click(function() {
	$(this).parent().slideUp(100);
    $(this).parent().siblings('p').children('.aratarasp').slideDown(200);
});


$('.pdfuri a').hover(function() {
	$(this).parents('.pdfuri').css("background","url(/gfx/pdfhvr.png) left center no-repeat");
	},
	   function() {
		$(this).parents('.pdfuri').css("background","url(/gfx/pdf.png) left center no-repeat");
});




/*
$(document).bind("contextmenu",function(e){
        return false;
    });

*/





if ($('.r2 div').outerHeight(true) < 500) {
	$('.r2 div').css("margin-top",(500 - $('.r2 div').outerHeight(true))/2  +"px");
};







$('#seljudcom').change(function() {
    $('#blankselectjudet').remove();
});



$('.deschide a').click(function() {
    $(this).parent().hide();
    $(this).parent().siblings('.sfascuns').show(200);
});

$('.inchide a').click(function() {
    $(this).parents('.sfascuns').hide();
    $(this).parents('.sfascuns').siblings('.deschide').show(200);
});




$('.hdlnk').hover(function() {
	$(this).css("text-decoration","underline !important");
	$(this).css("cursor","pointer !important");
}, 
function() {
	$(this).css("text-decoration","none");
});






$('.aredepoz').click(function() {
	$(this).parent().siblings('.txtfld3').attr('disabled',false);
    
});


$('.nuaredepoz').click(function() {
    $(this).parent().parent().prev().children('.txtfld3').attr('disabled',true);
});


$('.arefurnizor').click(function() {
    $('#alfurnizor .txtfld2').attr('disabled',false);
	$('#alfurnizor table').fadeIn(500);
});
$('.nuarefurnizor').click(function() {
	$('#alfurnizor .txtfld2').attr('disabled',true);
    $('#alfurnizor table').fadeOut(300, function() {
      });
});




var offsetpuncte = 3;

$('#nrpctlucru').change(function() {
	for (i=2; i<=$(this).val(); i++) {
		$('#tabel2 tr:nth-child('+(offsetpuncte+i)+')').fadeIn(500);
		$('input' , '#tabel2 tr:nth-child('+(offsetpuncte+i)+')').attr("disabled",false);
		
		$('#tabel2 tr:nth-child('+(offsetpuncte+11+i)+')').fadeIn(500);
		$('input' , '#tabel2 tr:nth-child('+(offsetpuncte+11+i)+')').attr("disabled",false);
		
		$('#tabel2 tr:nth-child('+(offsetpuncte+22+i)+')').fadeIn(500);
		$('input:not(.txtfld3)' , '#tabel2 tr:nth-child('+(offsetpuncte+22+i)+')').attr("disabled",false);	
		}
		
		
		
	for (i=$(this).val(); i<=10; i++) {
		$('input' , '#tabel2 tr:nth-child('+(offsetpuncte+i)+')').attr("disabled",true);
		$('#tabel2 tr:nth-child('+(offsetpuncte+i)+')').fadeOut(300, function() {
      });
		
		$('input' , '#tabel2 tr:nth-child('+(offsetpuncte+11+i)+')').attr("disabled",true);
		$('#tabel2 tr:nth-child('+(offsetpuncte+11+i)+')').fadeOut(300, function() {
      });
		
		$('input' , '#tabel2 tr:nth-child('+(offsetpuncte+22+i)+')').attr("disabled",true);	
		$('#tabel2 tr:nth-child('+(offsetpuncte+22+i)+')').fadeOut(300, function() {
      });
		}

    
});




$('[name="Catre"]').change(function() {
	$('.cntadrvanzari input, .cntadrvanzari select').attr("disabled",true);
	$('.cntadrvanzari').fadeOut(300);
	if ($(this).val() == 'sales.customer@cumpana.ro') {
	$('.cntadrvanzari').fadeIn(500);
	$('.cntadrvanzari input, .cntadrvanzari select').attr("disabled",false);
	};
	
});



$('#supp').validate({
        errorClass: 'eroarevalidare',
		validClass: 'succesvalidare',
        messages: {
            Nume: "",
			Adresa: "",
			Localitate: "",
			Judet: "",
            Email: "",
            Subiect: "",
            Telefon: "",
            Mesaj: ""
        }
});





$('#form_comanda').validate({
        errorClass: 'eroarevalidare',
		validClass: 'succesvalidare',
        rules: {
            Nume: "required", //input
            Denumire_firma: "required", //input
            CUI: "required", //input
            Adresa_livrare: "required", //input
            Localitate: "required", //input
            Judet: "required", //input
            Cantitate_apa: "required", //input
            Telefon: "required", //input
            Email: { required: true, email: true } //input
        },
        messages: {
            Nume: "",
            Denumire_firma: "",
            CUI: "",
            Adresa_livrare: "",
            Localitate: "",
            Judet: "",
            Cantitate_apa: "",
            Telefon: "",
            Email: ""
        }
});



$('#trcv').validate({
        errorClass: 'eroarevalidare2',
		validClass: 'succesvalidare2',
        rules: {
            filecv: { required: true, accept: "jpg|jpeg|bmp|gif|xls|doc|docx|png|txt|pdf" }
        },
        messages: {
            filecv: ""
        }
});



$('#oferta').validate({
        errorClass: 'eroarevalidare',
		validClass: 'succesvalidare',
        rules: {
            Denumire_societate: "required", //input
            CUI: "required", //input
            Nume: "required", //input
            Functie: "required", //input
            Adresa: "required", //input
            Localitate: "required", //input
            Judet: "required", //input
            Telefon: "required", //input
            Email: { required: true, email: true } //input
        },
        messages: {
            Denumire_societate: "",
            CUI: "",
            Nume: "",
            Functie: "",
            Adresa: "",
            Localitate: "",
            Judet: "",
            Telefon: "",
            Email: ""
        }
});

/*

$('.car>img').each(function() {
    var y0 = $(this).position().top;
	var y1 = y0 + $(this).outerHeight(true) + $(this).next('.lipit').outerHeight(true);
	$(this).siblings('p').each(function() {
        if ( y0 <= $(this).position().top && $(this).position().top <= y1) {
			$(this).addClass('pozast');
			};
    });
	$(this).siblings('ul').children('li').each(function() {
        if ( y0 <= $(this).position().top && $(this).position().top <= y1) {
			$(this).addClass('pozast');
			};
    });

});

*/






var sex = 36;
var activ = 12;
var initialgreu = 1;
$( "#grsld" ).slider({
			value: 45,
			min: 4,
			max: 150,
			slide: function( event, ui ) {
				$('.greutate').html(ui.value);
				initialgreu = 0;
			}
		});
$('.sex').click(function() {
	$('.rqaspunscalc').css("background","url(/gfx/calcraspuns.png) no-repeat");
	$('.sex div').css("background","none");
	$('div', this).css("background","url(/gfx/sexsel.png) no-repeat");
	sex = $(this).attr("valoare");
	$('.rqaspunscalc').html('');
	if (initialgreu == 1) {
			$('.greutate').html($(this).attr("gorig"));
			$( "#grsld" ).slider( "value" , $(this).attr("gorig"))
		};
});
$('.actpas').click(function() {
	$('.rqaspunscalc').css("background","url(/gfx/calcraspuns.png) no-repeat");
	activ = $(this).val();
	$('.rqaspunscalc').html('');
});
$('#calculeaza').click(function() {
	$('.rqaspunscalc').css("background","url(/gfx/calcraspunsgasit.png) no-repeat");
	$('.rqaspunscalc').html(((parseInt(sex) + parseInt(activ))*parseInt($('.greutate').html())/1000).toFixed(1) + "L \/ zi");
});


$('.sldsecefectiv a, .accesoriu a, .lnkstire').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'changeFade' 	:   0,
		'overlayOpacity':   0.6,
		'overlayColor'  :   '#000',
		'easingIn'      :   'easeInOutQuart',
		'easingOut' 	:   'easeInOutQuart',
		'cyclic'        :   'true',
		'interval'      :   2000,
		'centerOnScroll':   true
});

$('.gmaps').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'changeFade' 	:   0,
		'overlayOpacity':   0.6,
		'overlayColor'  :   '#000',
		'easingIn'      :   'easeInOutQuart',
		'easingOut' 	:   'easeInOutQuart',
		'width'         :   600,   
		'height'        :   600, 
		'showNavArrows' :   false,
		'type'          :   'iframe',
		'centerOnScroll':   true
});





$('.pholder p').excerpt({ lines: 6, end: '...'});
$('.textstire').excerpt({ lines: 5, end: '...'});




$(function() {
	$("#poze_sld li").each(function() {
		$(".sld_titlu" , this).fontfit(31);
		$(".sld_cont" , this).fontfit(19);	
	});
	$('.sld_text').append('<div class="sld_nav"><div class="sld_prev"><div></div></div><div class="sld_next"><div></div></div></div>');
	$('#poze_sld').append('<ul class="number_slideshow_nav"></ul><div style="clear: both"></div>');
	$('#poze_sld li').each(function() {
		$('.number_slideshow_nav').append('<li><a href="javascript: void(0)"></a></li>');
		
	});
	$("#number_slideshow").number_slideshow({
		slideshow_autoplay: 'enable',//enable disable
		slideshow_time_interval: '4000',
		slideshow_window_background_color: "#fff",
		slideshow_window_padding: '0',
		slideshow_window_width: '980',
		slideshow_window_height: '365',
		slideshow_border_size: '0',
		slideshow_border_color: 'black',
		slideshow_show_button: 'enable',//enable disable
		slideshow_show_title: 'disable',//enable disable
		slideshow_button_text_color: '#CCC',
		slideshow_button_background_color: 'transparent',
		slideshow_button_current_background_color: 'transparent',
		slideshow_button_border_color: '#000',
		slideshow_loading_gif: '/gfx/loading.gif',//loading pic position, you can replace it use youself gif.
		slideshow_button_border_size: '0'
	 });
	 
	 
	$('#sld').css("visibility","visible");
	 
});





$('.dispens .dspslide').each(function(index, element) {
        $(this).scrbuline(103,103,3,0,3);
    });	

$('.galeriev2 .dspslide').each(function(index, element) {
        $(this).scrbuline(152,101,5,5,3);
    });	
	
	
$('#scrollnws').prepend('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>');
$('#scrollnws').tinyscrollbar({ size: 406 , sizethumb: 18 });



$(window).resize();



});
