var initRonorpFunction = [];
var ajaxPageLoadClass = [];
var ajaxLoadCache = [];

ajaxPageLoadClass.push('a.ajax-link');

var loadingFrame = 0;

function _getPageDimensions(){
	var xScroll, yScroll;
	var pageDimensions = {};
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.getElementsByTagName('html')[0].scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
    yScroll = document.getElementsByTagName('html')[0].scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	if(yScroll < windowHeight){
		pageDimensions.height = windowHeight;
	} else { 
		pageDimensions.height = yScroll;
	}
	if(xScroll < windowWidth){	
		pageDimensions.width = windowWidth;
	} else {
		pageDimensions.width = xScroll;
	}
	return pageDimensions;
}
function _browserDimensions() {
	var pageDimensions = {};
	if (Prototype.Browser.IE) {
		pageDimensions.height = document.documentElement.clientHeight;
		pageDimensions.width = document.documentElement.clientWidth;   
	} else {
		pageDimensions.height = window.innerHeight;
		pageDimensions.width = document.width || document.body.offsetWidth;
	}
	return pageDimensions;
}


function animatePreloader(){
	if ($("ajax_overlay").style.display == 'none'){
		clearInterval(loadingTimer);
		return;
	}

	$("ajax_preloader").style.backgroundPosition = '0px '+(loadingFrame * -40) + 'px';

	loadingFrame = (loadingFrame + 1) % 12; 
}
	
function ajaxRequestStart(){
	if ($("ajax_overlay") && $("ajax_overlay").style.display != 'none'){
		return false;
	}
	var pageSize = _getPageDimensions();
	var overlay, iframe, preloader;
	
	if (!$('ajax_overlay')){
		overlay = Element.extend(document.createElement('div'));
		preloader = Element.extend(document.createElement('div'));
		iframe = Element.extend(document.createElement('iframe'));
		
		overlay.setAttribute('id', 'ajax_overlay');
		preloader.setAttribute('id', 'ajax_preloader');
		iframe.setAttribute('id', 'ajax_iframe_overlay');
		
		document.body.appendChild(iframe);
		document.body.appendChild(overlay);
		document.body.appendChild(preloader);
		
		overlay.addClassName('ajax-overlay');
		
		iframe.style.border = 'none';
		
		iframe.style.zIndex = '997';
		overlay.style.zIndex = '998';
		preloader.style.zIndex = '999';
		
	}else{
		overlay = $('ajax_overlay');
		iframe  = $('ajax_iframe_overlay');
		preloader  = $('ajax_preloader');
		
		overlay.show();
		iframe.show();
		preloader.show();
	}
	
	overlay.style.width = '100%';
	overlay.style.height = pageSize.height+'px';
	
	iframe.style.width = '100%';
	iframe.style.height = pageSize.height+'px';

	preloader.style.left = ($(document.body).getWidth()/2-20)+'px';
	preloader.style.top  = ($(document.body).getHeight()/2-20)+'px';
	
	$$('object,embed,select').each(function(elem){
		elem.style.visibility = 'hidden';
	});
	
	loadingFrame = 0;
	loadingTimer = setInterval(animatePreloader, 66);
	
}

function ajaxRequestEnd(){
	if ($('ajax_overlay')){
		$('ajax_overlay').hide();
		$('ajax_iframe_overlay').hide();
		$('ajax_preloader').hide();
		
		$$('object,embed,select').each(function(elem){
			elem.style.visibility = 'visible';
		});
		
	}
}


function ajaxRequestError(){
}
function ajaxRequestErrorLoad(){
}

function ajaxUpdateContent(blocks, response){
	if (typeof blocks == 'object'){
		blocks.each(function(block){
			elem = $(block.element);
			if (elem){
				elem.innerHTML = '';
				new Insertion.Bottom(elem, block.html); 
			}
		});
	}
}

function ajaxExecuteResponse(func, response){
	if (!func.match(/[^a-zA-Z0-9_]/)){
		var element = arguments[2] || false;
		eval("if (typeof "+func+" == 'function'){ "+func+"(response, element); }")
	}
}

function loadAjaxPage(url, method, params, hidden){

	if (typeof params != 'object'){
		params = {}
	}
	if (typeof method == 'undefined'){
		method = 'GET';
	}
	var inCacheUrl = url;
	var cached = $A(ajaxLoadCache).find(function(el){return el.url == inCacheUrl});
	if (method.toLowerCase() == 'get' && cached)
	{
		if (!hidden){
			ajaxUpdateContent(cached.response.update_blocks, cached.response, cached.params.element || false);
			if (cached.response.func)
			{
				ajaxExecuteResponse(cached.response.func, cached.response, cached.params.element || false);
			}
			initializeFunction();
		}
		return true;
	}
	
	var ajax = new Ajax.Request(url, {
			method : method,
			parameters:	params,
			onLoading: function(){
				if (!hidden)
					ajaxRequestStart();
			},
			onComplete:	function(response) {
				if (!hidden)
					ajaxRequestEnd();

				if(response.responseText.isJSON()){
					response = response.responseText.evalJSON();
					ajaxLoadCache.push({"url": inCacheUrl, "response" : response, "params" : params});
					if(!hidden){
						switch(response.command){
							case 'update':
								ajaxUpdateContent(response.update_blocks, response, params.element || false);
								break;
							default:
								break;
						}
						if (response.func){
							ajaxExecuteResponse(response.func, response, params.element || false);
						}
						initializeFunction();
					}
				}else{
					ajaxRequestErrorLoad();
				}
			}
	});
	
}

