/* NicEdit - Micro Inline WYSIWYG
 * Copyright 2007 Brian Kirchoff
 *
 * NicEdit is distributed under the terms of the MIT license
 * For more information visit http://nicedit.com/
 * Do not remove this copyright message
 */
 
 var nicEditorConfig = {
	buttons : {
		'save' : {name : 'Kaydet', type : 'nicEditorSaveButton', tile : 1},
		'undo' : {name : 'Geri Al', command : 'undo', noActive : true, tile : 23},
		'redo' : {name : 'Yinele', command : 'redo', noActive : true, tile : 24},
		'bold' : {name : 'Kalın', command : 'Bold', tags : ['B','STRONG'], css : {'font-weight' : 'bold'}, tile : 2},
		'italic' : {name : 'Eğik', command : 'Italic', tags : ['EM','I'], css : {'font-style' : 'italic'}, tile : 3},
		'underline' : {name : 'Altı Çizili', command : 'Underline', tags : ['U'], css : {'text-decoration' : 'underline'}, tile : 4},
		'left' : {name : 'Sola Yasla', command : 'justifyleft', noActive : true, tile : 8},
		'center' : {name : 'Ortala', command : 'justifycenter', noActive : true, tile : 9},
		'right' : {name : 'Sağa Yasla', command : 'justifyright', noActive : true, tile : 10},
		'ol' : {name : 'Numaralı Liste Ekle', command : 'insertorderedlist', tags : ['OL'], tile : 12},
		'ul' : 	{name : 'Liste Ekle', command : 'insertunorderedlist', tags : ['UL'], tile : 13},
		'fontSize' : {name : 'Yazı Tipi Boyutu', type : 'nicEditorFontSizeSelect', command : 'fontsize'},
		'fontFamily' : {name : 'Yazı Tipi Ailesi', type : 'nicEditorFontFamilySelect', command : 'fontname'},
		'fontFormat' : {name : 'Yazı Tipi Biçimi', type : 'nicEditorFontFormatSelect', command : 'formatBlock'},
		'subscript' : {name : 'Alt Yazı', command : 'subscript', tags : ['SUB'], tile : 6, disabled : true},
		'superscript' : {name : 'Üst Yazı', command : 'superscript', tags : ['SUP'], tile : 5, disabled : true},
		'strikeThrough' : {name : 'Üstü Çizili', command : 'strikeThrough', css : {'text-decoration' : 'line-through'}, tile : 7, disabled : true},
		'indent' : {name : 'Girdi', command : 'indent', noActive : true, tile : 20},
		'unindent' : {name : 'Girdi Sil', command : 'outdent', noActive : true, tile : 21},
		'hr' : {name : 'Yatay Ayraç', command : 'insertHorizontalRule', noActive : true, tile : 22},
		'color' : {name : 'Renk Değiştir', type : 'nicEditorColorButton', tile : 25},
		'image' : {name : 'Resim Ekle', type : 'nicEditorImageButton', tile : 14},
		'html' : {name : 'HTML Düzenle', type : 'nicEditorHTMLButton', tile : 16},
		'link' : {name : 'Bağlantı Ekle', type : 'nicEditorLinkButton', tile : 17}
	},
	iconsPath : 'js/nicEditorIcons.gif',
	fullPanel : false,
	onSubmit : null,
	buttonList : ['undo','redo','bold','italic','underline','left','center','right','ol','ul','indent','unindent','fontSize','fontFamily','fontFormat','image','link'],
	toolTipOn : false,
	toolTipText : 'Düzenlemek için tıkla'
};


function bkClass() { }
bkClass.prototype.construct = function() {};
bkClass.extend = function(def) {
  var classDef = function() {
      if (arguments[0] !== bkClass) { this.construct.apply(this, arguments); }
  };
  var proto = new this(bkClass);
  var superClass = this.prototype;
  for (var n in def) {
      var item = def[n];                      
      if (item instanceof Function) item.$ = superClass;
      proto[n] = item;
  }
  classDef.prototype = proto;
  classDef.extend = this.extend;      
  return classDef;
};

Function.prototype.closure = function() {
  var __method = this, args = bkLib.toArray(arguments), obj = args.shift();
  return function() { return __method.apply(obj,args.concat(bkLib.toArray(arguments))); };
}

Function.prototype.closureListener = function() {
  var __method = this, args = bkLib.toArray(arguments), object = args.shift(); 
  return function(e) { 
  		e = e || window.event;
  		if(e.target) { var target = e.target; } else { var target =  e.srcElement };
  		return __method.apply(object, [e,target].concat(args) ); 
	};
}

function $N(itm) {
	return document.getElementById(itm);	
}

