var menuTimer = null;
var showMenuBlock = 0;
var currentX = -1;
var currentY = -1;
var signinPop = 0;
var $panelTimer = null;

/**
 * Start menu generation timer. It's needed so that browser is able to calculate
 * new mouse positions witch are cathed by userland onmousemove handler
 */
function showMenu(heading, timeout, x, y, html) {
	if (typeof timeout == "undefined") {
		timeout = 500;
	}
    if (typeof x == "undefined") {
        x = 0;
    }
    if (typeof y == "indefined") {
        y = 0;
    }
    if (typeof html == "undefined") {
        html = '';
    }
	menuTimer = setTimeout('showContextMenu("'+heading+'", '+x+','+y+', "'+escape(html)+'");', timeout);
}

/**
 * Generate menu HTML and draws it
 */
function showContextMenu(heading, x,y, html) {
	if (showMenuBlock == 1) {
		return;
	}
    if (typeof heading == "undefined") {
        heading = 'heading!';
    }
	var menu = '<div class="header">'+heading+'</div>'+html;
	displayBlock(menu, "contextmenu", x, y);
}





/**
 * Write HTML to specified element and show it near the mouse pointer
 */
function displayBlock(html, id, x, y) {

	var obj = document.getElementById(id);
	obj.innerHTML = html;
    obj.style.display = 'block';

    //calculatePosition(id);
	obj.style.left = currentX + x + 'px';
	obj.style.top = currentY + y + 'px';

	showMenuBlock = 1;

}





/**
 * Set timer for context menu hiding. Timer is needed because moving mouse over
 * context menu itself generates onmouseout within the image itself
 */
function hideMenu(id, force) {
	if (menuTimer) {
		clearTimeout(menuTimer);
		menuTimer = null;
	}

    if ($panelTimer) {
        clearTimeout($panelTimer);
        $panelTimer = null;
    }

	if (showMenuBlock == 1 || force) {
		showMenuBlock = 0;
		if (force) {
			hideContextMenu(id);
		} else {
			setTimeout('hideContextMenu("'+id+'")', 500);
		}
	}
}

/**
 * Drop menu before AJAX CALL
 *
 * @return
 */
function dropMenu() {
	if (menuTimer) {
		clearTimeout(menuTimer);
		menuTimer = null;
	}
	if (showMenuBlock == 1) {
		showMenuBlock = 0;
		hideContextMenu();
	}
}

/**
 * Hide block after timeout if hide flag is 0
 */
function hideContextMenu(id) {
	if (showMenuBlock == 0) {
			obj = document.getElementById(id);
			obj.innerHTML = '';
			obj.style.left = '-1000px';
			obj.style.top = '-1000px';
			//currentX = -1000;
			//currentY = -1000;
	}

}

/**
 * Calculate block drawing position taking into account browser window borders
 */
function calculatePosition(id) {
	var tabW = document.getElementById(id).offsetWidth;
	var tabH = document.getElementById(id).offsetHeight;

	var winW = 0;
	var winH = 0;
	var scrT = 0;
	if (window.innerWidth) {
		winW = window.innerWidth;
		winH = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
	} else if (document.body) {
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}

	if (window.pageYOffset) {
		scrT = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrT = document.documentElement.scrollTop;
	} else if (document.body) {
		scrT = document.body.scrollTop;
	}

	if (currentX > winW - tabW) {
		currentX = winW - tabW - 25;
	} else {
		currentX = currentX + 5;
	}

	if ((currentY - scrT) > (winH - tabH)) {
		currentY = scrT + winH - tabH - 5;
	} else {
		currentY = currentY + 10;
	}
}

function signinPopup(x,y, restore_url, captions) {
	if (signinPop) {
		hideMenu("manualpopup");
		signinPop = 0;
	} else {
		var html =  '<div>'+
						'<div class="signin-top"></div>'+
						'<div class="signin-mid">'+
							'<form id="login-form" method="post" onsubmit="procLogin(); return false;">'+
								'<div style="border-bottom: 1px solid #d2d3d2; padding-bottom: 4px; margin-bottom: 11px;">'+
									'<div class="floatl"><strong style="color: #6f0094">'+captions['signin']+'</strong></div>'+
									'<div class="floatr"><img class="closepic" src="/img/_.png"/>&nbsp;<a href="#" onclick="hideMenu(\'manualpopup\', true); return false;">'+captions['close']+'</a></div>'+
									'<div class="clear"></div>'+
								'</div>'+
								'<div id="loginerror" class="error"></div>'+
								captions['username']+'<br/>'+
								'<span class="input-text">'+
									'<input type="text" id="LoginForm_username" name="LoginForm[username]" style="width: 200px;" value=""/>'+
								'</span>'+
								'<br/>'+
								captions['password']+'<br/>'+
								'<span class="input-text">'+
									'<input type="password" id="LoginForm_password" name="LoginForm[password]" style="width: 200px;" value=""/>'+
								'</span>'+
								'<br/>'+
                                '<div><a href="'+restore_url+'">'+captions['forgot']+'</a></div>'+
                                 '<div class="hidden" id="login-form-code"></div>'+
								'<input type="hidden" name="ajax" value="login-form"/>'+
								'<span id="inbtn" class="input-button" style="margin-top: 4px; margin-bottom: 7px;">'+
									'<span>'+
										'<img class="signin" src="/img/_.png"/>'+
										"<input type=\"button\" onclick=\"procLogin();\">"+
									'</span>'+
								'</span>'+
							'</form>'+
						'</div>'+
						'<div class="signin-bottom"></div>'+
					'</div>';
		x = x-196;
		y = y+16;

		currentX = -1;
		currentY = -1;

		signinPop = 1;
		displayBlock(html, "manualpopup", x, y);
        $('#LoginForm_username').focus();

        $('#LoginForm_username').keypress(function(event) {
            if (event.keyCode == '13') {
                $('#LoginForm_password').focus();
                return false;
            }
        });

        $('#LoginForm_password').keypress(function(event) {
            if (event.keyCode == '13') {
                procLogin();
            }
        });
	}
}

function isEmpty(ob){
   for(var i in ob){ if(ob.hasOwnProperty(i)){return false;}}
  return true;
}


function floatPanel(x, y, html, key) {
    x = x - 3;
    y = y - 4;
    displayBlock('<div id="inner-block-'+key+'">'+html+'</div>', "panelpopup", x, y);

    clearTimeout($panelTimer);
    $panelTimer = setTimeout('checkPanelHover()', 500);
}

function checkPanelHover() {
    if (showMenuBlock) {
        $(document).mousemove(function(e) {
            if (
                (e.pageX > $('#panelpopup').offset().left + $('#panelpopup').outerWidth()) ||
                (e.pageY > $('#panelpopup').offset().top + $('#panelpopup').outerHeight()) ||
                (e.pageX < $('#panelpopup').offset().left) ||
                (e.pageY < $('#panelpopup').offset().top)
                ) {
                hideMenu('panelpopup');
            }
        });
        $panelTimer = setTimeout('checkPanelHover()', 500);
    }
}