function initAjaxLoader(){
	ajaxPageLoadClass.each(function(className){
		$$(className).each(function(elem){
			if (!elem._initAjaxLoad){
				elem._initAjaxLoad = true;
				elem.observe('click', function(event){
					var a = Event.findElement(event, 'A');
					if (a != document){
						var doit = true;
						if (a.rel && a.rel.indexOf('confirm') != -1)
						{
							var str = a.rel.split('=');
							if (str[1])
							{
								doit = confirm(str[1]);
							}
						}
						if (doit)
						{
							document.location.hash = '#url='+encodeURIComponent(a.href);
							loadAjaxPage(a.href, 'get', {element : a});
						}
						Event.stop(event); 
					}
					return false;
				});
			}
		});
	});
}


function ajaxResponseStatic(data, element)
{
	if (window.HTMLElement)
	{
		HTMLElement.prototype.removeNode = function(removeChildren)
		{
		  if (Boolean(removeChildren)) return this.parentNode.removeChild(this);
		  else
		  {
		    var r = document.createRange();
		    r.selectNodeContents(this);
		    return this.parentNode.replaceChild(r.extractContents(),this);
		  }
		}
	}


	if (document.getElementById('ajax_static_content'))
	{
		document.getElementById('ajax_static_content').removeNode(true);
	}
	element.parentNode.insert({'after': '<div id="ajax_static_content">' + data.output + '</div>'});
	$(element).scrollTo();
}


initRonorpFunction.push(initAjaxLoader);

function initializeFunction(){
	initRonorpFunction.each(function(func){
		if (typeof func == 'function'){
			func();
		}
	});
}

document.observe("dom:loaded", initializeFunction)

function scrollableBanner(place){
	if (place == 'widesky' || place == 'sky'){
		$$('.banner_right').invoke('addClassName', 'fixed-position');
	}
}

function inserate_hide(response)
{
	if (response && response['element'])
		$(response['element']).hide();
	if (response && response['url'])
		window.location = response['url'];
	if (response && response['advert_url'])
	{
		ajaxCacheClean(response['advert_url']);
	}
	if (response && response['all_url'])
	{
		ajaxCacheClean(response['all_url']);
	}
}
function ajaxCacheClean(url)
{
	var cached = null;
	var inCacheUrl = url;
	if (cached = $A(ajaxLoadCache).find(function(el){return el.url == inCacheUrl}))
	{
		ajaxLoadCache = $A(ajaxLoadCache).without(cached);
	}
}
function createUserPhotoObserve()
{
	$A($$('.mailbox_user')).each(function(elem){
			if (!elem._photo)
			{
				Event.observe(elem, "mouseout", hideUserPhoto);
				Event.observe(elem, "mouseover", showUserPhoto);
				Event.observe(elem, "mousemove", moveUserPhoto);
			}

	});
	var check = $('photo_tip');
	if (!check)
	{
		var el = new Element('div', {id: 'photo_tip'});
		el.setStyle({display: 'none', position: 'absolute', top: 0, left: 0, 'zIndex': 1000});
		$(document.body).insert({top: el});
	}
}
function showUserPhoto(event)
{
	var el = Event.findElement(event, 'DIV');
	if (el)
	{
		el = $(el);
		var mouseX=Event.pointerX(event);
		var mouseY=Event.pointerY(event);
		var id = el.id.gsub('user_', '');
		var photoBlock=$('user_'+id+'_photo');
		if (photoBlock)
		{
			var photoTip=$('photo_tip');
			if (photoTip)
			{
				$(photoTip).update(photoBlock.innerHTML).setStyle( {top : (mouseY+18) + 'px', left : (mouseX+8) + 'px', position: 'absolute'}).show();
			}
		}
	}
}
initRonorpFunction.push(createUserPhotoObserve);

function hideUserPhoto(event)
{
	if ($('photo_tip'))
		$('photo_tip').hide();
}
function moveUserPhoto(event)
{
	var mouseX=Event.pointerY(event);
	var mouseY=Event.pointerX(event);
	if ($('photo_tip'))
		$('photo_tip').setStyle( {top : (mouseX+18) + 'px', left : (mouseY+8) + 'px', position: 'absolute'});
}
Event.observe(window, 'load', createUserPhotoObserve);