(function ($) {

	$(function() {
		flashembed("flashbanner", {
			src: '/shannons/flash/home/home-noquote.swf',
			wmode: 'opaque',
			width: '715',
			height: '302'
		});
		
		$('.rotate').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 5000,
			pause: true
		});
		
		
		$('.swapper').find('.swapnav').each(function(a) {
			var $this = $(this);
			
			$this.click(function(e) {
				e.preventDefault();
	
				$('.swapper').find('.active').removeClass('active');
				$('.swapper-content').find('.active').removeClass('active');
				
				$this.addClass('active');
				$('.swapper-content').find('.swapcontent').eq(a).addClass('active').css({'opacity': 0}).animate({'opacity':1});
			});
		});
		
		var images = $('#images');
		var active = false;
		
		images.find('.image').click(function(e) {
			e.preventDefault();
			$(this).addClass('active');
			var options = $(this).attr('rel').split('|');
			enlargeImagePopup(options[0], options[1], options[2]);
		});
	
		function enlargeImagePopup(id, num, src) {
			$('body').append('<div id="popupBg" />').find('#popupBg').css({'background':'#000','width':'100%','height':'100%','opacity':'0.5','position':'fixed','top':0,'left':0,'z-index':'9998'});
	
			var bodyWidth = $(window).width(),
				popupWidth = 850,
				leftPos = bodyWidth/2-popupWidth/2,
				count = images.find('.image').size();
				
			$('html').addClass('popupWindow');
				
			$('body').append('<div class="popupContainer"><div id="popup" /></div>').find('#popup').css({'box-shadow':'0 5px 5px rgba(0,0,0,0.5)','background':'#fff','width':'820px', 'margin':'40px auto',
			'padding':'15px','text-align':'center','position':'relative','z-index':'9999'}).html('<div style="background: -moz-linear-gradient(center top , #000000, #202020) no-repeat scroll 0 0 transparent; padding: 20px; background: #000; position: relative;"><a class="close" href="#" style="position: absolute; top: 0; right: 0;"><img src="/club/images/icons/close.png" alt="Close" /></a><a style="width: 52px; height: 33px; display: block; border-radius: 2px; border: 1px solid #333; line-height: 33px; position: absolute; top: 45%; left: 10px;" href="#" class="prev"><img style="margin-top: 11px;" src="/club/images/general/gallery-prev.png" alt="Prev" /></a><a style="width: 52px; height: 33px; display: block; border-radius: 2px; border: 1px solid #333; line-height: 33px; position: absolute; top: 45%; right: 10px;" href="#" class="next"><img style="margin-top: 11px;" src="/club/images/general/gallery-next.png" alt="Next" /></a><div class="photo-container clearfix"><div class="photo-container-area clearfix"><ul style="list-style: none; margin: 0; padding: 0;"><li><img src="' + src + '" alt="" ></li></ul></div><div class="photo-status-bar clearfix" style="color: #fff;"><span>Image <span class="count">1</span> of <span class="howmany">2</span></span></div></div>');
			
			$('.next, .prev').hide();
			if ( count > 1 ) {
				$('.next, .prev').show();
			}
			
			var count = $('#images .image').size();
			$('.howmany').html('' + count + '');
			$('.count').html('' + num + '');
			
			
			$('.prev').click(function(e) {
				e.preventDefault();
				var active = images.find('.image.active');
				if (active.length == 0) active = images.find('.image:first');
				
				var prev = active.prev('.image').length ? active.prev('.image') : images.find('.image:last');
				
				active.removeClass('active');
				prev.addClass('active');
				
				var options = prev.attr('rel').split('|');
				$('#popupBg, #popup, .popupContainer').remove();
				enlargeImagePopup(options[0], options[1], options[2]);
			});
			
			$('.next').click(function(e) {
				e.preventDefault();
				var active = images.find('.image.active');
				if (active.length == 0) active = images.find('.image:last');
				
				var next = active.next('.image').length ? active.next('.image') : images.find('.image:first');
				
				active.removeClass('active');
				next.addClass('active');
		
				var options = next.attr('rel').split('|');
				$('#popupBg, #popup, .popupContainer').remove();
				enlargeImagePopup(options[0], options[1], options[2]);
				
			});
	
			$('.close').click(function(e) {
				e.preventDefault();
				$('html').removeClass('popupWindow');
				$('#popupBg, #popup, .popupContainer').remove();
			});
		}
		
		var maps = $('#main');
		maps.find('.maplink').click(function(e) {
			e.preventDefault();
			var options = $(this).attr('href');
			enlargeMapPopup(options);
		});	
	
		function enlargeMapPopup(url) {
			$('body').append('<div id="popupBg" />').find('#popupBg').css({'background':'#000','width':'100%','height':'100%','opacity':'0.5','position':'fixed','top':0,'left':0,'z-index':'9998'});
	
			var bodyWidth = $(window).width(),
				popupWidth = 355,
				leftPos = bodyWidth/2-popupWidth/2;
				
			$('body').append('<div id="popup" />').find('#popup').css({'box-shadow':'0 5px 5px rgba(0,0,0,0.5)','background':'#fff','width':'355px','height':'355px','padding':'0','text-align':'center','position':'absolute','top':100, 'left': leftPos,'z-index':'9999'}).html('<div style="background: -moz-linear-gradient(center top , #000000, #202020) no-repeat scroll 0 0 transparent; position: relative;"><a class="close" href="#" style="position: absolute; top: 0; right: 0;"><img src="/club/images/icons/close.png" alt="Close" /></a><img id="displayImage" src="' + url + '" alt="" /></div>');

			$('.close').click(function(e) {
				e.preventDefault();
				$('#popupBg, #popup').remove();
			});
		}
	
		$(document).keydown(function(e) {
			if ( e.keyCode === 27 ) {
				$('html').removeClass('popupWindow');
				$('#popupBg, #popup, .popupContainer').remove();
			}
		});
	});

})(jQuery);
