function send_form() {
	$("#formularz").submit();
}
function send_form1() {
	$("#formularz1").submit();
}
function send_form2() {
	if ($("#dietName").val().length < 3)
	{
		alert('Wpisz nazwę swojej diety (minimum 3 znaki)');
		return;
	}
	$("#formularz").submit();
}
function close_() {
	$("#closeBut").click();
}
function add_show() {
	$(".BoxNewReply").show();
	$("div.BoxBottom").css('top', $("body").outerHeight()+'px');
	document.location.hash = "formularz1";
}
function drukuj() {
	window.open('?print=1','_blank','toolbar=yes, location=no, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=715, height=500')
}

function isWeight(field) {
	var re = /^[0-9-'.'-',']*$/;
	if (!re.test(field.value)) {
		alert('Waga powinna zawierać jedynie cyfry oraz "."!');
		field.value = field.value.replace(/[^0-9-'.']/g,"");
		return;
	}
	loadKcal();
	loadBMI();
}

function isHeight(field) {
	var re = /^[0-9-'.'-',']*$/;
	if (!re.test(field.value)) {
		alert('Wzrost może zawierać jedynie cyfry!');
		field.value = field.value.replace(/[^0-9-'.']/g,"");
		return;
	}
	loadKcal();
	loadBMI();
}

function isBirthday(field) {
	var re = /^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$/;
	if (!re.test(field.value)) {
		alert('Data urodzenia może być jedynie w formacie rrrr-mm-dd (rok-miesiąc-dzień)!');
		field.value = field.value.replace(/[^0-9-'.'-',']/g,"");
		return;
	}

	var data_test = field.value.match(re);
	var rok = data_test[1];
	var msc = data_test[2];
	var dzien = data_test[3];
	
	if ((rok < 1900 || rok > (new Date()).getFullYear()) || (msc < 0 || msc > 12) || (dzien < 0 || dzien > 31)) {
		alert('Błędna data urodzenia!');
		return;
	}
	loadKcal();
	loadBMI();
}

function flashResize(flashHeight) {
	if (typeof(flashHeight) == "undefined")
	{
		flashHeight = "720";
	}
	var windowHeight = $(window).height()-60;
	if (flashHeight < windowHeight)
	{
		flashHeight = windowHeight;
	}
	$(".MainFlash").css("height", flashHeight+"px");
}

function loadBMI () {
	if (($('#birth').val().length == '10') && ($('#weight').val().length > '0') && ($('#height').val().length > '0'))
	{
		$("#BoxProfileBMIGenerate").load("/generateBMI/?birth="+$('#birth').val()+"&weight="+$('#weight').val()+"&height="+$('#height').val()+"");
		$("#BoxProfileBMIGenerate").show();
		resizeWindow();
	} else if ($("#BoxProfileBMIGenerate").children()) {
		$("#BoxProfileBMIGenerate").hide();
		resizeWindow();
	}
}

function loadKcal () {
	if (($('input[name=activity]:checked').val()) && ($('input[name=plan]:checked').val()) && ($('input[name=sex]:checked').val()) && ($('#weight').val().length > '0') && ($('#height').val().length > '0') && ($('#birth').val().length == '10'))
	{
		$("#BoxProfileKcal").load("/generateRequest/?plan="+$('input[name=plan]:checked').val()+"&activity="+$('input[name=activity]:checked').val()+"&sex="+$('input[name=sex]:checked').val()+"&birth="+$('#birth').val()+"&weight="+$('#weight').val()+"&height="+$('#height').val()+"");
		$("#BoxProfileKcal").show();
		resizeWindow();
	} else if ($("#BoxProfileKcal").children()) {
		$("#BoxProfileKcal").hide();
		resizeWindow();
	}
}



var tpl_path = 'http://www.fitea.pl/tpl/';

var diets1Timeout;

function resizeWindow() {
	var windowHeight = $(window).height();
	var bodyHeight = $("#page").outerHeight();
	if (windowHeight > bodyHeight) {
		resizeHeight = windowHeight-110;
	} else {
		resizeHeight = bodyHeight-50;
	}
	$("div.BoxBottom").css('top', resizeHeight+'px');
}

function add_portion(dietId, mealId, dishId, portionId) {
	$("#BoxMyPortion").load("/portion/?dietId="+dietId+"&mealId="+mealId+"&dishId="+dishId+"&portionId="+portionId+"");
}

function del_all_portion(delDietId) {
	$("#BoxMyPortion").load("/portion/?delDietId="+delDietId+"");
}

function del_portion(delDietId, delPortionId) {
	$("#BoxMyPortion").load("/portion/?delDietId="+delDietId+"&delPortionId="+delPortionId+"");
}

Cufon.replace('.GFont');

