// MediaWiki JavaScript support functions

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
var is_safari = ((clientPC.indexOf('applewebkit')!=-1) && (clientPC.indexOf('spoofer')==-1));
var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
// For accesskeys
var is_ff2_win = (clientPC.indexOf('firefox/2')!=-1 || clientPC.indexOf('minefield/3')!=-1) && clientPC.indexOf('windows')!=-1;
var is_ff2_x11 = (clientPC.indexOf('firefox/2')!=-1 || clientPC.indexOf('minefield/3')!=-1) && clientPC.indexOf('x11')!=-1;
if (clientPC.indexOf('opera') != -1) {
	var is_opera = true;
	var is_opera_preseven = (window.opera && !document.childNodes);
	var is_opera_seven = (window.opera && document.childNodes);
}



function addButton(parent,textareaID,imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
	// Don't generate buttons for browsers which don't fully
	// support it.
	var item =
		{
         "textareaID": textareaID,
         "imageId": imageId,
		 "imageFile": imageFile,
		 "speedTip": speedTip,
		 "tagOpen": tagOpen,
		 "tagClose": tagClose,
		 "sampleText": sampleText};
      mwInsertEditButton(parent, item);
}


function mwInsertEditButton(parent, item) {
	var image = document.createElement("img");
	image.width = 20;
	image.height = 20;
	image.className = "mw-toolbar-editbutton";
	if (item.imageId) image.id = item.imageId;
	image.src = item.imageFile;
	image.border = 0;
	image.alt = item.speedTip;
	image.title = item.speedTip;
	image.style.cursor = "pointer";
	image.align = "absmiddle";
	image.onclick = function() {
		insertTags(item.textareaID, item.tagOpen, item.tagClose, item.sampleText);
		return false;
	};

	parent.appendChild(image);
	return true;
}


function insertTags(textareaID,tagOpen, tagClose, sampleText) {
	var txtarea = document.getElementById(textareaID);


	// IE
	if (document.selection  && !is_gecko) {
		var theSelection = document.selection.createRange().text;
		if (!theSelection) {
			theSelection=sampleText;
		}
		txtarea.focus();
		if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		} else {
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}

	// Mozilla
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
		var replaced = false;
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		if (endPos-startPos) {
			replaced = true;
		}
		var scrollTop = txtarea.scrollTop;
		var myText = (txtarea.value).substring(startPos, endPos);
		if (!myText) {
			myText=sampleText;
		}
		var subst;
		if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
		} else {
			subst = tagOpen + myText + tagClose;
		}
		txtarea.value = txtarea.value.substring(0, startPos) + subst +
			txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();
		//set new selection
		if (replaced) {
			var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
			txtarea.selectionStart = cPos;
			txtarea.selectionEnd = cPos;
		} else {
			txtarea.selectionStart = startPos+tagOpen.length;
			txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
		}
		txtarea.scrollTop = scrollTop;

	// All other browsers get no toolbar.
	// There was previously support for a crippled "help"
	// bar, but that caused more problems than it solved.
	}
	// reposition cursor if possible
	if (txtarea.createTextRange) {
		txtarea.caretPos = document.selection.createRange().duplicate();
	}
}

function addPopupButton(parent, imageFile, speedTip, fun) {

	var image = document.createElement("img");
	image.width = 20;
	image.height = 20;
	image.className = "mw-toolbar-editbutton";

	image.src = imageFile;
	image.border = 0;
	image.alt =  speedTip;
	image.title =  speedTip;
	image.style.cursor = "pointer";
	image.align = "absmiddle";
	image.onclick = fun;


  	parent.appendChild(image);

	return true;
}



function initButtons(parent,textareaID) {
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/bold.gif','Bold text','\'\'\'','\'\'\'','Bold text','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/italic.gif','Italic text','\'\'','\'\'','Italic text','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/h1.gif','Level 1 headline','\n= ',' =\n','Headline 1 text','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/h2.gif','Level 2 headline','\n== ',' ==\n','Headline 2 text','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/image.gif','Image','[[Image:',']]','uploaded_image_name','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/youtube.gif','Youtube video','[[Youtube:',']]','youtube_video_id','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/google.gif','Google video','[[Google:',']]','google_video_id','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/dailymotion.gif','Dailymotion video','[[Dailymotion:',']]','dailymotion_video_id','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/hr.gif','Horizontal line (use sparingly)','\n----\n','','','mw-editbutton-hr');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/bullist.gif','List','\n* List Item 1\n* List Item 2\n* List Item 3\n','','','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/table.gif','Table','\n|| Cell 1 || Cell 2 ||\n|| Cell 1 || Cell 2 ||\n','','','mw-editbutton');
addButton(parent,textareaID,BASEURL+'/templates/'+THEME+'/images/wysiwyg/br.gif','Line break','\n','','','mw-editbutton');
addPopupButton(parent, BASEURL+'/templates/'+THEME+'/images/wysiwyg/smile.gif','Smiles',function(e){  smilePopup(e,textareaID);   return false;});

var image = document.createElement("img");
	image.width = 20;
	image.height = 20;
	image.className = "mw-toolbar-editbutton";

	image.src = BASEURL+'/templates/'+THEME+'/images/wysiwyg/help.gif';
	image.border = 0;
	image.alt = 'Editor Help';
	image.title = 'Editor Help';
	image.style.cursor = "pointer";
	image.onclick = function() {
		window.open(BASEURL+'/templates/'+THEME+'/images/wysiwyg/help.html','_blank','width=500,height=500,scrollbars=yes,status=no,location=no');
		return false;
	};

	//parent.appendChild(image);

}

function smilePopup(e,textareaID) { selectedtextareaID	 = textareaID; var x=0, y=0;
  if (IE) {
      x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
      y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
      x += window.event.clientX;
      y += window.event.clientY;

  } else {
      x = e.pageX;
      y = e.pageY;
  }



  document.getElementById('keyboards').style.top  = y+'px';
  document.getElementById('keyboards').style.left = x+'px';
  $('#keyboards').show();
  hideCnt = 1;
}




