// Create the tooltips only on document load
$(document).ready(function() {
	// remove empty sectiontitle links
	$('.sectionTitle a').each(function() {
		if($(this).attr('href') === "") {
			var title = $(this).text();
			$(this).parent().text(title);
			$(this).remove();
		}
	});

	// remove fancybox image "underlines"
	$('a[rel^="fancybox"] img').parent().css('text-decoration', 'none');
						   
	// countrySelect controls
	$('#countrySelect').hide();
	$('.bsbf_navCountrySelect').click(function() {
		$('#countrySelect').slideToggle('normal');
	});
	
	// set redTab Widths || add margin-left to non linked items
	if ($('.bsbf_redTabWrap').size() !== 0) {
		$('.bsbf_redTabWrap').each(function() {
			var startWidth = $(this).find('.redTitleText').width();
			$(this).css('width',startWidth + 60 + 'px');
			if ($(this).find('.bsbf_redTabTitle a').size() === 0) {
				$(this).find('.bsbf_redTabTitle').css('margin-left','23px');
			}
		});
	}
	
	// Setup Fancybox(overlay items)
	/* ===== Items that are not swfs will be handled as defined by fancybox
				-Items of type .swf however require some special attention:
					IF WIDTH/HEIGHT/FLASHVARS ARE PASSED THEY MUST BE IN THAT ORDER
					-Altering will break the overlay.
					DO NOT INCLUDE ATTRIBUTE NAMES:
						width is assumed, height is assumed, flashvars is assumed.
					-Altering will break the overlay.
	===== */
	$('a[rel^="fancybox"]').each(function() {		
		var fBoxVals = new Array();
		var setWidth = 'auto';
		var setHeight = 'auto';
		var setFlashvars = 'null';
		fBoxVals = $(this).attr('rel').split(":");		
		
		if (fBoxVals[1] !== undefined) {
			log('width: ' + fBoxVals[1]);
			setWidth = fBoxVals[1];
		}
		
		if (fBoxVals[2] !== undefined) {
			log('height: ' + fBoxVals[2]);
			setHeight = fBoxVals[2];
		}
		
		if (fBoxVals[3] !== undefined) {
			log('flashvars: ' + fBoxVals[3]);
			setFlashvars = fBoxVals[3];
		}
		
		$(this).fancybox({
			'autoScale'	: false,
			'width'		: fBoxVals[1],
			'height'	: fBoxVals[2],
			'swf'		: { 'flashvars' : fBoxVals[3] }
		});
		
	});
	
	// Bold last word in buttons
	if ($('.bsbfButton').size() !== 0) {
		$('.bsbfButton').each(function() {
			var textNode = $(this).find('p a').text().toString();
			var textArray = textNode.split(" ");
			var boldItem = '<strong>' + textArray[textArray.length - 1].toString() + '</strong>';
			textArray.pop();
			var joinArray = textArray.join(" ");
			var updatedText = joinArray.toString() + '&nbsp;' + boldItem;
			$(this).find('p a').html(updatedText);
		});
	}
	
	// form submit using bsbfButton
	if($('#bsbfButtonSubmit').size() !== 0) {
		$('#bsbfButtonSubmit').click(function() {
			var formId = $(this).find('p a').attr('href');
			$('#' + formId).submit();
			return false;
			this.blur();
		});
	}
	
	FAQ.init();
	showHideNews();
	// NavigationConroller.init();
	BSBFVS.regionDisplay();
	BSBFVS.contactValidation();
	
});

$( window ).load( function () {
	NavigationConroller.init();
});



// For legacy games, closes fancybox game when Quit is pressed
var locate = function() {
	$.fancybox.close();
};

// show/hide navigation
var navDisplay = function() {
	log('navDisplay() fired');
	$('#bsbf_mainNav').toggle();
};

var showHideNews = function () {
   if ( $( '.showMoreNews' ).size() === 0 ) return;
   $( '.readLess' ).hide();
   $( '.showMoreNews' ).hide();
   $( '.innerTab' ).each( function () {
       $( this ).find( '.learnMore' ).click( function ( event ) {
           event.preventDefault();
           $( this ).parent().find( '.showMoreNews' ).toggle();
           $( this ).find( '.readMore' ).toggle();
           $( this ).find( '.readLess' ).toggle();
       });
   });
};

