function iso8859_1_chars_to_entities(src)
{
	for (var i = 192; i <= 255; i++)
	{
		while (src.search(String.fromCharCode(i)) > -1)
			src = src.replace(String.fromCharCode(i), '&#' + i + ';');
	}
		
	return src;
}

function entities_to_iso8859_1_chars(src)
{
	for (var i = 192; i <= 255; i++)
	{
		var entity = '&#' + i + ';';
		while (src.search(entity) > -1)
			src = src.replace(entity, String.fromCharCode(i));
	}
		
	return src;
}

function searchInSelect(select, search)
{
	if (!select)
		return -1;
		
	for (var i = 0; i < select.options.length; i++)
	{
		if (select.options[i].value == search)
			return i;
	}
	
	return -1;
}

function geoLoadObjectMap(divId, lat, lng)
{
	if (lat != '' && lng != '' && $(divId))
	{
	  var map = new GMap2($(divId));
	  var point = new GLatLng(lat, lng);
	  map.setCenter(point, 15);
	  map.addOverlay(new GMarker(point));
	  //map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());  	
	}
}

////////////////////////////////////////////////////////

var _cursor = { x: 0, y: 0};

function handlerOnMouseMove(event)
{
	//document.title = event.pointerX() + ' ' + event.pointerY();
	_cursor['x'] = event.pointerX();
	_cursor['y'] = event.pointerY();
}


function getViewportSize() {
    var size = {};

    if (typeof window.innerWidth != 'undefined') {
        size.width  = window.innerWidth,
        size.height = window.innerHeight
    }
    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0) {
            size.width  = document.documentElement.clientWidth,
            size.height = document.documentElement.clientHeight
    } else {
        size.width  = document.getElementsByTagName('body')[0].clientWidth,
        size.height = document.getElementsByTagName('body')[0].clientHeight
    }

    return size;
}

function moveElementToViewportCenter(element)
{
	if (element==null) {
		alert('moveElementToViewportCenter error. Элемент не существует ')
		return '';
	}
	element.style.position = 'absolute';

	if (!document.viewport) {
		var scroll = new Array();
		scroll['top'] = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		var width  = window.innerWidth || document.getElementsByTagName('body')[0].clientWidth || document.documentElement.clientWidth;
	} else {
		var scroll = document.viewport.getScrollOffsets();
		var width  = document.viewport.getWidth();
	}

	if (width == 0)
		width = document.body.clientWidth;
	
	x = (width / 2) - ( element.getWidth() / 2);
	y = scroll['top'] + 100;
	
	element.style.left = x + 'px';
	element.style.top = y + 'px';
	
	//alert(element);
}

function cancelEvent(e)
{
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}


function hookEvent(element, eventName, callback)
{
  if(element.addEventListener)
  {
    if(eventName == 'mousewheel')
    {
      element.addEventListener('DOMMouseScroll', callback, false); 
    }
    element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}

function ISearchError() {
	moveElementToViewportCenter($('search_error'));
	$('search_error').style.display='block';
	$('search_error_black').style.display='block';
	$('iviewerror').value='';
    $('isenderror_check').style.display='none';
	$('isearcherror1').style.display='block';
	$('isearcherror2').style.display='none';		
	$('isenderror_form').style.display='block';		
}

function ISearchErrorSend(_text) {
	if ($('isearcherror1').className=="naddbut6n") return;
	$('isearcherror1').className="naddbut6n";
	$('senderros_loader').style.display = 'block';
	JsHttpRequest.query('/ajax', { 	'm': 'user->SendError',
									'url': _this_url,
        							'texterror': _text
								},
        function(result, errors) {
			$('senderros_loader').style.display = 'none';    	
            if (result['ok']) {
            	$('isenderror_check').style.display='block';
				$('isenderror_check').style.backgroundColor = '#ffffff';
				$('isenderror_check').className = 'auth11_';
				$('isenderror_check').innerHTML = '<span>Большое спасибо, мы обязательно разберемся в возникшей проблеме!</span>';
				new Effect.Highlight('isenderror_check',{duration:1, startcolor:'#ffffff', endcolor:'#ccffb3'});
				$('isenderror_check').style.backgroundColor = '#ccffb3';
				$('isearcherror1').style.display='none';
				$('isearcherror2').style.display='block';		
				$('isenderror_form').style.display='none';
            } else {
            	$('isenderror_check').style.display='block';
				$('isenderror_check').style.backgroundColor = '#ffffff';
				$('isenderror_check').className = 'auth11';
				$('isenderror_check').innerHTML = '<span>Не введен текст ошибки!</span>';
				new Effect.Highlight('isenderror_check',{duration:1, startcolor:'#ffffff', endcolor:'#fff2b3'});
				$('isenderror_check').style.backgroundColor = '#fff2b3';
				$('isearcherror1').className='naddbut6';
				
            }
        },
        true // true - не кешируется
    );
}

function JSGetDirById(id) {
	var id=id.toString();
	var dir=id.charAt(0);
	for (var i=1; i<id.length; i=i+1){
		dir=dir+'/'+id.charAt(i);
	}
	return dir;
}

function openPhotoInWindow(link, w, h) {
	window.open(link,'image_window','top=25,left=25,width=' + w + ',height=' + h + ',scrollbars=no,status=yes');return false;	
}

function TextTypograf(tiny_id) {
	var tiny_type = 1;
	if (tiny_id.substr(0,1)=='_') {
		tiny_type = 0;
		tiny_id=tiny_id.substr(1,tiny_id.length-1);
	}
	$('typograf_loader').style.display='block';
	JsHttpRequest.query('/ajax', { 
									'm': 'RemoteTypograf->TextTypograf',
									'text': ( (tiny_type==1) ? tinyMCE.get(tiny_id).getContent() : $(tiny_id).value )
		},
		function(result, errors) {
			if (result) {
				if (result['error']) {
					$('typograf_loader').style.display='none';
					alert(result['error']);
				}
				if (result['text']!='') {
					if (tiny_type==1) {
						tinyMCE.get(tiny_id).setContent(result['text']);
					} else $(tiny_id).value=result['text'];
				}
			}
			$('typograf_loader').style.display='none';
		},
		true
	);
}

function numberEnd(n, form1, form2, form5){
    n = Math.abs(n) % 100;
    n1 = n % 10;
    if (n > 10 && n < 20) return form5;
    if (n1 > 1 && n1 < 5) return form2;
    if (n1 == 1) return form1;
    return form5;
}

function removeHTMLTags(text) {
	strInputCode = text.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;
}

if (typeof Prototype!='undefined') {
	if(!e) var e = Event; 
	if (e) e.observe(window, 'mousemove', handlerOnMouseMove);
}