var bkLib = {
	getStyle : function( element, cssRule, d ) {
		var doc = (!d) ? document.defaultView : d; 
		return (doc && doc.getComputedStyle) ? doc.getComputedStyle( element, '' ).getPropertyValue(cssRule) : element.currentStyle[ cssRule ];
	},
	
	setStyle : function(element, st) {
		var elmStyle = element.style;
		for(itm in st) {
			switch(itm) {
				case 'float':
					elmStyle['cssFloat'] = elmStyle['styleFloat'] = st[itm];
					break;
				case 'opacity':
					elmStyle.opacity = st[itm];
					elmStyle.filter = "alpha(opacity=" + Math.round(st[itm]*100) + ")"; 
					break;
				case 'className':
					element.className = st[itm];
					break;
				default:
					if(document.compatMode || itm != "cursor") { // Nasty Workaround for IE 5.5
						elmStyle[itm] = st[itm];
					}		
			}
		}
	},
	
	cancelEvent : function(e) {
		e = e || window.event;
		if(e.preventDefault && e.stopPropagation) {
			e.preventDefault();
			e.stopPropagation();
		}
		return false;
	},
	
	domLoad : [],
	domLoaded : function() {
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		for (i = 0;i < bkLib.domLoad.length;i++) bkLib.domLoad[i]();
	},
	onDomLoaded : function(fireThis) {
		this.domLoad.push(fireThis);
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", bkLib.domLoaded, null);
		}
		/*@cc_on @*/
		/*@if (@_win32)
			var proto = "src='javascript:void(0)'";
			if (location.protocol == "https:") proto = "src=//0";
			document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
			var script = document.getElementById("__ie_onload");
			script.onreadystatechange = function() {
			    if (this.readyState == "complete") {
			        bkLib.domLoaded();
			    }
			};
		/*@end @*/
	    window.onload = bkLib.domLoaded;
	},
	
	addEvent : function(obj, type, fn) {
		(obj.addEventListener) ? obj.addEventListener( type, fn, false ) : obj.attachEvent("on"+type, fn);	
	},
	
	elmPos : function(obj) {
		var curleft = curtop = 0;
		var objHeight = obj.offsetHeight;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop+objHeight];
	},
	
	mousePos : function(e) {
		return [e.pageX||e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,e.pageY||e.clientY+document.body.scrollTop+document.documentElement.scrollTop];
	},
	
	getElementsByClassName : function(classname) {
		if(document.getElementsByClassName) {
			return document.getElementsByClassName(classname);
		}
	    var a = [];
	    var re = new RegExp('\\b' + classname + '\\b');
	    var els = document.getElementsByTagName("*");
	    for(var i=0,j=els.length; i<j; i++) {
	        if(re.test(els[i].className))a.push(els[i]);
	    }
	    return a;
	},
	
	inArray : function(arr,item) {
	    for (i=0; i < arr.length; i++) {
		    if (arr[i] === item) {
		        return true;
		    }
	    }
	    return false;
	},
	
	toArray : function(iterable) {
		var length = iterable.length, results = new Array(length);
    	while (length--) results[length] = iterable[length];
    	return results;	
	},
	
	unselectAble : function(element) {
		if(element.setAttribute && element.contentEditable != true && element.nodeName != 'input' && element.nodeName != 'textarea') {
			element.setAttribute('unselectable','on');
		}
	
		for(var i=0;i<element.childNodes.length;i++) {
			bkLib.unselectAble(element.childNodes[i]);
		}
	},
	
	ajaxRequest : function(requestMethod,ajaxURL,ajaxData) {
		var ajaxRequest = (window.XMLHttpRequest) ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		ajaxRequest.open((!requestMethod) ? 'GET' : requestMethod, ajaxURL, true);
		if(requestMethod == "POST") {
			ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		}
		ajaxRequest.send(ajaxData);
	}
};

var bkEvent = {
	
	addEvent : function(evType, evFunc) {
		if(evFunc) {
			this.eventList = this.eventList || {};
			this.eventList[evType] = this.eventList[evType] || [];
			this.eventList[evType].push(evFunc);
		}
	},
	
	fireEvent : function(evType,evArgs) {
		if(this.eventList && this.eventList[evType]) {
			for(var i=0;i<this.eventList[evType].length;i++) {
				this.eventList[evType][i](evArgs);
			}
		}
	}	
};


var nicEditors = {
	allTextAreas : function(nicOptions) {
		var textareas = document.getElementsByTagName("textarea");
		var editors = new Array();
		for(var i=0;i<textareas.length;i++) {
			editors.push(new nicEditor(nicOptions).panelInstance(textareas[i]));
		}
		return editors;
	}
};

var nicEditor = bkClass.extend({
	nicInstances : [],
	nicPanel : null,
	selectedInstance : null,
	
	construct : function(o) {
		var opt = bkClass.extend(nicEditorConfig);
		opt = (o) ? opt.extend(o) : opt;
		this.options = new opt();
		
		bkLib.addEvent(document.body,'mousedown', this.selectCheck.closureListener(this) );
	},
	
	selectCheck : function(e,t) {
		var found = false;
		do{
			if(t.className && t.className.indexOf('nicEdit') != -1) {
				return false;
			}
		} while(t = t.parentNode);
		this.fireEvent('noInstanceSelect',t);
		this.selectedInstance = null;
		return false;
	},
	
	panelInstance : function(e) {
		if(typeof(e) == "string") { e = $N(e); }
		var panelElm = document.createElement('DIV');
		e.parentNode.insertBefore(panelElm,e);
		panelWidth = e.width || e.clientWidth;
		panelElm.style.width = panelWidth+'px';
		this.setPanel(panelElm);
		return this.addInstance(e);	
	},
	
	findInstance : function(e) {
		if(typeof(e) == "string") { e = $N(e); }
		for(i=0;i<this.nicInstances.length;i++) {
			if(e == this.nicInstances[i].elm) {
				return this.nicInstances[i];
			}
		}
	},
	
	addInstance : function(e,o) {
		if(typeof(e) == "string") { e = $N(e); }
		if(e.contentEditable || !!window.opera) {
			this.nicInstances.push(new nicEditorInstance(e,o,this));
		} else {
			this.nicInstances.push(new nicEditorIFrameInstance(e,o,this));
		}
		return this;
	},
	
	instancesByClassName : function(className, o) {
		var editorAreas = bkLib.getElementsByClassName(className);
		var editors = new Array();
		for(var i=0;i<editorAreas.length;i++) {
			this.addInstance(editorAreas[i],o);
		}
	},
	
	nicCommand : function(cmd,args) {	
		if(this.selectedInstance) {
			this.selectedInstance.nicCommand(cmd,args);
		}
	},
	
	setPanel : function(e) {
		if(typeof(e) == "string") { e = $N(e); }
		this.nicPanel = new nicEditorPanel(e,this.options,this);
		return this;
	}
	
		
});
nicEditor = nicEditor.extend(bkEvent);

