var keycode = null;

function init_powered_by() {
	Event.observe(document,"keydown",powered_by_key_observer);
	Event.observe(document,"keyup",powered_by_key_observer);
	Event.observe($("powered_by"),"click",powered_by_observer);
}

function init_admin_elements() {
	role = readCookie("role");
	admin_elements = document.getElementsByClassName("admin_element");
	if(role && role=="admin") {
		for(i=0;i<admin_elements.length;i++) {
			Element.show(admin_elements[i]);
		}
	}else {
		for(i=0;i<admin_elements.length;i++) {
			Element.hide(admin_elements[i]);
		}
	}
}

var powered_by_observer = function(e) {
	/**if(keycode == 17) {
		//show_admin_layer();
		window_open('/admin/admin_layer?page=' + location.pathname);
	}else {
		location.href = "http://www.tmplus.be";	
	}**/
	window_open("/admin/admin_layer?page=" + location.pathname);
}

var powered_by_key_observer = function(e) {
	if(e.keyCode == 27) {
		window_close();
	}else{
		if(e.type == "keydown") {
			keycode = e.keyCode;
		}else {
			keycode = null;
		}
	}
}

function show_admin_layer() {
	show_tmplus_menu();
}

var tmplus_menu_move_observer = function(e) {
	//var pageSize = WindowUtilities.getPageSize(e.target.parent);
    //var windowScroll = WindowUtilities.getWindowScroll(e.target.parent);
	//alert("Size " + pageSize.windowHeight + " -- Scroll " + windowScroll.top);
	top = scroll_offset[1] + 50;
	$("tmplus_menu").setStyle({top: top + "px"});
};

var tmplus_menu_item_clicked = function(e) {
	//alert("hija");
	var pageSize = WindowUtilities.getPageSize(e.target);
	new Ajax.Updater('tmplus_menu_item_body', '/admin/menu_item?item=' + e.target.id, 
        {asynchronous:true, evalScripts:true, onLoading:function(request){Element.show('loading')}});
	Element.show("tmplus_menu_item_body");
	$("tmplus_menu_item_body").setStyle( {top: Position.cumulativeOffset($('tmplus_menu'))[1] + 'px'} );
	$("tmplus_menu_item_body").setStyle( {left: '-400px'} );
};

function show_tmplus_menu() {
	
	//Element.hide("tmplus_menu_item_body");
	Element.show("tmplus_menu");
	Event.observe(window, "resize", tmplus_menu_move_observer);
	Event.observe(window, "scroll", tmplus_menu_move_observer);
	var menu_items = $("tmplus_menu").getElementsByClassName("tmplus_menu_item");
	for(i=0;i < menu_items.length;i++) {
		Event.observe(menu_items[i].id,"click",tmplus_menu_item_clicked);
	}
}

function close_tmplus_menu_item() {
	Element.hide("tmplus_menu_item_body");
}

function position_on_top_of(layer,field) {
	Position.absolutize(layer);
	//Position.absolutize(field);
	pos_field = Position.cumulativeOffset(field);
	layer.style.left = pos_field[0] + "px";
	layer.style.top = pos_field[1] + "px";
	layer.style.zIndex = "50";
}

function getId(id_name) {
    return id_name.substring(id_name.lastIndexOf('_') + 1);
}

// CaretPosition object
function CaretPosition()
{
 var start = null;
 var end = null;
}

function getCaretPosition(oField)
{
 // Initialise the CaretPosition object
 var oCaretPos = new CaretPosition();

 // IE support
 if(document.selection)
 {
  // Focus on the text box
  oField.focus();

  // This returns us an object containing
  // information about the currently selected text
  var oSel = document.selection.createRange();

  // Find out the length of the selected text
  // (you'll see why below)
  var selectionLength = oSel.text.length;

  // Move the selection start to 0 position.
  //
  // This is where it gets interesting, and this is why
  // some have claimed you can't get the caret positions
  // in IE.
  //
  // IE has no 'selectionStart' or 'selectionEnd' property,
  // so we can not get or set this value directly. We can
  // only move the caret positions relative to where they
  // currently are (this should make more sense when you read
  // the next line of code).
  //
  // Note, that even though we have moved the start
  // position on our object in memory, this is not reflected
  // in the browser until we call oSel.select() (which we're
  // not going to do here).
  //
  // Also note, the start position will never be a negative
  // number, no matter how far we try to move it back.
  oSel.moveStart ('character', -oField.value.length);

  // This is where it should start to make sense. We now know
  // our start caret position is the length of the currently
  // selected text minus the original selection length
  // (think about it).
  oCaretPos.start = oSel.text.length - selectionLength;

  // Since the start of the selection is at the start of the
  // text, we know that the length of the selection is also
  // the index of the end caret position.
  oCaretPos.end = oSel.text.length;
 }
 // Firefox support
 else if(oField.selectionStart || oField.selectionStart == '0')
 {
  // This is a whole lot easier in Firefox
  oCaretPos.start = oField.selectionStart;
  oCaretPos.end = oField.selectionEnd;
 }

 // Return results
 return (oCaretPos);
}


function setCaretPosition(oField, iCaretStart, iCaretEnd)
{
 // IE Support
 if (document.selection)
 {
  // Focus on the text box
  oField.focus();

  // This returns us an object containing
  // information about the currently selected text
  var oSel = document.selection.createRange();

  // Since we don't know where the caret positions
  // currently are (see comments in getCaretPosition() for
  // further information on this), move them to position 0.
  //
  // Note, the caret positions will never be a negative
  // number, no matter how far we try to move them back.
  oSel.moveStart ('character', -oField.value.length);
  oSel.moveEnd ('character', -oField.value.length);

  // Now we know the caret positions are at index 0, move
  // them forward to the desired position (move end caret
  // position first - actually not sure if moving start
  // caret position first affects the end caret position -
  // it might).
  //
  // Note, we allow for a null end caret position and just
  // default it to the same as the start caret position.
  if(iCaretEnd != null)
   oSel.moveEnd ('character', iCaretEnd);
  else
   oSel.moveEnd ('character', iCaretStart);

  oSel.moveStart ('character', iCaretStart);

  // Everything thus far has just been on our object in
  // memory - this line actually updates the browser
  oSel.select();
 }
 // Firefox support
 else if(oField.selectionStart || oField.selectionStart == '0')
 {
  oField.selectionStart = iCaretStart;

  if(iCaretEnd != null)
   oField.selectionEnd = iCaretEnd;
  else
   oField.selectionEnd = iCaretStart;

  oField.focus();
 }
}

//myField accepts an object reference, myValue accepts the text strint to add
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();

//in effect we are creating a text range with zero
//length at the cursor location and replacing it
//with myValue
sel = document.selection.createRange();
sel.text = myValue;
}

//Mozilla/Firefox/Netscape 7+ support
else if (myField.selectionStart || myField.selectionStart == '0') {

//Here we get the start and end points of the
//selection. Then we create substrings up to the
//start of the selection and from the end point
//of the selection to the end of the field value.
//Then we concatenate the first substring, myValue,
//and the second substring to get the new value.
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
} 

function doSomething(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);
	alert('Character was ' + character);
}

function window_open(url,width) {
	window_close();
	if(width == null) {
		width = 500;
	}
	Dialog.info({url: url, options: {method: 'get'}},{className: "alphacube", width:width});
}

function window_close() {
	Dialog.closeInfo();
	if($("tmplus_menu")) {
		Element.hide("tmplus_menu");
	}
}

Position.getWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height]
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}