// JavaScript Document

var home = {


	init: function()
	{
		var locale = $('span#current-locale input').val();
	
		this.lasts('div#lasts');
		$('#lasts .controls a, #lasts .controls span.prev, #lasts .controls span.next, #depoimentos a, #acomodaoesBtn a, #reservasParte1 a, #faleConosco a, #formDepoimentos a').neoHover();
		$('#neighborhood-select span.up, #neighborhood-select span.down').hide();

	},




	lasts: function(obj)
	{
		var duration = 400;
		var visible = 1;
		var step = 1;
		var id = $(obj).attr('id');
		var holder = $('#' + id + ' .holder');
		var el = $('#' + id + ' .holder ol');
		var items = $('#' + id + ' .holder ol li');
		var prev = $('#' + id + ' .holder .prev');
		var next = $('#' + id + ' .holder .next');
		var pos = $('#' + id + ' .holder .position');
		var readMore = $('#' + id + ' .holder .controls a');
		var amountItems = $(items).length;

		$('#' + id + ' ol').width($(items).width() * amountItems);

		var widthStep = $(items).width() * step;

		$(el).css('left', widthStep);

		$(items).hover(function()
		{
			$(this).addClass('hover');
		}, function()
		{
			$(this).removeClass('hover');
		});


		$(prev).bind('click', fcPrev);
		$(next).bind('click', fcNext);
		$(next).trigger('click');

		function fcPrev()
		{
			if (parseInt($(el).css('left')) < 0)
			{
				var posCurrent = -((parseInt($(el).css('left')) - widthStep) / widthStep) - 1;
				$(pos).html(posCurrent + " / " + amountItems);
				var linkCurrent = $(items).eq(posCurrent - 1).find('a').attr('href');
				$(items).eq(posCurrent - 1).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(posCurrent - 1).click(function() { location.href = linkCurrent; });
				$(prev).unbind('click');
				$(el).animate({ left: parseInt($(el).css('left')) + widthStep }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(prev).bind('click', fcPrev);
				}
				});
			} else
			{
				$(pos).html(amountItems + " / " + amountItems);
				var linkCurrent = $(items).eq(amountItems - 1).find('a').attr('href');
				$(items).eq(amountItems - 1).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(amountItems - 1).click(function() { location.href = linkCurrent; });
				$(prev).unbind('click');
				$(el).animate({ left: -(widthStep * (amountItems - 1)) }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(prev).bind('click', fcPrev);
				}
				});
			}
		}

		function fcNext()
		{
			if (-(parseInt($(el).css('left'))) < ($(el).width() - $(holder).width()))
			{
				var posCurrent = -((parseInt($(el).css('left')) - widthStep) / widthStep) + 1;
				$(pos).html(posCurrent + " / " + amountItems);
				var linkCurrent = $(items).eq(posCurrent - 1).find('a').attr('href');
				$(items).eq(posCurrent - 1).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(posCurrent - 1).click(function() { location.href = linkCurrent; });
				$(next).unbind('click');
				$(el).animate({ left: parseInt($(el).css('left')) - widthStep }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(next).bind('click', fcNext);
				}
				});
			} else
			{
				$(pos).html(1 + " / " + amountItems);
				var linkCurrent = $(items).eq(0).find('a').attr('href');
				$(items).eq(0).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(0).click(function() { location.href = linkCurrent; });
				$(next).unbind('click');
				$(el).animate({ left: 0 }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(next).bind('click', fcNext);
				}
				});
			}
		}
	},





	slideShow: function(obj, duration, delay)
	{

		var easing = 'easeOutQuad';
		var id = $(obj).attr('id');
		var holder = $('#' + id);
		var imgs = $('#' + id + ' ul li a img');
		var txt = $('#' + id + ' ul li a span');
		var txtPos = $(txt).eq(0).css('top');
		$(txt).fadeTo(0, 0);

		var zIndex = 1;

		function anime(index)
		{

			var direction = $(imgs).eq(index).attr('class');
			var topDiff = $(imgs).eq(index).height() - $(obj).height();
			var leftDiff = $(imgs).eq(index).width() - $(obj).width();

			$(imgs).eq(index).fadeTo(0, 0);
			$(imgs).eq(index).css('display', 'block');
			$(txt).eq(index).fadeTo(0, 0);
			$(txt).eq(index).css('display', 'block');

			var newIndex = ($(imgs).length != index + 1) ? index + 1 : 0;

			zIndex += 1;
			$(imgs).eq(index).css('z-index', zIndex);

			$(txt).eq(index).css('z-index', zIndex + 10);
			$(txt).eq(index).css('top', (parseInt(txtPos) + $(txt).eq(index).height()) + 'px');

			switch (direction)
			{


				case 'top':
					$(imgs).eq(index).css({ 'top': 0, 'left': 0 });
					$(imgs).eq(index)
					//.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ opacity: 1 }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(imgs).eq(index).animate({ opacity: 0 }, { queue: false, duration: duration + 2000, easing: easing, complete: function() { $(this).css({ 'display': 'none' }) } });
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});

					break;


				/*case 'top':
				$(imgs).eq(index).css({ 'top': 0, 'left': 0 });
				$(imgs).eq(index)
				.animate({ opacity: 1 }, duration / 2, easing)
				.animate({ top: -topDiff, left: 0 }, { queue: false, duration: duration, easing: easing, complete: function()
				{
				$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
				}
				})
				.queue(function()
				{
				setTimeout(function()
				{
				$(imgs).eq(index).dequeue();
				$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
				}, delay);
				});

					break;*/ 

				case 'bottom':
					$(imgs).eq(index).css({ 'top': -topDiff, 'left': 0 });
					$(imgs).eq(index)
						.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ top: 0, left: 0 }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});
					break;

				case 'left':
					$(imgs).eq(index).css({ 'top': 0, 'left': 0 });

					$(imgs).eq(index)
						.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ top: 0, left: -leftDiff }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});
					break;

				case 'right':
					$(imgs).eq(index).css({ 'top': 0, 'left': -leftDiff });

					$(imgs).eq(index)
						.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ top: 0, left: 0 }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});
					break;

				default:
					$(imgs).eq(index).css({ 'top': 0, 'left': 0 });
					$(imgs).eq(index).fadeTo(1, 1);
					break;
			}
		}
		anime(0);
	},





	sideNav: function(obj)
	{
		var visible = 6;
		var step = 1;
		var id = $(obj).attr('id');
		var holder = $('#' + id + ' .holder');
		var el = $('#' + id + ' .holder ul');
		var items = $('#' + id + ' .holder ul li');
		var links = $('#' + id + ' .holder ul li a');
		var up = $('#' + id + ' .holder .up');
		var down = $('#' + id + ' .holder .down');
		var amountItems = $(items).length;
		var heightStep = $(items).height() * step + 1;

		$(links).neoHover();
		$(holder).height((visible + 1) * (heightStep - .1));
		$(down).hover(function() { $(this).addClass('hover') }, function() { $(this).removeClass('hover'); });
		$(up).hover(function() { $(this).addClass('hover') }, function() { $(this).removeClass('hover'); });
		$(up).hide();
		$(up).bind('click', fcUp);
		$(down).bind('click', fcDown);

		function fcUp()
		{
			if (parseInt($(el).css('top')) < 0)
			{
				$(up).unbind('click');
				$(el).animate({ top: parseInt($(el).css('top')) + heightStep }, { queue: false, duration: 250, easing: 'easeOutQuad', complete: function()
				{
					$(up).bind('click', fcUp);
					foo();
				}
				});
			}
		}

		function fcDown()
		{
			if (-(parseInt($(el).css('top'))) <= ($(el).height() - $(holder).height()))
			{
				$(down).unbind('click');
				$(el).animate({ top: parseInt($(el).css('top')) - heightStep }, { queue: false, duration: 250, easing: 'easeOutQuad', complete: function()
				{
					$(down).bind('click', fcDown);
					foo();
				}
				});
			}
		}

		function foo()
		{
			if (parseInt($(el).css('top')) < 0)
			{
				$(up).fadeIn(60);
			} else
			{
				$(up).fadeOut(60);
			}
			if (-(parseInt($(el).css('top'))) <= ($(el).height() - $(holder).height()))
			{
				$(down).fadeIn(60);
				$(down).removeClass('disabled');
			} else
			{
				$(down).addClass('disabled');
			}
		}

	}
};

