cAPI = function() {

	this.init = function() {
		API.includes();
	
//		console.log(ACL_ADMIN);
		
		API.editing = '';
		API.button = '';
		API.menuTimer = '';
		API.maxZ = 100;
		API.idGen = 1;
		API.mx = 0;
		API.my = 0;
		API.delta = 0;

		$.ajaxSetup({
			type: "POST"
		});
		
//		$.getScript(JS_LIBS_URL + 'libs/jquery/dimensions.js');
		$.getScript(JS_LIBS_URL + 'libs/jquery/hotkeys.js');
		$.getScript(JS_LIBS_URL + 'libs/jquery/mousewheel.js');
//		$.getScript(JS_LIBS_URL + 'libs/jquery/easing.js');
		$.getScript(JS_LIBS_URL + 'libs/jquery/form.js');
		$.getScript(JS_LIBS_URL + 'Cookies.js');

		$('<div id="systemWorking">&nbsp;</div>').appendTo(document.body);
//		$('#systemWorking').html('<p class="aCenter" style="margin-top: 200px;"><img alt="" src="' + WWW_ROOT + 'themes/images/loading.gif"/></p>');
		$('<img id="throbber" src="' + WWW_ROOT + 'themes/images/throbber.gif"/>').appendTo(document.body);
		$('#throbber').css({
			'position' : 'absolute',
			'display' : 'none',
			'left' : (($(window).width() / 2) - 104) + 'px',
			'top' : (($(window).height() / 2) - 6) + 'px'
		});
		 $("#throbber").ajaxStart(function(){
   			$(this).fadeIn();
 		});
		 $("#throbber").ajaxStop(function(){
   			$(this).fadeOut();
 		});
		
		o = new API.createElement('div', API.AJAXTarget = 'AJAXTarget', 'absolute', 0, 50, 500, 200, 10);
		o.innerHTML = '&nbsp;';
		o.style.backgroundColor = 'white';
		o.style.border = '1px solid black';
		o.style.overflow = 'hidden';
		o.style.display = 'none';
		document.body.appendChild(o);
	
//		console.log(DOCUMENT_BASE);

//		setTimeout(function(){API.editElement('id_23');}, 2000);
	
	};
	
	this.control = function() {


		if (typeof $.hotkeys == 'undefined' || typeof $.mousewheel == 'undefined') {
			setTimeout(function(){API.control();}, 100);
			return;
		}

		API.mousemove();
		
		$(document.body).mousewheel(function(event, delta) {
			API.mx = event.pageX;
			API.my = event.pageY;
			API.delta = delta || 0;
		});
		
		if (APL_LOGGED != 'YES') $.hotkeys.add('Ctrl+l',function (){
			$.getScript(WWW_ROOT + 'js/admin/core.login.js');
		});
		
		if (getCookie('API_mouse') == 'yes') {
			$.getScript(JS_LIBS_URL + 'admin/API.mouse.js');
		} else {
			$.hotkeys.add('Ctrl+m',function (){
				$.getScript(JS_LIBS_URL + 'admin/API.mouse.js');
			});
		}
		$.hotkeys.add('f5',function (){
			$('#systemWorking').fadeIn();
			window.location.reload();
		});
		$.hotkeys.add('Ctrl+f5',function (){
			$('#systemWorking').fadeIn();
			window.location.reload();
		});		
		$.hotkeys.add('Ctrl+r',function (){
			$('#systemWorking').fadeIn();
			window.location.reload();
		});
		$.hotkeys.add('Ctrl+q',function (){
			window.location.replace('?logout');
		});		
	};
	
	this.includes = function() {
		if (SEO_ADMIN == 1) {
			$.getScript(JS_LIBS_URL + 'admin/SEO.js');
		}
		if (ACL_ADMIN == 1) {
			$.getScript(JS_LIBS_URL + 'admin/gacl.js');
		}
		if (LAYOUT_ADMIN == 1) {
			$.getScript(JS_LIBS_URL + 'admin/layout.js');
		}
	};

	this.mousemove = function() {
		$(document).bind('mousemove', function(e){
			API.mx = e.pageX;
			API.my = e.pageY;
		});
	};
};

cAPI.prototype.throbber = function(id) {
		if (!id) return;
		if (document.getElementById(id)) $('#' + id).html('<p class="aCenter" style="margin-top: 25px;"><img alt="" src="' + WWW_ROOT + 'themes/images/throbber.gif"/></p>');
};

cAPI.prototype.editElement = function(id) {
	content = API.storage = $('#' + id).html();
	if (content.indexOf('<!--PH-->') != -1) {
		content = content.substr(content.indexOf('<!--PH-->') + 9);
		$('#' + id).html(content);
	}
	tinyMCE.execCommand('mceAddControl', false, id);
};

