var scrimDelay = 500;
var scrimDiagWidth = 30;
var scrimDiagWidthNoNav = 75;
var contentWidth = 1000;
var copyWidth = 500;
var scrollBarWidth = 15;
var scrollBarPadding = 23;
var buttonContainerPadding = 15;
var scrimDoneAnimating = false;
var bitly;

function onReady()
{
	$('#Navigation').find('a').each(initNavBtn);
	$('.SubNavigation').find('li').each(initSubNavBtn);
	$('.SubNavigationSlider').each(initSubNavSlider);
	
	onResize();
	initHeader();
	initAddThis();
	
	bitly = new jQuery.Bitly({ login:'ideawork', key:'R_d906459472240c3d509d3b38d300c044' });
}

function onResize()
{
	var width = '100%';
	if(getBrowserWidth() < (contentWidth + scrollBarWidth)){
		width = contentWidth + scrollBarWidth + 'px';
	}

	$('#HeaderBackground').css('width', width);
	$('#NavigationBackground').css('width', width);
	$('#FooterContainer').css('width', width);
	$('#SlideContainer').css('width', width);
	$('#ScrimContainer').css('width', width);
	if(scrimDoneAnimating){
		$('#ScrimImage').css( {left:$('#Content').offset().left - getScrimOffset() + 'px'} );
	}
}

function initHeader()
{
	$('#reservations_date').val(getToday());
	$('#reservations_date').datepicker({ showAnim:'slideDown', showOtherMonths:true, selectOtherMonths:false, onSelect:onSelectDate });
	$('#reservations_people').mcDropdown('#reservations_people_menu', { targetColumnSize:1, maxRows:100 });
	$('#OpenTableButton').click(submitOpenTable);
	
	var date = new Date();
	$.getJSON("/library/json/dining_times.cfm", { month:date.getMonth() + 1, day:date.getDate(), year:date.getFullYear() }, initTimeDropdown );
}

function initTimeDropdown(data)
{
	var times = data.timeSlots;
	$('#reservations_time_menu').empty();
	$('#reservations_time_menu').append('<li class="mc_root" rel="">Time</li>');
	for(var i = 0; i < times.length; i++){
		$('#reservations_time_menu').append('<li class="mc_root" rel="' + times[i].time + '">' + times[i].time + '</li>');
	}
	$('#reservations_time').mcDropdown('#reservations_time_menu', { targetColumnSize:1, maxRows:100 });
}

function initAddThis()
{
	$('#AddThisContainer').mouseover(showAddThis).mouseout(hideAddThis);
}

function showAddThis()
{	
	$('.addthis_toolbox').stop().animate({ opacity:1, right:70 + 'px' }, 300);
}

function hideAddThis()
{
	$('.addthis_toolbox').stop().animate({ opacity:0, right:0 + 'px' }, 300);
}

/* MAIN NAVIGATION FUNCTIONS */
function initNavBtn()
{
	var img = $(this).find('img');
	var filter = $(this).find('img').css('filter');
	
	$(this).mouseover(showNavBackground);
	if($(this).attr('className') == 'On'){
		$(this).mouseover();
	} else {
		$(this).find('img').css('opacity', 0);
		$(this).mouseout(hideNavBackground);
	}
	
	if(filter.indexOf('progid') >= 0){
		$(this).find('img').css('filter', filter);
		$(this).find('img').attr('src', '/content/images/ui/clear.gif');
	}
}

function showNavBackground()
{
	$(this).find('img').stop().fadeTo(500, 1);
}

function hideNavBackground()
{
	$(this).find('img').stop().fadeTo(300, 0);
}

/* SUB NAVIGATION FUNCTIONS */
function initSubNavBtn()
{
	$(this).mouseover(moveSubNavSlider).mouseout(revertSubNavSlider);	
}

function initSubNavSlider()
{
	$(this).css('left', $(this).parent().parent().width() + 'px');
}

