/* $Id: nx_bubble.js,v 1.11 2010/11/10 09:12:20 tejash Exp $ */

/* Detect if the browser is IE or not. If it is not IE, we assume that the browser is NS. */
var IEbbl = document.all?true:false;

var IEbblBody = ietruebody();

function displayBubble(bblId,e,contEmail)
{
	var musX = getMouseX(e);
	var musY = getMouseY(e);
    var contEmail;

	/* Absolute bottom position (including scroll value) of browser-window (viewport) */
	var wndwHeight = getWinHeight();

	document.getElementById(bblId).style.display = 'inline';

	var bblWidth = document.getElementById(bblId).offsetWidth;
	var bblHeight = document.getElementById(bblId).offsetHeight + 30;

	/* vertical distance between musY and bottom edge of browser-window (viewport) */
	var bottomDist = wndwHeight - musY;

	if(bottomDist < bblHeight)
	{
		var bblY = musY - bblHeight;
	}
	else
	{
		var bblY = musY + 30;
	}

	var yPos;
    if (self.pageYOffset)
        yPos = self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
        yPos = document.documentElement.scrollTop;
    else if (document.body)
        yPos = document.body.scrollTop;

    var xPos;
    if (self.pageXOffset)
        xPos = self.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft)
        xPos = document.documentElement.scrollLeft;
    else if (document.body)
        xPos = document.body.scrollLeft;

    if(bblId == 'bubble_map' || bblId == 'list_bubble_map')
    {
        document.getElementById(bblId).style.top = yPos + "px";
        document.getElementById(bblId).style.left = xPos + "25%";
    }
    else if(contEmail == 1 && (bblId == 'contact' || bblId.startsWith('hidePriceHistory')))
	{
        if(bblId == 'contact')
        {
    		document.getElementById(bblId).style.top = yPos + "px";
    		document.getElementById(bblId).style.margin =  "0 0 0 85px";
        }
        else
        {
            var winWidth = 0, winHeight = 0;
            if( typeof( window.innerWidth ) == 'number' ) {
              //Non-IE
              winWidth = window.innerWidth;
              winHeight = window.innerHeight;
            } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
              //IE 6+ in 'standards compliant mode'
              winWidth = document.documentElement.clientWidth;
              winHeight = document.documentElement.clientHeight;
            } 
            
            var bblWidth = $(bblId).getWidth();;
            var bblHeight = $(bblId).getHeight();
            if (bblHeight==0) bblHeight =400;
            document.getElementById(bblId).style.left =( winWidth - bblWidth) / 2+document.documentElement.scrollLeft + "px";
            document.getElementById(bblId).style.top =( winHeight - bblHeight) / 2+document.documentElement.scrollTop + "px";
        }
		document.getElementById('overlay').style.display='block';
	}
	else if(contEmail == 1 && location.hash != '#DealerInfo')
    {
        document.getElementById(bblId).style.top = (musY - bblHeight) + "px";
        document.getElementById(bblId).style.left = (musX - 390) + "px";
    }
    else if(location.hash == '#DealerInfo')
    {
        document.getElementById(bblId).style.top = (bblY + 275) + "px";
        //document.getElementById(bblId).style.left = (bblX - 425) + "px";
    }
    else if(contEmail == 3 )
    {
    	document.getElementById(bblId).style.top = yPos + "px";
        document.getElementById(bblId).style.left = (musX - 390) + "px";
    }
    //Contact request bubble positioning in purchase ads in NETTIVERAROSA
    else if(contEmail == 2 )
    {
        document.getElementById(bblId).style.top = (musY - bblHeight + 100 ) + "px";
        document.getElementById(bblId).style.left = (musX - 390) + "px";
    }
    else if(contEmail == 4)
	{
		document.getElementById("lm").checked = true;
		document.getElementById(bblId).style.top = (musY + 5) + "px";
		document.getElementById(bblId).style.left = (musX - 724) + "px";

		if(bblId == 'bubble_map_search' && document.getElementById("body_banner"))
			document.getElementById("body_banner").style.visibility = "hidden";

		if(bblId == 'bubble_map_search' && document.getElementById("videocontainer"))
			document.getElementById("videocontainer").style.visibility = "hidden";
	}
    else if(contEmail == 5 ) // iFrame for venelehti , bubble position adjustment
        document.getElementById(bblId).style.top = (musY - bblHeight + 100 ) + "px";
	else
	{
		document.getElementById(bblId).style.top = bblY + "px";
		document.getElementById(bblId).style.left = (musX - 100) + "px";
	}
}

function hideBubble(bblId)
{
	document.getElementById(bblId).style.display = 'none';

	if(bblId == 'bubble_map_search' && document.getElementById("body_banner"))
		document.getElementById("body_banner").style.visibility = "visible";

	if(bblId == 'bubble_map_search' && document.getElementById("videocontainer"))
		document.getElementById("videocontainer").style.visibility = "visible";

	if(document.getElementById('overlay'))
	    document.getElementById('overlay').style.display='none';
}

function getMouseX(e)
{
	var tempX = 0;

	if (IEbbl)
	{
		/* Get the x pos.s if browser is IE */
		tempX = event.clientX + IEbblBody.scrollLeft;
	}
	else
	{
		/* Get the x pos.s if browser is NS */
		tempX = e.pageX;
	}

	/* catch possible negative values in NS4 */
	if (tempX < 0)
	{
		tempX = 0;
	}

	return tempX;
}

function getMouseY(e)
{
	var tempY = 0;

	if (IEbbl)
	{
		/* Get the y pos.s if browser is IE */
		tempY = event.clientY + IEbblBody.scrollTop;
	}
	else
	{
		/* Get the y pos.s if browser is NS */
		tempY = e.pageY;
	}

	/* catch possible negative values in NS4 */
	if (tempY < 0)
	{
		tempY = 0;
	}

	return tempY;
}

/* Gives absolute bottom position (Including scroll value) of bottom edge of browser visible window (viewport) */
function getWinHeight()
{
	/* Default value for old browser if(parseInt(navigator.appVersion)<3) */
	/*	var winW = 630, winH = 460;	*/

	/* We can also use if(navigator.appName.indexOf("Microsoft")!=-1) for the following */
	if(IEbbl && !window.opera)	/* For IE */
	{
// 		var winW = IEbblBody.offsetWidth + IEbblBody.scrollLeft;
// 		var winH = (IEbblBody.offsetHeight - 1222) + IEbblBody.scrollTop;
// 		var winW = IEbblBody.clientWidth + IEbblBody.scrollLeft;
		var winH = IEbblBody.clientHeight + IEbblBody.scrollTop;
	}
	else	/* For Netscape if(navigator.appName=="Netscape") */
	{
// 		var winW = window.innerWidth + window.pageXOffset;
		var winH = window.innerHeight + window.pageYOffset;
	}

	return winH;
}

function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
