myMenu = {
    toggle : function(obj) {
        try {
            var parent = obj.parentNode;
            if (parent.className.match('opened')) {
                $(parent).children('ul').first().slideUp(250);
                parent.className = parent.className.replace(/opened/, 'closed');
            } else if (parent.className.match('closed')) {
                $(parent).children('ul').first().slideDown(250);
                parent.className = parent.className.replace(/closed/, 'opened');
            }
        } catch (e) {
        }
        return false;
    }
}
myPoll = {
    submit : function(sender) {
        try {
            if ($(sender).find('input[@name=\'option\']:checked').val()) {
                var data = $(sender).serialize();
                $(sender).find('input').attr('disabled', 'disabled');
                if ($(sender).data('pass') > 1) {
                    return false;
                }
                $(sender).data('pass', 2);
                $.post('/poll', data, function(result) {
                    $(sender).parent('div').html(result);
                });
            } else {
                alert('Selecteer een van de mogelijkheden');
            }
        } catch (e) {
        }
        return false;
    }
}
myFaq = {
    init : function(ulObj) {
        var patt = new RegExp('^#[0-9]+$');
        $(ulObj).children('li').children('div').hide();
        $(ulObj).children('li').children('h2').append('<a class="arrow"></a>');
        $(ulObj).children('li').children('h2').children('a').click(function(){
            $(this).parents('li').children('div').toggle(250);
        });
        if (patt.test(location.hash)) {
            $(ulObj).find('a[href=\'' + location.hash + '\']').parents('li').children('div').show();
        }
    }
}
myRotate = {
    init : function(divId, duration, speed, startrandom) {
        // Set opacity of all items to 0
        $('div#' + divId + ' ul li').css({opacity: 0.0}).hide();

        if (startrandom) {
            // Set random item to full opacity
            $('div#' + divId + ' ul li').removeClass('show');
            var index = Math.round(Math.random() * $('div#' + divId + ' ul li').length - 1);
            $('div#' + divId + ' ul li').eq(index).css({opacity: 1.0}).show().addClass('show');
        } else {
            // Set first item to full opacity
            $('div#' + divId + ' ul li').first().css({opacity: 1.0}).show();
        }

        // Call the rotate function, duration in milliseconds
        setInterval('myRotate.rotate(\'' + divId + '\', ' + speed + ', ' + duration + ')', duration);
//        this.progress(divId, duration + speed);
    },
    rotate : function(divId, speed, duration) {
        // Get the first image
        var current = ($('div#' + divId + ' ul li.show') ?  $('div#' + divId + ' ul li.show') : $('div#' + divId + ' ul li:first'));

        // Get next image, when it reaches the end, rotate it back to the first image
        var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#' + divId + ' ul li').first() : current.next()) : $('div#' + divId + ' ul li').first());

        // Set the fade in effect for the next image, the show class has higher z-index
        next.css({opacity: 0.0})
            .show()
            .addClass('show')
            .animate({opacity: 1.0}, speed);

        // Hide the current image
        current.animate({opacity: 0.0}, speed, function() { $(this).hide() })
               .removeClass('show');

        // progress bar
        this.progress(divId, duration + speed);
    },
    progress : function(divId, duration) {
        $('div#' + divId + ' div.progress div.bar').stop().css({width: '100%'}).animate({width: '0%'}, duration);
    }
}
myForm = {
    initSampleText : function() {
        $('form input.innerlabel').each(function() {
            if ($(this).attr('title') && (!$(this).val() || ($(this).val() == $(this).attr('title')))) {
                myForm.setSampleText($(this));
                $(this).focusin(function() { myForm.clearSampleText($(this)) })
                       .focusout(function() { myForm.setSampleText($(this)) });
            }
        });
    },
    clearSampleText : function(obj) {
        if ($(obj).attr('title') && (!$(obj).val() || ($(obj).val() == $(obj).attr('title')))) {
            $(obj).val('').removeClass('sampletext');
            $(obj).parent('form').find('input[class="submit"]').removeAttr('disabled');
        }
    },
    setSampleText : function(obj) {
        if ($(obj).attr('title') && (!$(obj).val() || ($(obj).val() == $(obj).attr('title')))) {
            $(obj).val($(obj).attr('title')).addClass('sampletext');
            $(obj).parent('form').find('input[class="submit"]').attr('disabled', 'disabled');
        }
    },
    initFormSubmit : function() {
        $('form').each(function() {
            $(this).submit(function() { myForm.submit($(this)) });
        });
    },
    submit : function(obj) {
        myForm.disableSubmit($(obj));
    },
    disableSubmit : function(form) {
        $(form).find('input[class="submit"]').attr('disabled', 'disabled');
    }
}

$(document).ready(function() { myForm.initSampleText() });
$(document).ready(function() { myForm.initFormSubmit() });

