/*
 * 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").easySlider({
		auto: true,
		continuous: true,
		numeric: true,
		pause: 10000,
		speed: 800
	});

 	Cufon.replace('h1');
 	Cufon.replace('#kontakt-box > h2');
 	Cufon.replace('#sidebar h2');
 	Cufon.replace('#logos h2');
 	Cufon.replace('#buttons span');
 	Cufon.replace('#top-menu > li > a');
/*	$('#top-menu > li:not([class~=active])').mouseover(function(ev) {
		Cufon.replace($(this).children('a'), { 'color': '#ffffff'});
	});
	$('#top-menu > li:not([class~=active])').mouseout(function(ev) {
		Cufon.replace($(this).children('a'), { 'color': '#827045'});
	});
	*/
	$('#product-photos 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'
	});

	
	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();
	});
	
	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'));
	
	var h = $('#sidebar').height();
	if ($('#main').height() < h) {
		$('#main').height(h);
	}
	
	
	$('#buttons a, #product-photos a').css('background', 'white');	
	$('#buttons a, #product-photos a').hover(
		function(){
			$(this).find('img[class!=zoom]').animate({opacity: ".6"}, 300);		
		}, 
		function(){
			$(this).find('img[class!=zoom]').animate({opacity: "1.0"}, 300);					
		}		
	);
	
});

