$.fn.preload = function() {
	this.each(function(){
		$('<img/>')[0].src = this;
	});
}

var navheights = {
    roomssuites: 0,
    teacuisine: 0
};

var mouse_is_inside = {
	navroomssuites: false,
	navteacuisine: false,
	navprivatedining: false,
	navhotelservices: false,
	navaboutus: false,
	languages: false
};

$(function() {
	resizeBgImage();
	
	$('#booknow').animate({top: '0px'}, 800, 'easeInCubic');
	
	// get all subnav heights
	navheights.roomssuites = $('#navroomssuites').find('.subnav').height();
	navheights.teacuisine = $('#navteacuisine').find('.subnav').height();
	navheights.privatedining = $('#navprivatedining').find('.subnav').height();
	navheights.hotelservices = $('#navhotelservices').find('.subnav').height();
	navheights.aboutus = $('#navaboutus').find('.subnav').height();
	
	$('#navroomssuites').find('.subsubnav').each(function(i, el) {
		$(el).css('top', 0 + (navheights.roomssuites - $(el).height()));
	});
	
	$('#navteacuisine').find('.subsubnav').each(function(i, el) {
		$(el).css('top', 0 + (navheights.teacuisine - $(el).height()));
	});
	
	$('#navprivatedining').find('.subsubnav').each(function(i, el) {
		$(el).css('top', 0 + (navheights.privatedining - $(el).height()));
	});
	
	$('#navhotelservices').find('.subsubnav').each(function(i, el) {
		$(el).css('top', 0 + (navheights.hotelservices - $(el).height()));
	});
	
	$('#navaboutus').find('.subsubnav').each(function(i, el) {
		$(el).css('top', 0 + (navheights.aboutus - $(el).height()));
	});
	
	// move subnavs
	$('#navroomssuites').css('top', '-' + navheights.roomssuites + 'px');
	$('#navteacuisine').css('top', '-' + navheights.teacuisine + 'px');
	$('#navprivatedining').css('top', '-' + navheights.privatedining + 'px');
	$('#navhotelservices').css('top', '-' + navheights.hotelservices + 'px');
	$('#navaboutus').css('top', '-' + navheights.aboutus + 'px');
	
	$('#navroomssuites').hover(function() {
		mouse_is_inside.navroomssuites = true; 
	}, function(){ 
		mouse_is_inside.navroomssuites = false; 
	});
	
	$('#navteacuisine').hover(function() {
		mouse_is_inside.navteacuisine = true; 
	}, function(){ 
		mouse_is_inside.navteacuisine = false; 
	});
	
	$('#navprivatedining').hover(function() {
		mouse_is_inside.privatedining = true; 
	}, function(){ 
		mouse_is_inside.privatedining = false; 
	});
	
	$('#navhotelservices').hover(function() {
		mouse_is_inside.hotelservices = true; 
	}, function(){ 
		mouse_is_inside.hotelservices = false; 
	});
	
	$('#navaboutus').hover(function() {
		mouse_is_inside.aboutus = true; 
	}, function(){ 
		mouse_is_inside.aboutus = false; 
	});
	
	$('body').mouseup(function(ev) {
		if ( ! mouse_is_inside.navroomssuites) {
			$('#roomssuites').css('background', '');
			hideSubNav('#navroomssuites');
		}
		
		if ( ! mouse_is_inside.navteacuisine) {
			$('#teacuisine').css('background', '');
			hideSubNav('#navteacuisine');
		}
		
		if ( ! mouse_is_inside.privatedining) {
			$('#privatedining').css('background', '');
			hideSubNav('#navprivatedining');
		}
		
		if ( ! mouse_is_inside.hotelservices) {
			$('#hotelservices').css('background', '');
			hideSubNav('#navhotelservices');
		}
		
		if ( ! mouse_is_inside.aboutus) {
			$('#aboutus').css('background', '');
			hideSubNav('#navaboutus');
		}
		
		if ( ! mouse_is_inside.languages) {
			if (ev.target.id != 'changelanguage' && ev.target.id != 'currentlanguage') {
				$('#languages').animate({top: '-285px'});
			}
		}
	});

	// Preload hover images
	$(['assets/images/facebook-hov.png',
		'assets/images/menu-bg-hov.gif',
		'assets/images/subnavigation-bg-hov.png',
		'assets/images/subnavigation-img-bg-hov.png',
		'assets/images/nav-light-wood-bot.png',
		'assets/images/nav-light-wood-panel.png',
		'assets/images/nav-light-wood-top.png',
		'assets/images/twitter-hov.png']).preload();
	
	$('#booknow a').click(function(e) {
		closeAllDropDowns();
		
		$(this).parent().animate({
			top: '-59px'
		}, 500, 'easeInCubic', function() {
			$('#bookform').animate({top: '0px'}, 800, 'easeInCubic');
		});
		
		e.preventDefault();
	});
	
	$('#bookformclose').click(function(ev) {
		closeAllDropDowns();
		
		$(this).parent().parent().animate({
			top: '-315px'
		}, 800, 'easeInCubic', function() {
			$('#booknow').animate({top: '0px'}, 500, 'easeInCubic');
		});
		
		ev.preventDefault();
	});
	
	$('#bookit_checkin').focus(function(ev) {
		showCalendar();
	});
	
	$('#bookin_calendar').click(function(ev) {
		showCalendar();
		
		ev.preventDefault();
	});
	
	$('#calendarback').click(function(ev) {
		hideCalendar();
		
		ev.preventDefault();
	});
	
	Date.format = 'mm/dd/yyyy';
	
	var today = new Date();
	
	$('#cal')
		.datePicker({startDate:(today.getMonth() + 1)  + '/' + (today.getDate() + 1) + '/' + today.getFullYear(),inline:true,showHeader: $.dpConst.SHOW_HEADER_LONG})
		.bind(
			'dateSelected',
			function(e, selectedDate, $td) {
				$('#bookit_checkin').val(selectedDate.asString());
				
				$('#bookit').animate({left: '30px'}, 500, 'easeInCubic');
				$('#calendar').animate({left: '300px'}, 500, 'easeInCubic');
			}
		);
	
	$('#bookit_reserve').click(function(ev) {
		var arrivalDate = $('#bookit_checkin').val().split('/');
		var departureDate = $('#bookit_checkin').val().split('/');
		$('#arrivalDate').val(arrivalDate['2'] + '-' + arrivalDate['0'] + '-' + arrivalDate['1']);
		
		var dateIn = new Date(arrivalDate['2'], arrivalDate['0'], arrivalDate['1']);
		var dateOut = new Date(arrivalDate['2'], arrivalDate['0'], arrivalDate['1']);
		dateOut.setDate(dateIn.getDate() + parseInt($('#bookit_nights').val()));
		$('#departureDate').val(dateOut.getFullYear() + '-' + 
			pad(Number(dateOut.getMonth()), 2) + '-' +
			pad(dateOut.getDate(), 2));
		
		$('#roomOccupancyTotal').val(parseInt($('#bookit_adults').val()) + parseInt($('#bookit_children').val()));
		
		$('#bookit').submit();
		
		ev.preventDefault();
	});
	
	$('#changelanguage').click(function(ev) {
		closeAllDropDowns();
		
		var positionTop = $('#languages').css('top');
		
		if (positionTop == '-285px') {
			$('#languages').animate({top: '0px'}, 650);
			closeAllDropDowns('changelanguage');
		} else {
			$('#languages').animate({top: '-285px'}, 650);
		}
		
		ev.preventDefault();
	});
	
	$('#languages a.language').click(function(ev) {
		var code = $(this).attr('href');
		var theurl = document.URL;
		
		theurl = theurl.replace($('base').attr('href'), '');
		
		if (theurl.charAt(2) == '/') {
			theurl = theurl.replace(/[a-z][a-z]\/?/, '');
		}
		
		window.location.href = code + '/' + theurl;
		
		ev.preventDefault();
	});
	
	$('#roomssuites').mouseenter(function(ev) {
		closeAllDropDowns();
		
		hideFeature();
		if ($('#navroomssuites').find('.subnav').css('top') == '0px') {
			$('#navroomssuites').find('.subnav').animate({top: navheights.roomssuites + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else if (parseInt($('#navroomssuites').find('.subnav').css('top')) < navheights.roomssuites) {
			$('#navroomssuites').find('.subnav').stop();
			$('#navroomssuites').find('.subnav').animate({top: navheights.roomssuites + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else {
			showFeature();
			
			$(this).css('background', '');
			$('#navroomssuites').find('.subsubnav').animate({top: '0px'}, 643, 'linear', function() {
				$(this).css('visibility', 'hidden');
			});
		}
		ev.preventDefault();
	});
	
	$('#teacuisine').mouseenter(function(ev) {
		closeAllDropDowns();
		
		hideFeature();
		
		if ($('#navteacuisine').find('.subnav').css('top') == '0px') {
			$('#navteacuisine').find('.subnav').animate({top: navheights.teacuisine + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else if (parseInt($('#navteacuisine').find('.subnav').css('top')) < navheights.teacuisine) {
			$('#navteacuisine').find('.subnav').stop();
			$('#navteacuisine').find('.subnav').animate({top: navheights.teacuisine + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else {
			showFeature();
			
			$(this).css('background', '');
			
			$('#navteacuisine').find('.subsubnav').animate({top: '0px'}, 650, '', function() {
				$(this).css('visibility', 'hidden');
			});
		}
		
		ev.preventDefault();
	});
	
	$('#privatedining').mouseenter(function(ev) {
		closeAllDropDowns();
		
		hideFeature();
		
		if ($('#navprivatedining').find('.subnav').css('top') == '0px') {
			$('#navprivatedining').find('.subnav').animate({top: navheights.privatedining + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else if (parseInt($('#navprivatedining').find('.subnav').css('top')) < navheights.privatedining) {
			$('#navprivatedining').find('.subnav').stop();
			$('#navprivatedining').find('.subnav').animate({top: navheights.privatedining + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else {
			showFeature();
			
			$(this).css('background', '');
			
			$('#navprivatedining').find('.subsubnav').animate({top: '0px'}, 650, '', function() {
				$(this).css('visibility', 'hidden');
			});
		}
		
		ev.preventDefault();
	});

	$('#hotelservices').mouseenter(function(ev) {
		closeAllDropDowns();
		
		hideFeature();
		
		if ($('#navhotelservices').find('.subnav').css('top') == '0px') {
			$('#navhotelservices').find('.subnav').animate({top: navheights.hotelservices + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else if (parseInt($('#navhotelservices').find('.subnav').css('top')) < navheights.hotelservices) {
			$('#navhotelservices').find('.subnav').stop();
			$('#navhotelservices').find('.subnav').animate({top: navheights.hotelservices + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else {
			showFeature();
			
			$(this).css('background', '');
			
			$('#navhotelservices').find('.subsubnav').animate({top: '0px'}, 650, '', function() {
				$(this).css('visibility', 'hidden');
			});
		}
		
		ev.preventDefault();
	});

	$('#aboutus').mouseenter(function(ev) {
		closeAllDropDowns();
		
		hideFeature();
		
		if ($('#navaboutus').find('.subnav').css('top') == '0px') {
			$('#navaboutus').find('.subnav').animate({top: navheights.aboutus + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else if (parseInt($('#navaboutus').find('.subnav').css('top')) < navheights.aboutus) {
			$('#navaboutus').find('.subnav').stop();
			$('#navaboutus').find('.subnav').animate({top: navheights.aboutus + 'px'}, 650);
			$(this).css('background', "url('../assets/images/menu-bg-hov.gif') repeat-x top");
		} else {
			showFeature();
			
			$(this).css('background', '');
			
			$('#navaboutus').find('.subsubnav').animate({top: '0px'}, 650, '', function() {
				$(this).css('visibility', 'hidden');
			});
		}
		
		ev.preventDefault();
	});

	$('.nav').mouseleave(closeAllDropDowns);
	$('div.subnav ul li a').mouseenter(function(ev) {
		$('.subsubnav').css('visibility','hidden');
	});
	$('div.subnav ul li a').mouseenter(function(ev) {
		var that = this;
		var classes = $(this).attr('class');
		var animetime = 650;
		
		if (typeof classes !== 'undefined' && classes !== false) {
			var classList = classes.split(/\s+/);
			
			var hidden = false;
			var tohide;
			var baseTop = parseInt($(this).parents('.nav').css('top'))*-1;
			$.each(classList, function (index, className) {
				var baseHeight = $('a.'+className).position().top + baseTop;
				$.each($('div.'+className), function (ind, el) {
					$(el).css('top',baseHeight+'px');
					$(el).css('left','180px');
				});
				$('div.'+className).css('visibility','visible');
			});
			
			ev.preventDefault();
		}
	});
	
	$('#navroomssuites .navclose a').click(function(ev) {
		$('#roomssuites').trigger('click');
		
		ev.preventDefault();
	});
	
	$('#navteacuisine .navclose a').click(function(ev) {
		$('#teacuisine').trigger('click');
		
		ev.preventDefault();
	});
	
	$('#navprivatedining .navclose a').click(function(ev) {
		$('#privatedining').trigger('click');
		
		ev.preventDefault();
	});
	
	$('#navhotelservices .navclose a').click(function(ev) {
		$('#hotelservices').trigger('click');
		
		ev.preventDefault();
	});
	
	$('#navaboutus .navclose a').click(function(ev) {
		$('#aboutus').trigger('click');
		
		ev.preventDefault();
	});
});

function closeAllDropDowns() {
	if ($('#languages').css('top') != '-285px') {
		$('#languages').animate({top: '-285px'}, 650);
	}
	
	if ($('#bookform').css('top') != '-315px') {
		$('#bookform').animate({top: '-315px'}, 500, 'easeInCubic', function() {
			$('#booknow').animate({top: '0px'}, 500, 'easeInCubic');
		});
	}
	
	hideSubNav('#navroomssuites');
	hideSubNav('#navteacuisine');
	hideSubNav('#navprivatedining');
	hideSubNav('#navhotelservices');
	hideSubNav('#navaboutus');
}

function hideSubNav(id) {
	var subnav = $(id).find('.subnav');
	if (subnav.css('top') != '0px') {
		$(id.replace('nav', '')).css('background-image', 'none');
		$(id).find('.subsubnav').each(function(i, el) {
			el = $(el);
			var top = el.parent().find('.subnav').height() - el.height();
			if (top + 'px' != el.css('top')) {
				el.css('top', top + 'px');
				//$(this).css('visibility', 'hidden');
			}
		});
		subnav
			.stop()
			.animate({top:'0px'},500);
	}
}

function showCalendar() {
	$('#bookit').animate({left: '-270px'}, 500, 'easeInCubic');
	$('#calendar').animate({left: '24px'}, 500, 'easeInCubic');
}

function hideCalendar() {
	$('#bookit').animate({left: '30px'}, 500, 'easeInCubic');
	$('#calendar').animate({left: '300px'}, 500, 'easeInCubic');
}

function hideFeature() {
	if ($(window).height() < 890) {
		// $('#featureWrap').animate({opacity: 0}, 450, false);
		// $('#featureless').animate({opacity: 1}, 450, false);
	}
}

function showFeature() {
	// $('#featureWrap').animate({opacity: 1}, 450, false);
	// $('#featureless').animate({opacity: 0}, 450, false);
}

function pad(number, length) {
	var str = '' + number;
	
	while (str.length < length) {
		str = '0' + str;
	}
	
	return str;
}

function scaleSize(maxW, maxH, currW, currH) {
	var ratio = currH / currW;
	
	if (currW >= maxW && ratio <= 1 && currH >= maxH) {
		currW = maxW;
		currH = currW * ratio;
	} else if (currW < maxW) {
		currW = maxW;
		currH = currW * ratio;
	} else if(currH >= maxH){
		currH = maxH;
		currW = currH / ratio;
	} else if (currH < maxH) {
		 currH = maxH;
		 currW = currH / ratio;
	}
	
	return [currW, currH];
}

// this is a bit of a mess atm
function resizeBgImage() {
	var windowWidth = $(window).width();
	// var windowHeight = $(window).height();
	var windowHeight = document.body.clientHeight;
	var backgroundWidth = $('#backgroundimage').width();
	var backgroundHeight = $('#backgroundimage').height();
	var origHeight = $('#backgroundimage').attr('aheight');
	var origWidth = $('#backgroundimage').attr('awidth');
	
	var ultimateratio = origHeight / origWidth;
	
	if (backgroundHeight < windowHeight) {
		var sizes = scaleSize(windowWidth, windowHeight, backgroundWidth, backgroundHeight);
		
		$('#backgroundimage').width(sizes[0]);
		$('#backgroundimage').height(sizes[1]);
	}
	
	if (backgroundWidth < windowWidth) {
		var sizes = scaleSize(windowWidth, windowHeight, origWidth, origHeight);
		
		$('#backgroundimage').width(sizes[0]);
		$('#backgroundimage').height(sizes[1]);
	}
	
	var backgroundimageheight = $('#backgroundimage').height();
	
	if (windowHeight < backgroundimageheight) {
		$('#backgroundimage').css('position', 'absolute');
		$('#backgroundimage').css('top', '-' + ((backgroundimageheight + 180)/2 - windowHeight/2) + 'px');
	}
}

var resizeTimer;
$(window).resize(function() {
	clearTimeout(resizeTimer);
	resizeTimer = setTimeout(resizeBgImage, 300);
});
