/* $Id: szpital.js 8 2010-07-06 13:22:51Z choman $ */

/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);

$(document).ready(function() {


	Cufon.replace('h1');
	Cufon.replace('#sidebar h2');

	$('#banner-slideshow').shuffle();

	$('#banner-slideshow').after('<div id="banner-nav">').cycle({ 
	    fx:     'fade', 
	    speed:  2000, 
	    timeout: 10000, 
	    pager:  '#banner-nav',
	    slideExpr: 'li',
	    sync: true,
	    activePagerClass: 'active'
	});

	$('#faq-slideshow ul').cycle({ 
	    fx:     'fade', 
	    speed:  1000, 
	    timeout: 4000, 
	    slideExpr: 'li',
	    sync: true
	});

	function setError(f, msg) {
		$('#' + f).closest('li').children('label').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;
	}

	$('form.ncms-form').submit(function(ev) {
		
		var b = true;
		
		var msg = '';
		if (_lang == 'en') {
			msg = 'Required';
		} else {
			msg = 'To pole jest wymagane';
		}
		
		$('form li.required input, form li.required textarea, form li.required select').each(function() {
			b &= validateField(this.id, 0, msg, '');
		});
		
		if (b) {
			return;
		}

		ev.preventDefault();
	});
	
	function initLinks(elems) {
		$(elems).attr('origsrc', function(attr) {
			return $(this).attr('src');
		});
		$(elems).hover(function() {
			$(this).attr('src', $(this).attr('origsrc').replace(/\.png/, '-h.png'));
		},
		function() {
			$(this).attr('src', $(this).attr('origsrc'));
		});
	}
	
	initLinks($('#top2 .ncms-menu li[class!=active] img'));

	var l_h = $('#sidebar').height();
	var c_h = $('#main').height();
	$('#content').height((l_h > c_h ? l_h : c_h) + 30);
	
	$('.ncms-gallery-items .thumb').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'
	});
	
	$('a.lightbox').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'
	});
	
	$('#popup').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'
	}).click();

	$('#print').click(function(ev) {
		window.print();
		ev.preventDefault();
	});
	

});

