/*
 * Social Link Panel
 * Copyright(c), CBC Radio 3 - Phil Rabin.
 */


CBCR3.namespace("CBCR3.SocialLinkPanel.Events");CBCR3.SocialLinkPanel.MessageType={success:"success",error:"error",loading:"loading"};CBCR3.SocialLinkPanel.Events.ServiceEvent={shortenedUrlReceived:"shortenedUrlReceived",emailSent:"emailSent"};CBCR3.SocialLinkPanel.Events.ServiceErrorEvent={emailError:"emailError"};

CBCR3.namespace("CBCR3.SocialLinkPanel.Services");CBCR3.SocialLinkPanel.Services.PermalinkService=Class.create(CBCR3.Commons.EventDispatcher,{initialize:function($super)
{$super();},shortenUrl:function(permalink)
{CBC.Radio3.Web.WebServices.PermalinkWebService.ShortenUrl(permalink,this.onUrlShortenedSuccess.bind(this),this.onShortenUrlFailure.bind(this));},onUrlShortenedSuccess:function(response)
{this.dispatchEvent(CBCR3.SocialLinkPanel.Events.ServiceEvent.shortenedUrlReceived,{url:response});},onShortenUrlFailure:function(reponse)
{alert("could not shorten url");},sendEmail:function(senderName,email,message,permalink,permalinkTitle)
{CBC.Radio3.Web.WebServices.PermalinkWebService.SendEmail(senderName,email,message,permalink,permalinkTitle,this.onSendEmailSuccess.bind(this),this.onSendEmailFailure.bind(this));},onSendEmailSuccess:function(respose)
{this.dispatchEvent(CBCR3.SocialLinkPanel.Events.ServiceEvent.emailSent,null);},onSendEmailFailure:function(response)
{this.dispatchEvent(CBCR3.SocialLinkPanel.Events.ServiceErrorEvent.emailError,{message:response.get_message()});}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Controls");CBCR3.SocialLinkPanel.Controls.ModulePanel=Class.create({panel:null,modulePanel:null,permalink:"",shortLink:"",title:"",initialize:function()
{this.panel=new Element("div").update(this.getTemplate().evaluate({}));this.modulePanel=this.panel.down(".socialNetworkModules");},create:function()
{return this.panel;},setLinks:function(permalink,shortLink,title)
{this.permalink=permalink;this.shortLink=shortLink;this.title=title;},addModules:function(moduleCollection)
{this.modulePanel.update();moduleCollection.each(this.addModule.bind(this));this.modulePanel.appendChild(new Element("div",{className:"clear"}).update("&nbsp;"));},addModule:function(module)
{this.modulePanel.insert({bottom:module.render(this.permalink,this.shortLink,this.title)});},getTemplate:function()
{var markup="<div>"+"<label>Send this link to:</label>"+"</div>"+"<div class='socialNetworkModules'></div>";return new Template(markup);}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Modules");CBCR3.SocialLinkPanel.Modules.SocialNetworkModuleBase=Class.create({urlTemplate:null,cssClass:null,initialize:function(urlTemplate,cssClass)
{this.urlTemplate=urlTemplate;this.cssClass=cssClass;},render:function(url,title)
{throw"Render method must be overridden by subclass";},createElement:function(url,title)
{var hyperlink=new Template(this.urlTemplate).evaluate({url:url,title:encodeURIComponent(title)});return new this.getTemplate().evaluate({hyperlink:hyperlink,cssClass:this.cssClass});},getTemplate:function()
{return new Template("<a class='imageButton socialLinkStamp #{cssClass}' href='#{hyperlink}' target='_blank'>Post to a Social Network</a>");}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Modules");CBCR3.SocialLinkPanel.Modules.FacebookModule=Class.create(CBCR3.SocialLinkPanel.Modules.SocialNetworkModuleBase,{initialize:function($super)
{$super("http://www.facebook.com/sharer.php?u=#{url}&t=#{title}","facebook");},render:function(permalink,shortLink,title)
{return this.createElement(permalink,title);}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Modules");CBCR3.SocialLinkPanel.Modules.DeliciousModule=Class.create(CBCR3.SocialLinkPanel.Modules.SocialNetworkModuleBase,{initialize:function($super)
{$super("http://delicious.com/post?v=5&amp;jump=close&amp;url=#{url}&amp;title=#{title}","delicious");},render:function(permalink,shortLink,title)
{return this.createElement(permalink,title);}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Modules");CBCR3.SocialLinkPanel.Modules.TwitterModule=Class.create(CBCR3.SocialLinkPanel.Modules.SocialNetworkModuleBase,{initialize:function($super)
{$super("http://twitter.com/home?status=#{url}","twitter");},render:function(permalink,shortLink,title)
{return this.createElement(encodeURIComponent(title.truncate(80))+" "+shortLink+" via @cbcradio3");}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Controls");CBCR3.SocialLinkPanel.Controls.FriendEmailer=Class.create({panel:null,submitButton:null,nameInput:null,emailInput:null,messageInput:null,feedbackMessage:null,permalink:"",title:"",permalinkService:null,initialize:function(permalinkService)
{this.permalinkService=permalinkService;this.permalinkService.addEventListener(CBCR3.SocialLinkPanel.Events.ServiceEvent.emailSent,this.onEmailSent.bind(this))
this.permalinkService.addEventListener(CBCR3.SocialLinkPanel.Events.ServiceErrorEvent.emailError,this.onEmailError.bind(this))},setLinks:function(permalink,title)
{this.permalink=permalink;this.title=title;},create:function()
{this.panel=new Element("div",{className:"emailAFriend"}).update(this.getTemplate().evaluate({}));var nInput=this.panel.down("input[name='mailerNameInput']");this.nameInput=new CBCR3.Commons.Controls.InputPlaceHolder(nInput,"Enter your name...",{placeHolderColor:"#666666"});var eInput=this.panel.down("input[name='mailerEmailInput']");this.emailInput=new CBCR3.Commons.Controls.InputPlaceHolder(eInput,"Enter your friend's email...",{placeHolderColor:"#666666"});var mInput=this.panel.down("textarea[name='mailerMessageInput']");this.messageInput=new CBCR3.Commons.Controls.InputPlaceHolder(mInput,"Add a message (optional)...",{placeHolderColor:"#666666"});this.submitButton=this.panel.down(".sendEmailButton");this.submitButton.observe("click",this.onSubmitEmail_Click.bind(this));this.feedbackMessage=this.panel.down(".feedbackMessage");return this.panel;},onSubmitEmail_Click:function(event)
{this.attemptSendEmail();},attemptSendEmail:function()
{if(!this.nameInput.hasValue()){this.addMessage("Please enter your name",CBCR3.SocialLinkPanel.MessageType.error);return;}
if(!this.emailInput.hasValue()){this.addMessage("Please enter an email address",CBCR3.SocialLinkPanel.MessageType.error);return;}
this.disableButton();this.addMessage("Sending...",CBCR3.SocialLinkPanel.MessageType.loading);this.permalinkService.sendEmail(this.nameInput.getValue(),this.emailInput.getValue(),this.messageInput.getValue(),this.permalink,this.title);},addMessage:function(message,type)
{var content=new Element("div",{className:type}).update(message);this.feedbackMessage.update(content);},onEmailSent:function(event)
{this.addMessage("Email successfully sent",CBCR3.SocialLinkPanel.MessageType.success);this.enableButton();this.resetForm();},onEmailError:function(event)
{this.addMessage("Error: "+event.data.message,CBCR3.SocialLinkPanel.MessageType.error);this.enableButton();},disableButton:function()
{this.submitButton.addClassName("disabled");this.submitButton.setAttribute("disabled","disabled");},enableButton:function()
{this.submitButton.removeClassName("disabled");this.submitButton.removeAttribute("disabled");},resetForm:function()
{this.nameInput.reset();this.emailInput.reset();this.messageInput.reset();},getTemplate:function()
{var markup="<label>Send to a friend:</label>"+"<input type='text' name='mailerNameInput' title=\"Enter your name here\" />"+"<input type='text' name='mailerEmailInput' title=\"Enter your friend&#39;s email address here\" />"+"<textarea name='mailerMessageInput' title='Enter an optional message here' cols='3' rows='5'></textarea>"+"<div class='feedbackMessage'></div>"+"<div class='defaultButton'>"+"<button type='button' class='sendEmailButton'>SEND</button>"+"</div>"+"<div class='clear'>&nbsp;</div>";return new Template(markup);}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Controls");CBCR3.SocialLinkPanel.Controls.LinkField=Class.create({item:null,initialValue:null,labelText:null,input:null,buttonContainer:null,swfCopyButton:null,initialize:function(initialValue,labelText)
{this.initialValue=initialValue||"";this.labelText=labelText||"";},create:function()
{this.item=new Element("div",{className:"permalinkField"});this.item.update(this.getTemplate().evaluate({inputValue:this.initialValue,labelText:this.labelText}));this.input=this.item.down("input");this.input.observe("click",this.input_Click.bind(this));this.buttonContainer=this.item.down(".swfCopyButton");this.buttonContainer.identify();return this.item;},createButton:function()
{this.swfCopyButton=new CBCR3.SocialLinkPanel.Controls.SwfCopyButton(this.buttonContainer,this.initialValue);},input_Click:function(event)
{this.hightlightInput();},hightlightInput:function()
{this.input.focus();this.input.select();},getTemplate:function()
{var markup="<label>#{labelText}:</label>"+"<input type='text' value='#{inputValue}' readonly='readonly' />"+"<div class='swfCopyButton'></div>";return new Template(markup);}});

