Parchment.Plugins.Video={type:"button",attributes_whitelist:{div:{"class":/js-item-cage/,rel:/video/,id:/\d+/},object:{width:/\d+/,height:/\d+/},param:{name:/\.*/,value:/\.*/,site:/\.*/},embed:{src:/\.*/,type:/application\/x-shockwave-flash/,allowscriptaccess:/\.*/,allowfullscreen:/\.*/,width:/\d+/,height:/\d+/,wmode:/transparent/,flashvars:/\.*/},iframe:{src:/\.*/,frameborder:/\d+/,width:/\d+/,height:/\d+/}},empty_node_whitelist:{param:function(a){return true},embed:function(a){return true},iframe:function(a){return true}},init:function(){this.parchment.editor.addEvents({keydown:function(d){var a=this.parchment.getSelectionObject(),b=this.parchment.getRangeObject(a),c=b.commonAncestorContainer;while(c!=this.parchment.editor&&c!=document){if(c.nodeType!=Node.TEXT_NODE&&c.getProperty("rel")=="video"){if(c.getNext()){b.setStart(c.getNext(),0);b.setEnd(c.getNext(),0);a.removeAllRanges();a.addRange(b)}else{d.stop()}break}c=c.parentNode}}.bind(this)});this.buildToolbar()},callback:function(a){this.openCodePanel()},openCodePanel:function(a){this.edit_mode=a?true:false;if(this.pad){this.pad.show()}else{var b='<div class="parchment-2-insert">';b+='<img src="'+MEDIA_URL+'vine/img/generic/logo-youtube.png" />';b+='<img src="'+MEDIA_URL+'vine/img/generic/logo-gametrailers.png" />';b+='<img src="'+MEDIA_URL+'vine/img/generic/logo-google.png" />';b+='<span class="description">Insert a URL or embed code from our site or any of these services</span>';b+='<textarea rows="5" cols="80" class="link"></textarea>';if(window.VINES_USER_PERMISSION){b+='<span class="force-width">Force width 550px <input class="jsForceWidth" type="checkbox" /></span>'}b+='<a class="jsOk btn">Insert</a>';b+="</div>";this.linkPanel=new Element("div",{html:b}).inject(document.body);this.inputField=this.linkPanel.getElement(".link").addEvent("keydown",function(c){if(c.key=="enter"){this.save(c)}}.bind(this));this.linkPanel.getElement(".jsOk").addEvent("click",this.save.bind(this));this.pad=new LaunchPad.Base({titleTextStr:"Add a Video",destroy_on_hide:false});this.pad.insert(this.linkPanel);this.pad.addEvent("hideComplete",function(){this.inputField.value=""}.bind(this))}this.inputField.select();this.forceWidth=window.VINES_USER_PERMISSION?this.linkPanel.getElement(".jsForceWidth"):false;if(a){this.inputField.value=a}},save:function(c){c.stop();var l=this.inputField.value,e="",j="",g="",k=false,f=false,i="",b="",d;if(!l){alert("Whoa, you need to enter something.");return}if(l.contains(MEDIA_URL)){match=l.match(/param name="whiskey-video-id" value="([0-9]+)"/);if(!match[1]){alert("Whoops, I don't think that is a valid Whiskey Media video url.");return}d=match[1];k=550;f=360;if(l.contains("www.giantbomb.com")){b="www.giantbomb.com"}else{if(l.contains("www.animevice.com")){b="www.animevice.com"}else{if(l.contains("www.comicvine.com")){b="www.comicvine.com"}else{if(l.contains("www.tested.com")){b="www.tested.com"}else{if(l.contains("www.screened.com")){b="www.screened.com"}else{if(l.contains(VINES_BASE_URL)){b=VINES_BASE_URL.replace("http://","").substring(-1);b="www."+b.split(":")[0]+".com"}}}}}}g="paramsURI=http%3A//"+b+"/video/params/"+d+"/?w=1";j="http://"+b+"/video/video.swf";i='<param name="whiskey-video-id" value="'+d+'" site="'+MEDIA_URL.replace("/media/","/video/")+'"></param>'}else{if(l.contains(VINES_BASE_URL)||l.contains("www.giantbomb.com")||l.contains("www.animevice.com")||l.contains("www.comicvine.com")||l.contains("www.tested.com")||l.contains("www.screened.com")){match=l.match(/([0-9]+)\/?([?#].*)*$/);if(!match[1]){alert("Whoops, I don't think that is a valid Whiskey Media video url.");return}d=match[1];k=550;f=360;if(l.contains("www.giantbomb.com")){b="www.giantbomb.com"}else{if(l.contains("www.animevice.com")){b="www.animevice.com"}else{if(l.contains("www.comicvine.com")){b="www.comicvine.com"}else{if(l.contains("www.tested.com")){b="www.tested.com"}else{if(l.contains("www.screened.com")){b="www.screened.com"}else{if(l.contains(VINES_BASE_URL)){b=VINES_BASE_URL.replace("http://","").substring(-1);b="www."+b.split(":")[0]+".com"}}}}}}g="paramsURI=http%3A//"+b+"/video/params/"+d+"/?w=1";j="http://"+b+"/video/video.swf";i='<param name="whiskey-video-id" value="'+d+'" site="'+MEDIA_URL.replace("/media/","/video/")+'"></param>'}else{if(l.contains("<object")||l.contains("<embed")){e=l}else{if(l.contains("youtube.com")||l.contains("youtu.be")){var a=l.match(/[A-Za-z0-9_\-]{11}/);this.insertIframe('<iframe width="560" height="315" src="http://www.youtube.com/embed/'+a[0]+'?wmode=opaque" frameborder="0" allowfullscreen></iframe>');return}else{if(l.contains("video.google.com")){j=l.replace(/videoplay\?docid=/,"googleplayer.swf?docid=");k=425;f=344}else{if(l.contains("www.gametrailers.com")){match=l.match(/([0-9]+)\/?([?#].*)*$/);if(!match[1]){alert("Whoops, I don't think that is a valid video url.");return}d=match[1];j="http://www.gametrailers.com/remote_wrap.php?mid="+d;k=480;f=392}else{alert("Sorry, I don't know this video service.");return}}}}}}if(e===""){e+='<object width="'+k+'" height="'+f+'">';e+='<param name="movie" value="'+j+'"></param>';e+='<param name="allowFullScreen" value="true"></param>';e+='<param name="allowscriptaccess" value="always"></param>';e+='<param name="wmode" value="transparent"></param>';e+=i;if(g){e+='<param name="flashvars" value="'+g+'"></param>'}e+='<embed src="'+j+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+k+'" height="'+f+'" wmode="transparent"';if(g){e+=' flashvars="'+g+'"'}e+="></embed>";e+="</object>"}e=e.replace(new RegExp("color=","gi"),"badVar=");e=e.replace(new RegExp("color1=","gi"),"badVar=");e=e.replace(new RegExp("color2=","gi"),"badVar=");e=e.replace(new RegExp("border=","gi"),"badVar=");this.insertVideo(e)},insertIframe:function(a){src=a.match(/src\s*=\s*['"](.[^'"]*)['"]/i)[1];width=(a.match(/width\s*=\s*['"](\d+)['"]/i)||[null,560])[1];height=(a.match(/height\s*=\s*['"](\d+)['"]/i)||[null,349])[1];this.insertVideo('<iframe src="'+src+'" width="'+width+'" height="'+height+'" frameborder="0" allowfullscreen></iframe>')},insertVideo:function(f){if(this.forceWidth&&this.forceWidth.checked===true){if(f.contains('width="')){var b=f.split('width="')[1].split('"')[0].toInt(),g=f.split('height="')[1].split('"')[0].toInt();if(b!=550){var e=((550*g)/b).toInt();if(f.contains('width="'+b+'px"')){f=f.replace(new RegExp('width="'+b+'px"',"gi"),'width="550px"');f=f.replace(new RegExp('height="'+g+'px"',"gi"),'height="'+e+'px"')}if(f.contains('width="'+b+'"')){f=f.replace(new RegExp('width="'+b+'"',"gi"),'width="550px"');f=f.replace(new RegExp('height="'+g+'"',"gi"),'height="'+e+'px"')}}}else{alert("The string provided did not contain a width attribute. :(")}}f='<p>&nbsp;</p><div class="js-item-cage" rel="video">'+f+"</div><span></span>";this.pad.hide();if(this.edit_mode){var d=this.toolbar.toolbar.retrieve("cage"),a=this.parchment.getSelectionObject(),c=this.parchment.getRangeObject(a);c.setStartAfter(d);c.setEndAfter(d);a.removeAllRanges();a.addRange(c);this.parchment.saveSelection();d.destroy()}this.parchment.insertCage(f)},buildToolbar:function(){this.toolbar=new Parchment.Utils.Toolbar(this.parchment.editor,{"class":"video",relay:'.js-item-cage[rel="video"]',display:{position:[{command:"moveup",text:"U"},{command:"movedown",text:"D"}],edit:[{command:"edit",text:"change"},{command:"delete",text:"delete"}]}});this.toolbar.addEvents({command:this.videoCommand.bind(this)})},videoCommand:function(a){switch(a){case"edit":this.openCodePanel(this.toolbar.toolbar.retrieve("cage").get("html"));break;case"delete":this.toolbar.toolbar.retrieve("cage").destroy();this.toolbar.toolbar.removeClass("on");break;case"moveup":case"movedown":this.reposition(this.toolbar.toolbar.retrieve("cage"),a);break;default:break}},reposition:function(d,b){var c=d.getFirst(),a=c.getFirst(),e;if(b=="moveup"&&d.getPrevious()){e=d.getPrevious();while(e.tagName.test(/^h\d$/i)&&e.getPrevious()){e=e.getPrevious()}if(e.tagName.test(/^h\d$/i)){while(e.tagName.test(/^h\d$/i)&&e.getNext()){e=e.getNext()}}d.inject(e,"before")}else{if(b=="movedown"&&d.getNext()){e=d.getNext();while(e.getNext()&&e.getNext().tagName.test(/^h\d$/i)){e=e.getNext()}d.inject(e,"after")}}this.toolbar.setPosition(d)}};
