$(document).ready(function(){
    $('form').validate();

    $('textarea.tinymce').tinymce({
        // Location of TinyMCE script
        script_url : '/js/tiny_mce/tiny_mce.js',

        // General options
        width: '100%',
        height: '400px',
        theme : 'advanced',
        plugins : 'pagebreak,style,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist',

        // Theme options
        theme_advanced_buttons1 : 'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontsizeselect',
        theme_advanced_buttons2 : 'cut,copy,paste,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,image,cleanup,|,insertdate,inserttime,preview',
        theme_advanced_buttons3 : 'tablecontrols,|,hr,removeformat,visualaid,|,charmap,iespell,media,advhr,|,print,|,fullscreen',
        theme_advanced_buttons4 : 'forecolor,backcolor',
        theme_advanced_toolbar_location : 'top',
        theme_advanced_toolbar_align : 'left',
        theme_advanced_statusbar_location : 'none',
        theme_advanced_resizing : false
    });

    $(".toggler").css('cursor','pointer')
                 .click(function() {
                    var id_number = $(this).attr("id").split("-")[1];
                    if ($(this).text() == 'show') {
                        $("#FormSect-" + id_number).animate({opacity: 'show', height: 'show'}, 'fast');
                        $(this).parent().removeClass('plus').addClass('minus');
                        $(this).text('hide');
                    } else {
                        $("#FormSect-" + id_number).animate({opacity: 'hide', height: 'hide'}, 'fast');
                        $(this).parent().removeClass('minus').addClass('plus');
                        $(this).text('show');
                    }
                })

    $(".toggleAll").css('cursor','pointer')
                   .click(function() {
                        if ($(this).text() == 'show all') {
                            $(".toggler").text('show').click();
                            $(this).parent().removeClass('plus').addClass('minus');
                            $(this).text('hide all');
                        } else {
                            $(".toggler").text('hide').click();
                            $(this).parent().removeClass('minus').addClass('plus');
                            $(this).text('show all');
                        }
                    })
                   .click();

    $('.date-pick').DatePicker({
        format: 'd/m/y',
        date: $('.date-pick').val(),
        current: $('.date-pick').val(),
        starts: 1,
        position: 'right',
        onBeforeShow: function(){
            $('.date-pick').DatePickerSetDate($('.date-pick').val(), true);
        },
        onChange: function(formated, dates){
            $('.date-pick').val(formated);
        }
    });

    var button = $('#upload_button'), interval, defaultText = button.text(), field = $('#'+button.attr('name'));
    if (button.length > 0) {
        new AjaxUpload('upload_button', {
            action: '/admin/upload',
            onSubmit: function(file, ext){
                if (ext && /^(jpg|png|jpeg|gif|pdf)$/.test(ext)){
                    button.text('Uploading');
                    field.attr('value','Please wait...');
                    this.disable();
                    interval = window.setInterval(function(){
                        var text = button.text();
                        if (text.length < 13){
                            button.text(text + '.');
                        } else {
                            button.text('Uploading');
                        }
                    }, 200);
                } else {
                    alert('Only (.jpg | .png | .gif | .pdf) image files may be uploaded!');
                    return false;
                }
            },
            onComplete: function(file, response){
                button.text(defaultText);
                window.clearInterval(interval);
                this.enable();
                field.attr('value',response);
            }
        });
    }

    $(".addField").click(function() {
                    var val = $('#req_fields').val();
                    var text = $('#req_fields :selected').text();
                    if ($("input[value='field_"+val+"']").length == 0) {
                        $('.reqFieldsTable').append('<tr><td style="padding:0.5em;">'+text+'</td><td style="padding:0.5em;"><a href="#" class="removeField">x</a></td><input type="hidden" value="field_'+val+'" name="reqFields[]" /></tr>');
                    }
                    $(".removeField").click(function() {
                                    $(this).parent().parent().remove();
                                    return false;
                                })
                    return false;
                })

    $(".removeField").click(function() {
                    $(this).parent().parent().remove();
                    return false;
                })

});