var nicEditorInstance = bkClass.extend({
	nicEditor : null,
	elm : null,
	initalContent : null,
	isSelected : false,
	
	construct : function(e,options,nicEditor) {
		this.nicEditor = nicEditor;
		this.elm = e;
		this.options = options || {};

		newX = e.width || e.clientWidth;
		newY = e.height || e.clientHeight;
		if(e.nodeName == "TEXTAREA") {
			e.style.display = 'none';
				
			var editorContain = document.createElement('DIV');
			var editorElm = document.createElement('DIV');
			editorElm.innerHTML = e.value;
			
			bkLib.setStyle(editorContain,{width: (newX)+'px', border : '1px solid #ccc', borderTop : 0, overflow : 'hidden'});
			bkLib.setStyle(editorElm,{width : (newX-8)+'px', margin: '4px', minHeight : (newY-8)+'px'});
			
			var nav = navigator.appVersion;
			if(nav.indexOf("MSIE") != -1 && !((typeof document.body.style.maxHeight != "undefined") && document.compatMode == "CSS1Compat")) { // Set the height on all IE except IE7+ in standards mode
					editorElm.style.height = newY+'px';
			}
			
			editorContain.appendChild(editorElm);
			e.parentNode.insertBefore(editorContain,e);
			this.elm = editorElm;
			this.copyElm = e;
			
			var formElements = document.getElementsByTagName("FORM");
			for(var i=0;i<formElements.length;i++) {
				bkLib.addEvent(formElements[i],'submit',this.saveContent.closure(this));
			}
		}
		this.initialHeight = newY-8;
		this.nicEditor.addEvent('noInstanceSelect',this.unselected.closure(this));
		
		this.init();
		this.unselected();
	},
	
	init : function() {
		this.elm.setAttribute('contentEditable','true');	
		this.initialContent = this.getContent();
		if(this.initialContent == "") {
			this.setContent('<br />');
		}
		this.elm.className = 'nicEdit';
		bkLib.addEvent(this.elm,'mousedown',this.mouseDown.closureListener(this));
	},
	
	getSel : function() {
		return (window.getSelection) ? window.getSelection() : document.selection;	
	},
	
	getRng : function() {
		var s = this.getSel();
		if(!s) { return null; }
		return (s.rangeCount > 0) ? s.getRangeAt(0) : s.createRange();
	},
	
	selRng : function(rng,s) {
		if(window.getSelection) {
			s.removeAllRanges();
			s.addRange(rng);
		} else {
			rng.select();
		}
	},
	
	saveRng : function() {
		this.savedRange = this.getRng();
		this.savedSel = this.getSel();
	},
	
	restoreRng : function() {
		if(this.savedRange) {
			this.selRng(this.savedRange,this.savedSel);
		}
	},
	
	mouseDown : function(e,t) {
		if(this.nicEditor.selectedInstance != this) {
			this.nicEditor.fireEvent('noInstanceSelect',t);
		}	
		this.nicEditor.selectedInstance = this;	
		this.nicEditor.fireEvent('instanceSelect',t);
		this.selected();
	},
	
	selected : function() {
		this.isSelected = true;
		bkLib.setStyle(this.elm,{className : 'nicEdit nicEdit-instanceSelect'});	
		if(this.toolTip) {
			this.toolTip.remove();
			this.toolTip = null;
		}
	},
	
	unselected : function() {
		this.isSelected = false;
		bkLib.setStyle(this.elm,{className : 'nicEdit nicEdit-noInstanceSelect'});	
		
		if(!this.toolTip && (this.nicEditor.options.toolTipOn || this.options.toolTipOn)) {
			this.addTooltip();
			this.toolTip.setContent((this.options.toolTipText) ? this.options.toolTipText : this.nicEditor.options.toolTipText);
		}
	},
	
	addTooltip : function() {
		this.toolTip = new nicEditorTooltip(this.elm,this.nicEditor,this.getTipStyle());
	},
	
	saveContent : function() {
		if(this.copyElm) {
			this.copyElm.value = this.getContent();
		}	
	},
	
	getContent : function() {
		return this.elm.innerHTML;
	},
	
	setContent : function(newContent) {
			this.elm.innerHTML = newContent;	
	},
	
	getTipStyle : function() {
		return {padding : '3px', backgroundColor : '#ffffc9', fontSize : '12px', border : '1px solid #ccc', className : 'nicEdit-instanceTip'}
	},
	
	getDoc : function() {
		return document.defaultView;
	},
	
	nicCommand : function(cmd,args) {
		document.execCommand(cmd,false,args);
	}		
});

