﻿$(document).ready(function() {
    $("#pproducer").change(function() {
        var pproducer = $("option:selected", this).val();
		var pml = $("#pmodel");
		var loaderIndicator = $("#lamp-form-loader");
		var disable_pml = function(disable){
			if(disable){
				pml.attr("disabled","disabled");
				return;
			}
			pml.removeAttr("disabled");
			loaderIndicator.css('display' , 'none');
		}
		
		var setOptions = function(jsonData){
			var options = "";
			if(jsonData.success){
				options += '<option value="">Wybierz model</option>';
				$.each(jsonData.data, function(i,item){
            		options += '<option value="' + i +  '">' + item + '</option>';
          		});
			}
			pml.html(options);
			disable_pml(false);
		}
		
		disable_pml(true);
		
		if(pproducer != ""){
			pml.html('');
			loaderIndicator.css('display' , 'inline-block');
			$.getJSON(
				'/lampy_do_projektorow/' + pproducer  + '.html?_j=1'
				,setOptions
			);
		}
		else{
			pml.html('');
		}
    });
	

	
	
	$("#pmodel").change(function() {
		var ppl = $("#pproducer");
		var pmodel = $("option:selected", this).val();
        var pproducer = $("option:selected", ppl).val();
		if (pmodel) {
			window.location = '/lampa_do_projektora/' + pproducer + '__' + pmodel;
		}
	});




	$('a.hidden-m').nospam({ replaceText: true});

	var _mb = $('#messageBox1'); 
	var _cf = $('#c-f');
	
	$('#c-f .hi').hover(function() {
		$(this).addClass('input-hover');
	}, function() {
		$(this).removeClass('input-hover');
	});

	$('#c-f .hi').focus(function() {
		$(this).addClass('input-hover');
	});
	$('#c-f .hi').blur(function() {
		$(this).removeClass('input-hover');
	});
     
	$('#c-f-submit').hover(function() {
		$(this).addClass('c-f-submit-hover');
	}, function() {
		$(this).removeClass('c-f-submit-hover');
	});
      
      
	$("#contact-form-fancybox").validate({
		rules: {
    		d1: {
				required: true,
    			minlength: 5
    		},
    		d2: {
    			required: true,
       			email: true
     		},
			d4: {
				required: function(element) {return $("#d6").val() == 2;},
				minlength: 4
      		},
			d5: {
				required: function(element) { return $("#d6").val() == 3;},
				minlength: 4
      		},       			
     		d8: {
				required: true,
    			minlength: 4
    		}
	   	},
	   	
		messages: {
			d1: {
				required: "Prosimy podać <b>Imię i Nazwisko</b>",
				minlength: "Prosimy podać poprawne <b>Imię i Nazwisko</b>"				
			},
			d2: {
				required: "Prosimy podać <b>Adres email</b>",
				email: "Prosimy podać poprawny <b>Adres email</b>"
			},
			d4: {
				required: "Dla wybranej formy kontaktu prosimy podać <b>Telefon stacjonarny</b>",
				minlength: "Prosimy podać poprawny <b>Telefon stacjonarny</b>"	
      		},
      		d5: {
				required: "Dla wybranej formy kontaktu prosimy podać <b>Telefon komórkowy</b>",
				minlength: "Prosimy podać poprawny <b>Telefon komórkowy</b>"
      		},
			d8: {
				required: "Prosimy podać <b>Treść zapytania</b>",
				minlength: "Prosimy podać poprawną <b>Treść zapytania</b>"	
			}
			
		},
	   	 		
 		errorPlacement: function(error, element) {
    		return false;
   		},
		errorContainer: "#messageBox1",
		errorLabelContainer: "#messageBox1",
		wrapper: "span",
		onkeyup: false,
		submitHandler: function(form) {
			
			$('#c-f').block({ 
				message: 'Trwa wysyłanie wiadomościi ', 
 				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: '.8', 
					color: '#fff' 
 				}  
			});		
			
			var formData = $("#contact-form-fancybox").serialize();
			$.ajax({  
				type: "POST",  
				url: "/c-f.php",  
				data: formData,
				cache: false,
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					_mb.html('Wystąpił błąd w czasie wysyłania formularza. Prosimy sróbować ponownie.');
					_mb.css('display','block');
					_cf.unblock();
				},
				success: function(data) {
					_mb.html('');
					_mb.css('display','none');
					
					if(data == 1){
						_cf.unblock();
						_cf.fadeOut(800, function(){
							_cf.html('<div class="form-send">Wiadomość została wysłana. Dziękujemy!!!</div>');
							_cf.fadeIn(800);
						});
						
						if ($('#contact-window').size() > 0) {
							setTimeout('$.fancybox.close()', 3000); 
						}
						pageTracker._trackPageview('/c-f.php');
						
					}
					else{
						_mb.html('Wystąpił błąd w czasie wysyłania formularza. Prosimy sróbować ponownie.');
						_mb.css('display','block');
						_cf.unblock();
					}
				}  
			});  
   		}
	}); //$("#contact-form").validate({
	
	if($('#contact-window').size() > 0){
		$('#c-f-cancel').click(function(){
			$.fancybox.close();	
			return false;
		});
	}
	else{
		$('#c-f-cancel').css('display' , 'none');
	}
	
	if($('#c-f-link').size() > 0){
		$('#c-f-link').fancybox({
			transitionIn			: 'fade'
			,transitionOut			: 'fade'
			,hideOnContentClick		: false
			,overlayShow			: true
			,autoDimensions			: false
			,width	        		: 770
			,height					: 'auto'
		});
	}
	
	$('a.lampstock').cluetip({
		activation			: 'click'
		,width				: 440
		,closePosition		: 'title'
		,sticky				: true
		,closeText			: 'Zamknij'
  		
	});
	if (!$.browser.msie || $.browser.version >= 8) {
		$('a.tip-tip').tipTip();
	}
	
	$("#quicksearch").autocomplete("/quicksearch", {
		width				: 212
		,selectFirst			: false
		//,autoFill			: true
		,minChars			: 3
		,delay				: 100
		,max				: 1000
		,matchContains		: true
		//,mustMatch			: true

	});

	$("#quicksearch-submit").click(function(){
		$("#quicksearch-form").submit();
		return false;
	});
	
	$("#quicksearch").result(function(event, data, formatted) {
 		$("#quicksearch-form").submit();
		return false;
	});
	
});