var R_classes = new function(){
	this.existingClasses = new Array();
	this.declare = function(className, implementation){
		if(!this.existingClasses[className]){
			this.existingClasses[className] = new Object();
			this.existingClasses[className].god = this;
			this.existingClasses[className].implementation = implementation;
			this.existingClasses[className].extend = function(parentClassName){
				if(this.god.existingClasses[parentClassName]){
					this.extension = parentClassName;
					return 1;
				}else{
					return 0;//error if the requested parent class isn't declared
				}	
			};
			return this.existingClasses[className];
		}else{
			return 0;//error if the requested class is already declared
		}
	};
	this.create = function(className, params){
		if(this.existingClasses[className]){
			if(this.existingClasses[className].extension){
				return this.combine(new this.existingClasses[className].implementation(params), new this.existingClasses[this.existingClasses[className].extension].implementation(params));
			}else{
				return new this.existingClasses[className].implementation(params);
			}	
		}else{
			return 0;//error if the requested class isn't declared
		}
	};
	this.combine = function(child, parent){
		for(var i in parent){
			if(!child[i])child[i] = parent[i];
		}
		return child;
	};
};

function R_stack(){
	this.data = new Array();
	this.push = function(data){
		this.data.push(data);
	};
	this.pop = function(){
		return this.data.pop();
	};
}
function R_queue(){
	this.data = new Array();
	this.push = function(data){
		this.data.push(data);
	};
	this.pop = function(){
		return this.data.shift();
	};
}
function R_list(options){
	this.nxt = null;
	this.prv = null;
	if(options) this.body = options;
	this.insertBefore = function(options){
		var newElem = new R_list(options);
		if(this.prv) this.prv.nxt = newElem;
		newElem.prv = this.prv;
		newElem.nxt = this;
		this.prv = newElem;
	};
	this.insertAfter = function(options){
		var newElem = new R_list(options);
		if(this.nxt) this.nxt.prv = newElem;
		newElem.prv = this;
		newElem.nxt = this.nxt;
		this.nxt = newElem;
	};
	this.del = function(){
		if(this.nxt) this.nxt.prv = this.prv;	
		if(this.prv) this.prv.nxt = this.nxt;
		this.nxt = this.prv = this.body = null;
	};
	this.Next = function(){
		return this.nxt;
	};
	this.Previous = function(){
		return this.prv;
	};
}
function R_binarytree(options){
	this.left = null;
	this.right = null;
	this.parent = null;
	if(options) this.body = options;
	this.addLeft = function(options){
		this.left = new R_binarytree(options);
		this.left.parent = this;
	};
	this.addRight = function(options){
		this.right = new R_binarytree(options);
		this.right.parent = this;
	};
	this.Left = function(){
		return this.left;
	};
	this.Right = function(){
		return this.right;
	};
	this.del = function(){
		if(this.parent){
			if(this.parent.left == this) this.parent.left = null;
			if(this.parent.right == this) this.parent.right = null;
			this.parent = this.body = null;
		}
	};
}
function R_doc(){
	this.ajaxRequests = new Array();
	this.onloadqueue = new Array();
	this.addEvent = function(obj, eventtype, func){
		if(obj.addEventListener){
			obj.addEventListener(eventtype, func, false);
		}else if(obj.attachEvent){
			obj.attachEvent("on"+eventtype, func);
		}else{
			obj["on"+eventtype] = func;
		}
	};
	this.removeEvent = function(obj, eventtype, func){
		if(obj.removeEventListener){
			obj.removeEventListener(eventtype, func, false);
		}else if(obj.detachEvent){
			obj.detachEvent("on"+eventtype, func);
		}else{
			obj["on"+eventtype] = null;
		}
	};
	this.loadJS = function(address, callback){
		if(!this.jsExists(address)){
			var script = document.createElement('script');
			script.type = 'text/javascript';
			document.getElementsByTagName('head')[0].appendChild(script);
			script.src = address;
			script.onload = callback;//NORMAL BROWSERS
			script.onreadystatechange = function(){//IE
				if(this.readyState == "loaded"){
					if(typeof callback == 'function') callback();	
				}
			};
		}
		return true;
	};
	this.unloadJS = function(address){
		if(this.jsExists(address)){
			var scripts = document.getElementsByTagName('head')[0].getElementsByTagName('script');
			for(var key in scripts){
				if(scripts[key].src == address){
					document.getElementsByTagName('head')[0].removeChild(scripts[key]);
					break;
				}
			}
		}	
		return true;
	};
	this.jsExists = function(address){
		var scripts = document.getElementsByTagName('head')[0].getElementsByTagName('script');
		for(var key in scripts){
			if(scripts[key].src){
				if((scripts[key].src == address) || (scripts[key].src == window.location.protocol+'//'+window.location.host)){
					return true;
				}
			}	
		}
		return false;
	};
	this.loadCSS = function(address, callback){
		if(!this.cssExists(address)){
			var css = document.createElement('link');
			css.type = 'text/css';
			css.rel = 'stylesheet';
			document.getElementsByTagName('head')[0].appendChild(css);
			css.href = address;
			css.onload = callback;//NORMAL BROWSERS
			css.onreadystatechange = function(){//IE
				if(this.readyState == "loaded"  || this.readyState == "complete"){
					if(typeof callback == 'function') callback();
				}
			};
		}	
		return true;
	};
	this.unloadCSS = function(address){
		if(this.cssExists(address)){
			var css = document.getElementsByTagName('head')[0].getElementsByTagName('link');
			for(var key in css){
				if(css[key].href == address){
					document.getElementsByTagName('head')[0].removeChild(css[key]);
					break;
				}
			}
		}	
		return true;
	};
	this.cssExists = function(address){
		var css = document.getElementsByTagName('head')[0].getElementByTagName('link');
		for(var key in css){
			if(css[key].href){
				if((css[key].href == address) || (css[key].href == window.location.protocol+'//'+window.location.host)){
					return true;
				}
			}	
		}
		return false;
	};

	this.browser = function(){
		var b = {};
		b.ver = navigator.appVersion.toLowerCase();
		b.verNum = parseInt(b.ver);
		b.agent = navigator.userAgent.toLowerCase();
		b.dom = (document.getElementById?true:false);
	
		b.safari = (b.agent.indexOf('safari') != -1);
		b.konq = (!b.safari && (b.agent.indexOf('konqueror')!=-1) );
		b.moz = (!b.safari && !b.konq ) && (b.agent.indexOf("gecko") > -1);
		b.opera = (b.agent.indexOf("opera") > -1 && b.dom);
		b.ie = (b.ver.indexOf("msie") > -1 && b.dom && !b.opera);
		b.ns = (b.agent.indexOf("netscape") > -1);

		b.ff = (b.agent.indexOf("firefox") > -1);
		b.ffVer=0;
		if(b.ff) {
			var pos = b.agent.indexOf("firefox");
			if (pos != -1) {
				b.ffVer = parseInt(b.agent.substr(pos + 8));
			}
		}
		b.ff2 = (b.ff && b.ffVer==2);
		
		b.opera5 = (b.opera && b.verNum == 5);
		b.opera6 = (b.opera && b.verNum == 6);
		b.opera7 = (b.opera && b.verNum == 7);
		b.opera8 = (b.opera && b.verNum == 8);
		b.opera9 = (b.opera && b.verNum == 9);

		b.ieVer = 0;
		if (b.ie) {
			var pos = b.ver.indexOf("msie");
			if (pos != -1)
				b.ieVer = parseInt(b.ver.substr(pos + 5));
		}
		b.ie4 = (b.ie && !b.dom);
		b.ie5 = (b.ie && b.ieVer == 5);
		b.ie6 = (b.ie && b.ieVer == 6);
		b.ie7 = (b.ie && b.ieVer == 7);
		b.macOS = (b.agent.indexOf("mac") > -1);
		b.mac = (b.macOS && b.verNum >= 7);
		b.ns6 = (b.dom && b.ns && b.verNum >= 5);
		b.b = (b.ie || b.ns6 || b.opera7 || b.mac || b.moz || b.dom);
		return b;
	};
	this.get = function(pattern, parent){
		var result = new Array();
		if (parent == null) parent = document;
		var workparams = new Array();
		var params = pattern.split(' ');
		var tag = null;
		for(var key in params){
			if(key=='forEach') continue;
			if(key=='indexOf') continue;
			workparams[key] = new Array();
			workparams[key].extraparams = new Array();
			workparams[key].extraparams = params[key].split('=');
			if(typeof workparams[key].extraparams == 'string'){
				workparams[key].extraparams[0] = workparams[key].extraparams+'!';
				workparams[key].extraparams[1] = null;
			}
			if((workparams[key].extraparams[1].indexOf('*')!=-1) || (workparams[key].extraparams[1].indexOf('?')!=-1)){
				workparams[key].extraparams[1] = new RegExp('^'+workparams[key].extraparams[1].replace('?', '(.)').replace('*', '(.*)')+'$', 'i');
				workparams[key].extraparams[2] = true;
			}
			if(workparams[key].extraparams[0] == 'tagName'){
				tag = workparams[key].extraparams[1];
			}
		}
		if(!tag){
			if(R.browser().ie5){
				var elements = parent.all;
			}else{
				var elements = parent.getElementsByTagName('*');
			}
		}else{
			var elements = parent.getElementsByTagName(tag);
		}	
		var elemLength = elements.length;
		var i, j;
		var toresult;
		for (i = 0; i < elemLength; i++) {
			toresult = 0;
			for(key in workparams){
				if(key=='forEach') continue;
				if(key=='indexOf') continue;
				if(workparams[key].extraparams[0].substr(workparams[key].extraparams[0].length-2, 1) == '!'){
					if(workparams[key].extraparams[2]){
						if(!workparams[key].extraparams[1].test(elements[i][workparams[key].extraparams[0]].toUpperCase())){
							toresult = 1;
						}else{
							toresult = 0;
							break;
						}
					}else{
						if(elements[i][workparams[key].extraparams[0]].toUpperCase()!=workparams[key].extraparams[1].toUpperCase()){
							toresult = 1;
						}else{
							toresult = 0;
							break;
						}
					}
				}else{
					if(workparams[key].extraparams[2]){
						if(workparams[key].extraparams[1].test(elements[i][workparams[key].extraparams[0]].toUpperCase())){
							toresult = 1;
						}else{
							toresult = 0;
							break;
						}
					}else{
						if(elements[i][workparams[key].extraparams[0]].toUpperCase()==workparams[key].extraparams[1].toUpperCase()){
							toresult = 1;
						}else{
							toresult = 0;
							break;
						}
					}
				}	
			}
			if(toresult) result.push(elements[i]);
		}
		result.each = function(action){
			for(var key in this){
				if(key=='forEach') continue;
				if(key=='indexOf') continue;
				if(this[key] instanceof Function) continue; action(this[key]);
			}
		};
		return result;
	};
	this.getElementsByClass = function(searchClass, tag, root){
		var classElements = new Array();
		var i;
		var j;
		if (tag == null) tag = '*';
		if (root == null) root = document;
		if(R.browser().ie5){
			var elements = root.all;
		}else{
			var elements = root.getElementsByTagName(tag);
		}	
		var elemLength = elements.length;
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		for (i = 0, j = 0; i < elemLength; i++) {
			if ( pattern.test(elements[i].className) ) {
				classElements[j] = elements[i];
				j++;
			}
		}
		return classElements;
	};
	this.onLoad = function(func){
		if(func){
			this.onloadqueue.push(func);
		}else{
			for(var key in this.onloadqueue){
				this.onloadqueue[key]();
			}
		}	
		return true;
	};
	this.absolutePos = function(elem){
		var result = {x: 0, y: 0};
		while(true){
			result.x += elem.offsetLeft;
			result.y += elem.offsetTop;
			if(elem.offsetParent) elem = elem.offsetParent;
			else break;	
		}
		return result;
	};
	this.clone = function(objToClone){
		var clone = [];
		for(var i in objToClone){
			clone[i] = objToClone[i];
		}
		return clone;
	};
	this.combine = function(obj1, obj2){
		var result = [];
		var  i = 0;
		for(i in obj1){
			result[i] = obj1[i];
		}
		for(i in obj2){
			if(!result[i])result[i] = obj2[i];
		}
		return result;
	};
	this.callFrom = function(func, fromObj, params){
		if(params){
			return func.apply(fromObj, params);
		}else{
			return func.call(fromObj, params);
		}	
	};
	this.ajax = function(var_args){
		switch (arguments.length){
			case 2:
				var method = 'GET';
				var url = arguments[0];
				var data = null;
				var expect = null;
				var callback = arguments[1];
				var type = true;
			break;
			case 3:
				var method = 'GET';
				var url = arguments[0];
				var data = arguments[1];
				var expect = null;
				var callback = arguments[2];
				var type = true;
			break;
			case 4:
				var method = arguments[0].toUpperCase();
				var url = arguments[1];
				var data = arguments[2];
				var expect = null;
				var callback = arguments[3];
				var type = true;
			break;
			case 5:
				var method = arguments[0].toUpperCase();
				var url = arguments[2];
				var data = arguments[3];
				var expect = null;
				var callback = arguments[4];
				var type = arguments[1]?true:false;
			break;
			case 6:
				var method = arguments[0].toUpperCase();
				var url = arguments[2];
				var data = arguments[3];
				var expect = arguments[5]?arguments[5].toLowerCase():null;
				var callback = arguments[4];
				var type = arguments[1]?true:false;
			break;
			default:
				return false;
			break;
		}
		var id = (new Date()).getTime().toString();
		R.ajaxRequests[id] = new R_ajax(method, url, data, expect, callback, type, id);
		R.ajaxRequests[id].parent = this;
		return R.ajaxRequests[id].init();
	};
	window.onload = function(){
		this.R.onLoad();
	};
	return this;
}
var R = new R_doc();
function R_ajax(method, url, data, expect, callback, type, id){
	this.method = method;
	this.id = id;
	this.url = url;
	this.data = data;
	this.expect = expect;
	this.callback = callback;
	this.type = type;
	this.prepare = function(){
		this.ajax = new Object();
		this.ajax.parent = this;
		this.ajax.ready = function(e){
			for(var key in R.ajaxRequests){
				if(R.ajaxRequests[key].ajax.xhr.readyState == 4){
					if(R.ajaxStop)R.ajaxStop();
					var parent = R.ajaxRequests[key].ajax;
					var parentid = key;
					if(parent.parent.expect=='json'){
						parent.parent.callback(eval("("+R.ajaxRequests[key].ajax.xhr.responseText+")"));
					}else if(parent.parent.expect=='xml'){
						parent.parent.callback(R.ajaxRequests[key].ajax.xhr.responseXML);
					}else{
						parent.parent.callback(R.ajaxRequests[key].ajax.xhr.responseText);
					}	
					delete R.ajaxRequests[parentid];
				}	
			}	
		};
		this.ajax.xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		if(this.type) this.ajax.xhr.onreadystatechange = this.ajax.ready;
		return true;
	};
	this.send = function(){
		if(this.parent.ajaxStart)this.parent.ajaxStart();
		this.prepareddata = [];
		if(!this.data)this.data = [];
		if(this.method == "GET"){
			for(var key in this.data){
				this.prepareddata.push(encodeURIComponent(key)+'='+encodeURIComponent(this.data[key]));
			}
			this.url += (this.url.match(/\?/) ? "&" : "?") + this.prepareddata.join('&');
			this.data = [];
		}
		this.ajax.xhr.open(this.method, this.url, this.type);
		if(this.method == "POST"){
			for(var key in this.data){
				this.prepareddata.push(encodeURIComponent(key)+'='+encodeURIComponent(this.data[key]));
			}
			this.ajax.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this.data = [];
		}
		this.ajax.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		this.ajax.xhr.send(this.prepareddata.join('&'));
		if(!this.type){
			if(this.ajax.xhr.status == 200){
				this.ajax.ready();
			}
		}
		return true;
	};
	this.init = function(){
		if(this.prepare()){
			this.send();
			return true;
		}else{
			return false;
		}
	};
	return this;
}
function R_csort(A, callback){//sort with callback handler
	var A2 = new Array();
	for(key in A){
		A2.push(A[key]);
	}
	return R_cqsort(A2, 0, A2.length-1, callback);
}
function R_kcsort(A, callback){//sort with callback handler with keys saving
	var A2 = new Array();
	var K2 = new Array();
	for(key in A){
		A2.push(A[key]);
		K2.push(key);
	}
	var result = R_kcqsort(A2, K2, 0, A2.length-1, callback);
	var result_array = new Array();
	for(key in result.K){
		result_array[result.K[key]] = result.A[key];
	}
	return result_array;
}
function R_kasort(A){//ASC sort with keys moving
	var A2 = new Array();
	var K2 = new Array();
	for(key in A){
		A2.push(A[key]);
		K2.push(key);
	}
	var result = R_kqsort(A2, K2, 0, A2.length-1, 'asc');
	var result_array = new Array();
	for(key in result.K){
		result_array[result.K[key]] = result.A[key];
	}
	return result_array;
}
function R_kdsort(A){//DESC sort with keys moving
	var A2 = new Array();
	var K2 = new Array();
	for(key in A){
		A2.push(A[key]);
		K2.push(key);
	}
	var result = R_kqsort(A2, K2, 0, A2.length-1, 'desc');
	var result_array = new Array();
	for(key in result.K){
		result_array[result.K[key]] = result.A[key];
	}
	return result_array;
}
function R_asort(A){//ASC sort without keys moving
	var A2 = new Array();
	for(key in A){
		A2.push(A[key]);
	}
	return R_qsort(A2, 0, A2.length-1, 'asc');
}
function R_dsort(A){//DESC sort without keys moving
	var A2 = new Array();
	for(key in A){
		A2.push(A[key]);
	}
	return R_qsort(A, 0, A2.length-1, 'desc');
}
function R_qsort(A, p, q, dir){
	if(p >= q) return A;
	var i = R_qpartition(A, p, q, dir);
	A = R_qsort(A, p, i - 1, dir);
	A = R_qsort(A, i + 1, q, dir);
	return A;
}
function R_qpartition(A, p, q, dir){
	var x = A[q];
	var i = p - 1;
	var tmp = null;
	for(var j = p; j <= q - 1; j++){
		if(dir == 'desc'){
			if(A[j] >= x){
				i++;
				tmp = A[i];
				A[i] = A[j];
				A[j] = tmp;
			}
		}else{
			if(A[j] <= x){
				i++;
				tmp = A[i];
				A[i] = A[j];
				A[j] = tmp;
			}
		}	
	}
	i++;
	tmp = A[i];
	A[i] = A[q];
	A[q] = tmp;
	return i;
}
function R_kqsort(A, K, p, q, dir){
	if(p >= q) return {A: A, K: K};
	var i = R_kqpartition(A, K, p, q, dir);
	var res1 = R_kqsort(A, K, p, i - 1, dir);
	A = res1.A;
	K = res1.K;
	var res2 = R_kqsort(A, K, i + 1, q, dir);
	A = res2.A;
	K = res2.K;
	return {A: A, K: K};
}
function R_kqpartition(A, K, p, q, dir){
	var x = A[q];
	var i = p - 1;
	var tmpA = null;
	var tmpK = null;
	for(var j = p; j <= q - 1; j++){
		if(dir == 'desc'){
			if(A[j] >= x){
				i++;
				tmpA = A[i];
				A[i] = A[j];
				A[j] = tmpA;
				tmpK = K[i];
				K[i] = K[j];
				K[j] = tmpK;
			}
		}else{
			if(A[j] <= x){
				i++;
				tmpA = A[i];
				A[i] = A[j];
				A[j] = tmpA;
				tmpK = K[i];
				K[i] = K[j];
				K[j] = tmpK;
			}
		}	
	}
	i++;
	tmpA = A[i];
	A[i] = A[q];
	A[q] = tmpA;
	tmpK = K[i];
	K[i] = K[q];
	K[q] = tmpK;
	return i;
}
function R_cqsort(A, p, q, callback){
	if(p >= q) return A;
	var i = R_cqpartition(A, p, q, callback);
	A = R_cqsort(A, p, i - 1, callback);
	A = R_cqsort(A, i + 1, q, callback);
	return A;
}
function R_cqpartition(A, p, q, callback){
	var x = A[q];
	var i = p - 1;
	var tmp = null;
	var res = null;
	for(var j = p; j <= q - 1; j++){
		res = callback(A[j], x);
		if(res > 0){
			i++;
			tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}
	i++;
	tmp = A[i];
	A[i] = A[q];
	A[q] = tmp;
	return i;
}
function R_kcqsort(A, K, p, q, callback){
	if(p >= q) return {A: A, K: K};
	var i = R_kcqpartition(A, K, p, q, callback);
	var res1 = R_kcqsort(A, K, p, i - 1, callback);
	A = res1.A;
	K = res1.K;
	var res2 = R_kcqsort(A, K, i + 1, q, callback);
	A = res2.A;
	K = res2.K;
	return {A: A, K: K};
}
function R_kcqpartition(A, K, p, q, callback){
	var x = A[q];
	var i = p - 1;
	var tmpA = null;
	var tmpK = null;
	var res = null;
	for(var j = p; j <= q - 1; j++){
		res = callback(A[j], x);
		if(res > 0){
			i++;
			tmpA = A[i];
			A[i] = A[j];
			A[j] = tmpA;
			tmpK = K[i];
			K[i] = K[j];
			K[j] = tmpK;
		}
	}
	i++;
	tmpA = A[i];
	A[i] = A[q];
	A[q] = tmpA;
	tmpK = K[i];
	K[i] = K[q];
	K[q] = tmpK;
	return i;
}

var R_dialog_zindex = 10001;
function R_dialog(options){
	this.options = options;
	this.content = options.content;
	this.view = document.createElement('div');
	this.view.className = 'R_dialog';
	this.view.parent = this;
	this.view.innerHTML = this.content;
	if(this.options.buttons){
		var input;
		for(var key in this.options.buttons){
			input = document.createElement('input');
			input.type = 'button';
			input.value = key;
			input.parent = this;
			input.onclick = this.options.buttons[key];
			this.view.appendChild(input);
		}
	}
	document.body.appendChild(this.view);
	this.open = function(){
		this.view.style.zIndex = R_dialog_zindex++;
		if(this.options.modal){
			this.overlay = document.createElement('div');
			this.overlay.className = 'R_dialog_overlay';
			this.overlay.style.zIndex = 10000;
			if(window.innerWidth){
				//this.overlay.style.left = window.scrollX + 'px';
				//this.overlay.style.top = window.scrollY + 'px';
				this.overlay.style.left = '0px';//document.documentElement.scrollLeft + 'px';
				this.overlay.style.top = '0px';//document.documentElement.scrollTop + 'px';
				this.overlay.style.width = '100%';
				this.overlay.style.height = document.documentElement.scrollHeight + 'px';
			}else{
				this.overlay.style.left = '0px';//document.documentElement.scrollLeft + 'px';
				this.overlay.style.top = '0px';//document.documentElement.scrollTop + 'px';
				this.overlay.style.width = '100%';
				this.overlay.style.height = document.documentElement.scrollHeight + 'px';
			}	
			this.overlay.setOpacity = function(value){
				if(typeof document.body.style.opacity == 'string'){
					this.style.opacity = value/100;
				}else{
					if(typeof this.style.MozOpacity == 'string'){
						this.style.MozOpacity = value/100;
					}else{
						if(document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5){
							this.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+value+")";
						}
					}	
				}	
			};
			this.overlay.setOpacity(this.options.opacity);
			//document.body.parentNode.overflowOld = document.body.parentNode.style.overflow;
			//document.body.parentNode.style.overflow = 'hidden';
			this.overlay.style.background = this.options.overlay;
			document.body.appendChild(this.overlay);
		}
		this.view.style.display = 'block';
		if(this.position){
			if(this.position.x == 'center'){
				this.setX('center');
			}else{
				this.setX(this.position.x);
			}
			if(this.position.y == 'center'){
				this.setY('center');
			}else{
				this.setY(this.position.y);
			}
		}else{
			this.setX('center');
			this.setY('center');
		}
	};
	this.setX = function(x){
		if(x == 'center'){
			if(window.innerWidth){
				this.view.style.left = Math.round((window.innerWidth-this.view.clientWidth)/2) + window.scrollX + 'px';
			}else{
				this.view.style.left = Math.round((document.documentElement.clientWidth-this.view.clientWidth)/2) + document.documentElement.scrollLeft + 'px';
			}	
		}else{
			this.view.style.left = x;
		}
		if(!this.position)this.position = new Object();
		this.position.x = x;
	};
	this.setY = function(y){
		if(y == 'center'){
	        	if(window.innerHeight){
				this.view.style.top = Math.round((window.innerHeight-this.view.clientHeight)/2) + window.scrollY - 12 + 'px';
			}else{
				this.view.style.top = Math.round((document.documentElement.clientHeight-this.view.clientHeight)/2) + document.documentElement.scrollTop - 12 + 'px';
			}	
		}else{
			this.view.style.top = y;
		}
		if(!this.position)this.position = new Object();
		this.position.y = y;
	};
	this.hide = function(){
		this.view.style.display = 'none';
		if(this.overlay) this.overlay.parentNode.removeChild(this.overlay);
		//document.body.parentNode.style.overflow = document.body.parentNode.overflowOld;
	};
	this.destroy = function(){
		this.view.parentNode.removeChild(this.view);
		if(this.overlay) this.overlay.parentNode.removeChild(this.overlay);
		//document.body.parentNode.style.overflow = document.body.parentNode.overflowOld;
		for(var key in this){
			this[key] = null;
		}
		return true;
	};
	this.setContent = function(content){
		this.content = content;
		this.view.innerHTML = this.content;
	};
	if(this.options.autoopen){
		this.open();
	}	
}

function R_hint(content){
	this.content = content;
	if(!this.content) this.content = '';
	this.view = document.createElement('div');
	this.view.parent = this;
	this.view.className = 'R_hint';
	this.view.style.display = 'none';
	this.view.style.zIndex = 10000;
	this.view.innerHTML = "[content]";
	document.body.appendChild(this.view);

	this.setTemplate = function(template){
		this.view.innerHTML = template;
		return true;
	};
	this.show = function(x, y, content){
		if(content) this.content = content;
		this.view.innerHTML = this.view.innerHTML.replace(/\[content\]/g, this.content);
		this.view.style.left = x;
		this.view.style.top = y;
		this.view.style.display = 'block';
		return true;
	};
	this.hide = function(){
		this.view.style.display = 'none';
		return true;
	};
	this.destroy = function(){
		document.body.removeChild(this.view);
		for(var key in this){
			this[key] = null;
		}
		return true;
	};
	return this;
}
function R_tabs(target, buttons, fields, reverse){
	if(buttons) this.buttons = buttons;
	else this.buttons = new Array();
	if(fields) this.fields = fields;
	else this.fields = new Array();
	
	this.target = target;
	this.newbutton = new Array();
	this.newfield = new Array();
	this.active;
	this.count;
	this.last;

	this.newbuttons = document.createElement('div');
	this.newbuttons.className = 'R_tabs_top';

	this.newfields = document.createElement('div');
	this.newfields.className = 'R_tabs_fields';

	for(var key in this.buttons){
		if(this.buttons[key].tagName){
			this.newbutton.push(document.createElement('div'));
			var last = this.newbutton.length - 1;
			this.newbutton[last].parent = this;
			this.newbutton[last].index = key;
			this.newbutton[last].className = 'R_tabs_button';
			this.newbutton[last].innerHTML = this.buttons[key].innerHTML;
			if( typeof(this.buttons[key].style.cssText) == 'string' ){
				this.newbutton[last].style.cssText = this.buttons[key].style.cssText;
			}
			this.newbutton[last].onclick = function(){
				this.parent.setActiveTab(this.index);
			};

			this.newfield.push(document.createElement('div'));
			last = this.newfield.length - 1;
			this.newfield[last].index = key;
			this.newfield[last].className = 'R_tabs_field';
			this.newfield[last].innerHTML = this.fields[key].innerHTML;
			if( typeof(this.fields[key].style.cssText) == 'string' ){
				this.newfield[last].style.cssText = this.fields[key].style.cssText;
			}
			
			this.newbuttons.appendChild(this.newbutton[last]);
			this.newfields.appendChild(this.newfield[last]);
		}
	}
	this.last = this.newbutton.length-1;
	this.count = this.newbutton.length;
	this.insertTab = function(title, content, position){
		var newbuttons = new Array();
		var newfields = new Array();
		if(typeof position != 'undefined'){
			for(var key in this.newbutton){
				if(key == position){
					var newbtn = document.createElement('div');
					newbtn.parent = this;
					newbtn.index = key;
					newbtn.className = 'R_tabs_button';
					newbtn.innerHTML = title;
					newbtn.onclick = function(){
						this.parent.setActiveTab(this.index);
					};
					newbuttons.push(newbtn);
					this.newbutton[key].index = newbuttons.length;
					newbuttons.push(this.newbutton[key]);
					this.newbuttons.insertBefore(newbtn, this.newbutton[key]);

					var newfld = document.createElement('div');
					newfld.index = key;
					newfld.className = 'R_tabs_field';
					newfld.innerHTML = content;
					newfields.push(newfld);
					newfields.push(this.newfield[key]);
					this.newfields.insertBefore(newfld, this.newfield[key]);
				}else{
					this.newbutton[key].index = newbuttons.length;
					newbuttons.push(this.newbutton[key]);
					newfields.push(this.newfield[key]);
				}
			}
			this.newbutton = newbuttons;
			this.newfield = newfields;
		}else{
			var newbtn = document.createElement('div');
			newbtn.parent = this;
			newbtn.index = this.newbutton.length;
			newbtn.className = 'R_tabs_button';
			newbtn.innerHTML = title;
			newbtn.onclick = function(){
				this.parent.setActiveTab(this.index);
			};
			var newfld = document.createElement('div');
			newfld.index = this.newfield.length;
			newfld.className = 'R_tabs_field';
			newfld.innerHTML = content;

			this.newbutton.push(newbtn);
			this.newfield.push(newfld);
			this.newbuttons.appendChild(newbtn);
			this.newfields.appendChild(newfld);
		}
		this.last = this.newbutton.length-1;
		this.count = this.newbutton.length;
		return true;
	};
	this.deleteTab = function(index){
		var newbuttons = new Array();
		var newfields = new Array();
		for(var key in this.newbutton){
			if(key == index){
				this.newbuttons.removeChild(this.newbutton[index]);
				this.newfields.removeChild(this.newfield[index]);
			}else{
				this.newbutton[key].index = newbuttons.length;
				newbuttons.push(this.newbutton[key]);
				newfields.push(this.newfield[key]);
			}
		}
		this.newbutton = newbuttons;
		this.newfield = newfields;
		this.last = this.newbutton.length-1;
		this.count = this.newbutton.length;
	};
	this.setActiveTab = function(index){
		if(this.newbutton.length == 0) return;
		for(key in this.newbutton){
			if(this.newbutton[key].index == index){
				this.active = index;
				this.newbutton[key].className = 'R_tabs_button_selected';
				this.newfield[key].className = 'R_tabs_field_selected';
			}else{
				this.newbutton[key].className = 'R_tabs_button';
				this.newfield[key].className = 'R_tabs_field';
			}
		}
		if(this.onactivate) this.onactivate();
		return;
	};
	this.setOption = function(option, value){
		switch (option){
			case 'style':
				for(key in this.newfield){
					this.newfield[key].style.cssText = value;
				}
			break;
			case 'skin':
				for(key in this.newbutton){
					this.newbutton[key].className = this.newbutton[key].className.replace('R_tabs', 'R_tabs_'+value);
				}
				for(key in this.newfield){
					this.newfield[key].className = this.newfield[key].className.replace('R_tabs', 'R_tabs_'+value);
				}
				this.newbuttons.className = this.newbuttons.className.replace('R_tabs', 'R_tabs_'+value);
				this.newfields.className = this.newfields.className.replace('R_tabs', 'R_tabs_'+value);
			break;
		}
		return;
	};
	
	this.target.innerHTML = '';
	if(reverse){
		this.target.appendChild(this.newfields);
		this.target.appendChild(this.newbuttons);
	}else{
		this.target.appendChild(this.newbuttons);
		this.target.appendChild(this.newfields);
	}
	this.target.style.display = 'block';
	this.setActiveTab(0);
}
var R_zindex = 1000;
function R_window(options){
	//PRIVATE PROPERTIES	
	this.options = options;

	if(this.options.unique && document.getElementById(this.options.id)){
		document.getElementById(this.options.id).style.zIndex = R_zindex++;
		document.getElementById(this.options.id).style.display = 'block';
		return document.getElementById(this.options.id).parent;
	}
	
	this.maximized = false;
	this.view = document.createElement('div');
	this.view.style.zIndex = R_zindex++;
	this.view.parent = this;
	if(this.options.skin) this.view.className = this.options.skin;
	else this.view.className = 'default';
	if(!this.options.id){
		this.view.id = new Date().getTime();
	}else{
		this.view.id = this.options.id;
	}	
	this.id = this.view.id;
	//TEMPLATE INITIALISATION
	this.template = "<div id='head[id]' class='head'><a id='cb[id]' href='#' onclick='this.parent.parent.parent.destroy(\"[id]\");' class='cb fr'>&nbsp;</a><a id='lb[id]' href='#' onclick='this.parent.parent.parent.maximize(\"[id]\");' onfocus='this.blur();' class='lb fr'>&nbsp;</a><a id='db[id]' href='#' onclick='this.parent.parent.parent.minimize(\"[id]\");' onfocus='this.blur();' class='db fr'>&nbsp;</a>[title]</div><div id='content[id]' class='content gt12' style='[contentstyle]'>[content]</div>";;
	var reg = null;
	for(var key in this.options.data){
		reg = new RegExp("\\["+key+"\\]", "g");
		this.template = this.template.replace(reg, this.options.data[key]);
	}	
	this.template = this.template.replace(/\[id\]/g, this.id);
	this.view.innerHTML = this.template;

	document.body.appendChild(this.view);
	this.view.head = document.getElementById('head'+this.id);
	this.view.head.parent = this.view;
	this.view.head.closebutton = document.getElementById('cb'+this.id);
	this.view.head.closebutton.parent = this.view.head;
	this.view.head.maxbutton = document.getElementById('lb'+this.id);
	this.view.head.maxbutton.parent = this.view.head;
	this.view.head.minbutton = document.getElementById('db'+this.id);
	this.view.head.minbutton.parent = this.view.head;
	this.view.content = document.getElementById('content'+this.id);
	this.view.content.parent = this.view;

	this.view.onselectstart = function(){return false;};
	this.view.head.onselectstart = function(){return false;};
	this.view.head.ondblclick = function(event){
		this.parent.parent.maximize();
		event = event || window.event;
        	if(event.stopPropagation) event.stopPropagation();
        	event.cancelBubble = true;
        	return false;
	};

	if(!this.options.buttons){
		this.view.head.closebutton.style.display = 'none';
		this.view.head.maxbutton.style.display = 'none';
		this.view.head.minbutton.style.display = 'none';
	}
	this.view.head.closebutton.ondblclick = null;
	this.view.head.maxbutton.ondblclick = null;
	this.view.head.minbutton.ondblclick = null;
	if(!this.options.buttons.minimize){this.view.head.minbutton.style.display = 'none';}
	if(!this.options.buttons.close){this.view.head.closebutton.style.display = 'none';}
	if(!this.options.buttons.maximize){this.view.head.maxbutton.style.display = 'none';this.view.head.ondblclick = null;}
	
	this.view.head.closebutton.onclick = function(event){
		this.parent.parent.parent.destroy();
		event = event || window.event;
        	if(event.stopPropagation) event.stopPropagation();
        	event.cancelBubble = true;
        	return false;
	};
	this.view.head.closebutton.onmousedown = function(event){
		event = event || window.event;
        	if(event.stopPropagation) event.stopPropagation();
        	event.cancelBubble = true;
        	return false;
	};

        if(!this.options.position){
        	this.options.position = {x: 'center', y: 'center'};
        }
	this.view.style.left = '10000px';
	this.view.style.display = 'block';
	if(this.options.position.x == 'center'){
		if(window.innerWidth){
			this.view.style.left = Math.round((window.innerWidth-this.view.clientWidth)/2) + window.scrollX + 'px';
		}else{
			this.view.style.left = Math.round((document.documentElement.clientWidth-this.view.clientWidth)/2) + document.documentElement.scrollLeft + 'px';
		}	

	}else{
		this.view.style.left = this.options.position.x;
	}
	if(this.options.position.y == 'center'){
        	if(window.innerHeight){
			this.view.style.top = Math.round((window.innerHeight-this.view.clientHeight)/2) + window.scrollY - 12 + 'px';
		}else{
			this.view.style.top = Math.round((document.documentElement.clientHeight-this.view.clientHeight)/2) + document.documentElement.scrollTop - 12 + 'px';
		}	
	}else{
		this.view.style.top = this.options.position.y;
	}
	this.view.style.display = 'none';

	if(this.options.draggable){
		this.view.head.onmousedown = function(e){
			var event = e || window.event;
			this.parent.parent.startDrag(event);
	        	return false;
		};
		this.view.head.onmouseup = function(e){
			var event = e || window.event;
			if(this.parent.parent.dragged){this.parent.parent.endDrag(event);}
	        	return false;
		};
		this.view.head.onmousemove = function(e){
			var event = e || window.event;
			if(this.parent.parent.dragged){this.parent.parent.drag(event);}
	        	return false;
		};
		this.view.head.onclick = function(e){
			var event = e || window.event;
        		if(event.stopPropagation) event.stopPropagation();
        		event.cancelBubble = true;
	        	return false;
		};
	}	
	//WINDOW UI FUNCTIONS
	this.defPosition = function(e){
		var event = e || window.event;
		var x = 0;
		var y = 0;
		if (document.attachEvent != null) { // Internet Explorer & Opera
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		if (!document.attachEvent && document.addEventListener) { // Gecko
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}
		return {x:x, y:y};
	};
	this.toCenter = function(){
		if(window.innerWidth){
			this.view.style.left = Math.round((window.innerWidth-this.view.clientWidth)/2) + window.scrollX + 'px';
			this.view.style.top = Math.round((window.innerHeight-this.view.clientHeight)/2) + window.scrollY - 12 + 'px';
		}else{
			this.view.style.left = Math.round((document.documentElement.clientWidth-this.view.clientWidth)/2) + document.documentElement.scrollLeft + 'px';
			this.view.style.top = Math.round((document.documentElement.clientHeight-this.view.clientHeight)/2) + document.documentElement.scrollTop - 12 + 'px';
		}	
	};
	this.toXY = function(x, y){
		this.view.style.left = x;
		this.view.style.top = y;
	};
	this.setContent = function(content){
		this.view.content.innerHTML = content;
	};
	this.open = function(){
		switch (this.options.appearence){
			case 'zoom':
				this.view.style.zoom = '20%';
				this.view.style.display = 'block';
				this.currentzoom = 40;
				this.view.interval = setInterval("this.showZoom()", 5);
			break;
			default:
				this.view.style.display = 'block';
			break;
		}
		if(typeof this.onopen == 'function') this.onopen();
        	return true;
	};
	this.showZoom = function(){
		if(this.currentzoom + 20 <= 120){
			this.view.style.zoom = this.currentzoom+'%';
			this.currentzoom += 20;
	        	return true;
		}else{
			clearInterval(this.view.interval);
			return true;
		}
	};
	this.destroy = function(){
		switch (this.options.appearence){
			case 'zoom':
				this.currentzoom = 80;
				this.interval = setInterval("this.hideZoom()", 5);
			break;
			default:
				document.body.removeChild(this.view);
				for(var key in this){
					this[key] = null;
				}
			break;
		}
		if(typeof this.ondestroy == 'function') this.ondestroy();
        	return true;
	};
	this.hideZoom = function(){
		if(this.currentzoom - 20 >= 0){
			this.view.style.zoom = this.currentzoom+'%';
			this.currentzoom -= 20;
        		return true;
		}else{
			clearInterval(this.view.interval);
			document.body.removeChild(this.view);
			for(key in this){
				this[key] = null;
			}
			return true;
		}
	};
	this.minimize = function(){
		if(!document.getElementById('toolbar')){
			var toolbar = document.createElement('div');
			toolbar.className = 'toolbar';
			toolbar.id = 'toolbar';
			document.body.appendChild(toolbar);
		}
		var toolbartab = document.createElement('div');
		toolbartab.className = 'toolbartab';
		toolbartab.innerHTML = this.options.title;
		toolbartab.parentWindow = this.view;
		toolbartab.onclick = function(){
			this.parentWindow.style.display = 'block';
			this.parentWindow.style.zIndex = R_zindex++;
			document.getElementById('toolbar').removeChild(this);
			document.getElementById('toolbar').style.top = document.body.clientHeight - document.getElementById('toolbar').clientHeight - 1 + 'px';
		};
		this.view.style.display = 'none';
		document.getElementById('toolbar').appendChild(toolbartab);
		document.getElementById('toolbar').style.top = document.body.clientHeight - document.getElementById('toolbar').clientHeight - 1 + 'px';
		if(typeof this.onminimize == 'function') this.onminimize();
		return true;
	};
	this.maximize = function(){
		if(!this.maximized){
			this.maximized = true;
			this.view.memleft = this.view.style.left;
			this.view.memtop = this.view.style.top;
			this.view.style.left = '0px';
			this.view.style.top = '0px';
			this.view.style.zIndex = R_zindex++;

			if (document.attachEvent != null) { // Internet Explorer & Opera
				this.view.memwidth = this.view.clientWidth+'px';
				this.view.memheight = this.view.clientHeight+'px';
				this.view.content.memwidth = this.view.content.clientWidth+'px';
				this.view.content.memheight = this.view.content.clientHeight+'px';
				this.view.head.memwidth = this.view.head.clientWidth+'px';
				this.view.style.width = parseInt(document.body.clientWidth) - 2 +'px';
				this.view.style.height = parseInt(document.body.clientHeight) - 2 + 'px';
				this.view.head.style.width = parseInt(document.body.clientWidth) - 2 + 'px';
				this.view.content.style.width = parseInt(document.body.clientWidth) - 14 + 'px';
				this.view.content.style.height = parseInt(document.body.clientHeight) - 34 + 'px';
			}
			if (!document.attachEvent && document.addEventListener) { // Gecko
				this.view.memwidth = this.view.clientWidth+'px';
				this.view.memheight = this.view.clientHeight+'px';
				this.view.content.memwidth = this.view.content.clientWidth+'px';
				this.view.content.memheight = this.view.content.clientHeight+'px';
				this.view.head.memwidth = this.view.head.clientWidth+'px';
				this.view.style.width = parseInt(document.body.clientWidth) - 2 + 'px';
				this.view.style.height = parseInt(document.body.clientHeight) - 8 + 'px';
				this.view.head.style.width = parseInt(document.body.clientWidth) - 14 + 'px';
				this.view.content.style.width = parseInt(document.body.clientWidth) - 14 + 'px';
				this.view.content.style.height = parseInt(document.body.clientHeight) - 32 + 'px';
			}
		}else{
			this.maximized = false;
			this.view.style.zIndex = R_zindex++;
			this.view.style.left = this.view.memleft;
			this.view.style.top = this.view.memtop;
			this.view.style.width = this.view.memwidth;
			this.view.style.height = this.view.memheight;
				
			this.view.head.style.width = parseInt(this.view.head.memwidth) + 'px';
			this.view.content.style.width = parseInt(this.view.content.memwidth) + 'px';
			this.view.content.style.height = parseInt(this.view.content.memheight) + 'px';

			if(parseInt(this.view.style.height) < this.view.clientHeight){
				this.view.style.height = parseInt(this.view.memheight) - (this.view.clientHeight-parseInt(this.view.memheight)) + 'px';
			}

			if(parseInt(this.view.head.style.width) < this.view.head.clientWidth){
				this.view.head.style.width = parseInt(this.view.head.memwidth) - (this.view.head.clientWidth-parseInt(this.view.head.memwidth)) + 'px';
			}
		}
		if(typeof this.onmaximize == 'function') this.onmaximize();
		return true;
	};
	this.startDrag = function(e){
		var event = e || window.event;
		document.reservonmousemove = document.onmousemove;
		document.draggedElem = this;
		document.onmousemove = function(e){
			var event = e || window.event;
			document.draggedElem.drag(event);
			if(document.reservonmousemove) document.reservonmousemove();
		};
		this.view.style.zIndex = R_zindex++;
		this.dragged = true;

		this.x0 = this.defPosition(event).x;
		this.y0 = this.defPosition(event).y;
		this.divX0 = parseInt(this.view.style.left);
		this.divY0 = parseInt(this.view.style.top);
        	return true;
	};
	this.endDrag = function(e){
		var event = e || window.event;
		if(this.view.head){
			this.view.head.style.cursor = 'default';
			this.view.head.onselectstart = null;
		}	
		this.dragged = false;
		this.view.onselectstart = null;
		document.onmousemove = document.reservonmousemove;
		document.draggedElem = null;
        	return true;
	};
	this.drag = function(e){
		var event = e || window.event;
		if(this.view.head){
			this.view.head.style.cursor = 'move';
		}	
       		this.view.style.left = this.divX0 + this.defPosition(event).x - this.x0 + 'px';
		this.view.style.top  = this.divY0 + this.defPosition(event).y - this.y0 + 'px';
        	return true;
	};
	if(this.options.autoshow){
		this.open();
	}
	return this;
}
function R_border(target, src, type, inner){
	this.parent = target;
	this.inner = inner;	
	this.parent.borders = this;
	this.type = type;
	if(!this.type) this.type = 'trbl';
	this.image = new Image;
	this.image.src = src;
	this.image.parent = this;
	this.vml = false;
	var checkcvs = document.createElement('canvas');
	if (!checkcvs.getContext) {
		if(!document.styleSheets['v']){
			var ss = document.createStyleSheet();
		        ss.owningElement.id = 'v';
			ss.cssText = "v\\:*{behavior:url(#default#VML);/background-color:transparent;display:inline-block}";
		}
		this.vml = true;
	}
	checkcvs = null;

	this.destroy = function(){
		for(var key in this.parts){
			this.parent.removeChild(this.parts[key]);
		}
	};
	
	this.parentsize = function(){
		return {height: this.parent.offsetHeight, width: this.parent.offsetWidth};
	};	

	this.make = function(){
		this.size = {height: this.image.height, width: this.image.width};
		this.parts = this.getParts();
		for(var key in this.parts){
			this.parts[key].style.position = 'absolute';
			this.parts[key].style.top = '0';
			this.parts[key].style.left = '0';
			this.parent.appendChild(this.parts[key]);
		}

		if(this.type.indexOf('l') != -1){
			this.parts['nw'].style.marginLeft = -parseInt(this.parts['nw'].style.width)+'px';
			this.parts['nw'].style.display = 'block';
			this.parts['sw'].style.marginLeft = -parseInt(this.parts['sw'].style.width)+'px';
			this.parts['sw'].style.display = 'block';
			this.parts['w'].style.marginLeft = -parseInt(this.parts['sw'].style.width)+'px';
			this.parts['w'].style.display = 'block';
		}else{
			this.parts['n'].style.width = parseInt(this.parts['n'].style.width) - parseInt(this.parts['w'].style.width)+'px';
			this.parts['s'].style.width = parseInt(this.parts['s'].style.width) - parseInt(this.parts['w'].style.width)+'px';
			this.parts['n'].style.marginLeft = parseInt(this.parts['w'].style.width)+'px';
			this.parts['s'].style.marginLeft = parseInt(this.parts['w'].style.width)+'px';
		}
		if(this.type.indexOf('t') != -1){
			this.parts['nw'].style.marginTop = -parseInt(this.parts['nw'].style.width)+'px';
			this.parts['nw'].style.display = 'block';
			this.parts['ne'].style.marginTop = -parseInt(this.parts['ne'].style.width)+'px';
			this.parts['ne'].style.display = 'block';
			this.parts['n'].style.marginTop = -parseInt(this.parts['ne'].style.width)+'px';
			this.parts['n'].style.display = 'block';
		}else{
			this.parts['w'].style.height = parseInt(this.parts['w'].style.height) - parseInt(this.parts['n'].style.height)+'px';
			this.parts['e'].style.height = parseInt(this.parts['e'].style.height) - parseInt(this.parts['n'].style.height)+'px';
			this.parts['w'].style.marginTop = parseInt(this.parts['n'].style.height)+'px';
			this.parts['e'].style.marginTop = parseInt(this.parts['n'].style.height)+'px';
		}
		if(this.type.indexOf('b') != -1){
			this.parts['sw'].style.marginTop = this.parentsize().height+'px';
			this.parts['sw'].style.display = 'block';
			this.parts['se'].style.marginTop = this.parentsize().height+'px';
			this.parts['se'].style.display = 'block';
			this.parts['s'].style.marginTop = this.parentsize().height+'px';
			this.parts['s'].style.display = 'block';
		}else{
			this.parts['w'].style.height = parseInt(this.parts['w'].style.height) - parseInt(this.parts['s'].style.height)+'px';
			this.parts['e'].style.height = parseInt(this.parts['e'].style.height) - parseInt(this.parts['s'].style.height)+'px';
		}
		if(this.type.indexOf('r') != -1){
			this.parts['ne'].style.marginLeft = this.parentsize().width+'px';
			this.parts['ne'].style.display = 'block';
			this.parts['se'].style.marginLeft = this.parentsize().width+'px';
			this.parts['se'].style.display = 'block';
			this.parts['e'].style.marginLeft = this.parentsize().width+'px';
			this.parts['e'].style.display = 'block';
		}else{
			this.parts['n'].style.width = parseInt(this.parts['n'].style.width) - parseInt(this.parts['e'].style.width)+'px';
			this.parts['s'].style.width = parseInt(this.parts['s'].style.width) - parseInt(this.parts['e'].style.width)+'px';
		}
	};
	this.makeinner = function(){
		this.size = {height: this.image.height, width: this.image.width};
		this.parts = this.getParts();
		for(var key in this.parts){
			this.parts[key].style.position = 'absolute';
			this.parts[key].style.top = '0';
			this.parts[key].style.left = '0';
			this.parent.appendChild(this.parts[key]);
		}

		if(this.type.indexOf('l') != -1){
			this.parts['nw'].style.display = 'block';
			this.parts['sw'].style.display = 'block';
			this.parts['w'].style.display = 'block';
			this.parts['n'].style.width = parseInt(this.parts['n'].style.width) - parseInt(this.parts['w'].style.width)+'px';
			this.parts['s'].style.width = parseInt(this.parts['s'].style.width) - parseInt(this.parts['w'].style.width)+'px';
			this.parts['n'].style.marginLeft = parseInt(this.parts['nw'].style.width)+'px';
			this.parts['s'].style.marginLeft = parseInt(this.parts['nw'].style.width)+'px';
		}
		
		if(this.type.indexOf('t') != -1){
			this.parts['nw'].style.display = 'block';
			this.parts['ne'].style.display = 'block';
			this.parts['n'].style.display = 'block';
			this.parts['w'].style.height = parseInt(this.parts['w'].style.height) - parseInt(this.parts['nw'].style.height)+'px';
			this.parts['e'].style.height = parseInt(this.parts['e'].style.height) - parseInt(this.parts['nw'].style.height)+'px';
			this.parts['w'].style.marginTop = parseInt(this.parts['n'].style.height)+'px';
			this.parts['e'].style.marginTop = parseInt(this.parts['n'].style.height)+'px';
		}
		if(this.type.indexOf('b') != -1){
			this.parts['sw'].style.display = 'block';
			this.parts['se'].style.display = 'block';
			this.parts['s'].style.display = 'block';
			this.parts['w'].style.height = parseInt(this.parts['w'].style.height) - parseInt(this.parts['sw'].style.height)+'px';
			this.parts['e'].style.height = parseInt(this.parts['e'].style.height) - parseInt(this.parts['sw'].style.height)+'px';
			this.parts['s'].style.marginTop = parseInt(this.parentsize().height) - parseInt(this.parts['s'].style.height) + 'px';
		}
		if(this.type.indexOf('r') != -1){
			this.parts['ne'].style.display = 'block';
			this.parts['se'].style.display = 'block';
			this.parts['e'].style.display = 'block';
			this.parts['n'].style.width = parseInt(this.parts['n'].style.width) - parseInt(this.parts['se'].style.width)+'px';
			this.parts['s'].style.width = parseInt(this.parts['s'].style.width) - parseInt(this.parts['se'].style.width)+'px';
			this.parts['e'].style.marginLeft = parseInt(this.parentsize().width) - parseInt(this.parts['e'].style.width) + 'px';
		}
	};	
	this.getImagePartUrl = function(x, y, w, h){
		var cvs = document.createElement('canvas');
		cvs.setAttribute('width', w);
		cvs.setAttribute('height', h);
		if (cvs.getContext){
			var _ctx = cvs.getContext('2d');
			_ctx.drawImage(this.image, x, y, w, h, 0, 0, w, h);
			return cvs.toDataURL();
		}else{
			return '';
		}
	};
	this.getParts = function(){
		var result = new Array();
		var vml = false;
		result['nw'] = document.createElement('div');
		result['nw'].style.backgroundColor = 'transparent';
		result['nw'].style.backgroundRepeat = 'no-repeat';
		result['nw'].style.backgroundImage = 'url('+this.image.src+')';
		result['nw'].style.backgroundPosition = 'left top';
		result['nw'].style.width = parseInt(this.size.width/2)+'px';
		result['nw'].style.height = parseInt(this.size.height/2)+'px';
		result['nw'].style.display = 'none';
		result['nw'].style.marginLeft = '0px';
		result['nw'].style.marginTop = '0px';
		var pixelpercent;

		if(this.size.width % 2){
			result['n'] = document.createElement('img');
			result['n'].style.backgroundColor = 'transparent';
			result['n'].style.width = this.parentsize().width + 'px';
			result['n'].style.height = parseInt(this.size.height/2)+'px';
			result['n'].style.display = 'none';
			result['n'].src = this.getImagePartUrl(parseInt(this.size.width/2), 0, 1, parseInt(this.size.height/2));
			if((result['n'].src.indexOf('data:image')==-1)){
				result['n'] = document.createElement('div');
				result['n'].style.backgroundColor = 'transparent';
				result['n'].style.width = this.parentsize().width + 'px';
				result['n'].style.height = parseInt(this.size.height/2)+'px';
				result['n'].style.display = 'none';
				pixelpercent = 1.1/this.size.width;
				pixelpercent = pixelpercent.valueOf(2);
				result['n'].insertAdjacentHTML('beforeEnd', '<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" /><v:image src="'+this.image.src+'" style="width:100%;height:'+result['n'].style.height+';" cropleft="'+(parseInt(100*(0.5-pixelpercent))/100)+'" croptop="'+0+'" cropright="'+(parseInt(100*(0.5+pixelpercent))/100)+'" cropbottom="'+0.5+'" width="100%" height="'+result['n'].style.height+'"></v:image>');
			}	

			result['s'] = document.createElement('img');
			result['s'].style.backgroundColor = 'transparent';
			result['s'].style.width = this.parentsize().width + 'px';
			result['s'].style.height = parseInt(this.size.height/2)+'px';
			result['s'].style.display = 'none';
			result['s'].src = this.getImagePartUrl(parseInt(this.size.width/2), this.size.height-parseInt(this.size.height/2), 1, parseInt(this.size.height/2));
			if((result['s'].src.indexOf('data:image')==-1)){
				result['s'] = document.createElement('div');
				result['s'].style.backgroundColor = 'transparent';
				result['s'].style.width = this.parentsize().width + 'px';
				result['s'].style.height = parseInt(this.size.height/2)+'px';
				result['s'].style.display = 'none';
				pixelpercent = 1.1/this.size.width;
				result['s'].insertAdjacentHTML('beforeEnd', '<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" /><v:image src="'+this.image.src+'" style="width:100%;height:'+result['s'].style.height+';" cropleft="'+(parseInt(100*(0.5+pixelpercent))/100)+'" croptop="'+0.5+'" cropright="'+(parseInt(100*(0.5+pixelpercent))/100)+'" cropbottom="'+0+'" width="100%" height="'+result['s'].style.height+'"></v:image>');
			}	
		}

		result['ne'] = document.createElement('div');
		result['ne'].style.backgroundColor = 'transparent';
		result['ne'].style.backgroundRepeat = 'no-repeat';
		result['ne'].style.backgroundImage = 'url('+this.image.src+')';
		result['ne'].style.backgroundPosition = 'right top';
		result['ne'].style.width = parseInt(this.size.width/2)+'px';
		result['ne'].style.height = parseInt(this.size.height/2)+'px';
		result['ne'].style.display = 'none';
		result['ne'].style.marginLeft = this.parentsize().width-parseInt(result['ne'].style.width)+'px';
		result['ne'].style.marginTop = '0px';

		if(this.size.height % 2){
			result['e'] = document.createElement('img');
			result['e'].style.backgroundColor = 'transparent';
			result['e'].style.height = this.parentsize().height + 'px';
			result['e'].style.width = parseInt(this.size.width/2)+'px';
			result['e'].style.display = 'none';
			result['e'].src = this.getImagePartUrl(this.size.width-parseInt(this.size.width/2), parseInt(this.size.height/2), parseInt(this.size.width/2), 1);
			if((result['e'].src.indexOf('data:image')==-1)){
				result['e'] = document.createElement('div');
				result['e'].style.backgroundColor = 'transparent';
				result['e'].style.height = this.parentsize().height + 'px';
				result['e'].style.width = parseInt(this.size.width/2)+'px';
				result['e'].style.display = 'none';
				pixelpercent = 1.1/this.size.height;
				result['e'].insertAdjacentHTML('beforeEnd', '<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" /><v:image src="'+this.image.src+'" style="height:100%;width:'+result['e'].style.width+';" cropleft="'+(0.5)+'" croptop="'+(parseInt(100*(0.5-pixelpercent))/100)+'" cropright="'+(1)+'" cropbottom="'+(parseInt(100*(0.5+pixelpercent))/100)+'" width="'+result['e'].style.width+'" height="100%"></v:image>');
			}	

			result['w'] = document.createElement('img');
			result['w'].style.backgroundColor = 'transparent';
			result['w'].style.height = this.parentsize().height + 'px';
			result['w'].style.width = parseInt(this.size.width/2)+'px';
			result['w'].style.display = 'none';
			result['w'].src = this.getImagePartUrl(0, parseInt(this.size.height/2), parseInt(this.size.width/2), 1);
			if((result['w'].src.indexOf('data:image')==-1)){
				result['w'] = document.createElement('div');
				result['w'].style.backgroundColor = 'transparent';
				result['w'].style.height = this.parentsize().height + 'px';
				result['w'].style.width = parseInt(this.size.width/2)+'px';
				result['w'].style.display = 'none';
				pixelpercent = 1.1/this.size.height;
				result['w'].insertAdjacentHTML('beforeEnd', '<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" /><v:image src="'+this.image.src+'" style="height:100%;width:'+result['w'].style.width+';" cropleft="'+(0)+'" croptop="'+(parseInt(100*(0.5-pixelpercent))/100)+'" cropright="'+(0.5)+'" cropbottom="'+(parseInt(100*(0.5+pixelpercent))/100)+'" width="'+result['w'].style.width+'" height="100%"></v:image>');
			}	
		}
	
		result['se'] = document.createElement('div');
		result['se'].style.backgroundColor = 'transparent';
		result['se'].style.backgroundRepeat = 'no-repeat';
		result['se'].style.backgroundImage = 'url('+this.image.src+')';
		result['se'].style.backgroundPosition = 'right bottom';
		result['se'].style.width = parseInt(this.size.width/2)+'px';
		result['se'].style.height = parseInt(this.size.height/2)+'px';
		result['se'].style.display = 'none';
		result['se'].style.marginLeft = this.parentsize().width-parseInt(result['se'].style.width)+'px';
		result['se'].style.marginTop = this.parentsize().height-parseInt(result['se'].style.height)+'px';

		result['sw'] = document.createElement('div');
		result['sw'].style.backgroundColor = 'transparent';
		result['sw'].style.backgroundRepeat = 'no-repeat';
		result['sw'].style.backgroundImage = 'url('+this.image.src+')';
		result['sw'].style.backgroundPosition = 'left bottom';
		result['sw'].style.width = parseInt(this.size.width/2)+'px';
		result['sw'].style.height = parseInt(this.size.height/2)+'px';
		result['sw'].style.display = 'none';
		result['sw'].style.marginTop = this.parentsize().height-parseInt(result['sw'].style.height)+'px';

		return result;
	};
	this.parent.rbordereventListener = function(evt){
		var event = evt || window.event;
		if(event.attrName == 'borders') return false;
		if(this.borders){
			this.removeEventListener('DOMAttrModified', this.rbordereventListener, false);
			this.borders.destroy();
			this.borders = new R_border(this, this.borders.image.src, this.borders.type, this.borders.inner);
		}	
        	if(event.stopPropagation) event.stopPropagation();
        	event.cancelBubble = true;
        	return false;
	};
	this.image.onload = function(){
		if(!this.parent.inner){
			this.parent.make();
		}else{
			this.parent.makeinner();
		}
		if(document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5){
			this.parent.parent.onpropertychange = function(evt){
				var event = evt || window.event;
				if(event.propertyName == 'onpropertychange') return;
				if(event.propertyName == 'borders') return;
				if(this.borders){
					this.onpropertychange = null;
					this.borders.destroy();
					this.borders = new R_border(this, this.borders.image.src, this.borders.type, this.borders.inner);
				}	
			};
		}else{
			this.parent.parent.addEventListener('DOMAttrModified', this.parent.parent.rbordereventListener, false);
		}	
	};
	if(this.image.complete && document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5){
		this.image.onload();
	}
	return this;
}
function R_image(src, maxWidth, maxHeight, propW, propH, hasProportions){
	this.hasPreview = false;
	this.hasProportions = hasProportions;
	if(maxWidth && maxHeight) this.hasPreview = true;
	this.maxw = maxWidth;
	this.maxh = maxHeight;
	this.propw = propW;
	this.proph = propH;
	this.tmpimg = new Image();
	this.tmpimg.parent = this;
	this.src = src;
	this.file = {id: new Date().getTime()};

	this.init = function(){
		this.tmpimg.src = this.getSrc();
	};

	this.getSrc = function(){
		return this.src;
	};
	this.init();
}
function R_image_clipstopmove(){
	R.removeEvent(document, 'mouseover', R_image_clipmove);
	R.removeEvent(document, 'mouseup', R_image_clipstopmove);
	document.clipelem = null;
}
function R_image_clipmove(e){
	if(!document.clipelem) return false;
	var event = e || window.event;
	var left = parseInt(document.clipelem.divX0 + document.clipelem.defPosition(event).x - document.clipelem.x0)-1;
	var top = parseInt(document.clipelem.divY0 + document.clipelem.defPosition(event).y - document.clipelem.y0);
	if(left>=document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetLeft){
		if(left<=document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetLeft+document.clipelem.target.width-parseInt(document.clipelem.style.width)-2){
			document.clipelem.style.left = left + 'px';
		}else{
			document.clipelem.style.left = document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetLeft+document.clipelem.target.width-parseInt(document.clipelem.style.width)-2 + 'px';
		}
	}else{
		document.clipelem.style.left = document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetLeft + 'px';
	}
	if(top>=document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetTop){
		if(top<=document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetTop+document.clipelem.target.height-parseInt(document.clipelem.style.height)-2){
			document.clipelem.style.top = top + 'px';
		}else{
			document.clipelem.style.top = document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetTop+document.clipelem.target.height-parseInt(document.clipelem.style.height)-2 + 'px';
		}
	}else{
		document.clipelem.style.top = document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetTop + 'px';
	}
	document.clipelem.parent.clip_x1 = parseInt(document.clipelem.style.left)-document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetLeft+1;
	document.clipelem.parent.clip_y1 = parseInt(document.clipelem.style.top)-document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).offsetTop;
	document.clipelem.parent.clip_x2 = document.clipelem.parent.clip_x1+parseInt(document.clipelem.style.width)+2;
	document.clipelem.parent.clip_y2 = document.clipelem.parent.clip_y1+parseInt(document.clipelem.style.height)+2;
	document.getElementById('r_clippedimg_'+document.clipelem.parent.parent.file.id).style.clip = 'rect('+document.clipelem.parent.clip_y1+'px, '+document.clipelem.parent.clip_x2+'px, '+document.clipelem.parent.clip_y2+'px, '+document.clipelem.parent.clip_x1+'px)';
	document.clipelem.parent.updatePreview();
	if(event.stopPropagation) event.stopPropagation();
	event.cancelBubble = true;
       	return false;
}
function R_image_clipresize(e){
	if(!document.clipresizeelem) return false;
	var event = e || window.event;
	var left = parseInt(document.clipresizeelem.divX0 + document.clipresizeelem.parent.defPosition(event).x - document.clipresizeelem.x0)-1;
	var top = parseInt(document.clipresizeelem.divY0 + document.clipresizeelem.parent.defPosition(event).y - document.clipresizeelem.y0)-1;
	if(document.clipresizeelem.parent.parent.parent.hasProportions){
		if(left+document.clipresizeelem.parent.parent.clip_x1+2<=parseInt(document.clipresizeelem.parent.target.width)){
			if(parseInt(left*document.clipresizeelem.parent.parent.parent.proph/document.clipresizeelem.parent.parent.parent.propw)+document.clipresizeelem.parent.parent.clip_y1+2<=parseInt(document.clipresizeelem.parent.target.height)){
				document.clipresizeelem.parent.style.width = left+'px';
				document.clipresizeelem.parent.style.height = parseInt(left*document.clipresizeelem.parent.parent.parent.proph/document.clipresizeelem.parent.parent.parent.propw)+'px';
				document.clipresizeelem.style.left = document.clipresizeelem.parent.style.width;
				document.clipresizeelem.style.top = document.clipresizeelem.parent.style.height;
			}
		}
	}else{
		if(left+document.clipresizeelem.parent.parent.clip_x1+2<=parseInt(document.clipresizeelem.parent.target.width)){
			document.clipresizeelem.parent.style.width = left+'px';
		}else{
			document.clipresizeelem.parent.style.width = parseInt(document.clipresizeelem.parent.target.width)-document.clipresizeelem.parent.parent.clip_x1-2+'px';
		}	
		if(top+document.clipresizeelem.parent.parent.clip_y1+2<=parseInt(document.clipresizeelem.parent.target.height)){
			document.clipresizeelem.parent.style.height = top+'px';
		}else{
			document.clipresizeelem.parent.style.height = parseInt(document.clipresizeelem.parent.target.height)-document.clipresizeelem.parent.parent.clip_y1-2+'px';
		}
		document.clipresizeelem.style.left = document.clipresizeelem.parent.style.width;
		document.clipresizeelem.style.top = document.clipresizeelem.parent.style.height;
	}
	document.clipresizeelem.parent.parent.clip_x1 = parseInt(document.clipresizeelem.parent.style.left)-document.getElementById('r_clippedimg_'+document.clipresizeelem.parent.parent.parent.file.id).offsetLeft+1;
	document.clipresizeelem.parent.parent.clip_y1 = parseInt(document.clipresizeelem.parent.style.top)-document.getElementById('r_clippedimg_'+document.clipresizeelem.parent.parent.parent.file.id).offsetTop;
	document.clipresizeelem.parent.parent.clip_x2 = document.clipresizeelem.parent.parent.clip_x1+parseInt(document.clipresizeelem.parent.style.width)+2;
	document.clipresizeelem.parent.parent.clip_y2 = document.clipresizeelem.parent.parent.clip_y1+parseInt(document.clipresizeelem.parent.style.height)+2;
	document.getElementById('r_clippedimg_'+document.clipresizeelem.parent.parent.parent.file.id).style.clip = 'rect('+document.clipresizeelem.parent.parent.clip_y1+'px, '+document.clipresizeelem.parent.parent.clip_x2+'px, '+document.clipresizeelem.parent.parent.clip_y2+'px, '+document.clipresizeelem.parent.parent.clip_x1+'px)';
	document.clipresizeelem.parent.parent.updatePreview();
	if(event.stopPropagation) event.stopPropagation();
	event.cancelBubble = true;
       	return false;
}
function R_image_clipstopresize(){
	R.removeEvent(document, 'mouseover', R_image_clipresize);
	R.removeEvent(document, 'mouseup', R_image_clipstopresize);
	document.clipresizeelem = null;
}
