/* javascript.js */
var aMenuY = new Array();
var section;

var INIT		= 0;
var HOME		= 1;
var SUB			= 2;
var SUBSUB		= 3;
var state = INIT;

$(document).ready(function() {
	$('#menu-a li').each(function(){
		aMenuY[$(this).attr('id')] = $(this).position().top;
	});
});

function pageTrans(hash){
	// this is to keep the old 'medical' link - switch it to 'pharma':
	hash = hash.replace('medical', 'pharma');
	
	hash = hash.replace('#', '');
	//alert('section = '+section+' state = '+state+' hash = '+hash);
	var aHash = hash.split('/');
	section = aHash[0];
	//var jsonquery = 'module/getState.json.php?hash='+location.hash.replace('#', '');
	
	if(state==INIT && hash.length>0){
		state = HOME;
	}
	switch(state){
		case INIT:
		$('#innercontent').hide();
		$('#bkg').css('display', 'none');
		$('#bkg').fadeIn('slow');
		break;
		
		case HOME:
		$('#innercontent').hide();
		$('#menu-a').animate({ left: -500 }, { duration: 200, easing: 'easeInOutQuad' });
		$('#bkg').animate({ right: -1000 }, { duration: 300, easing: 'easeInOutQuad', complete: function(){
			state = SUB;
			showInner(hash);
		}});
		break;
				
		case SUB:
		if(hash.length>0){
			// test for videoBox already on page or are we going to a submenu page:
			if($('#videoBox').length == 0 || hash == "television" || hash == "non-broadcast"){
				showInner(hash);
			} else {
				$('#videoBox').load('module/videoBox.php?hash='+hash);
				$('#selectPaneHolder a').each(function(){
					$(this).removeClass('selected');
					if($(this).attr('href').replace('#', '')==hash){
						$(this).addClass('selected');
					}
				});
			}
		} else {
			state = HOME;
			$('#innercontent').hide();
			$('#innercontent').html('');
			$('#bkg').attr('class', '');
			$('#bkg').css({ right:-1000 });
			$('#bkg').animate({ right: 0 }, { duration: 300, easing: 'easeInOutQuad' });
			$('#menu-a li').each(function(){
				$(this).children('a').removeClass('selected');
			});
			$('#menu-a').animate({ left: 0 }, { duration: 200, easing: 'easeInOutQuad' });
		}
		break;
		
		case SUBSUB:
		state = SUB;
		showInner(hash);
		break;
	}	
}

function showInner(hash){
	$('#innercontent').hide();
	$('#innercontent').load('module/portfolio.php?hash='+hash, function(){
		$('#innercontent').show();
		$('#header').css('margin-left', -600);																		
		$('#headimg').load(function(){
			$('#header').animate({ 'margin-left': 0 }, { duration: 200, easing: 'easeInOutQuad' });
		});
		var bgclass = section.replace('#', '');
		//ugly but in a hurry:
		//if(bgclass == 'television' || 'non-broadcast'){
		if(bgclass == 'non-broadcast'){
			var aHash = hash.split('/');
			if(aHash.length>1){
				bgclass = aHash[1];
			}
		}
		if(!$('#bkg').hasClass(bgclass)){
			$('#bkg').attr('class', bgclass);
			$('#bkg').css({right:0, display:'none'});
			$('#bkg').fadeIn('slow');
			//$('#selectPane').jScrollPane({showArrows:true});
		}
	});
}

// HASH HANDLING
$(function(){
	$(window).bind( 'hashchange', function(){
		pageTrans(location.hash);
	});
	$(window).trigger( 'hashchange' );
 });

