var reSender = /[\w\d]+/i; 
var reEmail = /^[a-z0-9\.\-\_]{2,}\@[a-z0-9\.\-\_]+\.[a-z]{2,4}$/i; 
var reLocation = /.+/i; 
var reSubject = /[\w\d]+/i; 
var reContent = /[\w\d]+/i; 

function contactFormValid()
{
    var sender = document.getElementById('cSender').value;
    var email = document.getElementById('cEmail').value;
    var subject = document.getElementById('cSubject').value;
    var location = document.getElementById('cLocation').value;
    var content = document.getElementById('cContent').value;
    
    invalidFields = new Array();
    if(sender.replace(/^[\s]+/ig, '').replace(/[\s]+$/ig, '').length != 0 && !sender.match(reSender)) invalidFields.push('nadawca');
    if(email.length < 6 || !email.match(reEmail)) invalidFields.push('e-mail');
    if(location.replace(/^[\s]+/ig, '').replace(/[\s]+$/ig, '').length == 0 || !location.match(reLocation)) invalidFields.push('miejscowość uroczystości');
    if(subject.replace(/^[\s]+/ig, '').replace(/[\s]+$/ig, '').length != 0 && !subject.match(reSubject)) invalidFields.push('temat');
    if(content.length < 6 || !content.match(reContent)) invalidFields.push('treść');
    
    if(invalidFields.length > 0)
    {
        alert('Wypełnij poprawnie ' + ((invalidFields.length == 1)?'pole ':'pola: ') + invalidFields.join(', ') + '.');
        return true;
    }
    else
        return false;
}

$(document).ready(function() {
    introSlider();
    galleryEngine();
});


var intro = {
    images: [],
    interval: 5000,
    index: 0,
    container: -1,
    containers: {},

    init: function() {
        this.containers[-1].attr('src', this.images[0]);
        this.containers[1].attr('src', this.images[1]);
        this.showNext();
    },

    getNext: function(){
        if(this.index < this.images.length) {
            this.index++;
        } else {
            this.index = 0;
        }
        this.container = this.container * (-1);

        return this.images[this.index];
    },

    showNext: function() {
        this.containers[this.container].fadeIn('slow');
        this.containers[this.container * (-1)].fadeOut('slow', function(){
            var path = intro.getNext();
            intro.containers[intro.container].attr('src', path);
        });
    }
};

introSlider = function() {
    intro.containers[-1] = $('#intro-1 img');
    intro.containers[1] = $('#intro-2 img');

    intro.init();
    setInterval(function(){
        intro.showNext();
    }, intro.interval);
}

var gallery = {
    thumbSize: 140,
    showIndex: 0,
    portion: 32,

    init: function() {
        this.show();
    },

    show: function(){
        $('#galleryList li').slice(this.showIndex, this.showIndex + this.portion).each(function(){
            var a = $('a', this);
            if(a.length > 0) {
                a.append($('<img src="' + (a.attr('href').replace('/photos/', '/thumbs/')) + '" alt="" width="' + gallery.thumbSize + '" height="' + gallery.thumbSize + '" />'));
                $(this).fadeIn(800, function(){ $(this).removeAttr('class'); });
                gallery.showIndex++;
            }
        });
    }
};

galleryEngine = function() {
    var galleryLoad = $('#galleryLoad');
    if(galleryLoad) {
        $(window).scroll(function(){
            var winOff = $(this).height() + $(this).scrollTop() + 100;
            var loadOff = galleryLoad.offset().top;
            if(winOff >= loadOff) {
                gallery.show();
            }
        });

        gallery.init();
        $('#galleryTh a').colorbox({
            loop: false,
            current: '{current}/{total}',
            next: 'następne',
            previous: 'poprzednie',
            close: 'zamknij',
        });
    }
}

