/* PTX-Script */
/* 2008 by Marco Mühlen */
/* requires prototype.js */
/* Do not change! */

/* default value for InternetExplorerCheck */
var isIE = document.all;

function ptx_init(){
  var blocker = document.createElement('div');
  blocker.className = 'ptx_blocker';
  blocker.style.display = 'none';
  blocker.style.position = 'absolute';
  blocker.style.top = '0px';
  blocker.style.left = '0px';
  blocker.style.width = '100%';
  blocker.style.height = '100%';
  blocker.style.zIndex = 100000;
  blocker.onclick = function(){this.style.display = 'none';}
  
  var window_div = document.createElement('div');
  window_div.id = 'ptx_windows';
  
  var contextmenu_div = document.createElement('div');
  contextmenu_div.id = 'ptx_context_menus';
  
  ptx_document_body().appendChild(blocker);
  ptx_document_body().appendChild(window_div);
  ptx_document_body().appendChild(contextmenu_div);
  
  //ptx_remove_class(ptx_document_body(),'no_js');
  ptx_document_body().className = 'ptx_body';
  ptx_dd_blocker = blocker;
}

/* load a script to the header */
function ptx_load_script(filename){
	var head= document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = filename;
	head.appendChild(script);
}

/* load a css-file to the header */
function ptx_load_css(filename){
	var head= document.getElementsByTagName('head')[0];
	var css = document.createElement('link');
	css.type = 'text/css';
	css.rel = 'stylesheet';
	css.media = 'all';
	css.href = filename;
	head.appendChild(css);
}

/* set opacity of an object */
function ptx_opacity( object, value ){
	if (isIE){
		object.style.filter="alpha(opacity="+value+")";
	} else object.style.opacity = value/100;
}

/* create an individual id */
var ptx_last_id = 0;
function ptx_unique_id(){
	var time = new Date();
	ptx_last_id++;
	if (ptx_last_id > 65535)
		ptx_last_id = 0;
	return "IDPTX-"+time.getTime()+"-"+ptx_last_id;
}

/* find the absolute position of the object */
function ptx_find_position(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft + obj.scrollLeft;
			curtop += obj.offsetTop + obj.scrollTop;
		} while (obj = obj.offsetParent);
		return [ptx_integer(curleft),ptx_integer(curtop)];
	}
	return null;
}

/* returns the object with the internal id */
function ptx_get_by_id(id){
	return document.getElementById(id);
}

/* returns the object with the classname id */
function ptx_get_by_class(object, classname){
	return object.getElementsByClassName(classname);
}

/* get an integer value of an object */
function ptx_integer(value){
	if (value == '0px')
		return 0;
	if (parseInt(value)){
		return parseInt(value);
	} else
		return value;
}

/* get the width of the browsersize */
function ptx_window_width() {
	if (window.innerWidth) {
		return ptx_integer(window.innerWidth);
	} else if (document.body && document.body.offsetWidth) {
		return ptx_integer(document.body.offsetWidth);
	} else {
		return 0;
	}
}

/* get the height of the browsersize */
function ptx_window_height() {
	if (window.innerHeight) {
		return ptx_integer(window.innerHeight);
	} else if (document.body && document.body.offsetHeight) {
		return ptx_integer(document.body.offsetHeight);
	} else {
		return 0;
	}
}

/* returns the document body */
function ptx_document_body(){
	if (isIE) {
		return document.all.body;
	} else
		return document.body;
}

/* removes a class from object */
function ptx_remove_class(object,classname){
  if (ptx_has_class(object,classname)){
    var all_classes = ' '+object.className+' ';
    var start = all_classes.indexOf(classname);
    all_classes = all_classes.substring(1,start-1)+all_classes.substring(start+classname.length, all_classes.length-1);
    object.className = all_classes;
  }
}

/* appends a class from object */
function ptx_append_class(object,classname){
  if (!(ptx_has_class(object,classname))){
    object.className += ' '+classname;
  }
}

/* switches classes on hover */
function ptx_hover(object){
	var cls = object.className+' hover';
	if (object.className.substring( object.className.length-6, object.className.length ) != ' hover')
		object.className = cls;
}
function ptx_unhover(object){
	var cls = object.className.substring( 0, object.className.length-6 );
	if (object.className.substring( object.className.length-6, object.className.length ) == ' hover')
		object.className = cls;
}

