/*
 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 Code licensed under the BSD License:
 http://developer.yahoo.com/yui/license.html
 version: 3.3.0
 build: 3167
 */
YUI.add('selection',function(Y){var textContent='textContent',INNER_HTML='innerHTML',FONT_FAMILY='fontFamily';if(Y.UA.ie){textContent='nodeValue';}
Y.Selection=function(domEvent){var sel,par,ieNode,nodes,rng,i;if(Y.config.win.getSelection){sel=Y.config.win.getSelection();}else if(Y.config.doc.selection){sel=Y.config.doc.selection.createRange();}
this._selection=sel;if(sel.pasteHTML){this.isCollapsed=(sel.compareEndPoints('StartToEnd',sel))?false:true;if(this.isCollapsed){this.anchorNode=this.focusNode=Y.one(sel.parentElement());if(domEvent){ieNode=Y.config.doc.elementFromPoint(domEvent.clientX,domEvent.clientY);}
if(!ieNode){par=sel.parentElement();nodes=par.childNodes;rng=sel.duplicate();for(i=0;i<nodes.length;i++){if(rng.inRange(sel)){if(!ieNode){ieNode=nodes[i];}}}}
this.ieNode=ieNode;if(ieNode){if(ieNode.nodeType!==3){if(ieNode.firstChild){ieNode=ieNode.firstChild;}
if(ieNode&&ieNode.tagName&&ieNode.tagName.toLowerCase()==='body'){if(ieNode.firstChild){ieNode=ieNode.firstChild;}}}
this.anchorNode=this.focusNode=Y.Selection.resolve(ieNode);this.anchorOffset=this.focusOffset=(this.anchorNode.nodeValue)?this.anchorNode.nodeValue.length:0;this.anchorTextNode=this.focusTextNode=Y.one(ieNode);}}else{if(sel.htmlText){var n=Y.Node.create(sel.htmlText);if(n.get('id')){var id=n.get('id');this.anchorNode=this.focusNode=Y.one('#'+id);}else{n=n.get('childNodes');this.anchorNode=this.focusNode=n.item(0);}}}}else{this.isCollapsed=sel.isCollapsed;this.anchorNode=Y.Selection.resolve(sel.anchorNode);this.focusNode=Y.Selection.resolve(sel.focusNode);this.anchorOffset=sel.anchorOffset;this.focusOffset=sel.focusOffset;this.anchorTextNode=Y.one(sel.anchorNode);this.focusTextNode=Y.one(sel.focusNode);}
if(Y.Lang.isString(sel.text)){this.text=sel.text;}else{if(sel.toString){this.text=sel.toString();}else{this.text='';}}};Y.Selection.filter=function(blocks){var startTime=(new Date()).getTime();var nodes=Y.all(Y.Selection.ALL),baseNodes=Y.all('strong,em'),doc=Y.config.doc,hrs=doc.getElementsByTagName('hr'),classNames={},cssString='',ls;var startTime1=(new Date()).getTime();nodes.each(function(n){var raw=Y.Node.getDOMNode(n);if(raw.style[FONT_FAMILY]){classNames['.'+n._yuid]=raw.style[FONT_FAMILY];n.addClass(n._yuid);raw.style[FONT_FAMILY]='inherit';raw.removeAttribute('face');if(raw.getAttribute('style')===''){raw.removeAttribute('style');}
if(raw.getAttribute('style')){if(raw.getAttribute('style').toLowerCase()==='font-family: '){raw.removeAttribute('style');}}}});var endTime1=(new Date()).getTime();Y.all('.hr').addClass('yui-skip').addClass('yui-non');Y.each(hrs,function(hr){var el=doc.createElement('div');el.className='hr yui-non yui-skip';el.setAttribute('readonly',true);el.setAttribute('contenteditable',false);if(hr.parentNode){hr.parentNode.replaceChild(el,hr);}
var s=el.style;s.border='1px solid #ccc';s.lineHeight='0';s.fontSize='0';s.marginTop='5px';s.marginBottom='5px';s.marginLeft='0px';s.marginRight='0px';s.padding='0';});Y.each(classNames,function(v,k){cssString+=k+' { font-family: '+v.replace(/"/gi,'')+'; }';});Y.StyleSheet(cssString,'editor');baseNodes.each(function(n,k){var t=n.get('tagName').toLowerCase(),newTag='i';if(t==='strong'){newTag='b';}
Y.Selection.prototype._swap(baseNodes.item(k),newTag);});ls=Y.all('ol,ul');ls.each(function(v,k){var lis=v.all('li');if(!lis.size()){v.remove();}});if(blocks){Y.Selection.filterBlocks();}
var endTime=(new Date()).getTime();};Y.Selection.filterBlocks=function(){var startTime=(new Date()).getTime();var childs=Y.config.doc.body.childNodes,i,node,wrapped=false,doit=true,sel,single,br,divs,spans,c,s;if(childs){for(i=0;i<childs.length;i++){node=Y.one(childs[i]);if(!node.test(Y.Selection.BLOCKS)){doit=true;if(childs[i].nodeType==3){c=childs[i][textContent].match(Y.Selection.REG_CHAR);s=childs[i][textContent].match(Y.Selection.REG_NON);if(c===null&&s){doit=false;}}
if(doit){if(!wrapped){wrapped=[];}
wrapped.push(childs[i]);}}else{wrapped=Y.Selection._wrapBlock(wrapped);}}
wrapped=Y.Selection._wrapBlock(wrapped);}
single=Y.all(Y.Selection.DEFAULT_BLOCK_TAG);if(single.size()===1){br=single.item(0).all('br');if(br.size()===1){if(!br.item(0).test('.yui-cursor')){br.item(0).remove();}
var html=single.item(0).get('innerHTML');if(html===''||html===' '){single.set('innerHTML',Y.Selection.CURSOR);sel=new Y.Selection();sel.focusCursor(true,true);}}}else{single.each(function(p){var html=p.get('innerHTML');if(html===''){p.remove();}});}
if(!Y.UA.ie){}
var endTime=(new Date()).getTime();};Y.Selection.REG_CHAR=/[a-zA-Z-0-9_]/gi;Y.Selection.REG_NON=/[\s\S|\n|\t]/gi;Y.Selection.REG_NOHTML=/<\S[^><]*>/g;Y.Selection._wrapBlock=function(wrapped){if(wrapped){var newChild=Y.Node.create('<'+Y.Selection.DEFAULT_BLOCK_TAG+'></'+Y.Selection.DEFAULT_BLOCK_TAG+'>'),firstChild=Y.one(wrapped[0]),i;for(i=1;i<wrapped.length;i++){newChild.append(wrapped[i]);}
firstChild.replace(newChild);newChild.prepend(firstChild);}
return false;};Y.Selection.unfilter=function(){var nodes=Y.all('body [class]'),html='',nons,ids,body=Y.one('body');nodes.each(function(n){if(n.hasClass(n._yuid)){n.setStyle(FONT_FAMILY,n.getStyle(FONT_FAMILY));n.removeClass(n._yuid);if(n.getAttribute('class')===''){n.removeAttribute('class');}}});nons=Y.all('.yui-non');nons.each(function(n){if(!n.hasClass('yui-skip')&&n.get('innerHTML')===''){n.remove();}else{n.removeClass('yui-non').removeClass('yui-skip');}});ids=Y.all('body [id]');ids.each(function(n){if(n.get('id').indexOf('yui_3_')===0){n.removeAttribute('id');n.removeAttribute('_yuid');}});if(body){html=body.get('innerHTML');}
Y.all('.hr').addClass('yui-skip').addClass('yui-non');return html;};Y.Selection.resolve=function(n){if(n&&n.nodeType===3){try{n=n.parentNode;}catch(re){n='body';}}
return Y.one(n);};Y.Selection.getText=function(node){var txt=node.get('innerHTML').replace(Y.Selection.REG_NOHTML,'');txt=txt.replace('<span><br></span>','').replace('<br>','');return txt;};Y.Selection.DEFAULT_BLOCK_TAG='p';Y.Selection.ALL='[style],font[face]';Y.Selection.BLOCKS='p,div,ul,ol,table,style';Y.Selection.TMP='yui-tmp';Y.Selection.DEFAULT_TAG='span';Y.Selection.CURID='yui-cursor';Y.Selection.CUR_WRAPID='yui-cursor-wrapper';Y.Selection.CURSOR='<span><br class="yui-cursor"></span>';Y.Selection.hasCursor=function(){var cur=Y.all('#'+Y.Selection.CUR_WRAPID);return cur.size();};Y.Selection.cleanCursor=function(){var cur,sel='br.yui-cursor';cur=Y.all(sel);if(cur.size()){cur.each(function(b){var c=b.get('parentNode.parentNode.childNodes'),html;if(c.size()>1){b.remove();}else{html=Y.Selection.getText(c.item(0));if(html!==''){b.remove();}}});}};Y.Selection.prototype={text:null,isCollapsed:null,anchorNode:null,anchorOffset:null,anchorTextNode:null,focusNode:null,focusOffset:null,focusTextNode:null,_selection:null,_wrap:function(n,tag){var tmp=Y.Node.create('<'+tag+'></'+tag+'>');tmp.set(INNER_HTML,n.get(INNER_HTML));n.set(INNER_HTML,'');n.append(tmp);return Y.Node.getDOMNode(tmp);},_swap:function(n,tag){var tmp=Y.Node.create('<'+tag+'></'+tag+'>');tmp.set(INNER_HTML,n.get(INNER_HTML));n.replace(tmp,n);return Y.Node.getDOMNode(tmp);},getSelected:function(){Y.Selection.filter();Y.config.doc.execCommand('fontname',null,Y.Selection.TMP);var nodes=Y.all(Y.Selection.ALL),items=[];nodes.each(function(n,k){if(n.getStyle(FONT_FAMILY)==Y.Selection.TMP){n.setStyle(FONT_FAMILY,'');n.removeAttribute('face');if(n.getAttribute('style')===''){n.removeAttribute('style');}
if(!n.test('body')){items.push(Y.Node.getDOMNode(nodes.item(k)));}}});return Y.all(items);},insertContent:function(html){return this.insertAtCursor(html,this.anchorTextNode,this.anchorOffset,true);},insertAtCursor:function(html,node,offset,collapse){var cur=Y.Node.create('<'+Y.Selection.DEFAULT_TAG+' class="yui-non"></'+Y.Selection.DEFAULT_TAG+'>'),inHTML,txt,txt2,newNode,range=this.createRange(),b;if(node&&node.test('body')){b=Y.Node.create('<span></span>');node.append(b);node=b;}
if(range.pasteHTML){newNode=Y.Node.create(html);try{range.pasteHTML('<span id="rte-insert"></span>');}catch(e){}
inHTML=Y.one('#rte-insert');if(inHTML){inHTML.set('id','');inHTML.replace(newNode);return newNode;}else{Y.on('available',function(){inHTML.set('id','');inHTML.replace(newNode);},'#rte-insert');}}else{if(offset>0){inHTML=node.get(textContent);txt=Y.one(Y.config.doc.createTextNode(inHTML.substr(0,offset)));txt2=Y.one(Y.config.doc.createTextNode(inHTML.substr(offset)));node.replace(txt,node);newNode=Y.Node.create(html);if(newNode.get('nodeType')===11){b=Y.Node.create('<span></span>');b.append(newNode);newNode=b;}
txt.insert(newNode,'after');if(txt2){newNode.insert(cur,'after');cur.insert(txt2,'after');this.selectNode(cur,collapse);}}else{if(node.get('nodeType')===3){node=node.get('parentNode');}
newNode=Y.Node.create(html);html=node.get('innerHTML').replace(/\n/gi,'');if(html===''||html==='<br>'){node.append(newNode);}else{if(newNode.get('parentNode')){node.insert(newNode,'before');}else{Y.one('body').prepend(newNode);}}
if(node.get('firstChild').test('br')){node.get('firstChild').remove();}}}
return newNode;},wrapContent:function(tag){tag=(tag)?tag:Y.Selection.DEFAULT_TAG;if(!this.isCollapsed){var items=this.getSelected(),changed=[],range,last,first,range2;items.each(function(n,k){var t=n.get('tagName').toLowerCase();if(t==='font'){changed.push(this._swap(items.item(k),tag));}else{changed.push(this._wrap(items.item(k),tag));}},this);range=this.createRange();first=changed[0];last=changed[changed.length-1];if(this._selection.removeAllRanges){range.setStart(changed[0],0);range.setEnd(last,last.childNodes.length);this._selection.removeAllRanges();this._selection.addRange(range);}else{range.moveToElementText(Y.Node.getDOMNode(first));range2=this.createRange();range2.moveToElementText(Y.Node.getDOMNode(last));range.setEndPoint('EndToEnd',range2);range.select();}
changed=Y.all(changed);return changed;}else{return Y.all([]);}},replace:function(se,re){var range=this.createRange(),node,txt,index,newNode;if(range.getBookmark){index=range.getBookmark();txt=this.anchorNode.get('innerHTML').replace(se,re);this.anchorNode.set('innerHTML',txt);range.moveToBookmark(index);newNode=Y.one(range.parentElement());}else{node=this.anchorTextNode;txt=node.get(textContent);index=txt.indexOf(se);txt=txt.replace(se,'');node.set(textContent,txt);newNode=this.insertAtCursor(re,node,index,true);}
return newNode;},remove:function(){this._selection.removeAllRanges();return this;},createRange:function(){if(Y.config.doc.selection){return Y.config.doc.selection.createRange();}else{return Y.config.doc.createRange();}},selectNode:function(node,collapse,end){if(!node){return;}
end=end||0;node=Y.Node.getDOMNode(node);var range=this.createRange();if(range.selectNode){range.selectNode(node);this._selection.removeAllRanges();this._selection.addRange(range);if(collapse){try{this._selection.collapse(node,end);}catch(err){this._selection.collapse(node,0);}}}else{if(node.nodeType===3){node=node.parentNode;}
try{range.moveToElementText(node);}catch(e){}
if(collapse){range.collapse(((end)?false:true));}
range.select();}
return this;},setCursor:function(){this.removeCursor(false);return this.insertContent(Y.Selection.CURSOR);},getCursor:function(){return Y.all('#'+Y.Selection.CURID);},removeCursor:function(keep){var cur=this.getCursor();if(cur){if(keep){cur.removeAttribute('id');cur.set('innerHTML','<br class="yui-cursor">');}else{cur.remove();}}
return cur;},focusCursor:function(collapse,end){if(collapse!==false){collapse=true;}
if(end!==false){end=true;}
var cur=this.removeCursor(true);if(cur){cur.each(function(c){this.selectNode(c,collapse,end);},this);}},toString:function(){return'Selection Object';}};},'3.3.0',{requires:['node'],skinnable:false});