/*
 *	FAQ Controller
*/
var FAQ = {
	
	init: function () {
		log( '---->FAQ.init()' );
		if ( $( '.faqContent' ).size() === 0 ) return;
		$( '.bsbf_content:last' ).hide();
		$( '.question' ).addClass( 'inactiveQuestion' );
		$( '.answer' ).hide();
		$( '.answer:first' ).show();
		$( '.answer:first' ).prev().addClass( 'activeQuestion' ).removeClass( 'inactiveQuestion' );
		FAQ.clickSet();
	}, //-- FAQ.init();
	
	clickSet: function () {
		log( '---->FAQ.clickSet()' );
		$( '.question' ).click( function () {
			if ( $( this ).hasClass( 'activeQuestion' ) ){
				$( this ).next( '.answer' ).slideUp( 'slow' );
				$( '.activeQuestion' ).removeClass( 'activeQuestion' ).addClass( 'inactiveQuestion' );
			}
			if ( $( '.activeQuestion' ).size() > 0 ) {
  				$( '.activeQuestion' ).next( '.answer' ).slideUp( 'slow' );
				$( '.activeQuestion' ).removeClass( 'activeQuestion' ).addClass( 'inactiveQuestion' );
			}
			$( this ).addClass( 'activeQuestion' ).removeClass( 'inactiveQuestion' );
			$( this ).next( '.answer' ).slideDown( 'slow' );
		});
	} //-- FAQ.clickSet();
	
};

/**
 *	NavigationController
	- Controls Top Navigation
	- Initialized in doc ready call
 */
var NavigationConroller = {
	
	NC: '', // prep shorten namespace
	
	/**
	 *	NC.init()
		- set shortened namespace to NC
		- call NC.setHandles()
	 */
	init: function () {
		log( '---->NC.init()' );
		NC = this; // set namespace to NC instead of NavigationController
		NC.setHandles();
	}, //-- NC.init()
	
	/**
	 *	NC.setHandles()
		- setup unique ids for tabs and dropdowns
		- to be used to match parent with child
	 */
	setHandles: function () {
		log( '---->NC.setHandles()' );
		$( '#nav li.hasSub' ).each( function ( t ) {
		    $( this ).attr( 'id','navTab_' + t );
		});
		$( '.dropdownnav' ).each( function ( g ) {
		    $( this ).attr( 'id','slideGroup_' + g );
		});
		NC.setSlideWidth();
	}, //-- NC.setHandles()
	
	/**
	 *	NC.setSlideWidth()
		- set dropdowns width to match parent tabs width
	 */
	setSlideWidth: function () {
		log( '---->NC.setSlideWidth()' );
		$( 'li[id^="navTab"]' ).each( function ( i ) {
			var currTab = $( this ),
				currTabWidth = currTab.width() + 6,
				currSlide = $( '#slideGroup_' + i ),
				currSlideWidth = currSlide.width();
			currSlide.css( 'width',currTabWidth + 'px' );
			currSlide.find( 'li' ).css( 'width', currTabWidth + 'px' );
		});
		NC.setSlideLoc();
	}, //-- NC.setSlideWidth()
	
	/**
	 *	NC.setSlideLoc()
		- set dropdown items to match parent tabs location
		- hide dropdowns
		- call NC.setHover()
	 */
	setSlideLoc: function () {
		log( '---->NC.setSlideLoc()' );
		$( 'li[id^="navTab"]' ).each( function ( i ) {
			var currTab = $( this ),
				currSlide = $( '#slideGroup_' + i ),
				currSlideLeft = Utils.itemOffset( currSlide, 'left' ),
				currTabTop = Utils.itemOffset( currTab, 'top' ),
				currTabLeft = Utils.itemOffset( currTab, 'left' ),
				currHeight = currTab.height(),
				slideTop = currTabTop + currHeight;
			log( 'TOP: ' + ( slideTop - 36 ) + ' || LEFT: ' + ( currTabLeft - currSlideLeft ) );
			$( currSlide ).css({
				'top'	:	slideTop - 36,
				'left'	:	currTabLeft - currSlideLeft
			});
		});
		$( '.dropdownnav' ).fadeOut().slideUp(); // fade out and slide up nav on load
		NC.setHover();
	}, //-- NC.setSlideLoc()
	
	/**
	 *	NC.setHover()
		- setup hover calls for tabs
	 */
	setHover: function () {
		log( '---->NC.setHover()' );
		$( 'li[id^="navTab"]' ).each( function ( e ) {
			$( this ).hoverIntent({
				over: NC.showSlide,
				timeout: 150,
				out: NC.hideSlide
			});
		});
	}, //-- NC.setHover()
	
	/**
	 *	NC.showSlide()
		- hide any dropdowns that are showing
		- display dropdown whos handle corresponds with parent tab
	 */
	showSlide: function () {
		log( '---->NC.showSlide()' );
		$( 'ul[id^="slideGroup"]' ).hide();
		var tabId = $( this ).attr( 'id' ),
			slideId = $( '#' + tabId.replace( 'navTab_', 'slideGroup_' ) );
		slideId.slideDown( '500','easeOutExpo' );
	}, //-- NC.showSlide()
	
	/**
	 *	NC.hideSlide()
		- hide visible dropdown when #topnavigationContainer or #bsbf_Hero is moused over
		- setup hover states for visible dropdown, slide up when moused off
	 */
	hideSlide: function () {
		log( '---->NC.hideSlide()' );
		/*
		$( '#topnavigationContainer, #bsbf_Hero' ).mouseover( function() {
			$( 'ul[id^="slideGroup"]' ).slideUp( '150', 'easeInExpo' );
			alert('hideSlide - slideGroup slide up on navContainer/hero');
		});
		*/
		
		var tabId = $( this ).attr( 'id' ),
			slideId = $( '#' + tabId.replace( 'navTab_', 'slideGroup_' ) );
		slideId.hoverIntent({
			timeout: 100,
			over: function () {
				log( 'over' );
			},
			out: function () {
				slideId.slideUp( '150', 'easeInExpo' );
			}
		});
	} //-- NC.hideSlide()
	
}; //-- NavigationController

