$(document).ready(function() {


	/*
	 * NEWSLETTER
	 */
	$("#newsletter-field")
	.focus( function() {
		if($(this).val() == $(this)[0].defaultValue)
		{
			$(this).val('');
		}
		jQuery('#newsletter-link').css('opacity', 1);
	})
	.blur( function() {
		if($(this).val() == "")
		{
			$(this).val($(this)[0].defaultValue);
			jQuery('#newsletter-link').css('opacity', 0.3);
		}
	})
	.change( function()	{
		$('#email').val($(this).val());
	});
	
	var tabNumber = Math.floor(Math.random()*4);
	$("#tab-"+tabNumber).click();
	
	if($('#newsletter-field').length) 
	{
		if($('#newsletter-field').val() == $('#newsletter-field')[0].defaultValue);
			jQuery("#newsletter-link").css('opacity', 0.3);
		var newsletterBoxy =  new Boxy('#newsletter-boxy', {modal: true, show: false, title: 'Subskrybuj Newsletter !', closeText: 'ZAMKNIJ'});
		var regulaminBoxy =  new Boxy('#regulamin', {modal: true, show: false, title: 'Regulamin Newslettera', closeText: 'ZAMKNIJ'});
		//jQuery("#newsletter-regulamin").click(function(){regulaminBoxy.show(); return false;});
		jQuery("#newsletter-link").click(function(){
			newsletterBoxy.show(); $('#email').focus(); return false;
		}).next().hide();
		jQuery("#newsletter-form").validate({
	        rules: 
			{
	            email: 
				{
	                required: true,
	                email: true
	            },
				zgoda: 'required'
	        },
	        messages: {
	            email: {
	                email: 'Nieprawidłowy adres e-mail'
	            }
	        },
			errorElement: 'span',
			errorPlacement: function(error, element) 
			{
				error.insertBefore(element);
		        element.addClass('error');
			}
		});
	}
	
	/*
	 * SEARCH FIELD
	 */
	$("#search-field").autocomplete("php/search.php", {
		width: 170,
		selectFirst: false
	});
	$("#search-field").focus(function() {
		if($(this).val() == 'Szukaj...')
			$(this).val('');
	});
	$("#newsletter-submit").click(function() {
		$(".form-error").remove();
		if( !validateEmail( $("#newsletter-field").val() ) )
		{
			$(this).after('<p class="form-error" style="margin: 4px 0 0 12px; padding: 0;color: #ff0000">Nieprawidłowy e-mail</p>');
		}
		else
		{
			$.ajax({  
				type: "post",
				url: "php/newsletter.php", 
				data: "email="+$("#newsletter-field").val(),
				cache: true, 
				beforeSend: function() {
				},
				success: function(data) {
					if(data == 0)
						$("#newsletter-submit").after('<p class="form-error" style="margin: 4px 0 0 12px; padding: 0;color: #ff0000">Newsletter subskrybowano</p>');
					else
						$("#newsletter-submit").after('<p class="form-error" style="margin: 4px 0 0 12px; padding: 0;color: #ff0000">E-mail już subskrybuje newsletter</p>');				
						
				},
				error: function() {
					$("#newsletter-submit").after('<p class="form-error" style="margin: 4px 0 0 12px; padding: 0;color: #ff0000">Nieudane połączenie z bazą. Spróbuj ponownie.</p>');
				}
			}); 
		}
		return false;
	});
	
	if($("#wojewodztwo").val() != -1 && $("#wojewodztwo").val() != undefined)
	{
		$.ajax({  
			type: "post",
			url: "php/getCities.php", 
			data: "wojewodztwo="+$("#wojewodztwo").val(),
			cache: true, 
			dataType: "json",
			beforeSend: function() {
			},
			success: function(data) {
				$("#miasto").children().remove();
				$(document.createElement('option')).html('wybierz...').val(-1).appendTo("#miasto");
				var item;
				for(i=0;i<data.length;++i)
				{
					item = $(document.createElement('option')).html(data[i].title).val(data[i].title);
					if($("#city").html() == data[i].title)
					{
						item.attr("selected","selected");
					}
					item.appendTo("#miasto");
				}				
					
			},
			error: function() {
				
			}
		}); 
		$("#miasto").attr("disabled","");
		
	}
	
	$("#wojewodztwo").change(function() {
		getCities($(this).val());
	});
	
	$("#miasto").change(function() {
		if( $(this).val() == -1 )
			return false;
		else
			this.form.submit();
	});
	
	$.listen('click','.unfold',function () {
		$('.products-more').show();
		$(this).attr("class","fold");
		$(this).html("zwiń");
		return false;
	});
	
	$.listen('click','.fold',function () {
		$('.products-more').hide();
		$(this).attr("class","unfold");
		$(this).html("rozwiń");
		return false;
	});
	
	$.listen('click','.unfold2',function () {
		$(this).parent("h2").next('div.results').css("display","block");
		$(this).attr("class","fold2");
		return false;
	});
	
	$.listen('click','.fold2',function () {
		$(this).parent("h2").next('.results').css("display","none");
		$(this).attr("class","unfold2");
		return false;
	});
	
	$("#city-map-change").change(function() {
		this.form.submit();
	});
	
	$(".faq-question").click(function () {
		var answer = $(this).parent("h4").next("div");
		if(answer.css("display") == "none")
			answer.slideDown();
		else
			answer.slideUp();
		return false;
	});
	
	$(".prod-thumb").click(function () {
		var img = new Image();
		if($(this).attr("href") == $(".product-big-image img").attr("src"))
			return false;
		else
		{	
			$(".product-big-image img").css("visibility",'hidden');		
			img.src = $(this).attr("href");
			$(".product-big-image img").attr("src",img.src);
			if(img.complete)
				manageImg();
			else
				img.onload = manageImg;
			$("#photo-zoom").attr("href",$(this).attr("href"));
			$("#photo-zoom").lightBox();
			$(".prod-thumb").parent("li").removeClass('selected');
			$(this).parent("li").addClass("selected");
			
			return false;
		}
	});
	
	$("#marka-select, #kat-select").change(function() {
			this.form.submit();
	});
	
	$("#opinion-toggle").click(function () {
		if($("#opinion-form").css("display") == 'none')
			$("#opinion-form").slideDown("slow");
		else
			$("#opinion-form").slideUp("slow");
		return false;
			
	});
	
	$("#wyslij-opinion").click(function() {
		$(".form-error").remove();
		if($("#email-opinion").val() == '' || $("#text-opinion").val() == '' || $("#title-opinion").val() == '')
		{
			$("#opinion-form").prepend('<p class="form-error" style="margin: 4px 0; padding: 0;color: #ff0000">Uzupełnij wszystkie pola</p>');
		}
		else if( !validateEmail( $("#email-opinion").val() ) )
		{
			$("#opinion-form").prepend('<p class="form-error" style="margin: 4px 0; padding: 0;color: #ff0000">Nieprawidłowy e-mail</p>');
		}
		else
		{
			$.ajax({  
				type: "post",
				url: "php/opinion.php", 
				data: "email="+$("#email-opinion").val()+"&tresc="+$("#text-opinion").val()+"&title="+$("#title-opinion").val()+"&shops="+$("#shop-opinion").val(),
				cache: true, 
				beforeSend: function() {
				},
				success: function(data) {
					if(data == -1)
						$("#opinion-form").prepend('<p class="form-error" style="margin: 4px 0; padding: 0;color: #ff0000">Nieudane połączenie z bazą. Spróbuj ponownie.</p>');	
					else
						$("#opinion-form").prepend('<p class="form-error" style="margin: 4px 0; padding: 0;color: #ff0000">Opinia wysłana</p>');			
						
				},
				error: function() {
					$("#opinion-form").prepend('<p class="form-error" style="margin: 4px 0; padding: 0;color: #ff0000">Nieudane połączenie z bazą. Spróbuj ponownie.</p>');
				}
			}); 
		}
		return false;
	});
	
	$(".print").click(function () {
		window.print();
		return false;
	});
	

});

function validateEmail(emailAddress) {

var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

return pattern.test(emailAddress);

}

function popupTab (url, title, wt, ht) {

	window.open(url,title,'status=0, toolbar=0, scrollbars=1, height='+ht+', width='+wt);

}

function toggleSpec () {
	$(".hide").toggle();
}


function getCities(id) {

	if(id == -1)
		$("#miasto").attr("disabled","disabled");
	else
	{
		$.ajax({  
			type: "post",
			url: "php/getCities.php", 
			data: "wojewodztwo="+id,
			cache: true, 
			dataType: "json",
			beforeSend: function() {
			},
			success: function(data) {
				$("#miasto").children().remove();
				$(document.createElement('option')).html('wybierz...').val(-1).appendTo("#miasto");
				for(i=0;i<data.length;++i)
				{
					$(document.createElement('option')).html(data[i].title).val(data[i].title).appendTo("#miasto");
				}
				$("#wojewodztwo").children("option").each(function () {
					$(this).attr("selected","");
					if($(this).val() == id)
						$(this).attr("selected","selected");
				});				
					
			},
			error: function() {
				
			}
		}); 
		$("#miasto").attr("disabled","");			
	}

}



