function wcl(m){ if(window.console) window.console.log(m); }
function px(i){ return i + 'px'; }

function $S(name){ 
    // Return a Flash movie suitable for ExternalInterface calls.
    return (Prototype.Browser.IE)? window[name] : document[name];
}

function query_params_as_object(params){
    // Return the query parameters from the URL as an object suitable for use
    // as flashvars in a SWFObject embed, extend the passed object if provided.
    var query = document.location.search || document.location.hash;
    return Object.extend(query.toQueryParams(), params || {});
}

function adjust_size(){
    var spacer = $('left_spacer'), 
        flash = $('main_flash'), 
        viewport_width = document.viewport.getWidth(), 
        scrollbar_width = viewport_width - $$('html').first().getWidth(), 
        spacer_width = (viewport_width - (1010 + scrollbar_width)) / 2, 
        flash_width = viewport_width - (spacer_width + 205) - scrollbar_width;
    
    if(viewport_width > 1010 + scrollbar_width){
        spacer.setStyle({ width: px(spacer_width) }).show();
        flash.setStyle({ width: px(flash_width) });
    } else {
        spacer.hide();
        flash.setStyle({ width: px(805) });
    }
}


function popup(href, width, height, scrollbars){
	var scroll = (scrollbars)? 'yes' : 'no', 
		left = (screen.width - width) / 2, 
		top = ((screen.height - height) / 2) -50;
	
	var popup = window.open(href, "_blank", "scrollbars=" + scroll + ",width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no");
	popup.moveTo(left, top);
	popup.focus();
	
	return false;
}



if(!RP) var RP = {};

RP.Config = {
    templates: {
        invite_dialog:"<fb:request-form action=\"#{return_to}\" method=\"POST\" invite=\"true\" type=\"Extract\" content=\"View new photos, videos, and more at the &lt;a href=&quot;#{site_url}&quot;&gt;official Extract website&lt;/a&gt;!\"><fb:multi-friend-selector showborder=\"false\" actiontext=\"Invite your friends to connect.\"></fb:request-form>", 
        status:"<img src=\"#{image}\"><p>Welcome, #{name}</p><a href=\"#\" onclick=\"RP.FacebookConnect.logout(); return false;\">Logout of Facebook</a>"
    }, 
    feed_stories: {
        on_login: {
            bundle_id: 112845760078, 
            template_data: { images: [
                { src:"http://www.extract-the-movie.com/assets/images/facebook_share.gif", href:"http://www.extract-the-movie.com/"}
            ]}, 
            body:"", 
            prompt:"Tell your friends about Extract.", 
            prefill:"Check out the official website for Extract, a new movie from Mike Judge. In theaters September 4th."
        }
    }
};

RP.Template = (function(){
    var templates = RP.Config.templates, 
        dynamic_methods = {};
    
    // Render a template, including any collections.
    function render(template, attributes){
        attributes = attributes || {};
        $H(attributes).each(function(a){
            if(Object.isArray(a.value))
                attributes[a.key] = render_collection(a.key, a.value);
        });
        return interpolate_template(template, attributes);
    }
    
    // Render a template for each item in collection and concatenate.
    function render_collection(template, collection){
        return collection.collect(function(item){
            return interpolate_template(template, item);
        }).join('\n');
    }
    
    // Interpolate provided attributes into template.
    function interpolate_template(template, attributes){
        return templates[template].interpolate(attributes);
    }
    
    // Create a render method for each template.
    $H(templates).each(function(t){
        dynamic_methods['render_' + t.key] = function(attributes){
            return render(t.key, attributes);
        }
    });
    
    return Object.extend(dynamic_methods, { render: render });
})();

RP.FacebookConnect = (function(){
    var connected = false, 
        user = {};
    
    function initialize(){
        $(document).observe('rpfb:login', function(){
            if(connected) return false;
            
            connected = true;
            user = { uid: FB.Connect.get_loggedInUser() };
            build_status();
        });
        
        $(document).observe('rpfb:logout', function(){
            $('facebook_status').update().hide();
        });
    }
    
    function share(location){
        return login(function(){
            FB.Connect.showShareDialog(location);
        });
    }
    
    function invite(){
        var dialog = new FB.UI.FBMLPopupDialog(
            "Invite your friends", 
            RP.Template.render_invite_dialog({
                return_to: window.location, 
                site_url: "http://" + window.location.host
            }));
        dialog.setContentWidth(755);
        dialog.setContentHeight(650);
        return dialog.show();
    }
    
    function publish_story(data){
        return FB.Connect.showFeedDialog(data.bundle_id, data.template_data, 
            null, data.body, null, FB.RequireConnect.require, null, data.prompt, 
            { value: data.prefill });
    }
    
    function login(callback){
        return FB.Connect.requireSession(function(){
            $(document).fire('rpfb:login');
            if(callback) callback();
        });
    }
    
    function logout(callback){
        return FB.Connect.logout(function(){
            $(document).fire('rpfb:logout');
            if(callback) callback();
        });
    }
    
    function build_status(){
        return FB.Facebook.apiClient.users_getInfo(
            [user.uid], ['name', 'pic_square_with_logo'], function(info, e){
            $('facebook_status').update(RP.Template.render_status({
                name: info[0].name, 
                image: info[0].pic_square_with_logo
            })).blindDown({ 
                duration: 0.5, 
                transition: Effect.Transitions.Quart.easeInOut
            });
        });
    }
    
    return {
        initialize: initialize, 
        share: share, 
        invite: invite, 
        publish_story: publish_story, 
        login: login, 
        logout: logout
    }
})();
