/*
 * 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() {
	
	$('#slideshow ul').shuffle();
	
	$('#slideshow ul').innerfade({ 'speed': 'slow', 'timeout': 5000 });

 	Cufon.replace('#main-menu a');
 	Cufon.replace('h1');
 	Cufon.replace('#logos h2');
 	Cufon.replace('#boxes h2');
	
	$('#main-menu > li:not([class~=active]) > a').mouseover(function(ev) {
		Cufon.replace($(this), { 'color': '#ec1824'});
	});
	$('#main-menu > li:not([class~=active]) > a').mouseout(function(ev) {
		Cufon.replace($(this), { 'color': '#ffffff'});
	});

	$('.ncmsGallery-page .ncmsGallery-items a').lightBox({
		imageLoading: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-ico-loading.gif',
		imageBtnPrev: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-btn-next.gif',
		imageBtnClose: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-btn-close.gif',
		imageBlank: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-blank.gif'
	});

	$('a[rel=lightbox]').lightBox({
		imageLoading: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-ico-loading.gif',
		imageBtnPrev: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-btn-next.gif',
		imageBtnClose: _root + '/' + _template_root + '/static/gfx/lightbox/lightbox-btn-close.gif',
		imageBlank: _root + '/' + _template_root + '/static/gfx/lightbox/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 validateCheckboxField(f, e_msg) {
		var s = $('input:checkbox[name=' + f + '[]]:checked').val();
		if (!s) {
			setError(f, e_msg);
			return false;
		}
		clearError(f);
		return true;
	}
	
	function validateRadioField(f, e_msg) {
		var s = $('input:radio[name=' + f + ']:checked').val();
		if (!s) {
			setError(f, e_msg);
			return false;
		}
		clearError(f);
		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[type=text], form li.required textarea').each(function() {
			b &= validateField(this.id, 0, _lang_req, '');
		});
		$('form li.required select').each(function() {
			b &= validateSelect(this.id, _lang_req);
		});
		$('form li.required ul.check-list').each(function() {
			b &= validateCheckboxField(this.id, _lang_req);
		});
		$('form li.required ul.radio-list').each(function() {
			b &= validateRadioField(this.id, _lang_req);
		});
		
		if (b) {
			return;
		}
	
		ev.preventDefault();
	});
	
	$('.n-scroll').each(function() {

		var speed = 45;
		
		$(this).wrapInner('<div class="content"></div');
		var content = $(this).children('.content');
		content.width(90000);
		var w = $(this).find('.content p').width();
		var h = $(this).find('.content p').height();
		$(this).find('.content').width(w);
		
		var content2 = $(this).children('.content').clone()
		content2.appendTo($(this));
		w += 20;
		content2.css('left', w + 'px');
		content2.css('top', '-' + h + 'px');
		
		$(this).wrapInner('<div class="scroll-window"></div>');
		
		var callback = function() {
			content.css('left', w + 'px');
			content.animate({ 'left': -w }, 2*w*speed, 'linear', callback);
		}
		var callback2 = function() {
			content2.css('left', w + 'px');
			content2.animate({ 'left': -w }, 2*w*speed, 'linear', callback2);
		}
		content.animate({ 'left': -w }, w*speed, 'linear', callback);
		content2.animate({ 'left': -w }, 2*w*speed, 'linear', callback2);
	});

	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($('#logos img'));
	
});

