var currentURL = new String( document.location );
if( currentURL.indexOf( 'www.myunionterminal.org' ) > 0 ){
	var baseURL = 'http://www.myunionterminal.org/';
}else if( currentURL.indexOf( 'myunionterminal.org' ) > 0 ){
	var baseURL = 'http://myunionterminal.org/';
}else if( currentURL.indexOf( 'myunionterminal' ) > 0 ){
	var baseURL = 'http://labs.landor.com/myunionterminal/';
}else{
	var baseURL = 'http://labs.landor.com/siteManager/sites/n4vcCeT96qV7/';
}
//document.write( '**' + baseURL + '**' );
var holdTime = 10000;	// milliseconds to hold on each home page panel

$(document).ready( function(){
	
	// hook up hover states for top menus for IE6
	$('#topmenu li').hover(
		function(){
			$(this).addClass('sfhover');
			if( isIE7() ){
				$(this).children('ul').show();
			}
		}, 
		function(){
			$(this).removeClass('sfhover');
			if( isIE7() ){
				$(this).children('ul').hide();
			}
		}
	);
	
	// add "has children" indicator to top menu, second-level items
	$('#topmenu li li ul').parent().children('a').append( ' &raquo;' );
	
	// load news feed
//	$('#newsfeed').load( baseURL + 'scripts/newsfeed.php' ).removeClass( 'loading' );
	var footerPosition =  $('#pagefooter').offset().top;
	var topAreaHeight = $('#pageheader').height() + $('#topmenu').height() + $('h1').height();
	var newsHeight = footerPosition - topAreaHeight;
	if( isIE7() ){
		newsHeight = newsHeight - 46;
	}
	if( isIE6() ){
		newsHeight = newsHeight;
	}
//debug( 'footerPosition: ' + footerPosition );
//debug( 'topAreaHeight: ' + topAreaHeight );
//debug( 'newsHeight: ' + newsHeight );
//debug( '#newsfeed.height (before resize): ' + $('#newsfeed').height() );
	$('#newsfeed').height( newsHeight );
//debug( '#newsfeed.height (after resize): ' + $('#newsfeed').height() );
	$('#home #newsfeed').height( $('.five-column').outerHeight() );
//	$('.five-column').height( $('#contentarea').height() );
	// home page scroller	
	if( $('.home-splash div.panel').length > 0 ){
		// auto scroll on home page
		var paneWidth = $('.home-splash').outerWidth(true);
		var numPanes = $('div.panel').length;
		var str = "There are " + numPanes + " panes, totaling " + (paneWidth * numPanes) + "px wide";
		$('.home-splash-window').width( paneWidth * numPanes );
		homeScrollInterval = setInterval( scrollMore, holdTime );
		
		// pause auto scroll when mouse is over yellow box
		$('.home-splash div.panel').hover(
			function(){
				clearInterval( homeScrollInterval );
			}, 
			function(){
				homeScrollInterval = setInterval( scrollMore, holdTime );
			}
		);
	}
	
	// hook up 'view-all-memories' button on memories page
	$('#view-all-memories').click( function(){
		document.location = baseURL + 'BeAStar/ViewMemories.php';
	});

	// hook up 'add-your-memories' button on memories page
	$('#add-your-memories').click( function(){
		document.location = baseURL + 'BeAStar/CreateMemories.php';
	});

	// discourage multiple clicks of submit buttons
	$("input[type='submit']").click( function(){
		$(this).attr( 'value', 'working...' );
	});


	// hook up lightbox for gallery page(s)
	// check count of gallery links first to workaround IE bug
	if( $('div.gallery a').length > 0 ){
		$('div.gallery a').lightBox({
			imageLoading: baseURL+'images/lightbox-ico-loading.gif', 
			imageBtnClose: baseURL+'images/lightbox-btn-close.gif', 
			imageBtnPrev: baseURL+'images/lightbox-btn-prev.gif', 
			imageBtnNext: baseURL+'images/lightbox-btn-next.gif' 
		});
	}
	// hook up lightbox for memories page(s)
	// check count of gallery links first to workaround IE bug
	if( $('a.gallery').length > 0 ){
		$('a.gallery').lightBox({
			imageLoading: baseURL+'images/lightbox-ico-loading.gif', 
			imageBtnClose: baseURL+'images/lightbox-btn-close.gif', 
			imageBtnPrev: baseURL+'images/lightbox-btn-prev.gif', 
			imageBtnNext: baseURL+'images/lightbox-btn-next.gif' 
		});
	}
	
});

function debug( message ){
	$('#debug').append( "DEBUG: " + message + '<br />' );
}

function isIE7(){
	if( navigator.appVersion.indexOf( 'MSIE 7' ) > 0 ){
		return true;
	}else{
		return false;
	}
}
function isIE6(){
	if( navigator.appVersion.indexOf( 'MSIE 6' ) > 0 ){
		return true;
	}else{
		return false;
	}
}

function scrollMore(){
	var paneWidth = $('div.panel').outerWidth(true);
	if( $('.home-splash').scrollLeft() + paneWidth < $('.home-splash-window').width() - 25 ){
		$('.home-splash').scrollTo( '+=' + paneWidth + 'px', { axis:'x', duration:800, easing:'easeInOutExpo' } );
	}else{
		$('.home-splash').scrollTo( 0, { axis:'x', duration:800, easing:'easeInOutExpo' } );
	}
}
