var iShine = (function(){
  
    setupLightboxes = function(selector){
      $(selector).fancybox({
        frameWidth: 640,
        frameHeight: 400,
        hideOnContentClick: false,
        easingIn: null
      })
    }
    
    setupApplyForm = function()
    {
        /*
            Toggles the inputs of the apply form,
            so that if you choose an already uploaded movie,
            the new file upload input is reset and vice versa.
        */
        var form = $('form.apply-form');
        
        $('input', form).change(function(){
            var changed = this;
            
            $('input[type=radio], input[type=file]', form).each(function(){
                
                if (changed !== this)
                {
                    var type = $(this).attr('type');
                    
                    if (type === 'radio')
                        $(this).removeAttr('checked');
                    else
                        $(this).val('');
                }
            });
            
        });
    }
    
    setupPlayButtons = function()
    {
        $('ul.movies-list img.play, ul#interesting a.movie img.play').each(function(){
            var anchor = $(this).parent(),
                img = $(this);
            
            anchor.hover(
                function(){
                 img.fadeIn(100);
                },
                function(){
                 img.fadeOut(200);
                }
            );
        });
        
    }
    
    setupAdComment = function()
    {
        $('ul#interesting a.show-form').click(function() {
            $(this).parent().find('form.comment').show(0);
            $(this).parent().find('p.content').hide(0);
            $(this).hide(0);
            return false;
        });
    }
    
    return {
      init: function(){
          
        $("a.lightbox").fancybox();
        setupLightboxes(".lightbox a[class!='nolightbox']");
        setupApplyForm();
        setupPlayButtons();
        setupAdComment();
        /*
            images delete form submission
        */
        $("ul#images-list a").click(function(){
            $(this).parent().submit();
            return false;
        });
        
        /*
            Sidebar ad-list refresher
        */
        $('select#ads-list').change(function(){
            var selected = $(this).val(),
                current = $('form#ads-selector').attr('action');
            
            document.location = current.replace(new RegExp("ad/([0-9]+)/", "i"), "ad/" + selected + "/");
        });
        
        /*
            Submission specific
        */
        $('a#show-cv').click(function(){
           $('div#cv-wrap').fadeIn(200);
           $(this).hide(0);
           return false; 
        });
        
        $('.show-next').click(function(){
            $(this).next().fadeIn(200);
            $(this).hide(0);
            return false;
        });
        
        /*
            Submit the filter form when select updates
        */
        $('form#filter-form select').change(function(){
            $(this).parent().parent().submit();
        });
        
        /*
            Fade the unreviewied movie thumbnails
        */
        $('ul.movies-list.unreviewed img:not(.play)').fadeTo(0, 0.2);
      }
    }
  }());


$(document).ready(iShine.init)
