var slideLeft = -1;
var slideWidth = -1;
var pics = new Array( '/m/slides/cozumel.jpg','/m/slides/sunset.jpg','/m/slides/cem.jpg','/m/slides/plaza.jpg','/m/slides/head.jpg','/m/slides/tambor.jpg','/m/slides/chen.jpg','/m/slides/lights.jpg','/m/slides/murano.jpg');
var picIndex = Math.floor( Math.random() * pics.length );
var picOpacity = 0;
var picRotateDelay = 10000;
var picFadeDelay = 50;
var picFadeStep = 10;

function init()
{
	pubinit();
}

function pubinit()
{
	if( document.all )
	{
		var obj = document.getElementById( 'slide' );
		obj.style.backgroundImage = '';
		obj.style.backgroundColor = null;
		layoutCheck();

		// uncomment to allow rotation
		setTimeout( "loadPic2()", picRotateDelay );

		// set menu link states depending on page
		setMenuLinks();
	}
}

function loadPic1()
{
	picIndex++;
	if( picIndex >= pics.length )
		picIndex = 0;
	backimg1.onload = pic1Loaded;
	backimg1.src = pics[picIndex];
}

function loadPic2()
{
	picIndex++;
	if( picIndex >= pics.length )
		picIndex = 0;
	backimg2.onload = pic2Loaded;
	backimg2.src = pics[picIndex];
}

function pic1Loaded()
{
	back1.style.zIndex = -10;
	back2.style.zIndex = -20;
	picOpacity = 0;
	fadeInPic1();
}

function fadeInPic1()
{
	picOpacity += picFadeStep;
	back1.filters(0).opacity = picOpacity;
	if( picOpacity == 100 )
		setTimeout( "loadPic2()", picRotateDelay );
	else
		setTimeout( "fadeInPic1()", picFadeDelay );
}

function pic2Loaded()
{
	back2.style.zIndex = -10;
	back1.style.zIndex = -20;
	picOpacity = 0;
	fadeInPic2();
}

function fadeInPic2()
{
	picOpacity += picFadeStep;
	back2.filters(0).opacity = picOpacity;
	if( picOpacity == 100 )
		setTimeout( "loadPic1()", picRotateDelay );
	else
		setTimeout( "fadeInPic2()", picFadeDelay );
}


function layoutCheck()
{
	var x, y, w, h;
	var obj = document.getElementById( 'slide' );
	x = getLeftX( obj );
	w = obj.offsetWidth;
	if( x != slideLeft || w != slideWidth )
	{
		y = getTopY( obj );
		h = obj.offsetHeight;
		back1.style.left = x;
		back1.style.top = y;
		back1.style.width = w;
		back1.style.height = h;
		back1.style.display = 'block';
		back2.style.left = x;
		back2.style.top = y;
		back2.style.width = w;
		back2.style.height = h;
		back2.style.display = 'block';
		slideLeft = x;
		slideWidth = w;
	}
	setTimeout( "layoutCheck()", 500 );
}

function bg1()
{
	picIndex++;
	if( picIndex >= pics.length )
		picIndex = 0;
	slide.style.backgroundImage = "url(" + pics[picIndex] + ")";
}

function showCoors( obj )
{
	var x, y, w, h;
	x = getLeftX( obj );
	y = getTopY( obj );
	w = obj.offsetWidth;
	h = obj.offsetHeight;
	//alert( "left:" + x + " top:" + y + " width:" + w + " height:" + h );
	back1.style.left = x;
	back1.style.top = y;
	back1.style.width = w;
	back1.style.height = h;
	back1.style.zIndex = 2000;
	//alert( "done" );
}

function getRightX( obj )
{
	var pos = 0;
	var n = 0;
	var x = obj.offsetParent;
	while(x.offsetParent != undefined) {
		n += x.offsetLeft;
		x = x.offsetParent;
	}
	pos = n + obj.offsetLeft;
	pos += obj.offsetWidth;
	return pos;
}

function getLeftX( obj )
{
	var pos = 0;
	var n = 0;
	var x = obj.offsetParent;
	while(x.offsetParent != undefined) {
		n += x.offsetLeft;
		x = x.offsetParent;
	}
	pos = n + obj.offsetLeft;
	return pos;
}

function getTopY( obj )
{
	var pos = 0;
	var n = 0;
	var x = obj;
	while(x.offsetParent != undefined) {
		n += x.offsetParent.offsetTop;
		x = x.offsetParent;
	}
	pos = n + obj.offsetTop;
	return pos;
}

function getBottomY( obj )
{
	var pos = 0;
	var n = 0;
	var x = obj;
	while(x.offsetParent != undefined) {
		n += x.offsetParent.offsetTop;
		x = x.offsetParent;
	}
	pos = n + obj.offsetTop;
	pos += obj.offsetHeight;
	return pos;
}

function setMenuLinks()
{
	var i;
	var ids = new Array( 'home', 'mytrips', 'recent', 'countries', 'contributors', 'about', 'privacy');
	for( i = 0; i < ids.length; i++ )
	{
		var id = ids[i];
		var obj = document.getElementById( 'ml_' + id );
		if( obj != null )
		{
			if( _page == id )
			{
				obj.style.color = 'black';
			}
			if( id == 'mytrips' )
			{
				var url = getUserUrl();
				if( url != null )
				{
					//obj.href = url;
					obj = document.getElementById( 'mr_' + id );
					if( obj != null )
						obj.style.display = 'block';
				}
			}
		}
	}
}

// getUserUrl - decides whether we're in a vanity url or not
function getUserUrl()
{
	var host = location.hostname;
	if( host.length <= 16 || host.substr( host.length - 16 ) != 'clubanywhere.com' )
	{
		return null;
	}

	var user = host.substr( 0, host.length - 17 );
	if( user == 'www' )
	{
		return null;
	}

	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-";
	var i;
	for( i = 0; i < user.length; i++ )
	{
		var c = user.charAt( i );
		if( valid.indexOf( c ) == -1 )
		{
			return null;
		}
	}
	return '/c/a/user_js.html?name=' + user;
}