cAPI.prototype.saveElement = function(ed) {
    var content = ed.getContent(); //-> get the processed content
	var editAreaId = ed.id; //-> get the id of the editor in a multiple editor environment, so you can handle the right editor area after the server response
	content = content.replace(/\+/g, "&#43");
	content = content.replace(/\\/g, "&#92");
	if (content.indexOf('<!--PH-->') != -1) content = content.substr(content.indexOf('<!--PH-->') + 9);
	content = escape(content);
	var url = WWW_ROOT + 'admin/saveElement/?id=' + editAreaId;
	var pars = editAreaId + '=' + content;
	$.post(url, pars, function(data, textStatus) {
		tinyMCE.execCommand('mceRemoveControl', false, editAreaId);
		$('#' + editAreaId).html(data);
		API.attachPlaceholder('#' + editAreaId);
	});
};

cAPI.prototype.attachPlaceholder = function(id){
	$(id).prepend('<div rel="' + $(id).id + '" class="placeholder none" style="position: absolute; top: 0px; left: 0px; width: ' + $(id).width() + 'px; height: ' + $(id).height() + 'px; border: 1px dotted black;">&nbsp;</div><!--PH-->');
	$(id).hover(function(){
		$(this).children('div[rel=\'' + $(id).id + '\']').fadeIn();
	}, function(){
		$(this).children('div[rel=\'' + $(id).id + '\']').fadeOut();
	});
};

cAPI.prototype.cancelElement = function(ed) {
	var editAreaId = ed.id;
	tinyMCE.execCommand('mceRemoveControl', false, editAreaId);
	$('#' + editAreaId).html(API.storage);
};

cAPI.prototype.createElement = function(element, id, position, x, y, w, h, zindex) {
	if (document.getElementById(id)) $('#' + id).remove();
	el = document.createElement(element);
	el.id = id;
	el.setAttribute('id', id);
	el.style.position = position;
	if (x < 0) {
		el.style.right = (0 - x) + 'px';
	} else {
		el.style.left = x + 'px';
	}
	if (y < 0) {
		el.style.bottom = (0 - y) + 'px';
	} else {
		el.style.top = y + 'px';
	}
	if (w == 0) {
		el.style.width = 'auto';
	} else {
		el.style.width = w + 'px';
	}
	if (h == 0) {
		el.style.height = 'auto';
	} else {
		el.style.height = h + 'px';
	}
	el.style.visibility = 'visible';
	el.style.display = 'block';
	el.style.zIndex = zindex;
	return el;
};

cAPI.prototype.ParseInt = function(s) {
	var ret = parseInt(s);
	return (isNaN(ret) ? 0 : ret);
};

cAPI.prototype.getBrowserInfo = function() {
		this.ver = navigator.appVersion;
		this.agent = navigator.userAgent;
		this.dom = document.getElementById ? 1 : 0;
		this.opera5 = this.agent.indexOf("Opera 5") > -1;
		this.opera = this.agent.indexOf("Opera") > -1;
		this.ie5 = (this.ver.indexOf("MSIE 5") > -1 && this.dom && !this.opera5) ? 1 : 0;
		this.ie6 = (this.ver.indexOf("MSIE 6") > -1 && this.dom && !this.opera5) ? 1 : 0;
		this.ie7 = (this.ver.indexOf("MSIE 7") > -1 && this.dom && !this.opera5) ? 1 : 0;
		this.ie4 = (document.all && !this.dom && !this.opera5) ? 1 : 0;
		this.ie = this.ie5 || this.ie6 || this.ie7;
		this.mac = this.agent.indexOf("Mac") > -1;
		this.ns6 = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0;
		this.ns4 = (document.layers && !this.dom) ? 1 : 0;
		this.bw = (this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
		return this;
};
	
cAPI.prototype.getPageInfo = function() {
		if (API.ParseInt(API.maxZ) < 100) API.maxZ = 100;
		this.x1 = 0;this.y1 = 0;
		this.x2 = (API.browser.ie7)?document.documentElement.clientWidth:(API.browser.ns6) ? innerWidth : document.body.clientWidth;
		this.y2 = (API.browser.ie7)?document.documentElement.clientHeight:(API.browser.ns6) ? innerHeight : document.body.clientHeight;
		this.x50 = Math.round(this.x2 / 2);
		this.y50 = Math.round(this.y2 / 2);		
};

cAPI.prototype.stylize = function(o) {
		o.style.backgroundColor = 'white';
		o.style.border = '1px solid black';
		o.style.padding = '0px';
		o.style.margin = '0px';
		o.style.fontSize = '7pt';
		o.style.fontFamily = 'Verdana';
		o.style.overflow = 'hidden';
		return o;
};

var API = new cAPI();

$(function() {
  API.init();
});

Array.prototype.serializeC = function (){
   var temp = '';
   for(var vars in this){
      if(!(this[vars] instanceof Function)){
         temp += vars + ':' + this[vars] + ',';
      }
   }
   return temp;
   return temp.substr(0, temp.length - 1);
};
 
$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};