﻿var toggle_square = null;
var toggle_square_device = null;

var html_ext = '.html';
var timeout_max = 500;
var device_timeout_max = 1000;
var device_timeout_reference = null;

function init(target_page)
{

	var array_index = 0;

	if(target_page=='index')
	{
		toggle_square = new Array(17);
	}
	else if(target_page=='contact')
	{
		toggle_square = new Array(1);
	}
	else if(target_page=='device')
	{
		toggle_square = new Array(222);
		toggle_square_device = new Array(17);
	}	
	
	for(array_index=0;array_index<toggle_square.length;array_index++)
	{
		toggle_square[array_index] = 0;
	}
	
	array_index = 0;
	
	if(toggle_square_device)
	{
	
		for(array_index=0;array_index<toggle_square_device.length;array_index++)
		{
			toggle_square_device[array_index] = 0;
		}
		
	}

}

function toggleSquare(object, initial_color, change_color, index1, index1_offset, index2)
{

	var object_id = "";
	var index_position = "";

	if(object)
	{
		object_id = object.id;
		index_position = object_id.substring((object_id.indexOf(index1)+index1_offset), object_id.indexOf(index2));
		index_position = parseInt(index_position);
		index_position = index_position - 1;
		
		if(object_id.indexOf('device_div_bar_square_s')>-1)
		{
		
			if(toggle_square_device[index_position]==0)
			{
				object.style.backgroundColor=change_color;
				toggle_square_device[index_position]=1;
			}
			else
			{
				object.style.backgroundColor=initial_color;
				toggle_square_device[index_position]=0;
			}
		
		}
		else
		{
		
			if(toggle_square[index_position]==0)
			{
				object.style.backgroundColor=change_color;
				toggle_square[index_position]=1;
			}
			else
			{
				object.style.backgroundColor=initial_color;
				toggle_square[index_position]=0;
			}
			
		}
		
	}

}

function goToPage(target)
{

	var code_string = 'window.location.href=\''+target+html_ext+'\';';
	var go_to_page_timeout = setTimeout(code_string, timeout_max);
	
}

function getRandomNumber(max_value)
{

	return (Math.floor(Math.random()*(max_value+1)));

}

function render_device()
{

	var row_index = 1;
	var column_index = 1;
	var square_count = 1;
			
	for(row_index=1;row_index<13;row_index++)
	{
		document.write('<!-- ROW ' + row_index + ' -->');
		document.write('<DIV CLASS=\"device_div_bar_1\">');
			
		for(column_index=1;column_index<18;column_index++)
		{
			document.write('<DIV ONCLICK=\"toggleSquare(this, \'rgb(255,255,255)\', \'rgb(\'+getRandomNumber(255)+\',\'+getRandomNumber(255)+\',\'+getRandomNumber(255)+\')\', \'device_div_bar_square_\', 22, \'_id\');\" STYLE=\"width:40px;height:40px;background-color:rgb(255,255,255);float:left;margin-left:5px;\" ID=\"device_div_bar_square_' + square_count + '_id\"></DIV>');	
			square_count++;
		}
				
		document.write('</DIV>');
		document.write('<DIV CLASS=\"device_div_bar_2\"></DIV>');
	}

}

function action_device(mode, timeout_value)
{

	if(mode==0)
	{
	
		clearTimeout(device_timeout_reference);
	
	}
	
	if(mode==1)
	{
		
		var square_object = null;
		var square_index = 0;
		var color = "";
	
		for(square_index=1;square_index<222;square_index++)
		{
			square_object = document.getElementById('device_div_bar_square_' + square_index + '_id');
			
			if(square_object)
			{
				color = square_object.style.backgroundColor;
				toggleSquare(square_object, color, 'rgb('+getRandomNumber(255)+','+getRandomNumber(255)+','+getRandomNumber(255)+')', 'device_div_bar_square_', 22, '_id');
			}
			
		}
		
		clearTimeout(device_timeout_reference);
		device_timeout_reference = setTimeout('action_device(' + mode + ', ' + timeout_value + ');', timeout_value);
		
	}

}
