var shipMoving = false;
var shipLive = false;

// hover state configs
var frontMenuConfig = {
	sensitivity: 20, // number = sensitivity threshold (must be 1 or higher)    
	interval: 300, // number = milliseconds for onMouseOver polling interval    
	over: function () {},//openMenu, // function = onMouseOver callback (REQUIRED)    
	timeout: 400, // number = milliseconds delay before onMouseOut    
	out: closeMenu // function = onMouseOut callback (REQUIRED)    
};

var hoverShipConfig = {
	sensitivity: 20,
	interval: 25,
	over: stopShip,
	timeout: 400,
	out: moveShip
};


$(document).ready(function(){ 
	
	
	// initialize season passport offer - front page only
	//$('#seasonPassportOffer').click(moveShip);
	
	
	// initialize main menu
	
	// rollover effect for main images in nav menu
    $('.navCap').hover(	
    	// on hover
    	function() {
    		$(this).attr('src', $(this).attr('src').replace('.png', '_msover.png'));
    	},
    	
    	// on mouse out
    	function() {
    		$(this).attr('src', $(this).attr('src').replace('_msover.png', '.png'));
    	}
    );
    	
	$("#contentArea3 ul.navSliderHead").hoverIntent( frontMenuConfig );
	$("#contentArea3 ul.navSliderHead > li > a").click(openMenu);//function () { openMenu(); return false; });

	$(".slider").easySlider({
			auto: true,
			continuous: false,
			pause: 6000,
			speed: 1500,
			controlsHover: true,
			controlsShow: false
	});
	
	$('.cateringCheckbox').click(calculateCatering);
	
	// fix png images
	$(document).pngFix();
	
	window.setTimeout(function() {
 		//$('#contentArea3 ul li ul').slideUp(500);
 		//moveShip();
	}, 800);
	

});


function calculateCatering() {
	var startingValue = document.getElementById('basePriceSpan').innerHTML;
	var total = Math.round(parseFloat(startingValue)*100)/100;
	$('.cateringCheckbox:checked').each(function() {
		var value = parseFloat($(this).attr('value'));
		total = total + value;
	});
	$('.priceTotal').html('$'+roundNumber(total, 3).toFixed(2));
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var tempNum = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return tempNum;
}

function basePrice(val) {
	document.getElementById('basePriceSpan').innerHTML = val;
	calculateCatering();
}

// add hand to left of table row on click, delete others before adding.
function addHand(iObject) {
	$('.pointer').remove();
	if('ActiveXObject' in window && !('XMLHttpRequest' in window)) // is I.E. 6 or below
		$(iObject).append('<img class="pointer" src="/images/common/hand_pointer.gif" />');
	else	
		$(iObject).append('<img class="pointer" src="/images/common/hand_pointer.png" />'); 
}

function removeHand() {
	$('.pointer').remove();
}


function moveShip() {
	
	if (shipMoving == false ) {
	
		if (!shipLive) {
			$('body').append('<div id="offerShip"><img src="/images/common/ship_offer.png" alt="Click for offer" /></div>');
			$('#offerShip').hoverIntent( hoverShipConfig );
			shipLive = true;
		}
		
		var bwidth = $('body').width();
			
		var shipDisplay = $('#offerShip').css('display');
	
		if (shipDisplay = 'none'); {
			$('#offerShip').fadeIn('slow');	
		}
	
		var shipRightVal = $('#offerShip').css('right').replace('px', '');
		
		var moveTo = bwidth - shipRightVal;
		var moveTime = moveTo * 5; // time to get across the screen.
	
		shipMoving = true; // global
		$('#offerShip').animate({
			right: '+='+moveTo,
			top: '200'
			}, moveTime, function() {
				$('#offerShip').fadeOut();
				var shipDisplay = $('#offerShip').css('display');
				if (shipDisplay = 'block')
					resetShip(); // write function to reset ship to starting position.
			}
		);
	}
}

function resetShip() {
	$('#offerShip').css('display', 'none');
	$('#offerShip').css('top', '0px');
	$('#offerShip').css('right', '0px');
	$('#offerShip').remove();
	
	shipMoving = false; // global
	shipLive = false; // able to add another ship to site
}

function stopShip() {
	$('#offerShip').clearQueue();
	$('#offerShip').stop();
	shipMoving = false; // global
}


function openMenu() {
	var $menu = $(this).parent().parent();
	if ($menu.hasClass('isOpen')) {
		return;
	}
	
	// Close other menus
	$('#contentArea3 ul.navSliderHead.isOpen').each(closeMenu);

	$menu.find('ul').slideDown('slow');
	$menu.addClass('isOpen');
	return false;
}

function closeMenu() {
	$(this).find('ul').slideUp('slow');
	$(this).removeClass('isOpen');
}
