function showLoginForm(bScroll)
{	
	if ($('loginDiv'))
	{
		var bScroll = bScroll==null || bScroll;
		$('loginResult').innerHTML = '';
		if (bScroll)
			new Effect.ScrollTo('loginDiv');		
		new Effect.SlideDown('loginDiv', {
			duration: 0.3, 
			afterFinish: function(){ if ($('login').value != ""){$('password').focus();} else $('login').focus(); }
		});
	}
	return false;
}
function hideLoginForm()
{
	new Effect.SlideUp('loginDiv', {duration: 0.3});
	return false;
}
function notLogin()
{
	new Effect.ScrollTo('loginDiv');
	showLoginForm();
	$('loginResult').innerHTML = 'Необходима авторизация';
	new Effect.Pulsate('loginResult');
	return false;
}
function tryLogin()
{
	$('loginResult').innerHTML = '';
	$('loginForm').request({
		onComplete: function(oReq, oJson){
			var data = eval('('+ oReq.responseText + ')');
			$('loginResult').innerHTML = data.error;	
			if (!data.error) {
				if (location.href.match(/(.*)register\/success$/i))
					location.href = location.href.match(/(.*)register\/success$/i)[1];
				else
					location.href = location.href.replace(/#.*$/, '');
			}
		}
	});			
	return !($('loginResult').innerHTML == '');
}

function tryLoginOpenID()
{
	$('loginResultOpenID').innerHTML = '';
	$('openidForm').request({
		onComplete: function(oReq, oJson){
			var data = eval('('+ oReq.responseText + ')');
			$('loginResultOpenID').innerHTML = data.error;	
			if (!data.error) {
				location.href = data.redirect;
			}
		}
	});			
	return !($('loginResult').innerHTML == '');
}

// force hide AJAX loading indicator(if some bugs occured)
function forceLoadingHide() {
  if (Ajax.activeRequestCount < 1 && document.getElementById('loadingIndicator')) {
    Element.hide('loadingIndicator');
  }
  setTimeout('forceLoadingHide', 2000);
}

function initializePage() {
    forceLoadingHide();

    responder = {
        onComplete : function() {
            if (Ajax.activeRequestCount < 1) {
                Element.hide('loadingIndicator');
            }
        },

        onCreate : function() {
            if (document.getElementById('loadingIndicator')) {
                document.getElementById('loadingIndicator').style.display = 'block';
            }
        }
    }
    Ajax.Responders.register(responder);
}

function ShowHide(id)
{
	if ($(id).visible()) {		
		$(id).hide();
	} else {
		$(id).show();
	}	
	return false;			
}

function ShowHideImg(postid, imgId, hideImgSrc, showImgSrc)
{
	var id = 'pagetext'+postid;
	if ($(id).visible()) {		
		$(id).hide();
		$(imgId).src = hideImgSrc;
		//new Effect.ScrollTo('body');
	} else {
		$(id).show();
		$(imgId).src = showImgSrc;
	}	

	//console.log(id);
	if ($('poston'+postid))
	{
		$('poston'+postid).hide();
		$('poston'+postid).show();
	}
	
	return false;			
}

var activeMenu = null;
function ShowHideMenu(id)
{
	if ($(id).visible()) {		
		$(id).hide();
		activeMenu = null;
	}		
	else {
		$(id).show();
		if (activeMenu) 
			$(activeMenu).hide();
		activeMenu = id;
	}	
	return false;			
}
function closeActiveMenu(e)
{
	e = e || window.event; // IE doesn't pass event as argument.
	var target = e.target || e.srcElement; // IE doesn't use .target
	var id = target.id;	
	if (activeMenu && 
		id!=activeMenu && 
		id!=activeMenu+'_i' && 
		id!=activeMenu+'_ia' && 
		id!=activeMenu+'_d')
	{
		$(activeMenu).hide();
		activeMenu = null;
	}			
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name+"="+escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
	  	if (c_start!=-1)
	    { 
	    	c_start=c_start + c_name.length+1; 
	    	c_end=document.cookie.indexOf(";",c_start);
	    	if (c_end==-1) 
	    		c_end=document.cookie.length;
	    	return unescape(document.cookie.substring(c_start,c_end));
	    } 
	}
	return "";
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// #############################################################################
// function to handle the different event models of different browsers
// and prevent event bubbling
function prBub(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function viewUserMenu(oImg, nUserId)
{
	var userMenu = $('userMenu');
	var imgSize = $(oImg).getDimensions();
	$('menuProfile').href = '/user/'+nUserId;
	$('menuBlog').href = '/blog/'+nUserId;
	$('menuCalendar').href = '/usercalendar/'+nUserId;
	$('menuStat').href = '/userstat/'+nUserId;
	
	Position.clone(oImg, userMenu, {
            setHeight: false, 
            setWidth: false, 
            offsetTop: oImg.offsetHeight+2,
            offsetLeft: oImg.offsetWidth-imgSize.width
    });	
	userMenu.show();
}

/** Open popup window.
 * @param string url       url to open in popup window
 * @param string name      window name
 * @param int    width     window width
 * @param int    height    window height
 * @param bool   bIsResize true - window resizable, false - fixed size
 */
function openWin(url, name, width, height, bIsResize){
  if (!width)
        width = screen.width*0.9;
  if (!height)
        height = screen.height*0.75;
  IsReize = bIsResize?1:0;
  var newWindow = window.open(url, name, 'left=' + Math.ceil((screen.width - width)/2) + ',top=' + Math.ceil((0+screen.height - height)/2) + ',width=' + width + ',height=' + height + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable='+bIsResize+',channelmode=0,fullscreen=0');
  return false;
} 

function ShowOptionsMenu()
{
	var optionsMenu = $('optionsMenu');
	if (!optionsMenu.visible())
	{
		var oImg = $('menu_options_img');
		Position.clone(oImg, optionsMenu, {
	            setHeight: false, 
	            setWidth: false, 
	            offsetTop: 26
	    });
	}
	ShowHide('optionsMenu');
	return false;
}

function ShowAddMenu()
{
	var addMenu = $('addMenu');
	if (!addMenu.visible())
	{
		var oImg = $('addMenu_li');
		Position.clone(oImg, addMenu, {
	            setHeight: false, 
	            setWidth: false, 
	            offsetTop: -1,
	            offsetLeft: 0
	    });
	}
	ShowHide('addMenu');
	return false;
}


// Common functions
document.onclick = closeActiveMenu;