$(document).ready(function() {

	$('body').fadeIn(0);

	if ($("#BoxMyPortion").length)
	{
		$("#BoxMyPortion").load("/portion/");
	} else {
		resizeWindow();
	}

	if ($("#MainFlash").length) {
		$("#page").css("padding-bottom", "0");
		var so1 = new SWFObject("http://www.fitea.pl/tpl/swf/main.swf", "MainFlash1", "100%", "100%", "8", "#ffffff");
			so1.addParam("scale","noscale");
			so1.addVariable("xmlFile","http://www.fitea.pl/cache/products.xml");
			so1.addVariable("logged","0");
			so1.addVariable("userName","");
			so1.addVariable("logout","http://www.fitea.pl/wyloguj-sie/");
			so1.addVariable("edit","http://www.fitea.pl/zmien-dane/");
						/*SWFAddress.addId("MainFlash");*/
			so1.write("MainFlash");
	}
	if ($("#BoxMenuFlash").length) {
		var so2 = new SWFObject("http://www.fitea.pl/tpl/swf/menu.swf", "BoxMenuFlash1", "100%", "100%", "8", "#ffffff");
			so2.addParam("scale","noscale");
			so2.addVariable("button0","http://www.fitea.pl/main/");
			so2.addVariable("name0","HOME");
			so2.addVariable("button1","http://www.fitea.pl/poznaj-fitea/");
			so2.addVariable("name1","POZNAJ FITEA");
			so2.addVariable("button2","/zapytaj-eksperta/");
			so2.addVariable("name2","ZAPYTAJ EKSPERTA");
			so2.addVariable("button3","/artykuly/");
			so2.addVariable("name3","ARTYKUŁY");
			so2.addVariable("button4","/twoja-dieta/");
			so2.addVariable("name4","TWOJA DIETA");
			so2.addVariable("button5","/konkurs/");
			so2.addVariable("name5","KONKURS");
			/*
			so2.addVariable("button4","/porozmawiajmy-o-dietach/");
			so2.addVariable("name4","POROZMAWIAJMY O DIETACH");
			*/
			so2.addVariable("nr","0");
			/*SWFAddress.addId("BoxMenuFlash");*/
			so2.write("BoxMenuFlash");
	}
	$('input[type=radio]').checkBox();
	$('input[type=checkbox]').checkBox();
	$('#markItUp').markItUp(mySettings);
	//$('#BoxMenuFlash').flash({
	//	src: 'http://www.fitea.pl/tpl/swf/menu.swf',
	//	flashvars: { button0: 'http://www.fitea.pl/', name0: 'HOME', button1: 'http://www.fitea.pl/poznaj-fitea/', name1: 'POZNAJ FITEA', button2: '/zapytaj-eksperta/', name2: 'ZAPYTAJ EKSPERTA', button3: '/artykuly/', name3: 'ARTYKUŁY', button4: '/porozmawiajmy-o-dietach/', name4: 'POROZMAWIAJMY O DIETACH', nr: '0' }
	//});
	$('.ButtonNewThread').flash({
		flashvars: { btnText: '   nowy temat   ', url: 'javascript:add_show();' }
	});
	$('.ButtonSearch').flash({
		src: 'http://www.fitea.pl/tpl/swf/button22.swf',
		flashvars: { btnText: 'szukaj', url: 'javascript:send_form();' }
	});
	$('.ButtonOk').flash({
		flashvars: { btnText: 'ok', url: 'javascript:send_form();' }
	});
	//$('.BoxPrev').flash({
	//	flashvars: { btnText: 'poprzednia' }
	//});
	//$('.BoxNext').flash({
	//	flashvars: { btnText: 'następna' }
	//});
	//$('.ButtonBack').flash({
	//	flashvars: { btnText: 'wstecz', url: 'javascript:history.back()' }
	//});
	$('.ButtonPrint').flash({
		flashvars: { btnText: 'drukuj', url: 'javascript:drukuj();' }
	});
	$('.BoxSubmit').flash({
		flashvars: { btnText: 'wyślij', url: 'javascript:send_form();' }
	});
	$('.InputSend').flash({
		flashvars: { btnText: 'zarejestruj się', url: 'javascript:send_form();' }
	});
	$('.InputChange').flash({
		flashvars: { btnText: 'zmień dane', url: 'javascript:send_form();' }
	});
	$('.ToThread').flash({
		src: 'http://www.fitea.pl/tpl/swf/button22.swf',
		flashvars: { btnText: 'lista wątków', url: 'http://www.fitea.pl/porozmawiajmy-o-dietach/' }
	});
	$('.ButtonReply').flash({
		flashvars: { btnText: 'dodaj poradę', url: 'javascript:add_show();' }
	});
	$('.BoxSave').flash({
		flashvars: { btnText: 'wyślij', url: 'javascript:send_form1();' }
	});
	$('.ButtonDietsDetailsSave').flash({
		flashvars: { btnText: 'zapisz' }
	});
	$('.ButtonDietsDetailsPrint').flash({
		flashvars: { btnText: 'drukuj', url: 'javascript:drukuj();' }
	});
	$('body.orange .BoxBottom').flash({
		src: 'http://www.fitea.pl/tpl/swf/footer.swf',
		flashvars: { trawa: '0' }
	});
	if ($('.BoxPageForum').length) {
		$('body.blue .BoxBottom').flash({
			src: 'http://www.fitea.pl/tpl/swf/footer-konkurs.swf',
			flashvars: { trawa: '1' }
		});
	} else {
		$('body.blue .BoxBottom').flash({
			src: 'http://www.fitea.pl/tpl/swf/footer.swf',
			flashvars: { trawa: '1' }
		});
	}
	//$('.MainFlash').flash({
	//	src: 'http://www.fitea.pl/tpl/swf/main.swf',
	//	flashvars: { xmlFile: 'http://www.fitea.pl/cache/products.xml', logged: '0', userName: '', logout: 'http://www.fitea.pl/wyloguj-sie/' }
	//});
	$('.BoxPageForumLeft:not(#DivBoxPrzepisy)').flash({
		src: 'http://www.fitea.pl/tpl/swf/porozmawiajmy.swf'
	});
	$('#DivBoxPrzepisy').flash({
		src: 'http://www.fitea.pl/tpl/swf/przepisy.swf'
	});
	$('#BoxPageFormFlash').flash({
		src: 'http://www.fitea.pl/tpl/swf/zapytaj-eksperta.swf'
	});
	$('#BoxFlashAdvice').flash({
		src: 'http://www.fitea.pl/tpl/swf/artykuly.swf'
	});
	$('.BoxLoginImg').flash({
		src: 'http://www.fitea.pl/tpl/swf/logowanie.swf'
	});




	$('.BoxMealName').click(function() {
		if ($(this).hasClass('active'))
		{
			$(this).next().hide();
			$(this).removeClass("active");
		} else {
			$(this).next().show();
			$(this).addClass("active");
		}
		resizeWindow();
		return false;
	});

	$('.BoxMealProducts1.FirstProductList li a').click(
		function () {
			//clearTimeout(document.diets1Timeout);
			var id = $(this).attr("id");
			var rel = $(this).attr("rel");

			//$('.SecondProductList').hide();
			//$('.SecondProductList li').hide();
			$('.FirstProductList a').removeClass('active');
			$('.SecondProductList a').removeClass('active');
			$(this).addClass('active');
			$('.SecondProductList').hide();
			$('.SecondProductList li').hide();

			$('.ThirdProductList').hide();
			$('.ThirdProductList li').hide();

			$('div[rel^="Div'+rel+'"]').show();
			$('.SecondProductList li[rel^="'+id+'_li"]').show();
			resizeWindow();
			return false;
		}
		/*, 
		function () {
			var id = $(this).attr("id");
			var rel = $(this).attr("rel");
			
			//$('.SecondProductList li').hide();
			//alert(id+'---'+rel);
			//$('div[rel^="Div'+rel+'"]').show();
			//$('.SecondProductList li[rel^="'+id+'_li"]').show();

			//document.diets1Timeout = setTimeout(function(){
			//	$('.SecondProductList li').hide();
			//	$('.SecondProductList').hide();
			//}, 5000);
		}*/
	);
	$('.BoxShowAllProfile *').click(
		function () {
			if (($('#fullProfile').next().hasClass('ui-checkbox-state-checked')) || ($('#fullProfile').next().hasClass('ui-checkbox-state-checked-hover'))) {
				$('.BoxAllProfile').show();
				resizeWindow();
			} else {
				$('.BoxAllProfile').hide();
				resizeWindow();
			}
		}
	);

	$('.BoxMealProducts1.SecondProductList li a').click(
		function () {
			//clearTimeout(document.diets1Timeout);
			var id = $(this).attr("id");
			var rel = $(this).attr("rel");

			//$('.SecondProductList').hide();
			//$('.SecondProductList li').hide();
			//$('.SecondProductList').hide();
			$('.SecondProductList a').removeClass('active');
			$(this).addClass('active');
			$('.ThirdProductList').hide();
			$('.ThirdProductList li').hide();

			$('div[rel^="Div'+rel+'"]').show();
			$('.ThirdProductList li[rel^="'+id+'_li"]').show();
			resizeWindow();
			return false;
		}
	);
	
	$('.BoxMealProducts1.SecondProductList li a span').click(
		function () {
			var rel = $(this).attr("rel");
			location.href = "http://www.fitea.pl/twoja-dieta/przepisy/"+rel+"";
			return false;
		}
	);
	if ($(".SweetTable").length) {
		$('.SweetTable tr td:first-child').addClass('TdFirst');
		$('.SweetTable tr td:last-child').addClass('TdLast');
		$('.SweetTable tr').append($('<td/>').addClass('TdRight')).prepend($('<td/>').addClass('TdLeft'));
	}
	setTimeout(resizeWindow, 2000);

	
	$(window).unload( function () {
		$('body').css('visibility', 'hidden');
		$('.flash-replaced').each(function() {
			$(this).css('visibility', 'hidden');
		});
	});

	
});
