////////////////////////////////////////////////////////////////////
//
//		Sliding to various positions
//
////////////////////////////////////////////////////////////////////


/* ---------------------------- Set up basic scroll effect  ---------------------------- */

// IMPORTANT - to avoid shuddering when starting an effect while another is still running
// the effect needs to be defined by iteself like this and then called from other functions
// not inluded in the other functions. You can set options for the effect with scroll.setOptions({ duration: 1000 })

var scroll = new Fx.Scroll(window, {
	wait: true,
	duration: 1500,
	offset: { 'x': -20, 'y': -20 },
	transition: Fx.Transitions.Quad.easeInOut
});

var scrollQuickly = new Fx.Scroll(window, {
	wait: true,
	duration: 1,
	offset: { 'x': -20, 'y': -20 },
	transition: Fx.Transitions.Quad.easeInOut
});



/* ---------------------------- Go to a particular cell  ---------------------------- */

function goToCell(cell_id,cell_width,row_height)
{		
		scroll.stop();
		
		// Stop any media players that might be playing
		stopAll();
		
		// Hide video under nav menu
		hideNavVideo();
		
		// Cell div ID is in format div_row_0_cell_1
		// Break it up into row and cell numbers
		var row_cell_array = cell_id.split("_");
		var r = row_cell_array[2];
		var c = row_cell_array[4];
		
		// Load the row containing the selected cell
		loadRow(r);
		
		// Set the selected cell as selected
		selected_cell = cell_id;
		
		// Set the nav link to show selected row
		// Special Mootools selector - selects all a elements with this class
		var navlinks = $$('div.nav_a_div_selected');
		
		for (x=0;x<navlinks.length;x++)
		{
			navlinks[x].className="nav_a_div";
		}
		
		var selected_navlink_id = "nav_" + Rows[r].menu_Section + "_row_" + r;
		document.getElementById(selected_navlink_id).className="nav_a_div_selected";
		
		// Get screen_w and screen_h from sizes.js
		getBrowserSize();
		var offset_x = -20;
		var offset_y = ((screen_h/2)-(row_height/2)) * -1;
		
		scroll.setOptions({ offset: { 'x': offset_x, 'y': offset_y } });
		
		// Go to the selected cell
		scroll.toElement(cell_id);
}


/* ---------------------------- Go to the first cell in a particular row  ---------------------------- */

function goToRow(r)
{	
		scroll.stop();
		
		// Stop any media players that might be playing
		stopAll();

		// Load the selected row
		loadRow(r);
		
		// Hide video under nav menu
		hideNavVideo();
		
		// Set the first cell as selected
		var cell_id = "div_row_" + r + "_cell_0";
		selected_cell = cell_id;
		
		// Set the nav link to show selected row
		// Special Mootools selector - selects all a elements with this class
		var navlinks = $$('div.nav_a_div_selected');
		
		for (x=0;x<navlinks.length;x++)
		{
			navlinks[x].className="nav_a_div";
		}
		
		var selected_navlink_id = "nav_" + Rows[r].menu_Section + "_row_" + r;
		document.getElementById(selected_navlink_id).className="nav_a_div_selected";
		
		// Get width and height of selected cell
		cell_width = Rows[r].Cells[0].cellWidth;
		row_height = Rows[r].rowHeight;
		
		// Get screen_w and screen_h from sizes.js
		getBrowserSize();
		// var offset_x = ((screen_w/2)-(cell_width/2)) * -1;
		var offset_x = -20;
		var offset_y = ((screen_h/2)-(row_height/2)) * -1;
		

		scroll.setOptions({ offset: { 'x': offset_x, 'y': offset_y } });
		
		// Go to the selected cell
		scroll.toElement(cell_id);

}


/* ---------------------------- Go to Co-ordinates  ---------------------------- */

function slideEventCoords(x,y,nav_a_div_id)
	{
		
	scroll.stop();
	
	// Stop any media players that might be playing
	stopAll();
	
	// Hide video under nav menu
	hideNavVideo();
	
	// Set the nav link to show selected row
	// Special Mootools selector - selects all a elements with this class
	var navlinks = $$('div.nav_a_div_selected');
	
	
	for (e=0;e<navlinks.length;e++)
	{
		navlinks[e].className="nav_a_div";
	}
	
	document.getElementById(nav_a_div_id).className="nav_a_div_selected";
	
	scroll.setOptions({ offset: { 'x': -20, 'y': -20 } });
		
	scroll.scrollTo(x,y);
	}


/* ---------------------------- Go sideways along a row  ---------------------------- */

