$(document).ready(function(){

	var loc = new String(window.parent.document.location);
	if (loc.indexOf("https://")!= -1)
	{
		var prefix = "https://";
	}
	else
	{
		var prefix = "http://";
	}

	var linkAjax = "http://"+window.location.host+"/includes/ajax/";
	var linkAjaxSsl = prefix+""+window.location.host+"/includes/ajax/";

	$('a.lightbox').lightBox();

	$(".regZipCode").mask("99-999")

	$("img.leaf").css('zIndex', 9);

	$(".table .adv-navig span").click(function()
	{
		$(".table .adv-navig span").removeClass("active");
		$(this).addClass("active");

		$(".box-adv-hidden").hide();
		$(".box-"+this.id).show();
	});
	$(".box-adv-0").show();

	$(".dzieci").change(function()
	{
		$(".children").hide();
		var count = $(this).attr("value");

		for (var a = 1; a <= count; a++)
		{
			$("#child-"+a).show();
		}
	});

	$(".tabs-site span").click(function()
	{
		$(".tabs-site span").removeClass('active');
		$('.req').css({border: '1px solid #e0e4e8'});
		$('.alert').hide();
		$(this).addClass('active');
		$(".form-tabs-box").hide();
		$("#form-"+$(this).attr("id")).show();
	});

	$(".remind-submit").click(function()
	{
		var stan = $(".remind-box").css('display');
		$(".remind-box").show();
		if (stan == "none")
		{
			return false;
		}
	});

	$(".btn-register-modal").click(function()
	{
		$(".register-modal").show();
		$(".login-modal").hide();
	});

	$("a.jqbookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = 'http://www.mamaklub.pl/';
		var title = 'mamaklub.pl - portal dla mam dzieci w wieku 0-6 lat'

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}
	});

	$(".expand").mouseenter(function()
	{
		$(this).css({'height' : '300px'});
	})
	.mouseleave(function()
	{
		$(this).css({'height' : '100px'});
	});

		// formularz
	function validate(field)
	{
		var err = 0;
		var comm = "";
		var clr = "596e81";
		var loader = "blank";
		var id = $(field).attr("id");
		$(field).css({border: '1px solid #e0e4e8'});
		$("#comm-"+$(field).attr("id")).text("");
		var typ = $("#typ").attr("value");
		var sesid = $("#sesid").attr("value");

		if ($(field).hasClass('req') == true)
		{
			var param = $(field).attr("param");
			var val = $(field).attr("value");

			if ($(field).attr("value") == "")
			{
				err = 1;
				block(1, id);
				$(field).css({border: '1px solid #f00'});
			}
			else
			{
				if ($(field).hasClass('req-login'))
				{
					// sprawdz dlugosc znakow
					var loginLength = $(field).attr("value").length;
					if (loginLength < 6) { block(1, id); comm = "Nick jest za krótki, pownien składać się z minimum 6 znaków."; err = 1; clr = "a60000";  loader = "ico-err"; }
					else if (loginLength > 18) { block(1, id); comm = "Nick jest za długi, składać się z maksymalnie 12 znaków."; err = 1; clr = "a60000"; loader = "ico-err"; }
					else
					{
						err = 0;
						block(1, id);
						comm = "Trwa sprawdzanie dostępności loginu."; clr = "596e81"; loader = 'load-mini';
						$.get(linkAjaxSsl+"check.login.php?login="+$(field).attr("value")+"&typ="+typ+"&sesid="+sesid, function(data){ checkLogin(data); });
					}

					function checkLogin(data)
					{
						if (data == 0) { block(0, id); comm = "Nick jest dostępny"; err = 0; clr = "045d00"; loader = "ico-ok"; } else { block(1, id); comm = "Nick jest zajęty. Wpisz inny"; err = 1; clr = "a60000"; loader = "ico-err"; }
						if (comm != "") { $("#comm-"+$(field).attr("id")).show().css({display : 'block', color : '#'+clr}).html(comm); }
					}
				}
				else if  ($(field).hasClass('req-refield'))
				{
					var compareParam = $(field).attr("name").split("-");
					var compare = compareParam[1];
					var orginal = $("#"+compare).attr("value");
					var copies = $(field).attr("value")
					if (copies == orginal)
					{
						block(0, id);
					}
					else
					{
						block(1, id);
					}
				}
				else if ($(field).hasClass('req-email'))
				{
					if(val.match(/^[0-9a-z_.-]+@([0-9a-z-]+\.)+[a-z]{2,6}$/))
					{
						err = 0;
						block(1, id);
						comm = "Trwa sprawdzanie, czy adres e-mail jest już zarejestrowany w bazie danych."; clr = "596e81"; loader = "load-mini";
						$.get(linkAjaxSsl+"check.email.php?email="+$(field).attr("value")+"&typ="+typ+"&sesid="+sesid, function(data){ checkEmail(data); });

						function checkEmail(data)
						{
							if (data == 0) { block(0, id); comm = "Adres e-mail jest poprawny"; err = 0; clr = "1DC45D"; loader = "ico-ok"; } else { block(1, id); comm = "Adres e-mail jest już zarejestrowany w bazie danych."; err = 1; clr = "a60000"; loader = "ico-err"; }
							if (comm != "") { $("#comm-"+$(field).attr("id")).show().css({display : 'block', color : '#'+clr}).html(comm); }
						}

					}
					else
					{
						block(1, id);
						comm = "Podany adres e-mail jest nieprawidłowy.";
						err = 1;
						clr = "a60000";
						loader = "ico-err";
					}
				}
				else if ($(field).hasClass('req-mail'))
				{
					if(val.match(/^[0-9a-z_.-]+@([0-9a-z-]+\.)+[a-z]{2,6}$/))
					{
						block(0, id);
						comm = "Adres e-mail jest poprawny";
						err = 0;
						clr = "1DC45D";
						loader = "ico-ok";
					}
					else
					{
						block(1, id);
						comm = "Podany adres e-mail jest nieprawidłowy.";
						err = 1;
						clr = "a60000";
						loader = "ico-err";
					}
				}
				else
				{
					err = 0;
					block(0, id);
				}
				if (comm != "") { $("#comm-"+$(field).attr("id")).show().css({display : 'block', color : '#'+clr}).html(comm); }
				if (err == 1) { $(field).css({border: '1px solid #f00'}); }
			}
		}
		return err;
	}

	function block(e, id)
	{
		$("#req-"+id).val(e);
	}

	$("input.title, textarea.title").blur(function()
	{
		$(".register-items .label div.box-title").hide();
		validate($(this));
	});

	$("select.req").change(function()
	{
		if ($(this).attr("value") == "")
		{
			$(this).css({border: '1px solid #f00'});
			$("#req-"+this.id).val(1);
		}
		else
		{
			$(this).css({border: '1px solid #e0e3e7'});
			$("#req-"+this.id).val(0);
		}
	});

	$("input.req, textarea.req").click(function()
	{
		$(".register-items .checkbox span.border-checkbox").css({border: '1px solid #eeeced'});
		if ($(this).attr("type") == 'checkbox')
		{
			if ($(this).is(':checked') == true)
			{
				block(0,$(this).attr("id"));
				$("#check-"+$(this).attr("id")).css({border: '1px solid #fff'});
			}
			else
			{
				block(1,$(this).attr("id"));
				$("#check-"+$(this).attr("id")).css({border: '1px solid #f00'});
			}
		}
	});

	$("form.validate").submit(function()
	{
		var err = 0;
		$('#'+$(this).attr("id")+" input.req, #"+$(this).attr("id")+" select.req, #"+$(this).attr("id")+" textarea.req").each(function()
		{
			
			var ids = this.id;
			// sprawdz czy pole jest wypełnione
			var req = $("#req-"+this.id).attr("value");
			if (req == 1)
			{
				if (this.type == 'checkbox')
				{
					$("#check-"+this.id).css({border: '1px solid #f00'});
				}
				else
				{
					$(this).css({border: '1px solid #f00'});
				}
				err += 1;
			}

		});
		if (err > 0)
		{
		
			$(".alerts").show();
			$(".alerts").text('Wypełnij/zaznacz pola obowiązkowe');
			return false;
		}
		else
		{
			
			$(".alerts").hide();
			$(this).submit();
			
		}
	});

	// formularz

});