var nicEditorIFrameInstance = nicEditorInstance.extend({
	elm : null,
	
	init : function() {
		this.elmFrame = document.createElement('iframe');
        this.elmFrame.setAttribute('frameBorder','0');
        this.elmFrame.setAttribute('allowTransparency','true');
        this.elmFrame.setAttribute('scrolling','no');

       	bkLib.setStyle(this.elmFrame,{width: '100%', overflow : 'hidden', className : 'nicEdit-frame'});
       	if(this.copyElm) { this.elmFrame.style.width = (this.elm.offsetWidth-4)+'px'; }
		
		this.initialFontSize = bkLib.getStyle(this.elm,'font-size');
		this.initialFontFamily = bkLib.getStyle(this.elm,'font-family');
		this.initialFontWeight = bkLib.getStyle(this.elm,'font-weight');
		this.initialFontColor = bkLib.getStyle(this.elm,'color');
		this.initialContent = this.elm.innerHTML;
		if(this.initialContent == "") {
			this.initialContent = '<br />';
		}
		if(!this.copyElm) {
       		this.initialHeight = 0;
       	}
       	
		this.elm.innerHTML = '';
       	this.elm.appendChild(this.elmFrame);
       	
       	this.initFrame();
       	this.heightUpdate();
       	bkLib.addEvent(this.elmFrame.contentWindow.document,'mousedown',this.mouseDown.closureListener(this));
       	bkLib.addEvent(this.elmFrame.contentWindow.document,'keyup',this.heightUpdate.closureListener(this));
	},
	
	initFrame : function() {
		this.frameDoc = this.elmFrame.contentWindow.document;
        this.frameDoc.open();
        this.frameDoc.write('<html><head></head><body id="nicEditContent" style="margin: 0 !important; background-color: transparent !important;">');
        this.frameDoc.write(this.initialContent);
        this.frameDoc.write('</body></html>');
        this.frameDoc.close();
        
        this.frameDoc.designMode = "on";
        
        this.frameContent = this.elmFrame.contentWindow.document.body;
        
        bkLib.setStyle(this.frameContent,{fontSize : this.initialFontSize, fontFamily : this.initialFontFamily, fontWeight : this.initialFontWeight, color : this.initialFontColor});
	},
	
	getContent : function() {
        return this.frameContent.innerHTML;
	},
	
	addTooltip : function(e) {
		this.toolTip = new nicEditorTooltip(this.elmFrame,this.nicEditor,this.getTipStyle());
	},
	
	setContent : function(c) {
		this.frameContent.innerHTML = c;	
	},
	
	getDoc : function() {
		return this.elmFrame.contentWindow.document.defaultView;
	},
	
	getSel : function() {
		return (this.elmFrame.contentWindow) ? this.elmFrame.contentWindow.getSelection() : this.frameDoc.selection;
	},
	
	heightUpdate : function() {
			this.elmFrame.style.height = (this.frameContent.offsetHeight < this.initialHeight) ? this.initialHeight+'px' : this.frameContent.offsetHeight+'px';
    },
    
    nicCommand : function(cmd,args) {
		this.frameDoc.execCommand(cmd,false,args);
		setTimeout(this.heightUpdate.closure(this),100);
	}

	
});

var nicEditorPanel = bkClass.extend({
	panelId : null,
	panelButtons : [],
	panelElm : null,
	elm : null,
	
	construct : function(e,options,nicEditor) {
		this.elm = e;
		this.options = options;
		this.nicEditor = nicEditor;
		
		this.panelElm = document.createElement('div');
		this.panelContain = document.createElement('div');
		
		bkLib.setStyle(this.panelContain,{width : '100%', border : '1px solid #cccccc', backgroundColor : '#efefef', className : 'nicEdit-panelContain'});
		bkLib.setStyle(this.panelElm,{margin : '2px', overflow : 'hidden', className : 'nicEdit-panel'});
		
		this.panelButtons = new Array();

		if(this.options.fullPanel) {
				for(b in this.options.buttons) {
					this.addButton(this.options.buttons[b]);
				}
		} else {
			for(var i=0;i<this.options.buttonList.length;i++) {
					this.addButton(this.options.buttons[this.options.buttonList[i]]);
			}
		}
		
		this.panelContain.appendChild(this.panelElm);
		this.elm.appendChild(this.panelContain);
		bkLib.unselectAble(this.elm);
	},
	
	addButton : function(button) {
			var type = (button['type']) ? eval(button['type']) : nicEditorButton;
			this.panelButtons.push(new type(this.panelElm,button,this.nicEditor));
	}
	
});

