var height;
var opt_chk = {
	success			: showResponse_chk,
	beforeSubmit	: loading
};
var options_chk = {
	rules : {
		domain			: {
			required 	: true,
			remote		: '/index/check-domain-ajax'
		},
		captcha_input	: "required"
	},
	errorPlacement: function(error, element) {
		error.prependTo( element.parent().next() );
	},
	messages : {
		domain		: {
			remote	: 'Domena może zawierać tylko znaki a-z, cyfry i znak \'_\'<br>'
		}
	},
	submitHandler: function(form) {
	   	$(form).ajaxSubmit(opt_chk);
   }
};

var val_login = {
	rules : {
		login	: 'required',
		pass	: 'required'
	},
	submitHandler: function(form) {
		form.submit();
	}
};

var animation = 1;

$(document).ready(function(){
	$("a.show_pricelist").click(showPricelist);
	$("a.show_templates").click(showTemplates);
	$("a.check_domain").click(resizeIt);
	$("#check_domain").validate(options_chk);
	$("div.check_domain").hide();
	$("img.loader").hide();
	$("div.firmy a").click(showCompanies);
	$("div.instytucje a").click(showInstitutions);
	$("a.more-companies").live('click', showMore);
	var pic1 = new Image(110,32); 
	var pic2 = new Image(93,32); 
	pic1.src="/public/images/tlo_katalog_instytucji.png"; 
	pic2.src="/public/images/tlo_katalog_firm_n.png"; 
	height = $("ul.short").css('height');
	$("#login_form").validate(val_login);
	getWeather();
	$("div.make_flash").each(makeFlash);
	$("a.show-more").click(showAll);
});

function getWeather() {
	$("#pogoda .box").html('<img src="/public/images/loader32.gif" width="32" height="32" alt="" style="margin:30px 0 0 70px;"/>');
	$.get('/index/get-weather-ajax', function(data){
		if (data != 'false') {
			$("#pogoda .box").html(data);
		}
		else
			$("#pogoda").fadeOut('slow', function(){$(this).remove();});
	});
}

function resizeIt() {
	$("div.check_domain").slideDown();
	$.get('/index/generate-captcha', function(data){
		$("div.captcha_img").html('<img src="/public/images/captcha/' + data + '.png" id="captcha_img"/>');
		$("#captcha_id").val(data);
	});
	return false;
}

function loading() {
	$("#iCheck").hide();
	$("img.loader").show();
}

function showResponse_chk(responseData) {
	$("img.loader").hide();
	$("#iCheck").show();
	$("p.info, p.error2").remove();
	if (responseData == 'true')
		$("#check_domain").append('<p class="info">Domena jest wolna</p><p class="info"><a href="/page/add-form/domain/' + escape($("input[name='domain']").val()) + '">Rejestruj teraz!</a></p>');
	else if (responseData == 'false')
		$("#check_domain").append('<p class="error2">Domena jest już zajęta</p>');
	else if (responseData == 'captcha')
		$("input.captcha").after('<label class="error" for="captcha_input">Błędny wyraz</label>');
	reCaptcha();
}

function reCaptcha() {
	$("#captcha_img").attr('src', 'images/loader16.gif');
	$.get('/index/generate-captcha-ajax', function(data){
		$("input.captcha").val('');			//clear value
		$("#captcha_img").attr('src', 'images/captcha/' + data + '.png');
		$("#captcha_id").val(data);
	});
}
var more = false;
function showInstitutions() {
	more = false;
	$("div.instytucje").css('background-image', 'url(/public/images/tlo_katalog_instytucji.png)');
	$("div.instytucje a").css('color', '#ffffff');
	$("div.firmy").css('background-image', 'url(/public/images/tlo_katalog_firm_n.png)');
	$("div.firmy a").css('color', '#414144');
	var height = $("#companies_content").height();
	var margin = (height - 32) / 2;
	$("#companies_content").html('<img style="margin: ' + margin + 'px 0 ' + margin + 'px 70px;" src="/public/images/loader32blue.gif" width="32" height="32" alt=""/>');
	$.get('/index/get-companies-ajax/type/2', function(data){
		$("#companies_content").html(data);
	});
	return false;
}

function showCompanies() {
	more = false;
	$("div.instytucje").css('background-image', 'url(/public/images/tlo_katalog_instytucji_n.png)');
	$("div.firmy").css('background-image', 'url(/public/images/tlo_katalog_firm.png)');
	$("div.instytucje a").css('color', '#414144');
	$("div.firmy a").css('color', '#ffffff');
	var height = $("#companies_content").height();
	var margin = (height - 32) / 2;
	$("#companies_content").html('<img style="margin: ' + margin + 'px 0 ' + margin + 'px 70px;" src="/public/images/loader32blue.gif" width="32" height="32" alt=""/>');
	$.get('/index/get-companies-ajax/type/1', function(data){
		$("#companies_content").html(data);
	});
	return false;
}

function showMore() {
	var childsCount = $("ul.short").children().size();
	if (!more) {		// zwiększamy
		$("ul.short").animate({
			height: childsCount * 23 + 'px'
		}, 300);
		$(this).html('mniej');
	}
	else {
		$("ul.short").animate({
			height: height
		}, 300);
		$(this).html('więcej');
	}
	more = !more;
	return false;
}

function showPricelist() {
	tb_show('', '/public/images/cennik.jpg');
	return false;
}

function showTemplates() {
	tb_show('', '/index/show-layouts-view/?KeepThis=true&TB_iframe=true&height=600&width=900');
	return false;
}

function makeFlash() {
	var file = $(this).html();
	var width = $(this).width();
	var height = $(this).height();
	$(this).html('');
	$(this).flash({
		swf			: file,
		width		: width,
		height		: height
	});
}

function showAll() {
	if ($(this).next().is(':hidden'))
		$(this).next().show();
	else
		$(this).next().hide();
	return false;
}


