/*validators*/

function validatorRequire(field, msg, title)
{

    if ($(field).val() == '') {
        showDialog(title, msg, field);
        return false;
    }

    return true;
}

function validatorEmail(field, msg, title)
{
    var regex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

    if ( regex.test($(field).val()) == false ) {
        showDialog(title, msg, field);
        return false;
    }

    return true;
}

function validatorCaptcha(captchaWord, captchaId, msg, title)
{
    var ret;
    $.ajax({
        type: "POST",
        url:"/system/validate-captcha/",
        async: false,
        data: {
            captcha_word: $(captchaWord).val(),
            captcha_id: $(captchaId).val()
        },
        dataType: "text",
        success: function(data){
            ret = data;
    }
    });

    if (ret != 'true') {
        showDialog(title, msg, captchaWord);
        return false;
    }

    return true;
}

function validatorNumeric(field, msg, title)
{
    var regex = /^[0-9]*$/i;
    if ( regex.test($(field).val()) == false ) {
        showDialog(title, msg, field);
        return false;
    }

    return true;
}



/*custom func*/


function getRandomImg(gallery, timeout, images)
{
    var init = false;

    if (images == null) {
        $.ajax({
            type: "POST",
            url:"/system/random-img/",
            async: false,
            data: {
                gallery: gallery
            },
            dataType: "json",
            success: function(data){
                images = data;
                init = true;
            }
        });
    }

    var id = Math.floor(Math.random()*images.length);

    if (init) {
        var link='';

        if (images.length > 1) {
            for (i=1; i<images.length; i++) {
                link += '<a href="'+images[i].file+'" rel="latest-gal" title="'+images[i].desc+'"></a>';
            }
        }

        $("#latest-img").fadeOut(300, function(){$("#latest-img").html('<div id="latest-image-azc"><a href="'+images[0].file+'" title="'+images[0].desc+'" rel="latest-gal"><img src="'+images[id].th_file+'" alt="latest image"/></a></div>' + (link != '' ? link : ''));});
        $("a[rel='latest-gal']").colorbox({maxWidth:800, maxHeight:600, previous: "Předchozí", next: "Další", close: "Zavřít", current: "Obrázek {current} / {total}"});
        $("#latest-img").fadeIn(300);

    } else {
        $("#latest-img").fadeOut(300, function(){$("#latest-img #latest-image-azc a").html('<img src="'+images[id].th_file+'" alt="latest image" />');});
        $("#latest-img").fadeIn(300);
    }

        setTimeout(function(){getRandomImg(gallery, timeout, images)},timeout);
}


$(document).ready(function(){
    $("a[rel^='colorbox']").colorbox({
        maxWidth:800,
        maxHeight:600,
        previous: "Předchozí",
        next: "Další",
        close: "Zavřít",
        current: "Obrázek {current} / {total}"
    });
    $(".view-catalog").colorbox({iframe: true, innerWidth:'600px', innerHeight:'590px'});
});


function reloadCaptcha() {

    $.ajax({
        type: "POST",
        url: "/system/reload-captcha/",
        async: false,
        data: {
            captcha_text: "c_text"
        },
        dataType: "text",
        success: function(data){
            $("#captcha-img").attr("src", '/var/captcha/'+data+'.png');
            $("#captcha_word").val('');
            $("#captcha_id").val(data);

        }
    });

    $("#captcha_word").val('');
};

function validateForm()
{

    if (!validatorRequire('#name', $.i18n('Please enter name'), $.i18n('Error'))) return false;
    if (!validatorRequire('#email', $.i18n('Please enter email'), $.i18n('Error'))) return false;
    if (!validatorEmail('#email', $.i18n('Please enter valid email'), $.i18n('Error'))) return false;
    if (!validatorRequire('#msg', $.i18n('Please enter message'), $.i18n('Error'))) return false;
    if (!validatorCaptcha('#captcha_word', '#captcha_id', $.i18n('Please enter valid code'), $.i18n('Error'))) return false;

    return true;
}

function showDetail(id) {
    $("#but-"+id).hide();
    $("#t-detail-"+id).show();
    $("#but-h-"+id).show();
}

function hideDetail(id) {
    $("#but-"+id).show();
    $("#t-detail-"+id).hide();
    $("#but-h-"+id).hide();
}

