var menuTimer = null;
var showPhoneBlock = 0;
var phoneTimer = null;
var showMenuBlock = 0;
var currentX = -1;
var currentY = -1;
var mapPop = 0;
var servicePop = 0;



/**
 * 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;
	showPhotoBlock = 1;
	showPhoneBlock = 1;

	//$.AjaxCall();

}


/**
 * 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) {
//	if (menuTimer) {
//		clearTimeout(menuTimer);
		menuTimer = null;
//	}
	if (showMenuBlock == 1) {
		showMenuBlock = 0;
		//setTimeout('hideContextMenu("'+id+'")', 500);
        hideContextMenu(id);
    }
}

/**
 * Hide block after timeout if hide flag is 0
 */
function hideContextMenu(id) {
	if (showMenuBlock == 0) {
		//$('#contextmenu').fadeout('slow', function() {
			obj = document.getElementById(id);
			obj.innerHTML = '';
			obj.style.left = '-500px';
			obj.style.top = '-500px';
			currentX = -1;
			currentY = -1;
		//});
	}
}

/**
 * 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;

	if (window.innerWidth) {
		var winW = window.innerWidth;
		var winH = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		var winW = document.documentElement.clientWidth;
		var winH = document.documentElement.clientHeight;
	} else if (document.body) {
		var winW = document.body.clientWidth;
		var winH = document.body.clientHeight;
	}

	if (window.pageYOffset) {
		var scrT = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		var scrT = document.documentElement.scrollTop;
	} else if (document.body) {
		var 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 mapPopup(x, y, place, prefix) {
//	if (mapPop) {
//		hideMenu("manualpopup");
//		mapPop = 0;
//	} else {

        var html =  '<div class="map-popup-top"></div>'+
                    '<div class="map-popup-middle">'+
                        '<div align="center"><img src="../img/loader.gif"/></div>'+
                    '</div>'+
                    '<div class="map-popup-bottom"></div>'

        x = x-145;
        y = y-245;

        currentX = -1;
        currentY = -1;

        mapPop = 1;
        displayBlock(html, "manualpopup", x, y);

        $.post(prefix+'/en/site/getMap/'+place, {}, function (data) {
            if (data != null) {
                var html =  '<span class="country"><strong>'+data.name+'</strong>,</span>&nbsp;<span class="cities">'+data.cities+'</span><br/>'+
                            '<img src="'+prefix+'/img/maps/'+place+'.jpg" width="315" height="190" style="border: 1px solid #b8c7d2"/>';
            } else {
                var html =  '<div align="center"><strong>Error.</strong></div>';
            }
            $('.map-popup-middle').html(html);

        }, 'json');
	//}
}


function servicePopup(x, y, lang) {
    if (servicePop) {
        hideMenu("manualpopup");
        servicePop = 0;
    } else {
        var html =  '<div class="service-pop-top"></div>'+
                    '<div class="service-pop-middle" style="padding-left: 15px;">'+
                        '<strong>Choose service:</strong><br/><br/>'+
                        '<center><img src="/img/loader.gif"/></center><br/>'+
                    '</div>'+
                    '<div class="service-pop-bottom"></div>';

        x = x-186;
        y = y+37;

        servicePop = 1;
        displayBlock(html, "manualpopup", x, y);

        $.post('/'+lang+'/controlPanel/getServices', {}, function (data) {
            if (data.error == '') {
                $('.service-pop-middle').html(data.html);
            }
        }, 'json');

    }
}

function isEmpty(ob){
   for(var i in ob){ if(ob.hasOwnProperty(i)){return false;}}
  return true;
}

