
var Widget={view:null,inviteConnections:null,createCanvasView:function()
{gadgets.window.setTitle('Box.net Files');gadgets.window.adjustHeight(600);Widget.view=new CanvasView();Widget.view.init();},createProfileView:function()
{gadgets.window.setTitle('My Files');gadgets.window.adjustHeight(250);Widget.view=new ProfileView();Widget.view.init();},createHomeView:function()
{gadgets.window.setTitle('My Files');Widget.view=new HomeView();Widget.view.init();}};View=Base.extend({constructor:function()
{this.container=document.getElementById('box_body');},owner:null,widgetOwner:null,viewer:null,widgetHash:null,partnerAuthToken:null,container:null,widgetContainer:null,sfa:0,fao:0,can_register:1,register_on_box:0,init:function()
{this.getPeople();},getPeople:function()
{var req=opensocial.newDataRequest();req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER),'owner');req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER),'viewer');req.send(gadgets.util.makeClosure(this,this.setPeople));},setPeople:function(response)
{this.owner=response.get('owner').getData();this.viewer=response.get('viewer').getData();},getWidget:function(container,callback)
{this.widgetContainer=container;var id=this.widgetOwner;var params={};params[gadgets.io.RequestParameters.CONTENT_TYPE]=gadgets.io.ContentType.JSON;params[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;gadgets.io.makeRequest(url+'/index.php?rm=box_ning_get_widget_hash&box_widget_owner_id='+id,gadgets.util.makeClosure(this,callback),params);},setWidgetParams:function(response)
{if(response.data.error)
{var box_container=document.getElementById('box_container');box_container.innerHTML="You don't have access to this application.";}
else
{this.widgetHash=response.data.hash;this.partnerAuthToken=gadgets.json.stringify(response.data.partner_auth_token);this.displayWidget();}},displayWidget:function()
{if(business_app)
{if(this.viewer.getField(ning.Person.Field.ADMIN))
{this.register_on_box=1;this.can_register=1;}
else
{this.can_register=0;}}
var domain=url.substr(7);var widget='<embed src="'+widget_url+'?widgetHash='+this.widgetHash+'&sfa='+this.sfa+'&fao='+this.fao+'&can_register='+this.can_register+'&register_on_box='+this.register_on_box+'&can_register_text='+escape('Register your network for a free trial with Box')+'&cant_register_text='+escape('Don\'t have an account? Please contact your Ning network administrator to get provisioned with Box')+'&partner_user_id='+this.viewer.getId()+'&partner_auth_token='+escape(escape(this.partnerAuthToken))+'&partner_context='+this.widgetOwner+'&apiKey='+api_key+'&api_domain='+escape(domain)+'&copyright_url='+escape(url+'/static/html/terms.html')+'&partner_id='+partner_id+'&partnerFolderName=Profile Folder" width="100%" height="100%" wmode="transparent" type="application/x-shockwave-flash"></embed>';this.widgetContainer.innerHTML=widget;}});CanvasView=View.extend({constructor:function()
{this.base();this.initEvents();},myFilesView:0,selectedBoxConnection:null,setPeople:function(response)
{this.base(response);this.setHeader();this.setMyFilesView();},initEvents:function()
{var that=this;var myFiles=document.getElementById('box_my_files');myFiles.onclick=function(){that.setMyFilesView();};},setMyFilesView:function()
{if(!this.myFilesView)
{this.container.innerHTML='';var myFiles=document.getElementById('box_my_files');myFiles.className='selected';this.myFilesView=1;new MyFilesView();}},setConnectionsFilesView:function()
{if(this.myFilesView)
{var container=document.getElementById('box_body');container.innerHTML='';var myFiles=document.getElementById('box_my_files');myFiles.className='';this.myFilesView=0;var params={};params[gadgets.io.RequestParameters.CONTENT_TYPE]=gadgets.io.ContentType.TEXT;params[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;gadgets.io.makeRequest(url+'/index.php?rm=box_ning_get_user_connections',gadgets.util.makeClosure(this,this.displayConnectionsFilesView),params);}},displayConnectionsFilesView:function(response)
{var container=document.getElementById('box_body');var connections_list=new ConnectionsFilesView(eval('('+response.text+')'),container);},setHeader:function()
{var my_files=document.getElementById('box_my_files');var app_status=ning.core.getAppStatus();if(this.viewer.isOwner()||network_view||app_status=='network')
{my_files.innerHTML='My Files';}
else
{my_files.innerHTML=this.owner.getDisplayName()+"'s Files";}},setFooter:function()
{}});MyFilesView=function()
{this.init();}
MyFilesView.prototype={init:function()
{var app_status=ning.core.getAppStatus();var that=Widget.view;if(!network_view&&app_status=='child')
{that.widgetOwner=that.owner.getId();}
else
{that.widgetOwner=that.viewer.getId();}
if(that.viewer.getId()==that.widgetOwner)
{that.sfa=1;}
else
{that.sfa=0;}
that.getWidget(that.container,that.setWidgetParams);}};ConnectionsFilesView=function(connections,parentEl)
{this.connections=connections;this.parentEl=parentEl;this.render();}
ConnectionsFilesView.prototype={render:function()
{var title=document.createElement('div');title.className='connections_files';title.innerHTML="My Friends' Files";this.parentEl.appendChild(title);var box_connections_list_container=document.createElement('div');box_connections_list_container.className='box_connections_list_container';this.parentEl.appendChild(box_connections_list_container);var box_connections_list=document.createElement('div');box_connections_list.className='box_connections_list';box_connections_list_container.appendChild(box_connections_list);for(var i=0;i<this.connections.box_users.length;i++)
{var connection=new BoxConnection(this.connections.box_users[i],box_connections_list);}
var invite_connections_container=document.createElement('div');invite_connections_container.className='invite_connections';this.parentEl.appendChild(invite_connections_container);Widget.inviteConnections=new InviteConnections(this.connections,invite_connections_container);}};BoxConnection=function(data,parentEl)
{this.data=data;this.parentEl=parentEl;this.render();this.initEvents();};BoxConnection.prototype={data:null,container:null,parentEl:null,filesDisplayed:false,widgetContainer:null,toggle_files:null,render:function()
{this.container=document.createElement('div');this.container.className='box_connection';this.parentEl.appendChild(this.container);var picture_container=document.createElement('div');picture_container.className='box_picture_container';this.container.appendChild(picture_container);var picture=document.createElement('img');picture.className='box_connection_image';picture.width=80;picture.height=80;picture.src=this.data.photoUrl;picture_container.appendChild(picture);this.name=document.createElement('div');this.name.className='box_connection_name';this.name.innerHTML=$j.text(this.data.name);this.container.appendChild(this.name);this.toggle_files=document.createElement('div');this.toggle_files.className='show_files';this.toggle_files.innerHTML='View Files';this.container.appendChild(this.toggle_files);},initEvents:function()
{var that=this;this.toggle_files.onclick=function(){that.toggleFiles()};this.toggle_files.onmouseover=function(){that.toggle_files.className='show_files_over'};this.toggle_files.onmouseout=function(){that.toggle_files.className='show_files'};},toggleFiles:function()
{if(this.filesDisplayed)
{this.hideWidget();}
else
{this.showWidget();this.toggle_files.innerHTML='Loading Files ...';this.filesDisplayed=true;}},hideWidget:function()
{this.toggle_files.innerHTML='View Files';this.filesDisplayed=false;Widget.view.selectedBoxConnection=null;this.container.removeChild(this.widgetContainer);this.widgetContainer=null;},showWidget:function(id)
{var that=Widget.view;if(that.selectedBoxConnection)
{that.selectedBoxConnection.hideWidget();that.selectedBoxConnection=null;}
that.selectedBoxConnection=this;that.widgetOwner=this.data.id;that.sfa=0;this.widgetContainer=document.createElement('div');this.widgetContainer.className='connection_widget';this.container.appendChild(this.widgetContainer);that.getWidget(this.widgetContainer,this.displayWidget);},displayWidget:function(response)
{var connection=Widget.view.selectedBoxConnection;connection.toggle_files.innerHTML='Hide Files';Widget.view.widgetHash=response.data.hash;Widget.view.partnerAuthToken=gadgets.json.stringify(response.data.partner_auth_token);Widget.view.displayWidget();}};InviteConnections=function(connections,container)
{this.connections=connections.non_box_users;this.container=container;this.render();this.initEvents();};InviteConnections.prototype={connections:null,container:null,invitedConnectionsList:[],render:function()
{var invite_connections_title=document.createElement('div');invite_connections_title.className='invite_connections_title';invite_connections_title.innerHTML='Invite Your Connections';this.container.appendChild(invite_connections_title);var invited_counter=document.createElement('div');invited_counter.id='invited_counter';invited_counter.innerHTML='Connections selected : 0';this.container.appendChild(invited_counter);var invite_connections_list=document.createElement('div');invite_connections_list.className='invite_connections_list';this.container.appendChild(invite_connections_list);for(var i=0;i<this.connections.length;i++)
{var connection=new NonBoxConnection(this.connections[i],invite_connections_list);}
var invite_button=document.createElement('div');invite_button.id='invite_button';var invite_button_text=document.createElement('div');invite_button_text.id='invite_button_text';invite_button_text.innerHTML='Invite Connections';this.container.appendChild(invite_button);invite_button.appendChild(invite_button_text);},initEvents:function()
{var that=this;var invite_button=document.getElementById('invite_button');invite_button.onclick=function(){that.sendInvitations()};invite_button.onmouseover=function(){invite_button.id='invite_button_selected'};invite_button.onmouseout=function(){invite_button.id='invite_button'};},sendInvitations:function()
{var recipients=this.buildRecipientsList();var params={};params[opensocial.Message.Field.TYPE]=opensocial.Message.Type.EMAIL;params[opensocial.Message.Field.TITLE]="Try Box.net Widget";var body="Try Box.net Widget";var message=opensocial.newMessage(body,params);opensocial.requestSendMessage(recipients,message,function(){});},buildRecipientsList:function()
{var recipients="";for(var i=0;i<this.invitedConnectionsList.length;i++)
{recipients=recipients+this.invitedConnectionsList[i]+',';}
return recipients;}};NonBoxConnection=function(data,parentEl)
{this.data=data;this.parentEl=parentEl;this.render();this.initEvents();};NonBoxConnection.prototype={data:null,container:null,parentEl:null,render:function()
{this.container=document.createElement('div');this.container.className='non_box_connection';this.parentEl.appendChild(this.container);var picture_container=document.createElement('div');picture_container.className='non_box_picture_container';this.container.appendChild(picture_container);var picture=document.createElement('img');picture.className='non_box_connection_image';picture.width=50;picture.height=50;picture.src=this.data.photoUrl;picture_container.appendChild(picture);var name_el=document.createElement('span');name_el.className='non_box_connection_name';name_el.innerHTML=$j.text(this.data.name);this.container.appendChild(name_el);},initEvents:function()
{var that=this;this.container.onclick=function(){that.connectionClicked()};this.container.onmouseover=function(){if(!that.selected){that.container.className='non_box_connection_over'}};this.container.onmouseout=function(){if(!that.selected){that.container.className='non_box_connection'}};},connectionClicked:function()
{if(this.selected)
{this.unselectConnection();}
else
{this.selectConnection();}},selectConnection:function()
{this.container.className='non_box_connection_selected';this.selected=1;Widget.inviteConnections.invitedConnectionsList.push(this.data.id);var counter=document.getElementById('invited_counter');counter.innerHTML='Connections selected: '+Widget.inviteConnections.invitedConnectionsList.length;},unselectConnection:function()
{this.container.className='non_box_connection';this.selected=0;var unselected=null;var list=Widget.inviteConnections.invitedConnectionsList;for(var i=0;i<list.length;i++)
{if(list[i]==this.data.id)
{var unselected=i;}}
if(unselected!=null)
{list.splice(unselected,1);}
var counter=document.getElementById('invited_counter');counter.innerHTML='Connections selected: '+list.length;}};ProfileView=View.extend({constuctor:function()
{this.base();this.initEvents();},myFilesView:0,selectedBoxConnection:null,setPeople:function(response)
{this.base(response);this.initWidget();},initEvents:function()
{},navigateToCanvas:function()
{gadgets.views.requestNavigateTo(new gadgets.views.View('canvas'));},initWidget:function()
{this.widgetOwner=this.owner.getId();this.getWidget(this.container,this.setWidgetParams);}});