/*------------------------------------------------------------------------
# Copyright (C) 2005-2012 WebxSolution Ltd. All Rights Reserved.
# @license - GPLv2.0
# Author: WebxSolution Ltd
# Websites:  http://www.webxsolution.com
# Terms of Use: An extension that is derived from the JoomlaCK editor will only be allowed under the following conditions: http://joomlackeditor.com/terms-of-use
# ------------------------------------------------------------------------*/ 
CKEDITOR.dialog.add("video",function(a){function c(a,b){var c=this.getValue();if(!c&&this.id=="id")c=g();a.setAttribute(this.id,c);if(!c)return;switch(this.id){case"poster":b.backgroundImage="url("+c+")";break;case"width":b.width=c+"px";break;case"height":b.height=c+"px";break}}function d(a,b,c){var d=this.id.match(/(\w+)(\d)/),e=d[1],f=parseInt(d[2],10);var g=c[f]||(c[f]={});g[e]=this.getValue()}function e(a){if(a)this.setValue(a.getAttribute(this.id));else{if(this.id=="id")this.setValue(g())}}function f(a,b){var c=this.id.match(/(\w+)(\d)/),d=c[1],e=parseInt(c[2],10);var f=b[e];if(!f)return;this.setValue(f[d])}function g(){var a=new Date;return"video"+a.getFullYear()+a.getMonth()+a.getDate()+a.getHours()+a.getMinutes()+a.getSeconds()}var b=a.lang.video;var h=function(){var a=this.previewImage;a.removeListener("load",h);a.removeListener("error",i);a.removeListener("abort",i);this.setValueOf("info","width",a.$.width);this.setValueOf("info","height",a.$.height)};var i=function(){var a=this.previewImage;a.removeListener("load",h);a.removeListener("error",i);a.removeListener("abort",i)};return{title:b.dialogTitle,minWidth:400,minHeight:200,onShow:function(){this.fakeImage=this.videoNode=null;this.previewImage=a.document.createElement("img");var b=this.getSelectedElement();if(b&&b.getAttribute("_cke_real_element_type")&&b.getAttribute("_cke_real_element_type")=="video"){this.fakeImage=b;var c=a.restoreRealElement(b),d=[],e=c.getElementsByTag("source","");if(e.count()==0)e=c.getElementsByTag("source","cke");for(var f=0,g=e.count();f<g;f++){var h=e.getItem(f);d.push({src:h.getAttribute("src"),type:h.getAttribute("type")})}this.videoNode=c;this.setupContent(c,d)}else this.setupContent(null,[])},onOk:function(){var c=null;if(!this.fakeImage){c=CKEDITOR.dom.element.createFromHtml("<cke:video></cke:video>",a.document);c.setAttributes({controls:"controls"})}else{c=this.videoNode}var d={},e=[];this.commitContent(c,d,e);var f="",g="",h=b.linkTemplate||"",i=b.fallbackTemplate||"";for(var j=0;j<e.length;j++){var k=e[j];if(!k||!k.src)continue;f+='<cke:source src="'+k.src+'" type="'+k.type+'" />';g+=h.replace("%src%",k.src).replace("%type%",k.type)}c.setHtml(f+i.replace("%links%",g));var l=a.createFakeElement(c,"cke_video","video",false);l.setStyles(d);if(this.fakeImage){l.replace(this.fakeImage);a.getSelection().selectElement(l)}else a.insertElement(l)},onHide:function(){if(this.previewImage){this.previewImage.removeListener("load",h);this.previewImage.removeListener("error",i);this.previewImage.removeListener("abort",i);this.previewImage.remove();this.previewImage=null}},contents:[{id:"info",elements:[{type:"hbox",widths:["","100px"],children:[{type:"text",id:"poster",label:b.poster,commit:c,setup:e,onChange:function(){var a=this.getDialog(),b=this.getValue();if(b.length>0){a=this.getDialog();var c=a.previewImage;c.on("load",h,a);c.on("error",i,a);c.on("abort",i,a);c.setAttribute("src",b)}}},{type:"button",id:"browse",hidden:"true",style:"display:inline-block;margin-top:10px;",filebrowser:{action:"Browse",target:"info:poster",url:a.config.filebrowserImageBrowseUrl||a.config.filebrowserBrowseUrl},label:a.lang.common.browseServer}]},{type:"hbox",widths:["33%","33%","33%"],children:[{type:"text",id:"width",label:a.lang.common.width,"default":400,validate:CKEDITOR.dialog.validate.notEmpty(b.widthRequired),commit:c,setup:e},{type:"text",id:"height",label:a.lang.common.height,"default":300,validate:CKEDITOR.dialog.validate.notEmpty(b.heightRequired),commit:c,setup:e},{type:"text",id:"id",label:"Id",commit:c,setup:e}]},{type:"hbox",widths:["","100px","75px"],children:[{type:"text",id:"src0",label:b.sourceVideo,commit:d,setup:f},{type:"button",id:"browse",hidden:"true",style:"display:inline-block;margin-top:10px;",filebrowser:{action:"Browse",target:"info:src0",url:a.config.filebrowserVideoBrowseUrl||a.config.filebrowserBrowseUrl},label:a.lang.common.browseServer},{id:"type0",label:b.sourceType,type:"select","default":"video/mp4",items:[["MP4","video/mp4"],["OGG","video/ogg"],["WebM","video/webm"]],commit:d,setup:f}]},{type:"hbox",widths:["","100px","75px"],children:[{type:"text",id:"src1",label:b.sourceVideo,commit:d,setup:f},{type:"button",id:"browse",hidden:"true",style:"display:inline-block;margin-top:10px;",filebrowser:{action:"Browse",target:"info:src1",url:a.config.filebrowserVideoBrowseUrl||a.config.filebrowserBrowseUrl},label:a.lang.common.browseServer},{id:"type1",label:b.sourceType,type:"select","default":"video/ogg",items:[["MP4","video/mp4"],["OGG","video/ogg"],["WebM","video/webm"]],commit:d,setup:f}]}]}]}})