(function( $ ){

    // private function
    // outside of the jquery scope, so we
    // have to assign jquery and the according element first as arguments
    function _trigger($, thisElement, options) {
        // extract needed ids
        var containerId = thisElement.attr('id');

        var idString = containerId.replace('work-box-', '');

        var parts = idString.split('-');
        var id = parts[0];
        var targetId = thisElement.attr('target');
        var loader = $('#work-loader-'+id);

        // do the main effects
        thisElement.fadeTo(300, 0.5);
        thisElement.addClass('work-box-active');
        loader.fadeTo(0, 1.0);
        loader.show();

        // manipulate document title
        var titleParts = document.title.split('|');
        // <bloginfo> | <work_title> | <tagline>
        if (titleParts[2]){
            document.title = titleParts[0] + ' | ' + $('#work-holder-'+id).attr('title') + ' | ' + titleParts[2];
        }
        // <bloginfo> | <tagline>
        else if (titleParts[1]) {
            document.title = titleParts[0] + ' | ' + $('#work-holder-'+id).attr('title') + ' | ' + titleParts[1];
        } else{
            document.title = titleParts[0] + ' | ' + $('#work-holder-'+id).attr('title');
        }

        // fetch video details with ajax
        $.ajax({
            url: options.url+'?work_id='+id,
            dataType: 'html',
            success: function(data) {
                //$('#work-holder-'+id).html(data);

                var targetEle = $('#work-details-row-'+targetId);
                // replace details content
                targetEle.html(data);

                // mark current details row as active
                targetEle.addClass('work-details-row-active');

                // highlight area should be completely replaced
                if ($('#'+containerId).hasClass('work-highlight')) {
                    $('#'+containerId).hide();
                }

                // show new details content
                targetEle.slideDown(options.slideDownSpeed, function(){
                    loader.hide();
                    // show the player
                    //$('#work-details-row-'+targetId+' .player-container').fadeIn(options.fadeInSpeed);
                    $.scrollTo(targetEle, 1000, {
                        easing:'easeInOutQuart',
                        offset:-60
                    } );
                    VideoJS.setupAllWhenReady({
                        showControlsAtStart: true
                    });

                    //$('#work-details-row-'+targetId+' .details').fadeIn(options.fadeInSpeed+1500);
                    //$('#work-details-row-'+targetId+' .credits').fadeIn(options.fadeInSpeed+1500);
                    $('#work-details-row-'+targetId+' div.fillspace').fadeIn(options.fadeInSpeed+2000);
                //$('#work-details-row-'+targetId+' .credits').effect('slide', {}, options.fadeInSpeed+1200);
                });

                // add click event to new close image button
                $('div.work-header img.close').click(function(){
                    $().einmalich('reset', $(this), function(){
                        $.scrollTo(thisElement, 1000, {
                            easing:'easeInOutQuart',
                            offset:-60
                        });
                    });
                });

                // add onclick event to link placeholder
                $('a.link-toggle').click(function(){
                    var toggleId = $(this).attr('id').replace('link-toggle-', '');
                    $('#link-'+toggleId).slideToggle('fast');
                    return false;
                });

                $('.more input').focus(function(){
                    this.select();
                });

            }// end success
        }); // end ajax
    }

    var methods = {

        init : function( options ) {

            var defaults = {
                url: '#',
                slideDownSpeed: 500,
                fadeInSpeed: 300,
                expand: 0
            };

            var options = $.extend(defaults, options);

            // auto expand focused element (only once of course)
            if (options['expand']) {
                _trigger($, $(options['expand']), options);
            }
                
            return this.each(function(){

                // Do your awesome plugin stuff here
                $(this).click(function(){

                    // we should continue the script only
                    // when the reset stuff has finished
                    // therefore we need all init functionality
                    // in this callback
                    $().einmalich('reset', $(this), function(thisElement){

                        _trigger($, thisElement, options);
                        return false;

                    });// end reset callback

                }); // end click

            }); // end each
        }, // end init
        
        reset : function(thisElement, callback) {

            // reset document title
            var titleParts = document.title.split('|');
            // <bloginfo> | <work_title> | <tagline>
            if (titleParts[2]){
                document.title = titleParts[0] + ' | ' + titleParts[2];
            }
            // do not reset any other title case

            var list = $('.work-details-row-active');

            if (list.length > 0) {
                list.slideUp(400, function(){


                    // hide all image loaders

                    $('.work-loader').hide();

                    // reset opacity
                    $('.work-box-active').fadeTo(600, 1);

                    // remove "active" class again
                    $('.work-details-row-active').removeClass('work-details-row-active');
                    $('.work-box-active').removeClass('work-box-active');
                    
                    // reset detail contents
                    $('.work-details-row').html('');

                    if (typeof callback == 'function') { // make sure the callback is a function
                        callback.call(this, thisElement); // brings the scope to the callback
                    }

                });
            } else {
                // run the callback also when the list is empty
                if (typeof callback == 'function') { // make sure the callback is a function
                    callback.call(this, thisElement); // brings the scope to the callback
                }
            }
        }// end reset

    }; // end methods

    $.fn.einmalich = function( method ) {

        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.einmalich' );
        }

    };
  
})( jQuery );


jQuery(document).ready(function($) {

    var footer = $('#me');

    if (footer.length) {
        $('#top').click(function(){
            $.scrollTo(footer, 2000, {
                easing:'easeInOutQuart',
                offset:-50
            });
            return false;
        });
    }

    $('#me-dummy').click(function(){
        $('#footer-pre .me').effect("highlight", {color: "#b0c53c"}, 3000);
        return false;
    });

    $('#me').click(function(){
        $.scrollTo($('#top'), 2000, {
            easing:'easeInOutQuart',
            offset:-160
        });
        return false;
    });
});
