$(document).ready( function () {



	$('.c_all').corner('5px');
	$('.c_r').corner('right 5px');
	$('.c_b').corner('bottom 5px');
	$('.c_t').corner('top 5px');



	$('.lightbox').lightBox();

	if($('#txt').height() < $('#left_menu').height()){
		$('#txt').css('height', ($('#left_menu').height()-12)+'px');
	}


	/** 
		wyszukiwarka na belce
	*/
	
	$('#search_f').keyup(function(event) {
	
	
			var long_val = $(this).val();
			
		//jezeli enter to wyslij
			//if(event.keyCode==13) resultButton();
			
			if(long_val.length == 0) {$('#searchResult ul').css('display','none'); $('#searchResult').css('display','none');}//brak wynikow
			if(long_val.length < 3) return false; //za malo znakow
		
			moduleSearch(long_val);
	});
	
	/** 
		menu glowne
	*/
	
	$('#menu li').hover(function(){
		var rel = $(this).attr('rel');
		$('.l'+rel).show();
	
	}, function(){
		var rel = $(this).attr('rel');
		$('.l'+rel).hide();
	
	});
	
	/* lupka na karcie produktu */
	
	$('.loop_hover').hover(function(){
		$('.loop_zooming').hide();
		var rel = $(this).attr('rel');
		$('.show_'+rel).fadeIn('slow');
	}, function(){
	
	
	});
	
	$('.loop_end').hover(function(){
	
	
	}, function(){
		$(this).fadeOut('slow');
	
	});
	

	/**
	  wynajem dlugoterminowy szybka wyszukiwarka
	*/
	$('#form_brand_l').livequery('change', function() {
		 var brand = $("#form_brand_l option:selected").val();
		 change_model_car(brand, 'form_model_l');
	});
	
	/* sprzedaz samochodow poleasingowe*/
	 
	 
		var brand = $("#fbrand option:selected").val();
		change_model(brand, 'fmodel');

	$('#fbrand').livequery('change', function() {
		 var brand = $("#fbrand option:selected").val();
		 change_model(brand, 'fmodel');
	});
	
	
	$('#form_brand_s').livequery('change', function() {
		 var brand = $("#form_brand_s option:selected").val();
		 change_model_car(brand,'form_model_s');
	});
	
	$('a.send_long').livequery('click', function() {

			 var formData = $('#form_long').serialize();
			
			$(".dlugoterminowy").mask("wysyłanie...");	
			var url = URL+'ajaxLoad/send.php';

			$.post(url, formData, function(data) {
			$('#sendmsg').html(data);
			$(".dlugoterminowy").unmask("loading...");
			if(data!=''){
			$('#msg_container').fadeIn('slow');
			$(".dlugoterminowy").css('opacity','0.3');
			$(".dlugoterminowy").css('filter','alpha(opacity=30)');
			}
		});
	});
	
	$('.add_newsletter').livequery('click', function() {
	

			 var formData = $('#form_news').serialize();
			
			$("#news").mask("wysyłanie...");	
			var url = URL+'ajaxLoad/add_newsletter.php';

			$.post(url, formData, function(data) {
			$('#sendmsg_news').html(data);
			$("#news").unmask("loading...");
			if(data!=''){
			$('#msg_news_container').fadeIn('slow');
			$("#news").css('opacity','0.3');
			$("#news").css('filter','alpha(opacity=30)');
			}
		});
	});
	
	
	$('a.close_btn').livequery('click', function() {
	
		$(".dlugoterminowy").css('opacity','1');
		$(".dlugoterminowy").css('filter','alpha(opacity=100)');
		$('#msg_container').css('display','none');
	});
	
	$('a.close_btn_news').livequery('click', function() {
	
		$("#news").css('opacity','1');
		$("#news").css('filter','alpha(opacity=100)');
		$('#msg_news_container').css('display','none');
	});
	
	
	
	$(".button a").livequery('click', function(){
		 var type = $(this).attr('rel');
		 
		$(".dlugoterminowy").css('opacity','1');
		$(".dlugoterminowy").css('filter','alpha(opacity=100)');
		$('#msg_container').css('display','none');
		 
		 
		switch(type){
		 case 'dlugoterminowy':

			$('.poleasingowe').hide();
			$('.dlugoterminowy').fadeIn("slow");
			
			var temp = $('.button1').html();
			$('.button1 a').replaceWith($('.button2').html());
			$('.button2').html(temp);
			
			$('#send').html('<a class="send_long">Wyślij</a>');
		 
		 break;
		 case 'poleasingowe':
			
			$('.dlugoterminowy').hide();
			$('.poleasingowe').fadeIn("slow");
			
			var temp = $('.button2').html();
			$('.button2 a').replaceWith($('.button1').html());
			$('.button1').html(temp);
			
			$('#send').html('<a class="search_offer">Szukaj</a>');
			
		 break;
		 }
		 
	});
	
	/**
	  kalendarz na wynajmie długoterminowym
	*/
	 
	 $.datepicker.regional['pl'] = {
                closeText: 'Zamknij',
                prevText: '&#x3c;Poprzedni',
                nextText: 'Następny&#x3e;',
                currentText: 'Dziś',
                monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',
                'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
                monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze',
                'Lip','Sie','Wrz','Pa','Lis','Gru'],
                dayNames: ['Niedziela','Poniedzialek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
                dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
                dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
                weekHeader: 'Tydz',
                dateFormat: 'yy-mm-dd',
                firstDay: 1,
                isRTL: false,
                showMonthAfterYear: false,
                yearSuffix: ''};
        $.datepicker.setDefaults($.datepicker.regional['pl']);

	
	$( "#form_date" ).datepicker({ 
						format:'Y-m-d',
						date: $('#form_date').val(),
						current: $('#form_date').val(),
						minDate: new Date(),
						starts: 1,
						position: 'r',
						onBeforeShow: function(){
						$('#form_date').DatePickerSetDate($('#form_date').val(), true);
			},
			onChange: function(formated, dates){
			$('#form_date').val(formated);
			$('.datepicker').DatePickerHide();		
	}
});
		
		/** 
			wyszukiwarka na pierwszej stronie
		*/
		
		$('.search').toggle(function() {
			$('.searchinput').fadeIn("slow");
			$('#search_f').focus();

			}, function() {
			$('#searchResult').css('display','none');
			$('.searchinput').fadeOut("slow");

		});
		
		
		$('.gallery_img').hover(function(){
			var src = URL+'pliki/uznanie/'+$(this).attr('id');
			$(this).attr('src',src);        
		}, function(){

			var src = URL+'pliki/uznanie/'+$(this).attr('id')+'&fltr[]=gray';
			$(this).attr('src',src);
		});
		
		
		$('.miniatury ul li:first-child a img').css('opacity',0.5);
		
		$(".miniatury li").click(function(){
		

			var lval = $(this).attr('rel');
			
			$('.miniatury li a img').css('opacity',1);
			$('.miniatury li').css('opacity',1);
			$(this).css('opacity',0.5);
	
			$('.big').css('display','none');
	
			$('.img_l_'+lval).css('display','block');
	
 
		return false;
		});
		
		
		$('.calculator_button').click(function(){
		
			$('#calc').slideDown('slow');
		
		});
		
		$('a.roll').click(function(){
		
			$('#calc').slideUp('slow');
		
		})
		
		
		$('a.set_order').click(function() {
		var typ = $(this).attr('rel');
		
			$.cookie('set_order',typ );
		
			if($.cookie('set_type')=='asc'){
				$.cookie('set_type','desc');
			}else{
				$.cookie('set_type','asc');
			}
		
			location.reload();
	});
	
	$('#news_box').jcarousel({
        animation: 'slow',
        vertical: true,
        scroll: 1,
        auto: 3,
        wrap: 'circular',
        initCallback: mycarousel_initCallback

		});
        
        
        
  /* zwijany roll */
  
  $("#pageflip").hover(function() { //On hover...
	$("#pageflip img , .msg_block").stop()
		.animate({ //Animate and expand the image and the msg_block (Width + height)
			width: '307px',
			height: '319px'
		}, 500);
	} , function() {
	$("#pageflip img").stop() //On hover out, go back to original size 50x52
		.animate({
			width: '100px',
			height: '102px'
		}, 220);
	$(".msg_block").stop() //On hover out, go back to original size 50x50
		.animate({
			width: '100px',
			height: '96px'
		}, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
});
          
    
    
    //domyslnie Warszawa
    if($('#google_adres').length>0)
    codeAddress();          

});

	function mycarousel_initCallback(carousel)
	{
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});
                       
	carousel.buttonPrev.bind('click', function() {
	carousel.startAuto(0);
	});
                       
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
	carousel.stopAuto();
	}, function() {
		carousel.startAuto();
		});
	};

		function moduleSearch(inputValue){
		
	var url = URL+'ajaxLoad/findtext.php';
	
	$.post(url, 'value='+inputValue, function(data) {
		$('#searchResult').html(data);
		if(data!=''){
			$('#searchResult').css('display','block');
		}
	});
}
	
	function change_model_car(brand,ret){
	
		$.post(URL+"ajaxLoad/model_car.php", 'brand='+brand,
						function(data){
							$('#'+ret).html(data);
		});
	}
	
	function change_model(brand,ret){
	
		$.post(URL+"ajaxLoad/change_model.php", { brand:brand, fmodel:fmodel} ,
						function(data){
							$('#'+ret).html(data);
		});
	}
	
	function search_run() {

					
		var otherparams = new Array();
		var params = new Array();
				
		var params = $('#f_search').serializeArray();
	
		jQuery.each(params, function(i, field) {
			
			if(field.value!=''){
				otherparams.push(field.name+"="+field.value);
		}
	
		});

					
		otherparams=otherparams.join('&');

		new_url = URL+"oferta_samochodow_poleasingowych.html&"+otherparams;
					
		location.href= new_url;
			
	}
