// Библиотека

function GetPosition (obj) {
	var el = obj;
	var x = 0;
	var y = (typeof(el.offsetHeight) == 'undefined') ? 0 : el.offsetHeight;

	var w = el.offsetWidth;
	var h = el.offsetHeight;

	//Walk up the DOM and add up all of the offset positions.
	while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
	{
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	}

	x += el.offsetLeft;
	y += el.offsetTop;

	return {left: x, top: y, width: w, height: h};
}

function al(text) {
	Content.Echo(text);
}	

function doGetCaretPosition (ctrl) {

	var CaretPos = 0;
	// IE Support
	if (document.selection) {

		ctrl.focus ();
		var Sel = document.selection.createRange ();

		Sel.moveStart ('character', -ctrl.value.length);

		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;

	return (CaretPos);

}

function setCaretPosition(ctrl, pos)
{
	if(ctrl.setSelectionRange)
	{
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

// Получение объекта DOM
// При наличии параметров, возможно создание
function getDomElement(ElementId, params) {
	var params = ( typeof(params) == 'undefined' ) ? null : params;
	
	var Element = $(ElementId);
	if ( Element === null && params !== null ) {
		if ( params.create ) {
			var temp = document.createElement(params.ElementType);
			temp.id = ElementId;
			temp.className = params.className;
			
			if ( typeof params.width !== 'undefined' && typeof params.height != 'undefined' ) {
				temp.style.width = params.width;
				temp.style.height = params.height;
			}
						
			document.body.appendChild(temp);		
			var Element = $(ElementId);
		}
	}
	
	return Element;
}



function mouseX (e) {
	if (e.pageX) return e.pageX;
	if (e.clientX) return e.clientX + document.body.scrollLeft;
}

function mouseY(e) {
	if(e.pageY) return e.pageY;
	if(e.clientY) return e.clientY + document.body.scrollTop;
}

/* Определяем текущее положение прокрутки */
function getCurrentScrollPosition() {
	var currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
	return currentOffset;
}


/* Работа с окнами */
function WindowOpen(winData) {
	//al(1);
	
	var nameWindow = Math.round(100*Math.random());
	
	if ( typeof(winData.left) == 'undefined' ) winData.left = (screen.width - winData.width) / 2;
	if ( typeof(winData.top) == 'undefined' )  winData.top  =  (screen.height - winData.height) / 2;
	
	if ( typeof(winData.scrollbars) == 'undefined' ) winData.scrollbars = 0;
	
	window.open(winData.url, nameWindow, "location=0, status=0, scrollbars=" + winData.scrollbars + ", menubar=0, resizable=0, width=" + winData.width + ", height=" + winData.height + ", left=" + winData.left + ", top=" + winData.top);
}

function WindowChangeSize() {
	window.resizeTo(500,600);
}


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };


function click_on_page(e) {
	if ( e.target ) {
    	if ( typeof(e.target.tagName) == 'undefined' ) return;
    	if ( e.target.tagName == 'A' || e.target.tagName == 'a') { stop(); }
	}	
}


function stop_moovie() {
	
	try {
		//if ( $('flv') !== null ) { 
			stop();
			//stop();
			// $('flv').remove();
		//}	
	} catch (e) {}
}


function errorHandler(e)
{
	//al(e);
	return true;
}

//назначаем обработчик для события 
window.onerror = errorHandler;

window.onload = function () {
	
	var links = document.getElementsByTagName('a');
	for ( var i = 0; i < links.length; i++) {
		Event.observe(links[i], 'click', function () { 
			stop_moovie();
			if ( $('flv') ) $('flv').innerHTML = $('flv').innerHTML;
		});
	}
	
}