
var Transit = {
	
	currentLocation : null,
	linksEnabled : false,
	linkIDs : Array,
	
	init : function() {
		this.linkIDs = Array();
		this.setLocation(-1);
		this.setupLinks();
		this.linksEnabled=false;
		jQuery('ul#transitLinks').addClass('disabled');
		jQuery('#transitContents .transitLocation').hide();
	},
	
	exitLocation : function(nextLocation) {
		//this.linksEnabled = false;
		if (nextLocation != this.currentLocation) {
			this.setLocation(nextLocation);
		}
		//jQuery('ul#transitLinks').addClass('disabled');
		this.clearLinkClasses();
		
		found=false;
		for (n=0; n<this.linkIDs.length; n++) {
			if (this.linkIDs[n] == nextLocation) {
				this.linksEnabled = true;
				
				jQuery('#transitContents .transitHome').hide();
				jQuery('#transitContents .transitLocation').hide();
			
				jQuery('ul#transitLinks li').eq(n).addClass('target');
				jQuery('#transitContents .transitLocation').eq(n).fadeIn('500');
				found = true;
			}
		}
		if (!found) {
			jQuery('#transitContents .transitLocation').hide();
			
			jQuery('#transitContents .transitHome').fadeIn('500');
		}
		
	},
	
	enterLocation : function(nextLocation) {
		this.linksEnabled = true;
		jQuery('ul#transitLinks').removeClass('disabled');
		
		this.clearLinkClasses();
		
		for (n=0; n<this.linkIDs.length; n++) {
			if (this.linkIDs[n] == nextLocation) {
				jQuery('ul#transitLinks li').eq(n).addClass('selected');
				jQuery('ul#transitLinks li').eq(n).find('a').addClass('visited');
			}
		}
	},
	
	setLocation : function(locationID) {
		this.currentLocation = locationID;
	}, 
	
	clearLinkClasses : function() {
		jQuery('ul#transitLinks').find('a').each(function(index) {
			jQuery(this).closest('li').removeClass('target');
			jQuery(this).closest('li').removeClass('selected');
		});
	},
	
	setupLinks : function() {
		jQuery('ul#transitLinks').find('a').each(function(index) {
			fullURL = jQuery(this).attr('href');
			locationID = Transit.getQueryVal(fullURL, 'locationID', -1);
			Transit.linkIDs.push(locationID);
			
			jQuery(this).click(function (e) {
				e.preventDefault();
				jQuery(this).blur();
				if (Transit.linksEnabled == true) {
					fullURL = jQuery(this).attr('href');	
					Transit.goToDeepLink(fullURL);
					
				}
			});
			
			jQuery(this).mouseover(function (e) {
				if (Transit.linksEnabled == true) {
					if (jQuery(this).hasClass('visited')) {
						jQuery(this).removeClass('visited')
						jQuery(this).addClass('overVisited');
					} else {
						jQuery(this).addClass('over');
					}
				}
			});
			
			jQuery(this).mouseout(function (e) {
				if (Transit.linksEnabled == true) {
					if (jQuery(this).hasClass('overVisited')) {
						jQuery(this).removeClass('overVisited')
						jQuery(this).addClass('visited');
					} else {
						jQuery(this).removeClass('over');
					}
				}
			});
			
		});
		
		jQuery('.deepLink').click(function (e) {
			e.preventDefault();
			//jQuery(this).blur();
			if (Transit.linksEnabled == true) {
				fullURL = jQuery(this).attr('href');		
				Transit.goToDeepLink(fullURL);
				
			}
		});
		
	},
	
	goToDeepLink : function(fullURL) {
		locationID = Transit.getQueryVal(fullURL, 'locationID', -1);
		pageID = Transit.getQueryVal(fullURL, 'pageID', -1);
		
		if (locationID != Transit.currentLocation) {
			Transit.notifyFlash(locationID, pageID);
		}
	},
	
	notifyFlash : function(locationID, pageID) {
		this.getFlashMovieObject('flashHolder').notifyFlash(locationID, pageID);
	},  
	
	getFlashMovieObject : function (movieName) {
		if (window.document[movieName]) {
			return window.document[movieName];
		}
		if (navigator.appName.indexOf("Microsoft Internet")==-1) {
			if (document.embeds && document.embeds[movieName])
				return document.embeds[movieName];
			}
			else {
				return document.getElementById(movieName);
			}
		
	},
	
	getQueryVal : function (url, key, default_) {
		if (default_==null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(url);
		if(qs == null)
		return default_;
		else
		return qs[1];
	} 
	
	
}

jQuery(document).ready(function() {
	Transit.init();				   
});

