jQuery(function(){

	/* ************************** WELCOME MESSAGE VIDEO *************************************** */
	var WelcomeMessageTab = jQuery('#WelcomeMessage');
	var VideoPlaceholder = jQuery('#Placeholder');
	var VideoSRC = VideoPlaceholder.attr('href');
	var VideoContainer = jQuery('#VideoContainer');
	
	/*function PlayWelcomeVideo() {
		VideoPlaceholder.hide();
		VideoContainer.media({
			width: WelcomeMessageTab.width(),
			height: WelcomeMessageTab.height(),
			src: VideoSRC,
			params: { 
				//wmode: "opaque",
				wmode: "transparent",
				allowScriptAccess: "sameDomain"
			}
		});
	}
	
	function HideWelcomeVideo() {
		VideoContainer.html('');
	}
	
	VideoPlaceholder.click(function(){
		PlayWelcomeVideo();
		return false;
	});*/
	
	
	/* ************************************ VIDEO ******************************************** */
	var VideoAnchors = jQuery('#Video a');
	
	VideoAnchors.click(function(){
		var tgt = jQuery(this);
		var VideoSRC = tgt.attr('href');
		var VideoCode = decodeURIComponent(tgt.attr('rel'));
		//console.log(VideoCode);
		
		jQuery.facebox(VideoCode);
	
		return false;
	});
	
	/* ********************************* AUDIO ************************************************ */
	var AudioTab = jQuery('#Audio');
	var AudioAnchors = jQuery('#Audio a');
	var AudioSandbox = jQuery('<div></div>').appendTo(document.body).hide();
	
	AudioAnchors.click(function(){
		var tgt = jQuery(this);
		var AudioDump = jQuery('<div></div>').appendTo(AudioSandbox).hide();
		
		// we'll need to extend the default options since we're not calling the main media method
		var opts = jQuery.fn.media.defaults;
		opts.src = tgt.attr('href');
		//console.log(opts.src);
		opts.flashvars.type = "sound";
		opts.flashvars.autostart = "true";
		opts.height = 20;
		
		// get our generated code
		var code = jQuery.fn.media.mp3(AudioDump.get(0), opts).html();
		//console.log(code);
		AudioSandbox.html('');
		
		// and display in the facebox
		jQuery.facebox(code);

		return false;
	});
	
	/* ******************************** PHOTOS *********************************************** */
	var PhotosTab = jQuery('#Photos');
	var PhotoAnchors = jQuery('#Photos a[rel=phototab]');
	
	// workaround: facebox doesn't recognize these links as images, so we need to call it manually
	PhotoAnchors.click(function(){
		var tgt = jQuery(this);
		jQuery.facebox({ image: tgt.attr('href') });
		return false;
	});

	/* ******************************* TAB FUNCTIONALITY *********************************** */
	var defaultTab = 'WelcomeMessage';

	var Tabs = jQuery('#HomePageTabs .tab');
	var TabsListItems = jQuery('#HomePageTabsList li');
	var TabsListItemAnchors = jQuery('#HomePageTabsList li a');
	
	function showTab(tabID) {
		//HideWelcomeVideo();
		TabsListItems.removeClass('active');
		jQuery('#' + tabID + 'Tab').addClass('active');
		Tabs.hide();
		jQuery('#' + tabID).show();
	}
	
	TabsListItemAnchors.click(function(){
		var tgt = jQuery(this);
		showTab(tgt.attr('href').substr(1));
		return false;
	});
	
	showTab(defaultTab);
	
	/* ******************************* STATE MAP ******************************************** */
	var imagePath = '_skins/johanns/images/';
	var stateMapContainer = jQuery('#StateRegionMapContainer');
	var stateMapImage = jQuery('#StateRegionMap');
	var stateMapAreas = jQuery('#StateRegionMapAreas area');
	var regionItems = jQuery('#StateOffices li');
	var defaultRegion = 'Washington';
	var currentRegion = defaultRegion;
	
	function changeRegion(id) {
		regionItems.hide();
		jQuery('#Region_' + id).show();
	}
	
	// hover functionality
	stateMapAreas
		.mouseover(function(){
			var id = jQuery(this).attr('href').substr(1);
			// console.log('Entering ' + id);
			stateMapContainer.removeClass();
			stateMapContainer.addClass('region_' + id);
			changeRegion(id);
		})
		.mouseout(function(){
			var id = jQuery(this).attr('href').substr(1);
			// console.log('Leaving ' + id);
			stateMapContainer.removeClass('region_' + id);
			stateMapContainer.addClass('region_' + currentRegion);
			changeRegion(currentRegion);
		})
		.click(function(){
			var id = jQuery(this).attr('href').substr(1);
			// console.log('Clicked ' + id);
			if(id == currentRegion){
				currentRegion = defaultRegion;
				changeRegion(currentRegion);
			} else {
				currentRegion = id;	
				changeRegion(currentRegion);
			}
			return false;
		});
	
	// add a background image
	//stateMapContainer.css('background-image', "url('" + imagePath + "home/map_base.gif')");
	// replace the image src with a blank gif
	stateMapImage.attr('src', imagePath + 'blank.gif');
	
	// make the default region
	changeRegion(defaultRegion);
	
});