/* switches classes on mousedown */
function ptx_push(object){
	var cls = object.className+' pushed';
	if (object.className.substring( object.className.length-7, object.className.length ) != ' pushed')
		object.className = cls;
}
function ptx_unpush(object){
	var cls = object.className.substring( 0, object.className.length-7 );
	if (object.className.substring( object.className.length-7, object.className.length ) == ' pushed')
		object.className = cls;
}

/* initialize a button_object */
function ptx_button(object){
	if (!(object.onmouseout)){
		object.onmouseover = function(){ptx_hover(this);}
		object.onmouseout = function(){ptx_unhover(this);}
		object.onmousedown = function(){ptx_push(this);}
		object.onmouseup = function(){ptx_unpush(this);}
    ptx_hover(object);
	}
}

/* adds a value i.g. "ajax=true" to a path */
function ptx_add_path_value(path,value){
  if (path.indexOf('?') < 0) { path += '?'; } else path += '&';
  return path + value;
}

/* loads the specified document into the specified object */
function ptx_ajax_update(object_name, path){
	new Ajax.Updater(object_name, ptx_add_path_value(path,'ajax=true'), {evalScripts: true, asynchronous: true});
}

/* loads the specified document into the specified object and appends it */
function ptx_ajax_append(object_name, path){
  new Ajax.Updater(object_name, ptx_add_path_value(path,'ajax=true'), {evalScripts: true, asynchronous: true, insertion: Insertion.Bottom });
}

/* submits the specified document into the specified object */
function ptx_ajax_submit(object_name, path, btn){
  while ((btn.parentNode) && (btn.nodeName != 'FORM')) btn = btn.parentNode;
  if (btn.nodeName == 'FORM') new Ajax.Updater(object_name, ptx_add_path_value(path,'ajax=true'), {evalScripts: true, asynchronous: true, insertion: Insertion.Bottom, parameters:Form.serialize(btn) });
}

/* PTX-Drag&Drop/Resize */
var ptx_dd_item = null;
var ptx_dd_offset = null;
var ptx_dd_values = null;
var ptx_dd_action = null;
var ptx_dd_moving = false;
var ptx_dd_blocker = null;
var ptx_dd_max_x = null;
var ptx_dd_max_y = null;
var ptx_dd_min_x = null;
var ptx_dd_min_y = null;

/* returns the charcode of a key (used for inputfields) */
function ptx_charcode(evt) {
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode :((evt.which) ? evt.which : evt.keyCode);
  return charCode;
}

/* scrolls object to bottom */
function ptx_scroll_to_bottom(obj) {
  obj.scrollTop = obj.scrollHeight;
}

/* returns the absolute position of the cursor */
function ptx_cursor(e){
	var x = isIE ? event.clientX : e.clientX; 
	var y = isIE ? event.clientY : e.clientY; 
	return [ptx_integer(x),ptx_integer(y)];
}

/* starts the drag&drop/resize */
function ptx_dd_start(e){
	if (ptx_dd_blocker) ptx_dd_blocker.style.display = 'block';
	ptx_dd_moving = true;
	if (ptx_dd_item){
		ptx_dd_offset = ptx_cursor(e);
		if (ptx_dd_action == 'resize'){
			ptx_dd_values = [ptx_integer(ptx_dd_item.style.width), ptx_integer(ptx_dd_item.style.height)];
		} else
			ptx_dd_values = [ptx_integer(ptx_dd_item.style.left), ptx_integer(ptx_dd_item.style.top)];
	}
}

/* ends the drag&drop/resize */
function ptx_dd_end(e){
	ptx_dd_moving = false;
	ptx_dd_item = null;
	ptx_dd_offset = null;
	ptx_dd_values = null;
	ptx_dd_action = null;
	if (ptx_dd_blocker) ptx_dd_blocker.style.display = 'none';
	//ptx_dd_blocker = null;
  document.onmousemove = function(){};
	document.onmousedown = function(){};
	document.onmouseup = function(){};
}

