/***************************************************************************
 Jupiter Portal @ Jupiterportal.com
 Copyright (C) 2006 Cosmin Flavius (highstrike@gmail.com)

 Attribution-NonCommercial-ShareAlike 2.5 of Creative Commons
 (http://creativecommons.org)

 You are free:

    * to copy, distribute, display, and perform the work
    * to make derivative works

 Under the following conditions:

 Attribution - You must attribute the work in the manner specified
 by the author or licensor.

 Noncommercial - You may not use this work for commercial purposes.

 Share Alike - If you alter, transform, or build upon this work, you may
 distribute the resulting work only under a license identical to this one.

    * For any reuse or distribution, you must make
	  clear to others the license terms of this work.
    * Any of these conditions can be waived if you get
      permission from the copyright holder.

 Your fair use and other rights are in no way affected by the above.

 This is a human-readable summary of the Legal Code (the full license).
 (http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode)

****************************************************************************/

//-----------------------------------------------------------------
//  Take care of vars
//-----------------------------------------------------------------

var NaviArrayObjects 	= new Array;
var NaviArrayFadeState 	= new Array;
var NaviArraySize 		= 0;
var NaviFadeStateEnd 	= 15;
var NaviFadeStates		= new Array;
var NaviAllReady		= false;

////////////////////////////////////////////////////////
// Function:         fade_state
// Description: Function to define fade state

function fade_state()
{
	var state;
	var color_s;

	this.state = 0;
	this.color_s = 0;
}

////////////////////////////////////////////////////////
// Function:         NaviMouseOver
// Description: Execute when mouse is over

function NaviMouseOver(who)
{
	if(NaviAllReady)
	{
		NaviRemoveFromFade(who);
		who.style.background = '#fefefe';
	}
}

////////////////////////////////////////////////////////
// Function:         NaviMouseOut
// Description: Execute when mouse is out

function NaviMouseOut( who, color_s )
{
	if(NaviAllReady)
	{
		if (!color_s) color_s = 0;
		NaviAddFade(who, color_s);
		who.style.background = '#fefefe';
	}
}

////////////////////////////////////////////////////////
// Function:         NaviRemoveFromFade
// Description: Remove from Fade

function NaviRemoveFromFade( who )
{
	for(i = 0;i <= NaviArraySize; i++)
	{
		if(NaviArrayObjects[i] == who)
		{
			NaviArrayObjects[i] = NaviArrayObjects[NaviArraySize];
			NaviArrayFadeState[i] = NaviArrayFadeState[NaviArraySize];
			NaviArraySize--;
		}
	}
}

////////////////////////////////////////////////////////
// Function:         NaviAddFade
// Description: Adds Fade

function NaviAddFade(who, color_s)
{
	NaviArraySize++;
	NaviArrayObjects[NaviArraySize] = who;
	NaviArrayFadeState[NaviArraySize] = new fade_state();
	NaviArrayFadeState[NaviArraySize].color_s = color_s;
	if (NaviArraySize == 1) NaviUpdateFade();
}

////////////////////////////////////////////////////////
// Function:         NaviUpdateFade
// Description: Update Fade

function NaviUpdateFade()
{
	for(i = 1;i <= NaviArraySize; i++)
	{
		NaviArrayFadeState[i].state++;
		NaviArrayObjects[i].style.background = NaviConvertToBackground(NaviArrayFadeState[i].state, NaviArrayFadeState[i].color_s);

		if(NaviArrayFadeState[i].state == NaviFadeStateEnd)
		{
			NaviRemoveFromFade(NaviArrayObjects[i]);
			i--;
		}
	}

	if(NaviArraySize > 0) setTimeout('NaviUpdateFade()', 50);
}

////////////////////////////////////////////////////////
// Function:         NaviConvertToBackground
// Description: Convert to Background

function NaviConvertToBackground(state, color_s)
{
	return NaviFadeStates[color_s][state];
}

////////////////////////////////////////////////////////
// Function:         NaviInitFadeStates
// Description: Initialize Fade states

function NaviInitFadeStates(begin_color, end_color, number_of_states, color_s)
{
	if(number_of_states) NaviFadeStateEnd = number_of_states;

	NaviFadeStates[color_s] = new Array;
	r_now = HexToR(begin_color);
	g_now = HexToG(begin_color);
	b_now = HexToB(begin_color);

	r_trg = HexToR(end_color);
	g_trg = HexToG(end_color);
	b_trg = HexToB(end_color);

	r_add = (r_trg - r_now) / NaviFadeStateEnd;
	g_add = (g_trg - g_now) / NaviFadeStateEnd;
	b_add = (b_trg - b_now) / NaviFadeStateEnd;

	for(i = 0;i <= NaviFadeStateEnd; i++)
	{
		NaviFadeStates[color_s][i] = RGBToHex(Math.round(r_now), Math.round(g_now), Math.round(b_now));
		r_now += r_add;
		g_now += g_add;
		b_now += b_add;
	}

	NaviAllReady = true;
}

////////////////////////////////////////////////////////
// Function:         NumToHex
// Description: Num to Hex

function NumToHex(num)
{
	if(num > 255) num = 255;
	first = 0;
	while(num >= 16)
	{
		first++;
		num -= 16;
	}

	hex_value = '';

	hex_alphabets = new Array;
	hex_alphabets[10] = 'a';
	hex_alphabets[11] = 'b';
	hex_alphabets[12] = 'c';
	hex_alphabets[13] = 'd';
	hex_alphabets[14] = 'e';
	hex_alphabets[15] = 'f';

	if(first < 10) hex_value = first + '';
	else hex_value = hex_alphabets[ first ];

	if(num < 10) hex_value += num + '';
	else hex_value += hex_alphabets[ num ];

	return hex_value;
}

////////////////////////////////////////////////////////
// Function:         RGBToHex
// Description: RGB to Hex

function RGBToHex(r, g, b)
{
	return '#'+ NumToHex(r) + NumToHex(g) + NumToHex(b);
}

////////////////////////////////////////////////////////
// Function:         HexToNumEasy
// Description: Hex to Num Easy

function HexToNumEasy(hex_value)
{
	return parseInt(hex_value, 16)
}

////////////////////////////////////////////////////////
// Function:         HexToR
// Description: Hex to R (>R<GB)

function HexToR(hex_value)
{
	if(hex_value.charAt(0) == '#') hex_value = hex_value.substring(1, 7);
	return HexToNumEasy(hex_value.substring(0, 2));
}

////////////////////////////////////////////////////////
// Function:         HexToG
// Description: Hex to G (R>G<B)

function HexToG(hex_value)
{
	if(hex_value.charAt(0) == '#') hex_value = hex_value.substring(1, 7);
	return HexToNumEasy(hex_value.substring(2, 4));
}

////////////////////////////////////////////////////////
// Function:         HexToB
// Description: Hex to B (RG>B<)

function HexToB(hex_value)
{
	if(hex_value.charAt(0) == '#') hex_value = hex_value.substring(1, 7);
	return HexToNumEasy(hex_value.substring(4, 6));
}