// Global site functions
var site = {
	applyEasing: function(){
		$('.nav > .menu').each(function(i){

			var h = [];
			var m = $('ul.menu-item');
			m.each(function(i){
				h[i] = $(this).css({'display': 'block'}).height();
				$(this).css({'display': 'none'});
			});

			$(this)
				.mouseenter(function(){
					m.eq(i)
						.stop()
						.css({'display':'block', 'opacity': 0, 'height': 0})
						.animate({'opacity': .95, 'height': h[i]}, {'duration': 200})
						.parent().css({'zIndex': 9999});
				})
				.mouseleave(function(){
					m.eq(i)
						.stop()
						.animate({'opacity': 0, 'height': 0}, {'duration': 200}, function(){
								$(this).css({'height': 'auto'});
							});
				});

		});
	},

	installLightbox: function(){
		$('.lightbox a, .gallery a, .carousel a').fancybox({
			padding: 0,
			titlePosition: 'over',
			overlayOpacity: .8,
			titleFormat: function formatTitle(title, currentArray, currentIndex, currentOpts) {
				var text = $(currentArray[currentIndex]).attr('alt');
				return '<span id="fancybox-title-over"><h5>'+title+'</h5><p>'+text+'</p><span id="fancybox-pagination">'+(currentIndex+1)+' of '+currentArray.length+'</span></span>'
			}
		});
	},

	startSlideshow: function(){
		var d = $('<div class="description"></div>').appendTo('.slider');
		$('.slides').cycle({
			pager: '.pager',
			before: function(){
				var img = $(this).find('img');
				d.html('<h2>'+img.attr('title')+'</h2><p>'+img.attr('alt')+'</p>').fadeIn();
			}
		});
	},

	mapGym: function(){
		var pt = new google.maps.LatLng(47.8092350, -122.3858070);
		var opts = {
			zoom: 15,
			center: pt,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		var map = new google.maps.Map(document.getElementById('map'), opts);
		var marker = new google.maps.Marker({
			position: pt,
			animation: google.maps.Animation.BOUNCE,
			map: map,
			title:'Harbor Square Athletic Club'
		});
	},

	validateForm: function(){
		// Append error message wrapper
		$('#errors').append('<ol>');
		// Add US phone number validation logic
		jQuery.validator.addMethod('phone', function(phone_number, element) {
			phone_number = phone_number.replace(/\s+/g, "");
			return this.optional(element) || phone_number.length > 9 &&
				phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, 'Please specify a valid phone number');
		// Install validator
		var validator = $('form').validate({
			rules: {
				'name': 'required',
				'email': {
					'required': true,
					'email': true
				},
				'phone': {
					'required': true,
					'phone': true
				},
				'membership': 'required',
				'spam-filter': {
					'required': true,
					'remote': {
						'url': 'verify.php',
						'type': 'post'
					}
				}
			},
			messages: {
				'name': 'Please enter your full name.',
				'email': {
					'required': 'Please enter a valid e-mail address.',
					'email': 'Please enter a valid e-mail address.'
				},
				'phone': 'Please enter a valid US phone number.',
				'membership': 'Please select your membership status.',
				'spam-filter': 'Please answer the Spam Filter correctly.'
			},
			errorContainer: $('#errors'),
			errorLabelContainer: $('ol', $('#errors')),
			wrapper: 'li',
			onkeyup: false,
			onfocusout: false,
			submitHandler: function(form) {
				$.post('submit.php', $('form').serialize());
				$('form').hide();
				$('#success').fadeIn();
			}
		});


	},

	retrieveVideo: function(data){
		var feed = data.feed;
		var entries = feed.entry || [];
		/*
		// Lets Flash from another domain call JavaScript
		var params = { allowScriptAccess: 'always' };
		// The element id of the Flash embed
		var atts = { id: 'myytplayer', wmode: 'transparent' };
		// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
		swfobject.embedSWF('http://www.youtube.com/v/' + entries[0].media$group.yt$videoid.$t + '&enablejsapi=1&playerapiid=ytPlayer', 'ytapiplayer', '230', '155', '8', null, null, params, atts);

		*/
		$(function(){
			var thumb = '<a href="#" class="fancybox" title="'+entries[0].title.$t+'"><img src="http://img.youtube.com/vi/'+entries[0].media$group.yt$videoid.$t+'/0.jpg" alt="" /></a>';
			$('.latest-video')
				.html(thumb)
				.click(function(e){
					$.fancybox({
						'padding': 0,
						'autoScale': false,
						'transitionIn': 'none',
						'transitionOut': 'none',
						'width': 480,
						'height': 360,
						'href': 'http://www.youtube.com/v/' + entries[0].media$group.yt$videoid.$t + '&autoplay=1',
						'type': 'swf',
						'swf': {
							'wmode': 'transparent',
							'allowfullscreen': 'true'
						}
					});
					e.preventDefault();
				});
		});

	},

	init: function(){
		/* Apply site navigation effect */
		site.applyEasing();
		/* Start Homepage slideshow */
		if ($('.slides').length > 0) site.startSlideshow();
		/* Install carousel */
		if ($('.carousel').length > 0) $('.carousel').jcarousel();
		/* Image Lightbox */
		if ($('.lightbox, .gallery, .carousel').length > 0) site.installLightbox();
		/* Install Contact Us scripts */
		if ($('form').length > 0) site.validateForm();
		/* Install Contact Us scripts */
		if ($('body.contact-us').length > 0){
			/* Membership logic */
			$('#non-member').click(function(){ $('#goals').fadeIn(); });
			$('#member').click(function(){ $('#goals').fadeOut(); });
			/* Map gym location */
			if ($('#map').length > 0) site.mapGym();
		}

	}
};

// Initialize site
$(function(){site.init();});