var nicEditorButton = bkClass.extend({
	isDisabled : false,
	isHover : false,
	isActive : false,
	
	construct : function(e,options,nicEditor) {
		this.elm = e;
		this.options = options;
		this.nicEditor = nicEditor;
		this.buttonContain = document.createElement('div');
		this.buttonBorder = document.createElement('div');
		this.buttonElm = document.createElement('div');

		bkLib.setStyle(this.buttonElm,{backgroundImage : "url('"+this.nicEditor.options.iconsPath+"')", width : '18px', height : '18px', backgroundPosition : ((this.options.tile-1)*-18)+'px 0px', cursor : 'pointer', className : 'nicEdit-button'});
		bkLib.setStyle(this.buttonBorder,{border : '1px solid #efefef', width: '18px', height: '18px', backgroundColor : '#efefef'})
		bkLib.setStyle(this.buttonContain,{'float' : 'left', overflow: 'hidden', width : '20px', height : '20px', className : 'nicEdit-buttonContain'});
		
		bkLib.addEvent(this.buttonElm,'mouseover', this.hoverOn.closure(this));
		bkLib.addEvent(this.buttonElm,'mouseout',this.hoverOff.closure(this));
		bkLib.addEvent(this.buttonElm,'click',this.mouseClick.closure(this));
		
		if(!window.opera) {
			this.buttonElm.onmousedown = bkLib.cancelEvent;
			this.buttonElm.onclick = bkLib.cancelEvent;
		}
		
		this.nicEditor.addEvent('instanceSelect', this.enable.closure(this));
		this.nicEditor.addEvent('noInstanceSelect', this.disable.closure(this));			
		
		this.disable();
		
		this.buttonBorder.appendChild(this.buttonElm);
		this.buttonContain.appendChild(this.buttonBorder);
		this.elm.appendChild(this.buttonContain);
		
		this.init();
	},
	
	init : function() {  },
	
	hideButton : function() {
		this.buttonContain.style.display = 'none';	
	},
	
	updateState : function() {
		if(this.isDisabled) { this.setBg(); }
		else if(this.isHover) { this.setBg('hover'); }
		else if(this.isActive) { this.setBg('active'); }
		else { this.setBg(); }
	},
	
	setBg : function(state) {	
		if(state == "hover") {
			bkLib.setStyle(this.buttonBorder,{border : '1px solid #666', backgroundColor : '#ddd', className : 'nicEdit-buttonContain-hover'});	
		} else if(state == "active") {
			bkLib.setStyle(this.buttonBorder,{border : '1px solid #666', backgroundColor : '#ccc', className : 'nicEdit-buttonContain-active'});
		} else {
			bkLib.setStyle(this.buttonBorder,{border : '1px solid #efefef', backgroundColor : '#efefef', className : 'nicEdit-buttonContain-normal'});
		}
	},
	
	checkNodes : function(e) {
		var elm = e;
		do {
			if(this.options.tags && bkLib.inArray(this.options.tags,elm.nodeName)) {
				this.activate();
				return true;
			}
		} while(elm = elm.parentNode);
		var elm = e;
		if(this.options.css) {
			for(itm in this.options.css) {
				if(bkLib.getStyle(elm,itm,this.nicEditor.selectedInstance.getDoc()) == this.options.css[itm]) {
					this.activate();
					return true;
				}
			}
		}
		this.deactivate();
		return false;
	},
	
	activate : function() {
		if(!this.isDisabled) {
			this.isActive = true;
			this.updateState();	
		}
	},
	
	deactivate : function() {
		this.isActive = false;
		this.updateState();	
	},
	
	enable : function(t) {
		this.isDisabled = false;
		bkLib.setStyle(this.buttonContain,{'opacity' : 1, className : 'nicEdit-buttonContain-enabled'});
		this.updateState();
		this.checkNodes(t);
	},
	
	disable : function(t) {		
		this.isDisabled = true;
		bkLib.setStyle(this.buttonContain,{'opacity' : 0.6, className : 'nicEdit-buttonContain-disabled'});
		this.updateState();	
		this.removePane();
	},
	
	toggleActive : function() {
		(this.isActive) ? this.deactivate() : this.activate();	
	},
	
	hoverOn : function() {
		if(!this.isDisabled) {
			this.isHover = true;
			this.updateState();
			this.toolTimer = setTimeout(this.addTooltip.closure(this),500);
		}
	}, 
	
	addTooltip : function() {
		if(this.isHover && !this.buttonPane) {
			this.toolTip = new nicEditorPane(this.buttonContain,this.nicEditor,{margin : '4px', padding : '3px', backgroundColor : '#ffffc9', fontSize : '12px', border : '1px solid #ccc', className : 'nicEdit-tooltip'});
			this.toolTip.setContent(this.options.name);
		}
	},
	
	removeTooltip : function() {
		if(this.toolTimer) {
			clearTimeout(this.toolTimer);
		}
		if(this.toolTip) {
			this.toolTip.remove();
			this.toolTip = null;
		}
	},
	
	getPaneStyle : function() {
		return {width : '300px', fontSize : '12px', padding : '4px', textAlign: 'left', border : '1px solid #ccc', backgroundColor : '#fff', className : 'nicEdit-buttonPane'};
	},
	
	removePane : function() {
		if(this.buttonPane) {
			this.buttonPane.remove();
			this.buttonPane = null;
			return true;
		}
	},
	
	hoverOff : function() {
		this.isHover = false;
		this.updateState();
		this.removeTooltip();
	},
	
	mouseClick : function() {
		if(this.options.command) {
			this.nicEditor.nicCommand(this.options.command,this.options.commandArgs);
			if(!this.options.noActive) {
				this.toggleActive();
			}
		} else {
			(this.buttonPane) ? this.removePane() : this.addPane();
		}
	}
	
});