function moveSubNavSlider()
{
	var button = $(this);
	var subnav = $(this).closest('.SubNavigation');
	var slider = subnav.find('.SubNavigationSlider');
	var offset = button.offset().left - subnav.offset().left;
	subnav.find('.SubNavigationSliderContainer').css('width', subnav.width() + 'px')
	slider.stop().animate({ width: button.width() + 'px', left: offset + 'px' });
}

function revertSubNavSlider()
{
	var button = $(this);
	var subnav = $(this).parent().parent();
	var slider = subnav.find('.SubNavigationSlider');
	var offset = subnav.width();
	subnav.find('.SubNavigationSliderContainer').css('width', subnav.width() + 'px');
	slider.stop().animate({ left: offset + 'px' });
}

/* SCRIM AND CONTENT */
function showScrim()
{
	$('#ScrimImage').css('left', $('#Wrapper').width() + 'px');
	
	var calculatedWidth = getWidestSubNav();
	if(calculatedWidth > 0 && calculatedWidth > copyWidth){
		$('#Content').css('width', calculatedWidth + 'px');
		$('#CopyContainer').css('width', calculatedWidth - scrimDiagWidthNoNav + 'px');
		$('#MenuContainer').css('width', calculatedWidth - scrimDiagWidthNoNav + 'px');
	} else {
		$('#Content').css('width', copyWidth + 'px');
		$('#CopyContainer').css('width', copyWidth - scrimDiagWidthNoNav + 'px');	
		$('#MenuContainer').css('width', copyWidth - scrimDiagWidthNoNav + 'px');
	}
	
	$('#ScrimImage').animate({ left:$('#Content').offset().left - getScrimOffset() + 'px' }, '', '', showContent);
}

function showContent()
{
	scrimDoneAnimating = true;
	
	$('.ScrollPane').jScrollPane({ showArrows: true, verticalArrowPositions: 'split' , horizontalArrowPositions: 'split', verticalGutter:15 });
	
	initAccordion();
	initScrollPane();
	initMenuContainer();
	
	$('#ContentContainer').css('visibility', 'visible');
}

function initAccordion()
{
	$('.Accordion').accordion({ header:'h3', autoHeight:false, collapsible:true, activate:false, animated:false, change:accordionChange });
	$('.Accordion').accordion( 'activate', false );
	$('.Accordion').accordion({ animated:true });
	$('.Accordion').find('.ScrollPane').addClass('AccordionScrollPane');
	$('.Accordion').find('.jspPane').find('p').addClass('AccordionContent');
}

function accordionChange()
{
	$('#CopyContainer > .ScrollPane').data('jsp').getContentPane().css('height', $('.Accordion').height());
	$('#CopyContainer > .ScrollPane').data('jsp').reinitialise();
	$('.Accordion').parent().width($('.Accordion').parent().parent().width());
}

function initScrollPane()
{
	if($('#CopyContainer > .ScrollPane').length > 0){
		var h = parseInt($('#NavigationContainer').css('top')) - $('.ScrollPane').offset().top - buttonContainerPadding;
		
		if($('#ButtonContainer').length > 0){
			h -= ($('#ButtonContainer').height() + buttonContainerPadding);
		}
		
		$('#CopyContainer').css('height', h + 'px');
		$('#CopyContainer > .ScrollPane').find('.jspContainer').css('height', h + 'px');
		$('#CopyContainer > .ScrollPane').css('height', h + 'px');
		$('#CopyContainer > .ScrollPane').data('jsp').reinitialise();
	}
}

function initMenuContainer()
{
	if($('#MenuContainer').length > 0){
		var h = parseInt($('#NavigationContainer').css('top')) - $('#MenuContainer').offset().top - buttonContainerPadding;
		
		if($('#ButtonContainer').length > 0){
			h -= ($('#ButtonContainer').height() + buttonContainerPadding);
		}
		
		$('#MenuContainer').css('height', h + 'px');
	}
}

/* SLIDESHOW */
var slides;
var slidesIndex = 0;
var swapSlides = true;
function initSlideshow(data)
{
	slides = data.slides;
	if(slides.length > 0){
		$('#Slide1').bind('load', loadInitialSlide);
		$('#Slide1').attr('src', slides[slidesIndex].image);
	} else {
		setTimeout(showScrim, scrimDelay);
	}
}

