if(window.IXMO===undefined){throw Error('Inline XHTML Manipulation & Optimisation library (ixmo.js) is required.');}
if(window.jQuery===undefined){throw Error('JQuery JavaScript framework (jquery.js) is required.');}
if(window.SELECT===undefined){throw Error('Cross-browser content selection library (select.js) is required.');}
if(!Array.indexOf){Array.prototype.indexOf=function(obj){for(var i=0;i<this.length;i++){if(this[i]==obj){return i;}}
return-1;}}
var EDIT={};EDIT.id_counter=0;EDIT.Settings=function(){if(EDIT._settings_instance!=null){return EDIT._settings_instance;}
EDIT._settings_instance=this;this._selected_list=null;}
EDIT.Settings.prototype.request_id=function(){EDIT.id_counter++;return EDIT.id_counter;}
EDIT.Settings.prototype.set_selected_list=function(list){this._selected_list=list;}
EDIT.Settings.prototype.get_selected_list=function(list){return this._selected_list;}
EDIT.List=function(node){this._settings=new EDIT.Settings();this.node=node;this._node_list=new Array();this._selected_node=null;this._event_map=new Object();}
EDIT.List.prototype.bind=function(event,data,func){this._event_map[event]=[data,func];}
EDIT.List.prototype.trigger=function(event,data){if(this._event_map[event]){data.data=this._event_map[event][0];this._event_map[event][1](data);}}
EDIT.List.prototype.set_selected_node=function(node){if(node&&this._selected_node!=node){if(this._selected_node){this._selected_node.unselect();}
this.trigger('node_change',{'prev_node':this._selected_node,'node':node});this._selected_node=node;}
if(node){this._settings.set_selected_list(this);}else{this._settings.set_selected_list(null);}}
EDIT.List.prototype.get_selected_node=function(node){return this._selected_node;}
EDIT.List.prototype.add_text=function(node_type,content,pre_node){var text_node=new EDIT.Text(this,node_type,content);if(pre_node){this._splice(this._node_list.indexOf(pre_node)+1,0,text_node);}else{this._push(text_node);}
return text_node;}
EDIT.List.prototype.add_image=function(url,alt,long_desc,pre_node){var image_node=new EDIT.Image(this,url,alt,long_desc);if(pre_node){this._splice(this._node_list.indexOf(pre_node)+1,0,image_node);}else{this._push(image_node);}
return image_node;}
EDIT.List.prototype.move_node=function(node,pre_node){this._node_list.splice(this._node_list.indexOf(node),1);this._node_list.splice(this._node_list.indexOf(pre_node)+1,0,node);$(pre_node.node).after($(node.node));}
EDIT.List.prototype.remove_node=function(node){if(this.get_selected_node()==node){this._selected_node=false;}
node.remove();this._node_list.splice(this._node_list.indexOf(node),1);}
EDIT.List.prototype.get_previous_node=function(editable_node,node_type){var position=this._node_list.indexOf(editable_node);while(position>0){var node=this._node_list[position-1];if(node_type){if(node.is_a==node_type){return node;}}else{return node;}
position--;}}
EDIT.List.prototype.get_next_node=function(editable_node,node_type){var position=this._node_list.indexOf(editable_node);while(position<this._node_list.length-1){var node=this._node_list[position+1];if(node_type){if(node.is_a==node_type){return node;}}else{return node;}
position++;}}
EDIT.List.prototype._push=function(editable_node){var previous_node=null;if(this._node_list.length>0){previous_node=this._node_list[this._node_list.length-1];}
this._node_list.push(editable_node);editable_node.render();}
EDIT.List.prototype._pop=function(){this._node_list.pop();editable_node.remove();}
EDIT.List.prototype._splice=function(position,remove_count,editable_node){this._node_list.splice(position,remove_count,editable_node);editable_node.render();}
EDIT.Node=function(){}
EDIT.Node.prototype.remove=function(){$('#'+this.element_id).unbind();$('#'+this.element_id).remove();}
EDIT.Text=function(owner,node_type,content){this._settings=new EDIT.Settings();this.owner=owner;if(EDIT.Text.SUPPORTED_TAG_LIST.indexOf(node_type.toLowerCase())==-1){throw Error('The <'+node_type+'> is not supported for text editing.');}
this.remote_id=this._settings.request_id();this.element_id='editable-node-'+this.remote_id;this._node_type=node_type;this.node=null;if($('#'+this.element_id).length>0){this.node=$('#'+this.element_id)[0];}
if(this.node&&!content){content=$(this.node).html().replace(/^\s+/g,'').replace(/\s+$/g,'')}
content=content||EDIT.Text.DEFAULT_TEXT;var parser=new IXMO.Parser();this.soup=parser.parse(content);this.selection=new SELECT.Selection(0,0);if(content==EDIT.Text.DEFAULT_TEXT){this.selection.end=EDIT.Text.DEFAULT_TEXT.length;}
this.selected=false;this._style='';this._align='left';}
EDIT.Text.prototype=new EDIT.Node;EDIT.Text.prototype.is_a='text';EDIT.Text.SUPPORTED_TAG_LIST='h1,h2,h3,h4,h5,h6,p'.split(',');EDIT.Text.SUPPORTED_ALIGN_MAP={'left':'text-left','center':'text-center','right':'text-right','justify':'text-justify'};EDIT.Text.SUPPORTED_STYLE_MAP={};EDIT.Text.DEFAULT_TEXT='Insert text...';EDIT.Text.add_style=function(node_type,style_name,class_name){if(!EDIT.Text.SUPPORTED_STYLE_MAP[node_type]){EDIT.Text.SUPPORTED_STYLE_MAP[node_type]=new Array();}
EDIT.Text.SUPPORTED_STYLE_MAP[node_type].push([style_name,class_name]);}
EDIT.Text.get_style_list=function(node_type){return EDIT.Text.SUPPORTED_STYLE_MAP[node_type];}
EDIT.Text.prototype.render=function(){this.selected=false;var previous_node=this.owner.get_previous_node(this);this.remove();var node_html='<'+this._node_type+' id="'+this.element_id+'"></'+this._node_type+'>';if(previous_node){$(previous_node.node).after(node_html);}else{$(this.owner.node).prepend(node_html)}
this.node=$('#'+this.element_id)[0];$(this.node).html(this.soup.render(true));$(this.node).addClass('edit-text');$(this.node).addClass(EDIT.Text.SUPPORTED_ALIGN_MAP[this._align]);if(this._style){$(this.node).addClass(this._style);}
$(this.node).bind('mouseover',this,function(ev){if(!ev.data.selected){$(this).addClass('over');}});$(this.node).bind('mouseout',this,function(ev){$(this).removeClass('over');});$(this.node).bind('change',this,function(ev){});$(this.node).bind('mouseup',this,function(ev){ev.data.selection=SELECT.get(this)[0];ev.data.select();ev.data.owner.trigger('click',{'node':ev.data});});$(this.node).bind('blur',this,function(ev){ev.data.unselect();});$(this.node).bind('keydown',this,function(ev){var return_value=true;ev.data.selection=SELECT.get(this)[0];switch(ev.which){case 8:if(ev.data.selection.start==0&&ev.data.selection.is_collapsed()){var previous_node=ev.data.owner.get_previous_node(ev.data,'text');if(previous_node){var insert_position=previous_node.soup.get_length();previous_node.soup.paste(ev.data.soup,insert_position);previous_node.render();previous_node.selection.start=insert_position;previous_node.selection.collapse();previous_node.select();ev.data.remove();return_value=false;}}
break;case 9:if(ev.shiftKey){var previous_node=ev.data.owner.get_previous_node(ev.data,'text');if(previous_node){ev.data.unselect();previous_node.select();return_value=false;}}else{var next_node=ev.data.owner.get_next_node(ev.data,'text');if(next_node){ev.data.unselect();next_node.select();return_value=false;}}
break;case 13:ev.data.update_content();if(!ev.data.selection.is_collapsed()){ev.data.soup.erase(ev.data.selection.as_array());ev.data.selection.collapse()}
var text_list=ev.data.soup.split(ev.data.selection.start);ev.data.soup=text_list[0];if(ev.data.soup.get_length()==0){var parser=new IXMO.Parser();ev.data.soup=parser.parse(EDIT.Text.DEFAULT_TEXT);}
ev.data.render();var new_text=ev.data.owner.add_text('p',text_list[1].render(true),ev.data);new_text.select();return_value=false;break;case 37:if(ev.data.selection.start==0&&ev.data.selection.is_collapsed()){var previous_node=ev.data.owner.get_previous_node(ev.data,'text');if(previous_node){ev.data.unselect();previous_node.selection.start=previous_node.soup.get_length();previous_node.selection.collapse();previous_node.select();return_value=false;}}
break;case 38:if(ev.data.selection.start==0&&ev.data.selection.is_collapsed()){var previous_node=ev.data.owner.get_previous_node(ev.data,'text');if(previous_node){ev.data.unselect();previous_node.selection.start=previous_node.soup.get_length();previous_node.selection.collapse();previous_node.select();return_value=false;}}else{ev.data.selection.start=0;ev.data.selection.collapse();SELECT.set(ev.data.node,ev.data.selection,true);}
break;case 39:ev.data.update_content();if(ev.data.selection.start==ev.data.soup.get_length()&&ev.data.selection.is_collapsed()){var next_node=ev.data.owner.get_next_node(ev.data,'text');if(next_node){ev.data.unselect();next_node.selection.start=0;next_node.selection.collapse();next_node.select();return_value=false;}}
break;case 40:ev.data.update_content();if(ev.data.selection.start==ev.data.soup.get_length()&&ev.data.selection.is_collapsed()){var next_node=ev.data.owner.get_next_node(ev.data,'text');if(next_node){ev.data.unselect();next_node.selection.start=0;next_node.selection.collapse();next_node.select();}}else{ev.data.selection.start=ev.data.soup.get_length();ev.data.selection.collapse();SELECT.set(ev.data.node,ev.data.selection,true);}
return_value=false;break;}
return return_value;});$(this.node).bind('keyup',this,function(ev){ev.data.selection=SELECT.get(this)[0];ev.data.update_content();ev.data.owner.trigger('content_change',{'node':ev.data});});$(this.node).droppable({accept:'.edit-image'});$(this.node).bind('dropover',this,function(ev){$(this).addClass('over');});$(this.node).bind('dropout',this,function(ev){$(this).removeClass('over');});$(this.node).bind('drop',this,function(ev){$(this).removeClass('over');var image_node=ev.data.owner.get_selected_node();if(image_node){ev.data.owner.move_node(image_node,ev.data);image_node.select();}});}
EDIT.Text.prototype.select=function(){if(!this.selected){$(this.node).attr('contentEditable','true');$(this.node).addClass('edit');$(this.node).removeClass('over');$(this.node).focus();if(!this.selection){this.selection=SELECT.get(this.node)[0];}
SELECT.set(this.node,this.selection,true);this.selected=true;this.owner.set_selected_node(this);}}
EDIT.Text.prototype.unselect=function(){if(this.selected){this.selected=false;$(this.node).attr('contentEditable','false');$(this.node).removeClass('edit');this.update_content();this.owner.set_selected_node(null);if(this.soup.get_length()==0){this.owner.remove_node(this);}
this.render();}}
EDIT.Text.prototype.update_content=function(){var content=$(this.node).html().replace(/^\s+/g,'').replace(/\s+$/g,'');var parser=new IXMO.Parser();this.soup=parser.parse(content);}
EDIT.Text.prototype.set_type=function(node_type){if(node_type!=this._node_type){if(EDIT.Text.SUPPORTED_TAG_LIST.indexOf(node_type.toLowerCase())==-1){throw Error('The <'+node_type+'> is not supported for text editing.');}
this.update_content();this._node_type=node_type;this.render();}}
EDIT.Text.prototype.get_type=function(){return this._node_type;}
EDIT.Text.prototype.set_align=function(align){if(align!=this._align){if(!EDIT.Text.SUPPORTED_ALIGN_MAP[align]){throw Error('The text node can not be aligned \''+align+'\'.');}
$(this.node).removeClass(EDIT.Text.SUPPORTED_ALIGN_MAP[this._align]);$(this.node).addClass(EDIT.Text.SUPPORTED_ALIGN_MAP[align]);this._align=align;}}
EDIT.Text.prototype.get_align=function(){return this._align;}
EDIT.Text.prototype.set_style=function(style){if(style!=this._style){$(this.node).removeClass(this._style);$(this.node).addClass(style);this._style=style;}}
EDIT.Text.prototype.get_style=function(){return this._style;}
EDIT.Image=function(owner,url,alt,long_desc){this._settings=new EDIT.Settings();this.mode='none';this.width=261;this.height=400;this.owner=owner;this.remote_id=this._settings.request_id();this.element_id='editable-node-'+this.remote_id;this.url=url;this.alt=alt||'';this.long_desc=long_desc||'';this.selected=false;this._style='';this._align='center';}
EDIT.Image.prototype=new EDIT.Node;EDIT.Image.prototype.is_a='image';EDIT.Image.SUPPORTED_ALIGN_MAP={'left':'image-left','center':'image-center','right':'image-right'};EDIT.Image.prototype.render=function(){this.selected=false;this.mode='none';var previous_node=this.owner.get_previous_node(this);this.remove();var node_html='<a id="'+this.element_id+'"><img src="'+this.url+'" alt="'+this.alt+'" /></a>';if(previous_node){$(previous_node.node).after(node_html);}else{$(this.owner.node).prepend(node_html)}
this.node=$('#'+this.element_id)[0];$(this.node).css({'height':this.height,'width':this.width});$(this.node).addClass('edit-image');$(this.node).addClass(EDIT.Image.SUPPORTED_ALIGN_MAP[this._align]);if(this._style){$(this.node).addClass(this._style);}
$(this.node).bind('mouseover',this,function(ev){if(!ev.data.selected){$(this).addClass('over');}});$(this.node).bind('mouseout',this,function(ev){$(this).removeClass('over');});$(this.node).bind('mouseup',this,function(ev){ev.data.select();ev.data.owner.trigger('click',{'node':ev.data});});}
EDIT.Image.prototype.select=function(){if(!this.selected){$(this.node).addClass('edit');$(this.node).removeClass('over');this.selected=true;this.owner.set_selected_node(this);$(this.node).focus();this.do_move();}}
EDIT.Image.prototype.do_move=function(){$(this.node).resizable('destroy');$(this.node).draggable({revert:true,revertDuration:0});this.mode='move';}
EDIT.Image.prototype.do_resize=function(){$(this.node).draggable('destroy');$(this.node).resizable({aspectRatio:261/400});$(this.node).bind('resizestop',this,function(ev,ui){ev.data.width=parseInt(ui.size.width);ev.data.height=parseInt(ui.size.height);});this.mode='resize';}
EDIT.Image.prototype.unselect=function(){if(this.selected){this.selected=false;$(this.node).removeClass('edit');$(this.node).removeClass('ui-widget-content');this.owner.set_selected_node(null);this.render();}}
EDIT.Image.prototype.set_align=function(align){if(align!=this._align){if(!EDIT.Image.SUPPORTED_ALIGN_MAP[align]){throw Error('The text node can not be aligned \''+align+'\'.');}
$(this.node).removeClass(EDIT.Image.SUPPORTED_ALIGN_MAP[this._align]);$(this.node).addClass(EDIT.Image.SUPPORTED_ALIGN_MAP[align]);this._align=align;}}
EDIT.Image.prototype.get_align=function(){return this._align;}