/**
 * Validate Email Function
 * taken from http://www.zparacha.com/validate-email-address-using-javascript-regular-expression/ 
 */ 
function isValidEmail(elementValue){      
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue); 
}

/**
 * ---------------------------------------------------------------
 *  MessageBox Functions
 * ---------------------------------------------------------------
 */
function showNotice(message, showAlert) {
    if(showAlert) alert(message);
    $('#message-box').append('<p class="notice">'+message+'</p>');
}
function showError(message, showAlert) {
    if(showAlert) alert(message);
    $('#message-box').append('<p class="error">'+message+'</p>');
}
function cleanMessageBox() {
    $('#message-box').empty();
}

/**
 * ---------------------------------------------------------------
 *  On DOM Loaded >>> 
 * ---------------------------------------------------------------
 */
$(function(){

    /**
     * Share on Facebook button
     */
    var shareFacebook = $('.share-facebook');
    shareFacebook.attr('href','http://www.facebook.com/share.php?u='+window.location.href);

    /**
     * ---------------------------------------------------------------
     *  Form validation - Comments
     * ---------------------------------------------------------------
     */
    $('#form-comment').submit(function(){
        $(this).find('.highlight').removeClass('highlight');
        cleanMessageBox();
        send = true;

        var email = $.trim($(this).find('#email').val());
        if(email.length>0) {
            if(isValidEmail(email)==false) {
                showError("Nesprávný formát e-mailu.");
                $(this).find('#email').addClass('highlight');
                send = false;
            }
        }

        var name = $.trim($(this).find('#name').val())
        if(name.length==0) {
            showError("Vyplňte vaše jméno.");
            $(this).find('#name').addClass('highlight');
            send = false;
        }

        var txt = $.trim($(this).find('#message').val())
        if(txt.length==0) {
            showError("Vyplňte text komentáře.");
            $(this).find('#message').addClass('highlight');
            send = false;
        }

        if(send == false)
            alert("Formulář obsahuje chyby. Prosím, opravte je.");

        return send;
    });

    
    
    /**
     * ---------------------------------------------------------------
     *  Form validation - Competition
     * ---------------------------------------------------------------
     */
    $('#form-competition').submit(function(){
        $(this).find('.highlight').removeClass('highlight');
        cleanMessageBox();
        send = true;


        // name
        var name = $.trim($(this).find('#name').val())
        if(name.length==0) {
            showError("Vyplňte vaše jméno.");
            $(this).find('#name').addClass('highlight');
            send = false;
        }

        // email
        var email = $.trim($(this).find('#email').val());
        if(email.length>0) {
            if(isValidEmail(email)==false) {
                showError("Nesprávný formát e-mailu.");
                $(this).find('#email').addClass('highlight');
                send = false;
            }
        } else {
            showError("Vyplňte váš e-mail.");
            $(this).find('#email').addClass('highlight');
        }

        
        var files = $(this).find('.files input');
        var isOneFileSet = false;
        $.each(files, function(){
            if($(this).val().length > 0) {
                isOneFileSet = true;
            }
        });
        if(isOneFileSet == false) {
            showError("Nahrajte alespoň jednu ilustraci");
            send = false;
        }

        if(send == false)
            alert("Formulář obsahuje chyby. Prosím, opravte je.");

        return send;
    });

    // Hide favcharacter input field (default)
    $('.fav-character-other').hide();
    // Show favcharacter input field
    $('#fav-character').change(function(){
        var choice = $(this).find('option:selected').val();
        if(choice=='other') {
            $('.fav-character-other').show();
        } else {
            $('#fav-character-other').val('');
            $('.fav-character-other').hide();
        }
    });



    /**
     * ---------------------------------------------------------------
     *  Lightbox settings
     * ---------------------------------------------------------------
     */
    $(".lightbox a:has(img)").lightBox({
        overlayOpacity: 0.9,
        containerResizeSpeed: 150,
        imageLoading: 'clientside/images/lightbox-ico-loading.gif',
        imageBtnClose: 'clientside/images/lightbox-btn-close.gif',
        imageBtnPrev: 'clientside/images/lightbox-btn-prev.gif',
        imageBtnNext: 'clientside/images/lightbox-btn-next.gif',
        imageBlank: 'clientside/images/lightbox-blank.gif',
   });

});