/* $Id: agrobard.js 23 2009-08-05 12:29:45Z choman $ */

scrollerIntervalMs = 50;
scrollerStep = 1;

$(document).ready(function() {

	function start_slideshow(id, start_frame, end_frame, delay, speed) {
		setTimeout(switch_slides(id, start_frame,start_frame,end_frame, delay, speed), delay);
	}
	                        
	function switch_slides(id, frame, start_frame, end_frame, delay, speed) {
		return (function() {
			setTimeout("$('#" + id + frame + "').fadeOut(" + speed + ")");
			if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
			setTimeout("$('#" + id + frame + "').fadeIn(" + speed + ")");
			setTimeout(switch_slides(id, frame, start_frame, end_frame, delay, speed), delay + speed);
		})
	}
	
	function equalColumns() {
		var max = 0;
		$('.main').css('height', 'auto');
		$('.sidebar,.main').each(function(i) {
			var h = $(this).height();
			if (h > max) {
				max = h;
			}
		});
		$('.sidebar,.main').each(function(i) {
			if ($(this).height() != max) {
				$(this).height(max);
			}
		});
	}
	
	start_slideshow('slide', 1, 3, 5000, 2000);

	if ($('textarea#news_summary').length == 1) {
		var oFCKeditor = new FCKeditor('news_summary');
		oFCKeditor.BasePath = NFW.static_root + "/fckeditor/";
		oFCKeditor.Height = "150";
		oFCKeditor.ToolbarSet = "Basic";
		oFCKeditor.ReplaceTextarea();
	}

	if ($('textarea#news_text').length == 1) {
		var oFCKeditor = new FCKeditor('news_text');
		oFCKeditor.BasePath = NFW.static_root + "/fckeditor/";
		oFCKeditor.Height = "200";
		oFCKeditor.ToolbarSet = "Basic";
		oFCKeditor.ReplaceTextarea();
	}

	if ($('textarea#used_description').length == 1) {
		var oFCKeditor = new FCKeditor('used_description');
		oFCKeditor.BasePath = NFW.static_root + "/fckeditor/";
		oFCKeditor.Height = "200";
		oFCKeditor.ToolbarSet = "Basic";
		oFCKeditor.ReplaceTextarea();
	}

	$('a.remove').click(function(ev) {
		if (!confirm('Usunąć wpis?')) {
			ev.preventDefault();
		}
	});
	
	$('.photos a').lightBox({
		imageLoading: NFW.static_root + '/gfx/lightbox-ico-loading.gif', // (string) Path and the name of the loading icon
		imageBtnPrev: NFW.static_root + '/gfx/lightbox-btn-prev.gif', // (string) Path and the name of the prev button image
		imageBtnNext: NFW.static_root + '/gfx/lightbox-btn-next.gif', // (string) Path and the name of the next button image
		imageBtnClose: NFW.static_root + '/gfx/lightbox-btn-close.gif', // (string) Path and the name of the close btn
		imageBlank: NFW.static_root + '/gfx/lightbox-blank.gif'
	});
	
	$('.gallery a').lightBox({
		imageLoading: NFW.static_root + '/gfx/lightbox-ico-loading.gif', // (string) Path and the name of the loading icon
		imageBtnPrev: NFW.static_root + '/gfx/lightbox-btn-prev.gif', // (string) Path and the name of the prev button image
		imageBtnNext: NFW.static_root + '/gfx/lightbox-btn-next.gif', // (string) Path and the name of the next button image
		imageBtnClose: NFW.static_root + '/gfx/lightbox-btn-close.gif', // (string) Path and the name of the close btn
		imageBlank: NFW.static_root + '/gfx/lightbox-blank.gif'
	});
	
	
	$('#popup').lightBox({
		imageLoading: NFW.static_root + '/gfx/lightbox-ico-loading.gif', // (string) Path and the name of the loading icon
		imageBtnPrev: NFW.static_root + '/gfx/lightbox-btn-prev.gif', // (string) Path and the name of the prev button image
		imageBtnNext: NFW.static_root + '/gfx/lightbox-btn-next.gif', // (string) Path and the name of the next button image
		imageBtnClose: NFW.static_root + '/gfx/lightbox-btn-close.gif', // (string) Path and the name of the close btn
		imageBlank: NFW.static_root + '/gfx/lightbox-blank.gif'
	}).click();


	$('#contact-form').submit(function(ev) {
		function setError(f, msg) {
			$('#req-' + f).text(msg);
			$('#req-' + f).closest('li').addClass('error');
		}
		
		function clearError(f) {
			$('#req-' + f).text('*');
			$('#req-' + f).closest('li').removeClass('error');
		}
		
		function validateField(f, min_l, e_msg, s_msg) {
			var s = $('#' + f)[0].value;
			if (s == '') {
				setError(f, e_msg);
				return false;
			}
			if (s.length < min_l) {
				setError(f, s_msg);
				return false;
			}
			clearError(f);
			return true;
		}
		
		var b = validateField('name', 7, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
		b &= validateField('phone', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
		b &= validateField('msg', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
		b &= validateField('result', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
		
		if (b) {
			return;
		}
		
		ev.preventDefault();
		
	});

	equalColumns();
	
	$('.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);
	});


});

