/* $Id: mat-bud.js 4 2010-06-19 19:03:41Z choman $ */

$(document).ready(function() {
	
	function initLinks(elems) {
		$(elems).attr('origsrc', function(attr) {
			return $(this).attr('src');
		});
		$(elems).hover(function() {
			$(this).attr('src', $(this).attr('origsrc').replace(/\.png/, '-ho.png'));
		},
		function() {
			$(this).attr('src', $(this).attr('origsrc'));
		});
    }

    initLinks($('#top .ncms-menu li[class!=active] img'));

	Cufon.replace('h1');
	
	$('#logos ul').innerfade({ 'speed': 'slow', 'timeout': 5000 });
	
	$('.ncms-gallery-items a').lightBox({
		imageLoading: _root + '/' + _template_root + '/static/gfx/lightbox-ico-loading.gif',
		imageBtnPrev: _root + '/' + _template_root + '/static/gfx/lightbox-btn-prev.gif',
		imageBtnNext: _root + '/' + _template_root + '/static/gfx/lightbox-btn-next.gif',
		imageBtnClose: _root + '/' + _template_root + '/static/gfx/lightbox-btn-close.gif',
		imageBlank: _root + '/' + _template_root + '/static/gfx/lightbox-blank.gif'
	});

	if (!_lang_req) {
		var _lang_req = 'To pole jest wymagane.';
	}
	
	function setError(f, msg) {
		$('#' + f).closest('li').append('<strong>' + msg + '</strong>');
		$('#' + f).closest('li').addClass('error');
	}
	
	function clearError(f) {
		$('#' + f).closest('li').find('strong').remove();
		$('#' + f).closest('li').removeClass('error');
	}
	
	function validateField(f, min_l, e_msg, s_msg) {
		var s = $('#' + f)[0].value;
		clearError(f);
		if (s == '') {
			setError(f, e_msg);
			return false;
		}
		if (s.length < min_l) {
			setError(f, s_msg);
			return false;
		}
		return true;
	}
	
	function validateSelect(f, e_msg) {
		var s = $('select[name=\'' + f + '\']')[0].value;
		clearError(f);
		if (parseInt(s) == 0) {
			setError(f, e_msg);
			return false;
		}
		return true;
	}
	
	$('.ncms-form').submit(function(ev) {
		
		var b = true;
		
		$('form li.required input, form li.required textarea, form li.required select').each(function() {
			b &= validateField(this.id, 0, _lang_req, '');
		});
		
		if (b) {
			return;
		}
	
		ev.preventDefault();
	});

	
	function equalColumns() {
		var max = 0;
		$('#main').css('height', 'auto');
		$('.index .ncms-static-page').css('height', 'auto');
		$('#sidebar,#main,.index .ncms-static-page').each(function(i) {
			var h = $(this).height();
			if (h > max) {
				max = h;
			}
		});
		$('#sidebar,#main,.index .ncms-static-page').each(function(i) {
			$(this).height(max);
		});
	}
	
	equalColumns();

});