var nicEditorSelect = bkClass.extend({
	isDisabled : false,
	
	construct : function(e,options,nicEditor) {
		this.elm = e;
		this.options = options;
		this.nicEditor = nicEditor;	
		
		this.selectContain = document.createElement('DIV');
		this.selectItems = document.createElement('DIV');
		this.selectControl = document.createElement('DIV');
		this.selectTxt = document.createElement('DIV');
		this.selectOptions = new Array();

		bkLib.setStyle(this.selectContain,{overflow : 'hidden', width: '110px', height : '20px', 'float' : 'left', cursor : 'pointer', margin : '0 2px', className : 'nicEdit-selectContain'});
		bkLib.setStyle(this.selectItems,{overflow : 'hidden', border: '1px solid #ccc', paddingLeft : '3px', height : '18px', backgroundColor : '#fff'});
		bkLib.setStyle(this.selectControl,{backgroundImage : "url('"+this.nicEditor.options.iconsPath+"')", backgroundPosition : (17*-18)+'px 0px', 'float' : 'right', height: '16px', width : '16px', className : 'nicEdit-selectControl'});
		bkLib.setStyle(this.selectTxt,{'float' : 'left', width : '86px', overflow: 'hidden', height : '16px', marginTop : '1px', fontFamily : 'arial', fontSize : '12px', className : 'nicEdit-selectTxt'});
		
		if(!window.opera) {
			this.selectContain.onmousedown = this.selectControl.onmousedown = this.selectTxt.onmousedown = bkLib.cancelEvent;
		}
		
		this.selectItems.appendChild(this.selectTxt);
		this.selectItems.appendChild(this.selectControl);
		
		this.selectContain.appendChild(this.selectItems);
		this.elm.appendChild(this.selectContain);
		
		bkLib.addEvent(this.selectContain,'click',this.togglePane.closure(this));	
		this.nicEditor.addEvent('instanceSelect', this.enable.closure(this));
		this.nicEditor.addEvent('noInstanceSelect', this.disable.closure(this));	
		
		this.disable();
		this.init();
		
		bkLib.unselectAble(this.elm);
	},
	
	disable : function() {
		this.isDisabled = true;
		this.removePane();
		bkLib.setStyle(this.selectContain,{opacity : 0.6});
	},
	
	enable : function(t) {
		this.isDisabled = false;
		bkLib.setStyle(this.selectContain,{opacity : 1});
	},
	
	setDisplay : function(txt) {
		this.selectTxt.innerHTML = txt;
	},
	
	togglePane : function() {
		if(!this.isDisabled) {
			(this.selectPane) ? this.removePane() : this.showPane();
		}
	},
	
	showPane : function() {
		this.selectPane = new nicEditorPane(this.selectContain,this.nicEditor,this.getPaneStyle());
		for(var i=0;i<this.selectOptions.length;i++) {
			var optionKey = this.selectOptions[i][0];
			var optionValue = this.selectOptions[i][1];
			
			var itm = document.createElement('div');
			bkLib.setStyle(itm,{overflow : 'hidden', width: '88px', textAlign : 'left', padding : '0 4px', cursor : 'pointer', borderBottom : '1px solid #ccc'});
			itm.id = optionKey;
			itm.innerHTML = optionValue;
			itm.onclick = this.onSelect.closure(this,optionKey);
			itm.onmouseover = this.optionOver.closure(this,itm);
			itm.onmouseout = this.optionOut.closure(this,itm);
			if(!window.opera) {
				itm.onmousedown = bkLib.cancelEvent;
			}
			this.selectPane.append(itm);
			bkLib.unselectAble(itm);
		}
	},
	
	removePane : function() {
		if(this.selectPane) {
			this.selectPane.remove();
			this.selectPane = null;
		}	
	},
	
	getPaneStyle : function() {
		return {width : '88px', overflow : 'hidden', fontSize : '12px', borderLeft : '1px solid #ccc', borderRight : '1px solid #ccc', backgroundColor : '#fff', className : 'nicEdit-selectPane'};
	},
	
	optionOver : function(opt) {
		bkLib.setStyle(opt,{backgroundColor : '#ccc'});			
	},
	
	optionOut : function(opt) {
		bkLib.setStyle(opt,{backgroundColor : '#fff'});	
	},
	
	
	add : function(k,v) {
		this.selectOptions.push(new Array(k,v));	
	},
	
	onSelect : function(elm) {
		this.nicEditor.nicCommand(this.options.command,elm);
		this.removePane();	
	}
});

var nicEditorFontSizeSelect = nicEditorSelect.extend({
	selConfig : {1 : '1&nbsp;(8pt)', 2 : '2&nbsp;(10pt)', 3 : '3&nbsp;(12pt)', 4 : '4&nbsp;(14pt)', 5 : '5&nbsp;(18pt)', 6 : '6&nbsp;(24pt)'},
	init : function() {
		this.setDisplay('Yazı&nbsp;Tipi&nbsp;Boyutu..');
		for(itm in this.selConfig) {
			this.add(itm,'<font size="'+itm+'">'+this.selConfig[itm]+'</font>');
		}		
	}
});

var nicEditorFontFamilySelect = nicEditorSelect.extend({
	selConfig : {'arial' : 'Arial','comic sans ms' : 'Comic Sans','courier new' : 'Courier New','georgia' : 'Georgia', 'helvetica' : 'Helvetica', 'impact' : 'Impact', 'times new roman' : 'Times', 'trebuchet ms' : 'Trebuchet', 'verdana' : 'Verdana'},
	
	init : function() {
		this.setDisplay('Yazı&nbsp;Tipi&nbsp;Ailesi..');
		for(itm in this.selConfig) {
			this.add(itm,'<font face="'+itm+'">'+this.selConfig[itm]+'</font>');
		}
	}
});