function side_move(direction)
	{
		
		scroll.stop();
		
		// Hide video under nav menu
		hideNavVideo();
		
		var selected_cell_array = selected_cell.split("_");
		var r = selected_cell_array[2];
		var c = selected_cell_array[4];
			
		if (direction=="left")
		{
			// If this not the first cell in the row, prepare to move back
			if (c > 0)
			{
				c--;
			}
			cell_width = Rows[r].Cells[c].cellWidth;
			row_height = Rows[r].rowHeight;
		
			var next_prev_cell_id = "td_row_" + r + "_cell_" + c;
			
			goToCell(next_prev_cell_id,cell_width,row_height);
		}
		else if (direction=="right")
		{
			// If this is not the last cell in the row, prepare to move forward
			if (c < (Rows[r].Cells.length-1) )
			{
				c++;
			}
			cell_width = Rows[r].Cells[c].cellWidth;
			row_height = Rows[r].rowHeight;
		
			var next_prev_cell_id = "td_row_" + r + "_cell_" + c;
			
			goToCell(next_prev_cell_id,cell_width,row_height);
		}
		else if (direction=="down")
		{
			// If this is not the last row, prepare to move down
			if (r < (Rows.length-1) )
			{
				r++;
/*				if (Rows[r-1].Cells.length > Rows[r].Cells.length)
				{
					console.log("Alert");
					c = 0;
				}*/
				c = 0;
			}
			// console.log("R=" + r + " C=" + c + " Rows.Cells.length=" + Rows[r].Cells.length);
			cell_width = Rows[r].Cells[c].cellWidth;
			row_height = Rows[r].rowHeight;
		
			var next_prev_cell_id = "td_row_" + r + "_cell_" + c;
			
			goToCell(next_prev_cell_id,cell_width,row_height);
		}
		else if (direction=="up")
		{
			// If this is not the first row, prepare to move up
			if (r > 0)
			{
				r--;
				c = 0;
			}
			cell_width = Rows[r].Cells[c].cellWidth;
			row_height = Rows[r].rowHeight;
		
			var next_prev_cell_id = "td_row_" + r + "_cell_" + c;
			
			goToCell(next_prev_cell_id,cell_width,row_height);
		}
		
/*		// Get size of next/previous cell to pass to 
		// goToCell function so cell can be centred
		cell_width = Rows[r].Cells[c].cellWidth;
		row_height = Rows[r].rowHeight;
		
		var next_prev_cell_id = "td_row_" + r + "_cell_" + c;
		
		goToCell(next_prev_cell_id,cell_width,row_height);*/
		
	}


/* ---------------------------- Go to the first row but don't load it  ---------------------------- */

function goHome()
{	
		// scroll.stop();
		
		// Stop any media players that might be playing
		// stopAll();
		
		// Set the first cell as selected
		// selected_cell = "td_row_0_cell_0";
		
		// Set the nav link to show selected row
		// Special Mootools selector - selects all a elements with this class
		var navlinks = $$('div.nav_a_div_selected');
		
		for (x=0;x<navlinks.length;x++)
		{
			navlinks[x].className="nav_a_div";
		}
		
		document.getElementById("nav_to_top").className="nav_a_div_selected";
		
		// Get width and height of selected cell
		cell_width = Rows[0].Cells[0].cellWidth;
		row_height = Rows[0].rowHeight;
		
		// Get screen_w and screen_h from sizes.js
		getBrowserSize();
		// var offset_x = ((screen_w/2)-(cell_width/2)) * -1;
		var offset_x = -20;
		var offset_y = ((screen_h/2)-(row_height/2)) * -1;
		
		scroll.setOptions({ offset: { 'x': offset_x, 'y': offset_y } });
		
		// Go to the selected cell
		scroll.toElement("td_row_0_cell_0");

}


/* ---------------------------- Go to a cell quickly  ---------------------------- */

function goToRowQuickly(r)
{	
		scroll.stop();
		
		// Stop any media players that might be playing
		stopAll();
		
		// Hide video under nav menu
		hideNavVideo();

		// Load the selected row
		loadRow(r);
		
		// Set the first cell as selected
		var cell_id = "div_row_" + r + "_cell_0";
		selected_cell = cell_id;
		
		// Set the nav link to show selected row
		// Special Mootools selector - selects all a elements with this class
		var navlinks = $$('div.nav_a_div_selected');
		
		for (x=0;x<navlinks.length;x++)
		{
			navlinks[x].className="nav_a_div";
		}
		
		var selected_navlink_id = "nav_" + Rows[r].menu_Section + "_row_" + r;
		document.getElementById(selected_navlink_id).className="nav_a_div_selected";
		
		// Get width and height of selected cell
		cell_width = Rows[r].Cells[0].cellWidth;
		row_height = Rows[r].rowHeight;
		
		// Get screen_w and screen_h from sizes.js
		getBrowserSize();
		// var offset_x = ((screen_w/2)-(cell_width/2)) * -1;
		var offset_x = -20;
		// var offset_y = ((screen_h/2)-(row_height/2)) * -1;
		var offset_y = -12;

		scrollQuickly.setOptions({ offset: { 'x': offset_x, 'y': offset_y } });
		
		// Go to the selected cell
		scrollQuickly.toElement(cell_id);

}


/* ---------------------------- Hide nav video  ---------------------------- */

function hideNavVideo()
{	
	if (document.getElementById("nav_video"))
		{
			document.getElementById("nav_video").style.visibility = "hidden";
			document.getElementById("nav_video").style.display = "none";
		}
}

function showNavVideo()
{	
	if (document.getElementById("nav_video"))
		{
			document.getElementById("nav_video").style.display = "block";
			document.getElementById("nav_video").style.visibility = "visible";
		}
}