(function($){
    
    $.Boxer = function(options) {
        // Class initializer.  Both options and data are optional.
        
        console.debug(options.cls);

        this.options = $.extend({}, $.Boxer.options, options);
        
        this.options.selectors = {};
        $.extend(this.options.selectors, $.Boxer.selectors, options !== undefined ? options.selectors : {});
        
        console.debug(this.options.cls);

        $(this.options.html).attr('class', this.options.cls).appendTo('body').hide();
        $(this.options.selectors.close).click(this.close);
    };
    
    // Default settings.
    $.Boxer.options = {
        cls: 'boxer',
        opacity: 0.75, // obscura target opacity
        html: '<div id="boxer"><div class="boxer-obscura"><!-- IE --></div><div class="boxer-body"><img src="/static/img/lightbox/widget_close.png" class="boxer-close" /><div class="boxer-content"></div></div></div>'
    };
    
    // Override these if you use your own custom HTML.
    $.Boxer.selectors = {
        boxer: '#boxer',
        body: '#boxer .boxer-body',
        content: '#boxer .boxer-content',
        obscura: '#boxer .boxer-obscura',
        close: '#boxer .boxer-close, #boxer .boxer-obscura'
    };
    
    $.extend($.Boxer.prototype, {
        
        reveal: function(data) {
            $(this.options.selectors.content).empty().append(data);
            
            $(this.options.selectors.obscura).css({'opacity': this.options.opacity}).show();
            $(this.options.selectors.body).show().find(this.options.selectors.content).show();
            $(this.options.selectors.boxer).show();
        },
        
        close: function() {
            var instance = $('body').data('boxer');
            $(instance.options.selectors.boxer).hide();
            $('body').data('boxer', undefined);
            $('#boxer').remove();
        }
        
    });
    
    $.boxer = function(data, options) {
        $('#boxer').remove();
        $('body').data('boxer', new $.Boxer(options));
        var instance = $('body').data('boxer');
        
        instance.reveal(data);
    }
    
})(jQuery);


// pop up video player
function generatePlayerWidget(options) {
    return '<embed src="/static/swf/player.swf" width="'+options.width+'" height="'+options.height+'" allowscriptaccess="always" allowfullscreen="true" bgcolor="000000" flashvars="height='+options.height+'&amp;width='+options.width+'&amp;file=http://inlocation.ca:2895/video/'+options.file+'.mp4&amp;streamscript=lighttpd&amp;backcolor=0x000000&amp;frontcolor=0xEEEEEE&amp;logo=/themes/infilm/img/video.png&amp;searchbar=false&amp;autostart=true" />"';
}

function generatePlayerWidgetFromURL(options) {
    return '<embed src="http://inlocation.ca/static/swf/player.swf" width="'+options.width+'" height="'+options.height+'" allowscriptaccess="always" allowfullscreen="true" bgcolor="000000" flashvars="height='+options.height+'&amp;width='+options.width+'&amp;file='+options.url+'&amp;streamscript=lighttpd&amp;backcolor=0x000000&amp;frontcolor=0xEEEEEE&amp;logo=/themes/infilm/img/video.png&amp;searchbar=false&amp;autostart=true" />"';
}

$(function(){
    // $('#showreel').click(function(){ $.boxer(generatePlayerWidget({width: 864, height: 505, file: 'location-showreel'}), {cls: 'showreel'}); });
    $('a.popup-video').click(function() {
        var rel = $(this).attr('rel') || "864x505xshowreel";
        var w, h, c;
        
        console.debug(rel);
        var parts = rel.split('x');
        console.debug(parts);
        w = parseInt(parts[0]);
        h = parseInt(parts[1]);
        c = parts[2];

        $.boxer(generatePlayerWidgetFromURL({width: w, height: h, url: $(this).attr('href')}), {cls: c});
        return false;
    });
});