function loadInitialSlide()
{
	$('#Slide1').unbind('load', loadInitialSlide);
	$('#Slide1').fadeTo(1000, 1);
	setTimeout(showScrim, scrimDelay);
	
	if(slides.length > 1){
		$('#Slide2').attr('src', slides[++slidesIndex].image);
		setTimeout(nextSlide, slides[0].duration * 1000);
	}
}

function nextSlide()
{
	if(swapSlides){
		$('#Slide2').fadeTo(1000, 1);
		$('#Slide1').fadeTo(1000, 0);
	} else {
		$('#Slide1').fadeTo(1000, 1);
		$('#Slide2').fadeTo(1000, 0);
	}
	
	swapSlides = !swapSlides
	slidesIndex++;
	if(slidesIndex >= slides.length) slidesIndex = 0;
	
	setTimeout(loadSlide, 1500);
	setTimeout(nextSlide, slides[slidesIndex].duration * 1000);
}

function loadSlide()
{
	if(swapSlides){
		$('#Slide2').attr('src', slides[slidesIndex].image);
	} else {
		$('#Slide1').attr('src', slides[slidesIndex].image);
	}
}

/* FORM STUFF */
function onSelectDate(dateText, inst)
{
	var month = dateText.split('/')[0];
	var day = dateText.split('/')[1];
	var year = dateText.split('/')[2];
	$.getJSON("/library/json/dining_times.cfm", { month:month, day:day, year:year }, initTimeDropdown );
}

function onSelect()
{
}

function submitOpenTable()
{
	var queryString = '&d=' + $('#reservations_date').val();
	if($('#reservations_time').val() != 'Time' && $('#reservations_time').val() != ''){
		queryString += (' ' + $('#reservations_time').val());
	} else {
		queryString = '';
	}
	if($('#reservations_people').val() != 'People' && $('#reservations_people').val() != ''){
		queryString += ('&p=' + $('#reservations_people').val());
	} else {
		queryString = '';
	}
	if(queryString != ''){
		window.open($(this).attr('data-href') + queryString);
	} else {
		window.open($(this).attr('data-href2') + queryString);
	}
	
}

/* HELPER FUNCTIONS */
function shortenLink()
{
	bitly.shorten($(this).text(), $(this), function(url, el){
		$(el).attr('target', '_blank');
		$(el).attr('href', url);
		$(el).text(url);
		$('.ScrollPane').jScrollPane().data('jsp').reinitialise();
	});
}

function getToday()
{
	var date = new Date();
	var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
	var dateString = month + '/' + day + '/' + date.getFullYear();
	
	return dateString;
}

function getWidestSubNav()
{
	var subNavWidths = new Array();
	var subNavs = $('.SubNavigation');
	if(subNavs.length > 0){
		for(var i = 0; i < subNavs.length; i++){
			var w = 0;
			var subNavItems = $(subNavs[i]).find('li');
			for(var j = 0; j < subNavItems.length; j++){
				w += ($(subNavItems[j]).width() + parseInt($(subNavItems[j]).css('padding-right')));
			}
			subNavWidths[i] = w;
			return subNavWidths.sort(function numOrdD(a, b){ return b - a; })[0] + 10;
		}
	} else {
		return 0;
	}
}

function getScrimOffset()
{
	if(getWidestSubNav() > 0){
		return scrimDiagWidth;
	} else {
		return scrimDiagWidthNoNav;
	}
}

function getBrowserWidth()
{
	var browserWidth = 0;
  	if( typeof( window.innerWidth ) == 'number' ) {
		browserWidth = window.innerWidth;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		browserWidth = document.documentElement.clientWidth;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		browserWidth = document.body.clientWidth;
  	}
	
  	return browserWidth;
}

function getBrowserHeight()
{
	var browserHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		browserHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		browserHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		browserHeight = document.body.clientWidth;
	}
	
	return browserHeight;
}
