/***********************************************************************
  EVENTS
***********************************************************************/

jQuery(document).ready(function()
{
    jQuery('div.block-slideshow').each(function()
    {
        jQuery(this).slideshow();
    });
    jQuery('form.entry-form').submit(function()
    {
        return checkForm(this);
    });
    if (ISCPLAYER != null && ISCCONTROLS != null && flowplayer != null) {
        jQuery('a.flowplayer').each(function()
        {
            var config = {
                clip:
                {
                    autoPlay : false,
                    autoBuffering : true
                },
                plugins:
                {
                    controls:
                    {
                        url:ISCCONTROLS,
                        autoHide : 'always',
                        all : false,
                        play : true,
                        time : true,
                        scrubber : true,
                        volume : true,
                        mute : true
                    }
                }
            };
            if (jQuery(this).hasClass('flowplayer-autoplay')) {
                config.clip.autoPlay = true;
            }
            if (jQuery(this).hasClass('flowplayer-loop')) {
                config.clip.onBeforeFinish = function() {return false;};
            }
            flowplayer(this, ISCPLAYER, config);
        });
    }
});

/***********************************************************************
  SLIDESHOW
***********************************************************************/

jQuery.fn.slideshow = function()
{
    var e = this;
    var current = 0;
    var timeout = 8000;
    var fadein = 2000;
    var fadeout = 1000;

    var init = function()
    {
        if (jQuery(e).children('div').length < 1) {
            return;
        }
        jQuery(e).children('div').hide();
        jQuery(e).children('div:eq('+current+')').fadeIn(fadein);
        setTimeout(slide, timeout);
    }

    var next = function()
    {
        current = (current + 1) % jQuery(e).children('div').length;
    }

    var slide = function()
    {
        jQuery(e).children('div:eq('+current+')').hide((fadeout));
        next();
        jQuery(e).children('div:eq('+current+')').fadeIn(fadein);
        setTimeout(slide, timeout);
    }

    init();
}

/***********************************************************************
    Form Validation
***********************************************************************/

function checkForm(e)
{
    var error = 0;
    var fileerror = 0;
    var grouperror = 0;
    var message = '';

    if (jQuery(e) == null) {
        return false;
    }
    jQuery('.required', e).each(function()
    {
        if ((!jQuery(this).hasClass('checkbox') && jQuery(this).val() && jQuery(this).val() != 0) || (jQuery(this).hasClass('checkbox') && jQuery(this).attr('checked'))) {
            if (jQuery(this).hasClass('file') && !checkFile(jQuery(this).val())) {
                $(this).addClass('missing');
                error++;
                fileerror++;
            } else {
                jQuery(this).removeClass('missing');
            }
        } else {
            jQuery(this).addClass('missing');
            error++;
        }
    });
    if (jQuery('.validate-group:checked', e).length > 0) {
        var total = 0;
        jQuery('.validate-group:checked', e).each(function()
        {
            total += parseFloat($(this).val());
        });
        if (total == jQuery('.validate-group:checked', e).length) {
            error++;
            grouperror++;
        }
    }
    if (error > 0) {
        if (error > fileerror) {
            message += '- Mandatory field(s) missing.\n';
        }
        if (fileerror > 0) {
            message += '- Document type is not allowed, please use a pdf or jpg.\n';
        }
        if (grouperror > 0) {
            message += '- Which day(s) will you attend?\n';
        }
        alert(message);
        return false;
    }
    return true;
}

function checkFile(str)
{
    if (str.length < 5) return false;
    var exts = 'pdf|jpg|jpeg|jpe';

    var re = new RegExp("^.+\.(" + exts+ ")$", "i");
    return (re.test(str)) ? true : false;
}