var nicEditorFontFormatSelect = nicEditorSelect.extend({
		selConfig : {'p' : 'Parağraf', 'pre' : 'Pre', 'h6' : 'Başlık&nbsp;6', 'h5' : 'Başlık&nbsp;5', 'h4' : 'Başlık&nbsp;4', 'h3' : 'Başlık&nbsp;3', 'h2' : 'Başlık&nbsp;2', 'h1' : 'Başlık&nbsp;1'},
		
	init : function() {
		this.setDisplay('Yazı&nbsp;Tipi&nbsp;Biçimi..');
		for(itm in this.selConfig) {
			var tag = itm.toUpperCase();
			this.add('<'+tag+'>','<'+itm+' style="padding: 0px; margin: 0px;">'+this.selConfig[itm]+'</'+tag+'>');
		}
	}
});

var nicEditorSaveButton = nicEditorButton.extend({
	onSave : null,
	
	init : function() {
		this.onSave = this.nicEditor.options.onSave;
		if(!this.onSave) {
			this.hideButton();
		}
	},
	
	mouseClick : function() {
		if(this.nicEditor.options.onSave) {
			this.nicEditor.options.onSave(this.nicEditor.nicInstances);
		}
	}	
});

var nicEditorPane = bkClass.extend({
	nicPane : null,
	options : null,
	isVisible : true,
	isOver : true,
	
	construct : function(elm,nicEditor,options) {
		this.options = options;
		this.nicEditor = nicEditor;
		this.elm = elm;
		
		var nicPanelElm = this.nicEditor.nicPanel.panelElm;
		this.nicPane = document.createElement('DIV');
		this.pos = bkLib.elmPos(elm);
		
		bkLib.setStyle(this.nicPane,{position : 'absolute', left : this.pos[0]+'px', top : this.pos[1]+'px', className : 'nicEdit-pane'});
		bkLib.setStyle(this.nicPane,options);
		
		document.body.appendChild(this.nicPane);
		
		panelPos = bkLib.elmPos(nicPanelElm);
		var xPanel = panelPos[0]+nicPanelElm.offsetWidth;	
		xPos = this.pos[0]+this.nicPane.offsetWidth;
		
		if(xPos > xPanel) {
			bkLib.setStyle(this.nicPane,{left : (this.pos[0]-(xPos-xPanel+1))+'px'});
		}	
		
		this.init();
		bkLib.unselectAble(this.nicPane);	
	},
	
	init : function() {
	
	},
	
	hide : function() {
		this.nicPane.style.display = 'none';
		this.isVisible = false;	
	},
	
	show : function() {
		if(this.isOver) {
			this.nicPane.style.display = 'block';
			this.isVisible = true;	
		}
	},
	
	remove : function() {
		if(this.nicPane) {
			document.body.removeChild(this.nicPane);
		}
	},
	
	append : function(c) {
		this.nicPane.appendChild(c);
	},
	
	setContent : function(c) {
		this.nicPane.innerHTML = c;
	}
	
});

var nicEditorTooltip = nicEditorPane.extend({
	init : function() {
		bkLib.addEvent(this.elm,'mouseover',this.mouseOver.closureListener(this));
		bkLib.addEvent(this.elm,'mouseout',this.mouseOut.closureListener(this));
		bkLib.addEvent((this.elm.nodeName == "IFRAME") ? this.elm.contentWindow.document : this.elm,'mousemove',this.mouseMove.closureListener(this));
		this.hide();
	},
	
	mouseMove : function(e) {
		var pos = bkLib.mousePos(e);
		var isFrame = (this.elm.nodeName == "IFRAME");
		
		var xpos = isFrame ? pos[0]+this.pos[0] : pos[0];
		var ypos = isFrame ? pos[1]+this.pos[1]-this.elm.offsetHeight : pos[1];
		bkLib.setStyle(this.nicPane,{left : (xpos+10)+'px', top : (ypos+10)+'px'});
	},
	
	mouseOver : function(e,t) {
		var rt = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while(rt && rt != this.elm) {
			rt = rt.parentNode;
		}
		if(rt == this.elm) { return; }
		
		if(!this.isVisible) {
			this.toolTimer = setTimeout(this.show.closure(this),400);
		}
		this.isOver = true;
	},
	
	mouseOut : function(e,t) {
		var rt = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while(rt && rt != this.elm) {
			rt = rt.parentNode;
		}
		if(rt == this.elm) { return; }
		
		if(this.toolTimer) {
			clearTimeout(this.toolTimer);
		}
		this.hide();
		this.isOver = false;
	}
});

var nicEditorImageButton = nicEditorButton.extend({
	
	addPane : function() {
			this.removeTooltip();
			this.nicEditor.selectedInstance.saveRng();
			
			this.buttonPane = new nicEditorPane(this.buttonContain,this.nicEditor,this.getPaneStyle());
				
			var paneContent = "<form id=\"nicImageForm\" onSubmit=\"return false;\"><h3 style=\"padding: 0px; margin: 0px;\">Resim Ekle</h3>\
			<label style=\"display: block; float: left; width: 100px\" for=\"url\">Resim Adresi</label>\
			<input style=\"border: 1px solid #ccc; width: 150px;\" type=\"text\" name=\"url\" id=\"nicImageURL\" value=\"http://\" /><input type=\"button\" style=\"border: 1px solid #ccc\" id=\"nicImageSec\" value=\"...\" /><br />\
			<input type=\"submit\" style=\"border: 1px solid #ccc\" id=\"nicImageSubmit\" value=\"Ekle\" /></div></form>";
			
        	this.buttonPane.setContent(paneContent);
			this.imgURL = $N('nicImageURL');
			this.imgURL.focus();
			
			bkLib.addEvent($N('nicImageForm'),'submit',this.paneSubmit.closure(this));
	},
	
	paneSubmit : function() {
		var si = this.nicEditor.selectedInstance;
		if(si) {
			si.restoreRng();
			var url = this.imgURL.value;
			if(url == "http://" || url == "") {
				alert("Resim eklemek için bir adres girmelisiniz.");
				return false;
			}
			this.nicEditor.nicCommand("insertImage",url);
			this.removePane();
		}
	}
});


