$(function(){
    $('#close_notice').live('click', function() {
        nlc.closeNotice();
    });

    nlc.notice();

    $('a.destroy').click(function(){
        return confirm('Are you sure?');
    });

    $("#artwork_list div.thumb a").colorbox();
    $(".artwork_piece a.image").colorbox({maxWidth: 800, maxHeight: 600, scalePhotos:true});
    $(".artwork_piece > div.thumb > a.document").colorbox({iframe:true, width:718, height:700});
    $(".artwork_piece a.video").colorbox({iframe:true, innerWidth:425, innerHeight:344});
    $(".artwork_piece a.music").colorbox({iframe:true, innerWidth:425, innerHeight:344});
    $("#artwork_list a").embedly({maxWidth: 100, maxHeight: 100});
    $(".oembed_link").embedly({maxWidth:400, maxHeight:300});

    $(":text:visible:enabled:first").focus();
    $("#artist_login").click(function(e) {
        e.preventDefault();
        $("#artist_login_form").submit();
    
    });

    if ($('#artwork_type_select')) {
        nlc.change_upload_type($('#artwork_type_select'), $('#artwork_uploader'));
    }

    $('#artwork_type_select').change(function(e){
        nlc.change_upload_type($(this), $('#artwork_uploader'));
    });

    if ($('#user_type_select')) {
        nlc.change_user_type($('#user_type_select'), $('#user_reg_form'));
    }

    $('#user_type_select').change(function(e) {
        nlc.change_user_type($(this), $('#user_reg_form'));
    });
    /*
     * tags
     *
     */
    $('#categories').accordion({
        active: false,
        autoHeight: false,
        collapsible: true
    });

    $('.btns .all').live('click', function(){
        if($(this).text() == 'Select All'){
            $('.' + $(this).attr('id')).attr("checked", "checked");
            $(this).text('Deselect All');
        }
        else {
            $('.' + $(this).attr('id')).removeAttr("checked");
            $(this).text('Select All');
        }
    });


    /*
     * profiles classes
     *
     */
    if($('#offers_class')){
        nlc.toggle_class_blurb($(this), $('#profile_form'));
    }

    $('#offers_class').change(function(e){
        nlc.toggle_class_blurb($(this), $('#profile_form'));
    });

    /*
     * donations 
     *
     */

    $(document).ready(function(){
        $('#paypal').submit();
    });


    /*

    if($('.rotator').length > 0) {
        var options = {};
        
        $.get('/rotators/get_options/'+$('#rotator_id').val(), function (data) {
           options = data; 
           alert(options);
            $.each(options, function(index, option){
                var split = index.split('_');
                if(split.length == 2){
                    options[split[0] + split[1].charAt(0).toUpperCase() + split[1].slice(1)] = option;
                }
                else {
                    if(option == 'true'){
                        option = true;
                    }
                    if(option == 'false'){
                        option = false;
                    }
                    options[index] = option;
                }
            });

            $(".rotator").cycle(options);
            $('.rotator img').click(function(){
                document.location.href = $(this).attr('rel');
            });

        
        }, 'json');

    }
    */


    //$('#sortable').sortable();
});



/*
 * Keep the namespace clean!
 *
 */

// Initialize the class

var nlc = (function () {
    var noticeId = '#notice';
    var nlc = this;
    // Any function/variable in this scope is private to the nlc 'class'

    var createNotice = function(msg) {
        var html = '';

        html += '<div id="notice">';
        html +=     '<div id="close_notice">close</div>';
        html +=     '<p>'+msg+'</p>';
        html += '</div>';

        return html;
    };

    var showNotice = function (msg) {
        var notice = $(noticeId),
            speed = 'slow',
            timeout = 5000;

        if (msg !== undefined) {
            $(createNotice(msg)).prependTo('body');
        }

        notice.slideDown(speed, function(){
            setTimeout(function () {
                closeNotice();
            }, timeout);
        });
    };

    var closeNotice = function () {
        $(noticeId).slideUp('medium', function() {
            $(this).remove();
        });
    };





    // All methods within this return statement is public
    // Should follow the same basic rules as Lisp closures
    // Note - open bracket has to be on same line as return statement
    // due to implicit semicolon insertion.
    return {
        notice: function (msg, force) {
            var notice = $(noticeId),
                msg = msg !== undefined && msg !== '' ? msg : 'Action Complete';
            if (notice.length !== 0) {
                if (notice.is(':visible')) {
                    notice.slideUp('medium', function() {
                        notice.remove(); //Need to remove to kill last event
                        showNotice(msg);
                    });
                } else {
                    showNotice();
                }
            } else {
                if (force !== undefined) {
                   showNotice(msg);
                }
            }
        },
        closeNotice: function () {
            closeNotice();
        },
        change_upload_type: function(sel, form) {
            if ($(':selected', sel).val() == 'video') {
                form.attr((this.encoding ? 'encoding' : 'enctype'), "application/x-www-form-urlencoded" );
                $('#artwork_file').hide();
                $('#artwork_link').show();
            } else {
                form.attr((this.encoding ? 'encoding' : 'enctype'), "multipart/form-data" )
                $('#artwork_link').hide();
                $('#artwork_file').show();
            }
        },
        change_user_type: function(sel, form) {
            if ($(':selected', sel).val() == '2') {
                $('#ven_org_name').slideUp('medium', function() { 
                    $('input', $(this)).attr('disabled', true);
                    $('#member_name').slideDown('medium', function() {
                        $('input', $(this)).attr('disabled', false);
                    });
                });
            } else {
                $('#member_name').slideUp('medium', function() { 
                    $('input', $(this)).attr('disabled', true);
                    $('#ven_org_name').slideDown('medium', function() {
                        $('input', $(this)).attr('disabled', false);
                    });
                });
            }
        },

        toggle_class_blurb: function(sel, form){
            if ($('#offers_class').is(':checked')){
                $('#classes').slideDown('medium', function() { 
                    $('input', $(this)).attr('disabled', true);
                });
            } else {
                $('#classes').slideUp('medium', function() { 
                    $('input', $(this)).attr('disabled', false);
                });
            }
        }

    };
}());