/* while in drag/resize-mode */
function ptx_dd_move(e){
	if ((ptx_dd_item) && (ptx_dd_values)){
		var new_val = ptx_cursor(e);
		var x = ptx_dd_values[0]+(new_val[0]-ptx_dd_offset[0]);
		var y = ptx_dd_values[1]+(new_val[1]-ptx_dd_offset[1]);
		if (ptx_dd_action == 'resize'){
			if (x < 0) x = 0;
			if (y < 0) y = 0;
			if ((ptx_dd_min_x) && (x < ptx_dd_min_x)) x = ptx_dd_min_x;
			if ((ptx_dd_min_y) && (y < ptx_dd_min_y)) y = ptx_dd_min_y;
			if ((ptx_dd_max_x) && (x > ptx_dd_max_x)) x = ptx_dd_max_x;
			if ((ptx_dd_max_y) && (y > ptx_dd_max_y)) y = ptx_dd_max_y;
			ptx_dd_item.style.width = x+'px';
			ptx_dd_item.style.height = y+'px';
		} else {
			ptx_dd_item.style.left = x+'px';
			ptx_dd_item.style.top = y+'px';
		}
	}
}

/* initialize drag&drop */
function ptx_dd_init(object){
	if (ptx_dd_moving) return null;
	ptx_dd_item = object;
	ptx_dd_action = 'move';
	document.onmousemove = ptx_dd_move;
	document.onmousedown = ptx_dd_start;
	document.onmouseup = ptx_dd_end;
}

/* initialize resize */
function ptx_resize_init(object,min_x,min_y,max_x,max_y){
	if (ptx_dd_moving) return null;
	ptx_dd_item = object;
	ptx_dd_action = 'resize';
	document.onmousemove = ptx_dd_move;
	document.onmousedown = ptx_dd_start;
	document.onmouseup = ptx_dd_end;
	ptx_dd_max_x = max_x;
	ptx_dd_max_y = max_y;
	ptx_dd_min_x = min_x;
	ptx_dd_min_y = min_y;
}

/* returns true if classname is one of the classes */
function ptx_has_class(object, classname){
  var all_classes = object.className;
  if (!(all_classes)) return false;
  all_classes = ' '+all_classes+' ';
  if (all_classes.indexOf(' '+classname+' ') >= 0) return true;
  return false;
}

/* get an element by class name upwards */
/* stops at first result */
function ptx_get_by_class_up(object, classname){
  if (ptx_has_class(object, classname)) return object;
  if (object.parentNode) return ptx_get_by_class_up(object.parentNode, classname);
  return null;
}

/* center an absolute object */
function ptx_center(object){
  var width = ptx_integer(object.style.width);
  var height = ptx_integer(object.style.height);
  object.style.top = ((ptx_window_height() - height) / 2) + 'px';
  object.style.left = ((ptx_window_width() - width) / 2) + 'px';
}



var ptx_context_menu = null;
document.oncontextmenu = function(e){ptx_context_menu_open(e); return false;}

/* activates a context_menu */
function ptx_context_menu_init(object){
  var cm = ptx_get_by_class(object, 'ptx_context_menu');
  if (cm.length > 0) {
    ptx_context_menu = cm[0];
    object.onmouseout = function(){ptx_context_menu = null};
  }
}

/* opens a context_menu */
function ptx_context_menu_open(e){
  var pos = ptx_cursor(e);
  if ((ptx_context_menu) && (pos) && (pos.length == 2)) {
  
    var cms = ptx_get_by_id('ptx_context_menus');
    var parent = ptx_context_menu.parentNode;
    if ((cms) && (parent)) {
      cms.innerHTML = '';
      cms.appendChild(ptx_context_menu);
      var html = cms.innerHTML;
      parent.innerHTML += html;
      ptx_context_menu.style.left = pos[0]-10+'px';
      ptx_context_menu.style.top = pos[1]-10+'px';
      ptx_context_menu.style.display = 'block';
    }

  }
}



/* get current window */
function ptx_get_window(object){
  var p_win = ptx_get_by_class_up(object, 'ptx_window');
  /* set active window */
  if (p_win) {
    var win_frame = p_win.parentNode;
    if (win_frame) {
      win_frame.removeChild(p_win);
      win_frame.appendChild(p_win);
    }
  }
  return p_win;
}

/* closes the current window */
function ptx_close_window(object){
  var win = ptx_get_window(object);
  if ((win) && (win.parentNode)) win.parentNode.removeChild(win);
}
