﻿// Javascript for home page only.

// Avoids clashing the tooltip with the flash game where it will be layered over/under depending on browser.
function ShowToolTip(posx, posy, title, message)
{
    // Figure out where mouse is in relation to the flash game so we can position our tooltip away from the flash.
    if ( posx <= 10 ) // To the left of the flash game.
    {
    	overlib(message, CAPTION, title, LEFT);
    }
    else if ( posy <= 2 ) // Above the flash game.
    {
    	overlib(message, CAPTION, title, LEFT, ABOVE, HEIGHT, 50);
    }
    else
    {
    	overlib(message, CAPTION, title);
    }
}

function GetCellClicked(e)
{
    var TheGrid;
    var GridPos;
	var posx = 0;
	var posy = 0;

	if (null == e) var e = window.event;

	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}

    TheGrid = getItemByName( 'GridUnderlay' );
    GridPos = findItemPosition(TheGrid);

    // Calculate the cell reference for the current cursor position.
    GridPos[0] = Math.ceil((posx - GridPos[0]) / 10);
    GridPos[1] = Math.ceil((posy - GridPos[1]) / 10);

    return GridPos;
}

function CatchMouse(e)
{
	var CellPos;

    CellPos = GetCellClicked(e);

	ShowToolTip(CellPos[0], CellPos[1], 'Space available', 'The space at ' + CellPos[0] + ',' + CellPos[1] + ' is available, click to contact us about it.');
}

// User has chosen a spot they wish to rent, go to the purchase page.
function RentSpace(e)
{
	var CellPos;

    CellPos = GetCellClicked(e);

    // Go to the BuyPixels page.
    window.location = './Contact.aspx';
//    alert('Cell clicked @ ' + CellPos[0] + ',' + CellPos[1]);
}

// Called when the mouse leaves the grid underlay, hide the tooltip here.
function ExitUnderlay()
{
    // Destroy the overlib tooltip.
    nd();
}

// Set up the home page to capture mouse overs.
function Initialise()
{
    var TheGrid;

    TheGrid = getItemByName( 'GridUnderlay' );
	TheGrid.onmousemove = CatchMouse;
	TheGrid.onmouseout = ExitUnderlay;
	TheGrid.onclick = RentSpace;

	if (TheGrid.captureEvents)
	{
		TheGrid.captureEvents(Event.MOUSEMOVE, Event.CLICK);
	}
}