/**
 *	Van Schedule Controller
	- Van Schedule Tooltips
	- Contact Form
 */
BSBFVS = {  
	
	regionDisplay: function () {
		if ( $( '.schedule' ).size() === 0 ) return;
		var currentState = '';
		$( '.schedule:first' ).show();
		currentState = $( '.schedule:visible' ).attr( 'id' );
		BSBFVS.tooltipDisplay();
		$("#stateSelect").change(function () {
			$("select option:selected").each(function () {
				log( 'HIDING' );
				$('#'+currentState).hide();
				currentState = '';
				currentState = $(this)[0].value;
				log( currentState );
			});
			$('#'+currentState.toLowerCase() ).show();
			BSBFVS.tooltipDisplay();
		});
	},
	
	tooltipDisplay: function () {
		log( 'BSBFVS.tooltipDisplay()' );
		$( '.schedule:visible .section > ul li a' ).click( function( e ) {
			e.preventDefault()
		});
		// clean up address in tooltip
		var regCheck = /,\s/;
		$( '.details ol' ).each( function () {
			if ( $( this ).find( 'li:first span.cityStateZip' ).size() === 0 ) {
				var str = $( this ).find( 'li:first' ).html(), newStr, splitPos, firstStringPos, lastStringPos;
				splitPos = str.indexOf( ', ' ) + 1;
				firstStringPos = str.substring( 0, splitPos );
				lastStringPos = str.substring( splitPos, str.length );
				newStr = firstStringPos + '<br><span class="cityStateZip">' + lastStringPos + '</span>';
				$( this ).find( 'li:first' ).html( newStr );
			}
		});
		
		$( '.schedule:visible .section > ul > li' ).not( '.details' ).mouseover( function( e ) {
			var detailMargin = $( this ).outerWidth(),
				detailPosition = $( this ).position(),
				detailLeft = detailPosition.left,
				detailTop = detailPosition.top,
				detailItem = $( this ).find( '.details' ),
				detailItemHeight = $( detailItem ).outerHeight();
			if ( $( detailItem ).hasClass( 'ON' ) !== true ) {
				$( detailItem ).find( 'dd' ).not( ':last' ).css( 'borderBottom','1px dashed #3EA9F5' );
				$( detailItem ).css({
					'top': detailTop + 'px',
					'marginTop': '-' + ( detailItemHeight + 8 ) + 'px',
					'marginLeft': '-14px'
				}).addClass( 'ON' ).show();
			}
		}).mouseout( function ( e ) {
			$( this ).find( '.details' ).removeClass( 'ON' ).hide();
		});
	},//-- BSBFVS.tooltipDisplay()
	
	contactValidation: function () {
		log( '---->BSBFVS.contactValidation()' );
		if ( $( '#bsbf_Contact' ).size() === 0 ) return;
		$( '#bsbf_Contact' ).validate({
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				},
				subject: "required",
				comments: "required"
			}
		});
	} //-- BSBFVS.contactValidation()
	
};

RotatorPlayer = {
	
	Player: '',
	
	init: function () {
		log( '---->RotatorPlayer.init() called from swfobject callback method - call RotatorPlayer.setupPlayer()' );
		if( $( '.playerThumbs' ).size() === 0 ) return;
		RotatorPlayer.setupPlayer();
	}, //-- RotatorPlayer.init()
	
	setupPlayer: function () {
		log( '---->RotatorPlayer.setupPlayer() - get a handle on the swf - call RotatorPlayer.sendVideoData()' );
		RotatorPlayer.Player = document.getElementById( 'bsbfFlashItem' );
		log( 'Handle on swf - bsbfFlashItem:' );
		log( RotatorPlayer.Player );
		RotatorPlayer.sendVideoData();
	}, //-- CPRP.setupPlayer()
	
	sendVideoData: function () {
		log( '---->RotatorPlayer.sendVideoData() - call initVideo( vidPath, screenPath ) on thumb click' );
		try {
			$( '.playerThumbs' ).find( 'ul li' ).click( function ( ev ) {
				ev.preventDefault();
				var vidPath		=	$( this ).attr( 'data-videopath' ),
					screenPath	=	$( this ).attr( 'data-screenpath' );
				log( 'Thumb clicked - sending data: vidPath:\n' + vidPath + '\nscreenPath:\n' + screenPath );
				RotatorPlayer.Player.initVideo( vidPath, screenPath )
			});
		} catch ( error ) {
			log( '<!>RotatorPlayer.sendVideoData() ERROR: ' + error );
		}
	} //-- RotatorPlayer.sendVideoData();
	
};