CBCR3.namespace("CBCR3.SocialLinkPanel.Controls");CBCR3.SocialLinkPanel.Controls.SwfCopyButton=Class.create({container:null,swfId:null,swfObject:null,copyText:"",count:0,initialize:function(container,copyText)
{this.container=$(container);this.copyText=copyText;this.embedSwf();Event.observe(window,"scroll",this.onWindowScroll.bind(this));},swf:function()
{if(!this.swfObject)
this.swfObject=$(this.swfId);return this.swfObject;},embedSwf:function()
{this.swfId=this.container.id+"_swf";var swfBox=new Element("div",{id:this.container.id+"_swfBox"});this.container.update(swfBox);var flashVars={copyText:this.copyText};var params={menu:'false',allowScriptAccess:'always',wmode:"window"};var attributes={id:this.swfId};swfobject.embedSWF(CBCR3.Application.Config.applicationPath+"/lib/cbcr3/sociallinkpanel/flash/clipboard.swf",swfBox.id,"60","30","10.0.0",null,flashVars,params,attributes);},onWindowScroll:function(event)
{if(Prototype.Browser.Gecko)
{this.container.addClassName("fix");this.refresh.bind(this).delay(0.3);}},refresh:function()
{this.container.removeClassName("fix");}});

CBCR3.namespace("CBCR3.SocialLinkPanel");var _socialLinkPanelInstance;CBCR3.SocialLinkPanel.SocialLinkPanelTasks={launchPanel:function(permalink,title)
{if(!_socialLinkPanelInstance)
{var moduleCollection=[new CBCR3.SocialLinkPanel.Modules.FacebookModule(),new CBCR3.SocialLinkPanel.Modules.DeliciousModule(),new CBCR3.SocialLinkPanel.Modules.TwitterModule()];_socialLinkPanelInstance=new CBCR3.SocialLinkPanel.SocialLinkPanel(new CBCR3.SocialLinkPanel.Services.PermalinkService(),moduleCollection);}
_socialLinkPanelInstance.open(permalink,title);}};CBCR3.SocialLinkPanel.SocialLinkPanel=Class.create({panel:null,contentHolder:null,modal:null,closeButton:null,permalinkField:null,shortenedLinkField:null,linkField:null,permalinkService:null,moduleCollection:[],modulePanel:null,permalink:"",shortenedLink:"",title:"",friendEmailer:null,initialize:function(permalinkService,moduleCollection)
{this.permalinkService=permalinkService;this.permalinkService.addEventListener(CBCR3.SocialLinkPanel.Events.ServiceEvent.shortenedUrlReceived,this.onShortenedUrlReceived.bind(this));this.moduleCollection=moduleCollection;},open:function(permalink,title)
{if(this.isOpen())
return;this.initializePanel();this.modal=new Control.Modal(this.panel,{overlayOpacity:0.75,className:'modalWindow socialLinkWindow',fade:false});this.modal.open();this.permalink=permalink;this.title=title;this.permalinkService.shortenUrl(permalink);},initializePanel:function()
{if(this.panel!=undefined)
this.panel.remove();this.panel=new Element("div",{id:"socialLinkPanel",className:"socialLinkPanel"});$(document.body).insert({top:this.panel});this.panel.update(this.getTemplate().evaluate({}));this.closeButton=this.panel.down(".closeButton");this.closeButton.observe("click",this.onClose_Click.bind(this));this.contentHolder=this.panel.down(".panelContent");},onShortenedUrlReceived:function(event)
{this.shortenedLink=event.data.url;this.addControls();this.hideLoading();this.modal.position();},composeUrl:function(url)
{if(url.startsWith("http://"+CBCR3.Application.Config.hostName))
return url;return"http://"+CBCR3.Application.Config.hostName+url;},addControls:function()
{this.permalinkField=new CBCR3.SocialLinkPanel.Controls.LinkField(this.composeUrl(this.permalink),"Permalink");this.shortenedLinkField=new CBCR3.SocialLinkPanel.Controls.LinkField(this.shortenedLink,"Shortened Link");this.contentHolder.insert({bottom:this.permalinkField.create()});this.contentHolder.insert({bottom:this.shortenedLinkField.create()});this.permalinkField.createButton();this.shortenedLinkField.createButton();this.modulePanel=new CBCR3.SocialLinkPanel.Controls.ModulePanel();this.modulePanel.setLinks(this.permalink,this.shortenedLink,this.title);this.modulePanel.addModules(this.moduleCollection);this.contentHolder.insert({bottom:this.modulePanel.create()});this.friendEmailer=new CBCR3.SocialLinkPanel.Controls.FriendEmailer(this.permalinkService);this.friendEmailer.setLinks(this.permalink,this.title);this.contentHolder.insert({bottom:this.friendEmailer.create()});},close:function()
{this.modal.close();},isOpen:function()
{if(this.panel==undefined)
return false;return this.panel.isOpen;},hideLoading:function()
{this.panel.down(".loading").remove();},createCopyInput:function(input)
{},onClose_Click:function(event)
{Event.stop(event);this.close();},getTemplate:function()
{var markup="<div class='topCap'></div>"+"<div class='panelContent'>"+"<button type='button' class='imageButton closeButton'>Close</button>"+"<div class='loading'></div>"+"</div>"+"<div class='bottomCap'></div>";return new Template(markup);}});
