/* Author: MICHAEL TEMPEST

*/
jQuery(function($){
	var currWidth = 0;
	var currHeight = 0;
	$('#full-screen-image').fullSizeImage();
	$('section').each(function(){
		$(this).attr('rel',$(this).height());
	})

	if ($('.enquiry-form').size()>0) {
		$('.enquiry-form').css('height',13);
	}

	if ($('.private-dining').size()>0) {
		$('a.show-floorplan').click(function(e){
			e.preventDefault();

			$(this).parent().fadeOut(function(){
				linkText = $(this).children('a.show-floorplan').attr('href').replace('#','');
				$('#'+linkText).fadeIn();
			});
		})

		$('.floorplan').find('span').mouseenter(function(){
			$('#' + $(this).attr('class')).fadeIn();
		}).mouseleave(function(){
			$('#' + $(this).attr('class')).fadeOut();
		})
	};

	$('.enquiry-form a.show-text').click(function(e){
		e.preventDefault();
		if ($(this).parent().height()>"13" && $(this).parent(':not(":animated")').size()>0) {

			$(this).parent().animate({
				'height':'13px'
			});

			$(this).removeClass('hide-text').addClass('show-text');
		}

		if ($(this).parent().height()=="13" && $(this).parent(':not(":animated")').size()>0) {
			$(this).parent().animate({
				'height':$(this).parent().attr('rel') + 'px',
				'paddingBottom':'20px'
			});

			$(this).removeClass('show-text').addClass('hide-text');
		};
	});

	$('section:not(".enquiry-form") a.hide-text').click(function(e){
		e.preventDefault();
		if ($(this).parent().height()>"0" && $(this).parent(':not(":animated")').size()>0) {
			currWidth = $(this).parent().width();
			currHeight = $(this).parent().height();

			$(this).parent().animate({
				'height':'0px',
				'paddingBottom':'0px'
			},function(){
				$(this).animate({
					'width':'0px',
					'paddingLeft':'0px'
				});
			});

			$(this).removeClass('hide-text').addClass('show-text');
		}

		if ($(this).parent().height()=="0" && $(this).parent(':not(":animated")').size()>0) {
			$(this).parent().animate({
					'width':currWidth + 'px',
					'paddingLeft':'20px'
				},function(){
				$(this).animate({
					'height':$(this).attr('rel') + 'px',
					'paddingBottom':'20px'
				});
			});

			$(this).removeClass('show-text').addClass('hide-text');
		};
	});

	$(".hide-text.home").click();

	if ($('.gallery').size()>0) {
		imagesInRow=5;

		$.preload(['images/ajax-loader.gif','images/close.png']);
		$('.gallery-item:not(":animated")').click(function(e){
			e.preventDefault();
			if($(this).children('img:visible').size()>0){
				var linkURL = $(this).attr('href');
				$.preload([linkURL], {
					onRequest: function() {
						$('body').append('<div class="loading"><img src="img/ajax-loader.gif" alt="loading" /></div>');
					}, 
					onFinish: function(){
						$('.loading').remove();
						$('.gallery-item').css({
							'background':'url('+linkURL+') no-repeat'
						})
						orderBGimages();
						closeGap();
					}
				});
				currLink = $(this)
			} else {
				openGap();
			}
		})

		function closeGap() {
			$('.gallery-item img').fadeOut();

			$('.gallery-item').animate({
				'marginRight':0,
				'marginBottom':0
			},500)

			$('.row:last').after('<a href="#" class="close"><img src="img/close.png" alt="close" /></a>\n' +
									'<a href="#" class="left-arrow"><img src="img/left-arrow.png" alt="close" /></a>\n' +
									'<a href="#" class="right-arrow"><img src="img/right-arrow.png" alt="close" /></a>');

			$('.close').click(function(e){
				e.preventDefault();
				openGap();
			})

			$('.right-arrow').click(function(e){
				e.preventDefault();
				if ($('.gallery-item:not(":animated")').size()>0 && $('.loading').size() <1) {	
					nextImageNoTrans();
				};
			})

			$('.left-arrow').click(function(e){
				e.preventDefault();
				if ($('.gallery-item:not(":animated")').size()>0 && $('.loading').size() <1) {	
					prevImageNoTrans();
				};
			})
		}

		function openGap() {
			$('.gallery-item img').fadeIn();

			$('.gallery-item').animate({
				'marginRight':10,
				'marginBottom':10
				},500);
			
			$('.close, .left-arrow, .right-arrow').fadeOut(function(){
				$(this).remove();
			})
		}

		function orderBGimages() {
			imageLeft = 0;
			imageTop = 0;
			count = 0;
			$('.gallery-item').each(function() {
				$(this).css({
					'background-position' : imageLeft + 'px ' + imageTop + 'px'
				})
				imageLeft = imageLeft - $(this).width();
				count++;
				if(count==imagesInRow) {
					count=0;
					imageTop = imageTop - $(this).height();
					imageLeft = 0;
				}
			})
		}

		function nextImage() {
			if (currLink.next('a').size()>0) {
				currLink.next('a').trigger('click');
			} else {
				currLink.parent().next('div').children('a:first').trigger('click');
			};
		}

		function nextImageNoTrans() {
			if (currLink.next('a').size()>0) {
				var linkURL = currLink.next('a').attr('href');
			} else {
				var linkURL = currLink.parent().next('div').children('a:first').attr('href');
			};
			$.preload([linkURL], {
				onRequest: function() {
					$('body').append('<div class="loading"><img src="img/ajax-loader.gif" alt="loading" /></div>');
				}, 
				onFinish: function(){
					$('.loading').remove();
					$('.gallery-item').css({
						'background':'url('+linkURL+') no-repeat'
					})
					orderBGimages();
				}
			});
			if (currLink.next('a').size()>0) {
				currLink = currLink.next('a');
			} else {
				currLink = currLink.parent().next('div').children('a:first');
			};
		}

		function prevImage() {
			openGap();
			setTimeout(function(){
				if (currLink.prev('a').size()>0) {
					currLink.prev('a').trigger('click');
				} else {
					currLink.parent().prev('div').children('a:first').trigger('click');
				};
			},500);
		}

		function prevImageNoTrans() {
			if (currLink.prev('a').size()>0) {
				var linkURL = currLink.prev('a').attr('href');
			} else {
				var linkURL = currLink.parent().prev('div').children('a:first').attr('href');
			};
			$.preload([linkURL], {
				onRequest: function() {
					$('body').append('<div class="loading"><img src="img/ajax-loader.gif" alt="loading" /></div>');
				}, 
				onFinish: function(){
					$('.loading').remove();
					$('.gallery-item').css({
						'background':'url('+linkURL+') no-repeat'
					})
					orderBGimages();
				}
			});
			if (currLink.prev('a').size()>0) {
				currLink = currLink.prev('a');
			} else {
				currLink = currLink.parent().prev('div').children('a:last');
			};
		}
	};

	if ($('.contact').size()>0) {
  		initialize();
  	};
})
























