var $j = jQuery.noConflict();

$j(document).ready(function() {

	RunMask();
    
    $j("#country ul.links h1").css('display', 'none');
    $j("#country ul.links h2").css('display', 'none');

	$j("#menu li").hover(
		function() {
			$j(this).addClass('hover');
		},
		function() {
			$j(this).removeClass('hover');
		}
	);
	
	var tabName = "";
	$j("#menu li").click(
		function() {
			tabName = $j(this).attr('name');
			$j('#menu li.active').removeClass('active');
			$j(this).addClass('active');
			$j('#tabs ul').addClass('hidden');
			$j('#tabs #'+tabName).removeClass('hidden');
		}
	);	
	
	$j(".sectionMenu li").hover(
		function() {
			$j(this).addClass('hover');
		},
		function() {
			$j(this).removeClass('hover');
		}
	);
	
	var sectionName = "";
	$j(".sectionMenu li").click(
		function() {
			sectionName = $j(this).attr('name');
			$j('.sectionMenu li.active').removeClass('active');
			$j(this).addClass('active');
			$j('.sectionHolder .section .sec').addClass('hidden');
			$j('.sectionHolder .section .'+sectionName).removeClass('hidden');
		}
	);
	
	
	$j("#country .links li").click(function() {
		$j("#country .links li.active").removeClass('active');
		$j("#country .image img").attr('src', 'themes/cuf/images/box/'+$j(this).attr('class')+'.jpg');
		$j(this).addClass('active');	
	});
	
	
	
	var country = {
		wielka_brytania : 'Wielka Brytania',
		irlandia : 'Irlandia',
		holandia : 'Holandia',
		usa : 'USA',
		norwegia : 'Norwegia',
		niemcy : 'Niemcy',
		belgia : 'Belgia',
		austria : "Austria"
	};
		
	var y_wielka_brytania = {
		2008 : "2008/2009",
		2009 : "2009/2010",
		2010 : "2010/2011",
		2011 : "2011/2012"
	};
	
	var y_irlandia = {
		2008 : "2008",
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	var y_holandia = {
		2007 : "2007",
		2008 : "2008",
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	var y_usa = {
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	var y_norwegia = {
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	var y_niemcy = {
		2008 : "2008",
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	var y_belgia = {
		2008 : "2008",
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	var y_austria = {
		2007 : "2007",
		2008 : "2008",
		2009 : "2009",
		2010 : "2010",
		2011 : "2011"
	};
	
	$j.each(country, function(val, text) {
		$j("#Form_SidebarForm_Country").append(
			$j('<option></option>').val(val).html(text)
		);
	});	
	
	$j("#Form_SidebarForm_Country").change(function() {
		var val = $j("#Form_SidebarForm_Country :selected").val();		
		$j("#Form_SidebarForm_Year option[value != 0]").remove();		
		$j.each(eval('y_'+val), function(val, text) {
			$j("#Form_SidebarForm_Year").append(
				$j('<option></option>').val(val).html(text)
			);
		});		
	});

	$j("#DocsForm_Submit").click(function() {
		var c = $j("#DocsForm_Country").val();		
		var y = $j("#DocsForm_Year").val();
		
		if (c != 0 && y != 0) {
			location.href = '/dokumenty?c='+c+'&y='+y;
			return false;
		} else {
			if (c==0)
				$j("#DocsForm_Country").addClass('error');
			if (y==0)
				$j("#DocsForm_Year").addClass('error');
			return false;
		}
	});

	$j("#CalcForm_Submit").click(function() {
		var c = $j("#CalcForm_Country").val();
		
		if (c != 0) {
			location.href = '/kalkulator-'+c;
			return false;
		} else {
			if (c == 0)
				$j("#CalcForm_Country").addClass('error');
			return false;
		}
	});
	
	$j("#DocsForm_Country").change(function() {
		var val = $j("#DocsForm_Country :selected").val();		
		$j("#DocsForm_Year option[value != 0]").remove();		
		$j.each(eval('y_'+val), function(val, text) {
			$j("#DocsForm_Year").append(
				$j('<option></option>').val(val).html(text)
			);
		});
		if ($j(this).val() != 0)
			$j(this).removeClass('error');
	});
	
	$j("#DocsForm_Year").change(function() {
		if ($j(this).val() != 0)
			$j(this).removeClass('error');
	});
	
	$j("#navi .confirm").hover(function() {
		$j("#navi .full").show();
		return false;
	}, function() {
		$j("#navi .full").hide();
		return false;
	});
	
	$j(".confirm2").hover(function() {
		$j(".full2").show();
		return false;
	}, function() {
		$j(".full2").hide();
		return false;
	});
	
});

function RunMask() {

	var nameVal = "Imię i nazwisko";
	var emailVal = "E-mail";
	var stacVal = "Telefon stacjonarny";
	var komVal = "Telefon komórkowy";
	
	if ($j("#Form_SidebarForm_Name").val() == "")
		$j("#Form_SidebarForm_Name").val(nameVal);
		
	if ($j("#Form_SidebarForm_Email").val() == "")
		$j("#Form_SidebarForm_Email").val(emailVal);
		
	if ($j("#TelStac_TelKom-Number").val() == "")
		$j("#TelStac_TelKom-Number").val(stacVal);
		
	if ($j("#TelKom-Number").val() == "")
		$j("#TelKom-Number").val(komVal);

	$j("#Form_SidebarForm_Name").focus(function() {
		if ($j(this).val() == nameVal)
			$j(this).val('');
	}).focusout(function() {
		if ($j(this).val() == '')
			$j(this).val(nameVal);
	});

	$j("#Form_SidebarForm_Email").focus(function() {
		if ($j(this).val() == emailVal)
			$j(this).val('');
	}).focusout(function() {
		if ($j(this).val() == '')
			$j(this).val(emailVal);
	});
	
	$j("#TelStac_TelKom-Number").attr('size', 20).attr('maxlength', 20).focus(function() {
		$j("#TelStac_TelKom-Number").mask("(99)99-99-999");
	}).blur(function() {
		$j("#TelStac_TelKom-Number").unbind("keydown.mask").unbind("keypress.mask");
	}).focusout(function() {
		var stc_val = $j("#TelStac_TelKom-Number").val();
		if (stc_val == "(__)__-__-___" || stc_val == "") {
			$j("#TelStac_TelKom-Number").val(stacVal);
		}
	});

	$j("#TelKom-Number").attr('size', 20).attr('maxlength', 20).focus(function() {
		$j("#TelKom-Number").mask("999-999-999");
	}).blur(function() {
		$j("#TelKom-Number").unbind("keydown.mask").unbind("keypress.mask");
	}).focusout(function() {
		var kom_val = $j("#TelKom-Number").val();
		if (kom_val == "(__)__-__-___" || kom_val == "") {
			$j("#TelKom-Number").val(komVal);
		}
	});
	
	$j('#Form_SidebarForm').submit(function() {
		var tCountry	= $j("#Form_SidebarForm_Country").val();
		var tYear		= $j("#Form_SidebarForm_Year").val();
		var tName		= $j("#Form_SidebarForm_Name").val();
		var tStac		= $j("#TelStac_TelKom-Number").val();
		var tKom		= $j("#TelKom-Number").val();
		var tEmail		= $j("#Form_SidebarForm_Email").val();
		var tConfirm	= $j("#Form_SidebarForm_FConfirm").is(":checked");
		var error		= 0;


		if (tCountry == 0) {
			$j("#Form_SidebarForm #Country").append('<span class="message required">Proszę wybrać kraj podatkowy.</span>');
			error = 1;
		} else {
			$j("#Form_SidebarForm #Country .message").hide();
		}
		
		if (tYear == 0) {
			$j("#Form_SidebarForm #Year").append('<span class="message required">Proszę wybrać rok podatkowy.</span>');
			error = 1;
		} else {
			$j("#Form_SidebarForm #Year .message").hide();
		}
		if (tName == nameVal || tName == "") {
			$j("#Form_SidebarForm #Name").append('<span class="message required">"Imię i nazwisko" jest wymagane.</span>');
			error = 1;
		} else {
			$j("#Form_SidebarForm #Name .message").hide();
		}
		if ((tStac == stacVal || tStac == "") && (tKom == komVal || tKom == "")) {
			$j("#Form_SidebarForm #TelStac_TelKom").append('<span class="message required">Podaj nr telefonu stacjonarnego lub komórkowego.</span>');
			error = 1;
		} else {
			$j("#Form_SidebarForm #TelStac_TelKom .message").hide();
		}
		if (tEmail != emailVal && tEmail != "" && !isValidEmailAddress(tEmail)) {
			$j("#Form_SidebarForm #Email").append('<span class="message required">Wprowadź prawidłowy adres e-mail.</span>');
			error = 1;
		} else {
			$j("#Form_SidebarForm #Email .message").hide();
		}
		if (!tConfirm) {
			$j("#Form_SidebarForm #FConfirm").append('<span class="message required" style="font-size: 10px">Wyraź zgodę, abyśmy mogli przygotować i przesłać ofertę.</span>');
			error = 1;
		} else {
			$j("#Form_SidebarForm #FConfirm .message").hide();
		}
		
		if (error) {
			return false;
		} else {
			if ($j("#Form_SidebarForm_Name").val() == nameVal)
				$j("#Form_SidebarForm_Name").val('');
			if ($j("#Form_SidebarForm_Email").val() == emailVal)
				$j("#Form_SidebarForm_Email").val('');
			if ($j("#TelStac_TelKom-Number").val() == stacVal)
				$j("#TelStac_TelKom-Number").val('');
			if ($j("#TelKom-Number").val() == komVal)
				$j("#TelKom-Number").val('');
			return true;
		}
	});
	
	var searchDef = "Szukaj...";
	$j('#search form .text').val(searchDef).focus(function() {
		if ($j('#search form .text').val() == searchDef)
			$j('#search form .text').val('');
	}).focusout(function() {
		if ($j('#search form .text').val() == '')
			$j('#search form .text').val(searchDef);
	});

}

	function isValidEmailAddress(emailAddress) {
		if (emailAddress.match(/^([a-zA-Z0-9_+\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/))
			return true;
		else
			return false;
	}
