$(function() {
	$.fn.fancybox.defaults.padding = 20;

	$('#header .client form').validate();

	$('#header .contact a, #footer .contact a').fancybox({
		onComplete: function() {
			var $form = $('#contact form');

			$form.validate({
				submitHandler: function(form) {
					$.ajax({
						url: '/clients/rosetours/etc/contact.php',
						type: 'POST',
						data: {
							'name': $form.find('[name="name"]').val(),
							'address': $form.find('[name="address"]').val(),
							'city': $form.find('[name="city"]').val(),
							'state': $form.find('[name="state"]').val(),
							'zip': $form.find('[name="zip"]').val(),
							'phone': $form.find('[name="phone"]').val(),
							'email': $form.find('[name="email"]').val(),
							'trip': $form.find('[name="trip"]').val(),
							'comments': $form.find('[name="comments"]').val(),

							'to': $form.find('[name="to"]').val(),
							'subject': $form.find('[name="subject"]').val()
						},
						success: function() {
							$.fancybox('Your message has been sent successfully.');
						}
					});
				}
			});
		}
	});

	// Adding red swirls on divs
	$('#page .section').append('<div class="top-left"></div><div class="top-right"></div>');

	var slideshow = {
		$slideshow: $('#billboard'),
		url: '/api/json/features/SITE_SLIDESHOW',

		_init: function() {
			var self = slideshow;

			if (self.$slideshow.length) {
				self.get_slide_data(self.build_slideshow);
			}

		},

		get_slide_data: function(callback) {
			var self = slideshow;

			$.ajax({
				url: self.url,
				dataType: 'json',
				success: callback
			});
		},

		build_slideshow: function(data) {
			var self = slideshow,
			total = data.length;

			$.each(data, function(i, slide) {
				
				var $slide = $('<div class="section slide_' + (i + 1) + '" />'),
					$header = $('<div class="header" />')
						.append('<h3 class="title" />')
						.find('.title')
							.text(slide.title || '')
							.end(),
					$content = $('<div class="content" />');

				$slide.append($header, $content);

				if (slide.source.match(/(flv|mp4|m4v)$/i)) {
					// Video
					var $video = $('<div id="video_player-' + i + '">').player(slide.source, {
						mode: 'flash'
					});

					$content
					.append($video);
				} else if (slide.source.match(/(jpe?g|png|gif)$/i)) {
					// Image
					$content
					.append('<img src="' + (slide.source || '') + '" alt="' + (slide.title || '') + '"/>');

					if (slide.url != '' && slide.url != null) {
						$content.wrapInner('<a href="' + slide.url + '" />');
					}

				} else {
					// Custom
					$slide.find('.content').text('custom');
				}

				self.$slideshow.append($slide);
		});
		
		if (total > 1) { self.start_slideshow(); }
	},

	start_slideshow: function() {
		$(window).load(function() {
			slideshow.$slideshow.slider({
				transition: 'fade',
				auto: true,
				interval: 5000,
				navigation : false
			});
		});
	}
};
	
$(slideshow._init);

});