var nicEditorLinkButton = nicEditorButton.extend({
	
	addPane : function() {
			this.removeTooltip();
			this.nicEditor.selectedInstance.saveRng();
			
			this.buttonPane = new nicEditorPane(this.buttonContain,this.nicEditor,this.getPaneStyle());
			var paneContent = "<form id=\"nicLinkForm\" onSubmit=\"return false;\"><h3 style=\"padding: 0px; margin: 0px;\">Bağlantı Ekle</h3>\
			<label style=\"display: block; float: left; width: 100px\" for=\"url\">Bağlantı Adresi</label>\
			<input type=\"text\" style=\"border: 1px solid #ccc; width: 150px;\" name=\"url\" id=\"nicLinkURL\" value=\"http://\" /><br />\
			<input type=\"submit\" style=\"border: 1px solid #ccc\" value=\"Ekle\" /></form>";
			this.buttonPane.setContent(paneContent);
			this.linkURL = $N('nicLinkURL');
			this.linkURL.focus();
			
			bkLib.addEvent($N('nicLinkForm'),'submit',this.paneSubmit.closure(this));
	},
	
	paneSubmit : function() {
		var si = this.nicEditor.selectedInstance;
		if(si) {
			this.nicEditor.selectedInstance.restoreRng();
			var url = this.linkURL.value;
			
			if(url == "http://" || url == "") {
				alert("Bağlantı oluşturmak için bir adres girmelisiniz.");
				return false;
			}
			this.nicEditor.nicCommand("createlink",url);
		}
		this.removePane();
	}
});

var nicEditorHTMLButton = nicEditorButton.extend({
	
	addPane : function() {
			this.removeTooltip();
			this.buttonPane = new nicEditorPane(this.buttonContain,this.nicEditor,{width : (this.nicEditor.nicPanel.panelElm.offsetWidth-12)+'px', textAlign: 'left', border : '1px solid #ccc', zIndex : '9999', backgroundColor : '#fff'});
			paneContent = "<form id=\"nicHTMLForm\" onSubmit=\"return false;\"><textarea id=\"nicHTMLArea\" style=\"width: 100%; text-align: left; font-size: 13px; height: 200px; border: 0; border-bottom: 1px solid #ccc;\">\
			</textarea><input type=\"submit\" style=\"margin-left: 20px; border: 1px solid #ccc\" value=\"HTML Güncelle\" /></form>";
			this.buttonPane.setContent(paneContent);
			
			bkLib.addEvent($N('nicHTMLForm'),'submit',this.paneSubmit.closure(this));
			$N("nicHTMLArea").value = this.nicEditor.selectedInstance.getContent();
	},
	
	paneSubmit : function() {
		var si = this.nicEditor.selectedInstance;
		if(si) {
			si.restoreRng();
			si.setContent($N('nicHTMLArea').value);
		}

		this.removePane();
	}
});

var nicEditorColorButton = nicEditorButton.extend({
	
	addPane : function() {
			this.removeTooltip();
			this.buttonPane = new nicEditorPane(this.buttonContain,this.nicEditor,this.getPaneStyle());
			var colorList = {0 : '00',1 : '33',2 : '66',3 :'99',4 : 'CC',5 : 'FF'};
			
			var colorItems = document.createElement('DIV');
			bkLib.setStyle(colorItems,{width : '270px'});
			for(var r in colorList) {
				for(var b in colorList) {
					for(var g in colorList) {
						var colorSquare = document.createElement('DIV');
						var colorBorder = document.createElement('DIV');
						var colorInner = document.createElement('DIV');
						
						var colorCode = '#'+colorList[r]+colorList[g]+colorList[b];
						bkLib.setStyle(colorSquare,{'cursor' : 'pointer', 'height' : '15px', 'float' : 'left'});
						bkLib.setStyle(colorBorder,{border: '2px solid '+colorCode});
						bkLib.setStyle(colorInner,{backgroundColor : colorCode, overflow : 'hidden', width : '11px', height : '11px'});
						bkLib.addEvent(colorSquare,'click',this.colorSelect.closure(this,colorCode));
						bkLib.addEvent(colorSquare,'mouseover',this.borderOn.closure(this,colorBorder));
						bkLib.addEvent(colorSquare,'mouseout',this.borderOff.closure(this,colorBorder,colorCode));
						
						if(!window.opera) {
							colorSquare.onmousedown = bkLib.cancelEvent;
							colorInner.onmousedown = bkLib.cancelEvent;
						}
						
						colorSquare.appendChild(colorBorder);
						colorBorder.appendChild(colorInner);
						colorItems.appendChild(colorSquare);
					}	
				}	
			}
			bkLib.unselectAble(colorItems);
			this.buttonPane.append(colorItems);	
	},
	
	colorSelect : function(c) {
		this.nicEditor.nicCommand('foreColor',c);
		this.removePane();
	},
	
	borderOn : function(colorBorder) {
		bkLib.setStyle(colorBorder,{border : '2px solid #000'});
	},
	
	borderOff : function(colorBorder,colorCode) {
		bkLib.setStyle(colorBorder,{border : '2px solid '+colorCode});		
	}
});
