var BaseUI = 
{
	Initialize : function()
	{
		$(window).resize(function() {
			BaseUI.RotatorHeight()
		});
		
		$('.flexslider').flexslider({
			controlNav: false,
			animationDuration: 250
		})
		
		$("a#fancy").fancybox( {
			overlayColor: '#0b0501'
		})
		
		$("#fancybox-close").html("CLOSE");
						
		$('div.favorite-cards').css(
		{
			width: BaseUI.CardsWrapWidth,
			left: BaseUI.CardsCenter
		});
		
		$('.listing-sub-nav ul').localScroll()
		
		// Zebra stripe table rows
		$('.listing-detail tr:odd').css('background-color', '#eddebe')
		
		if ($('.listing-detail fieldset input.date').length > 0) {
			$('.listing-detail fieldset input.date').dateinput({
				trigger: true
			})
		}
		
		$('input.all').change(function()
		{
			if ($(this).is(':checked')) {
				$(this).parent('label').nextAll('label').children('input').prop('checked', true)
			} else {
				$(this).parent('label').nextAll('label').children('input').prop('checked', false)
			}
			
		})
		
		if ($('div.filters').length > 0) {
			BaseUI.FixFilterPositionScroll()
		}
		
		if ($('div.listing-sub-nav-wrap').length > 0) {
			BaseUI.FixListingSubnavPositionScroll()
		}
		
		if ($('div.slideshow').length > 0) {
			BaseUI.CardsLength = $('div.slideshow div.slides').children('.slide').size();
			BaseUI.CardsOffset = $('div.slideshow div.slides').position().left;
			BaseUI.CardWidth = $('div.slideshow div.slide').width();
			BaseUI.CardsWrapWidth = (BaseUI.CardsLength * BaseUI.CardWidth);
			BaseUI.CardCounter = 1;
			
			$('div.slideshow div.slides').css(
			{
				width: BaseUI.CardsWrapWidth
			});
			
			BaseUI.ChangeButtonState();
			BaseUI.ClickButtonNext();
			BaseUI.ClickButtonPrev();
		}
		
		BaseUI.HeroPhotosCycle()
		BaseUI.IntroSlideUp()
		BaseUI.FeatureHover()
		BaseUI.SeasonsHover()
	},
	RotatorHeight : function()
	{
		var pageY = $(window).height()
		var headY = $('div.header').height()
		var footY = $('div.footer').height()
		var subfootY = $('div.subfoot').height()
		var height = pageY - headY - footY - subfootY - 17
		$('.flexslider, .flexslider .slides').css('min-height', height)
	},
	HeroPhotosCycle : function()
	{
		BaseUI.RotatorHeight()
		
		if ($('#slideshow img').length > 1) {
			$('#slideshow img:gt(0)').hide()
			setInterval(function()
			{
				$('#slideshow img:first-child').hide().next('img').show().end().appendTo('#slideshow')
			}, 5000)
		}
		
	},
	IntroSlideUp : function()
	{
		var position = $('.intro').position();
		$(window).resize(function()
		   {
			   var position = $('.intro').position();
		   }
		)

		$('.slider').toggle(
			function () {
			$('#content .slider').addClass('close').html('CLOSE');
			$('.intro').css({'background-position':'0px 0px'}).animate({'top': '16px'}, 250);
			$('#content').css({'position':'absolute'}).animate({'top': '0%', 'margin-top':'107px'}, 500);
			},
			function () {
			$('#content .slider').removeClass('close').html('READ MORE');
			$('.intro').css({'background-position':'0px 20px'}).animate({'top': + position.top}, 500);
			$('#content').css({'position':'fixed'}).animate({'top': '100%', 'margin-top':'0'}, 500);
			}
		)
	},
	FeatureHover : function()
	{
		$(".feature.email").hover(emailHoverOn, emailHoverOff);
        function emailHoverOn() {
			$('.signup').fadeIn('fast');
            $(this).addClass('on');
            $(this).siblings().fadeTo(200, "0.5");
        }
        function emailHoverOff() {
			$('.signup').fadeOut('fast');
            $(this).removeClass('on');
            $(this).siblings().fadeTo(200, "1.0");
        }
	},
	SeasonsHover : function()
	{
		$('ul.seasons li').hover(function()
		{	
			$(this).children('div.seasons-info').show()
		}, function() 
		{
			$(this).children('div.seasons-info').hide()
		})
	},
	FixFilterPositionScroll : function()
	{
		var filtersOffset = $('.filters').offset().top

		$(window).bind('scroll', function ()
		{			
			if ($(this).scrollTop() > filtersOffset) {
				$('.filters').addClass('fixed')
			}

			if ($(this).scrollTop() < filtersOffset) {
				$('.filters').removeClass('fixed')
			}
		})
	},
	FixListingSubnavPositionScroll : function()
	{
		var filtersOffset = $('.listing-sub-nav-wrap').offset().top

		$(window).bind('scroll', function ()
		{			
			if ($(this).scrollTop() > filtersOffset) {
				$('.listing-sub-nav-wrap').addClass('fixed')
			}

			if ($(this).scrollTop() < filtersOffset) {
				$('.listing-sub-nav-wrap').removeClass('fixed')
			}
		})
	},
	ClickButtonNext : function() 
	{
		$('button#next').click(function(e)
		{
			if (BaseUI.CardCounter < BaseUI.CardsLength) {
				BaseUI.CardCounter++;
				console.log(BaseUI.CardCounter)
				BaseUI.ChangeButtonState();
				$('div.slides').stop().animate({
					left: '-=' + BaseUI.CardWidth
				});
			}
			e.preventDefault()
			return false
		});
	},
	ClickButtonPrev : function()
	{
		$('button#prev').click(function(e)
		{
			if (BaseUI.CardCounter > 1) {
				BaseUI.CardCounter--;
				BaseUI.ChangeButtonState();
				$('div.slides').stop().animate({
					left: '+=' + BaseUI.CardWidth
				});
			}
			e.preventDefault()
			return false
		});
	},
	ChangeButtonState : function() 
	{
		if (BaseUI.CardCounter == BaseUI.CardsLength) {
			$('button#next').css({
				opacity: 0.5
			})
		} else {
			$('button#next').css({
				opacity: 1
			})
		}
		if (BaseUI.CardCounter == 1) {
			$('button#prev').css({
				opacity: 0.5
			})
		} else {
			$('button#prev').css({
				opacity: 1
			})
		}
	}
}

$(document).ready(BaseUI.Initialize)
