/*
 * Author: Mike Malusa (mikemalusa.com) @ Bullfrog Communications (ebullfrog.com)
 */

$(document).ready(function() {	
		
	// Slideshow
	if ($('ul#gallery').length ) {
		$('ul#gallery').cycle({
			fx: 'fade'
		});
	}
	
	
	if ($('#contactForm').length ) {
		$("form#contactForm .extraField").hide();
		$("#cf_party").click(function(){
			if ($("#cf_party").is(":checked"))
				$("form#contactForm .extraField").slideDown('slow');
			else
				$("form#contactForm .extraField").slideUp('slow');
		});
		
		$("#contactForm").validate({
			 submitHandler: function(form) {
				var action = $(form).attr('action');
				$('#submit')
					.after('<img src="img/ajax-loader.gif" class="loader" />')
					.attr('disabled','disabled');
				
				$.post(action, { 
						name: $('#cf_name').val(),
						email: $('#cf_email').val(),
						pnumber: $('#cf_number').val(),
						message: $('#cf_message').val(),
						party: $('#cf_party:checked').val(),
						pdate: $('#cf_date').val(),
						people: $('#cf_people').val()
					},
					function(data){
						$('#contactForm #submit').attr('disabled','');
						$('#cf_response').remove();
						$('#contactForm').after('<div id="cf_response" class="response"><p>'+data+'</p></div>');
						$('#cf_response').slideDown();
						$('#contactForm img.loader').fadeOut(500,function(){$(this).remove()});
						if(data=='Your message has been sent.') $('#contactForm').slideUp(800);
					}
				);
				return false;
			}
		});
	}
	if ($('#mailingList').length ) {
		$("#mailingList").validate({
			 submitHandler: function(form) {
				var action = $(form).attr('action');
				$('#ml_submit')
					.after('<img src="images/ajax-loader.gif" class="loader" />')
					.attr('disabled','disabled');
				
				$.post(action, { 
						email: $('#ml_email').val()
					},
					function(data){
						$('#mailingList #ml_submit').attr('disabled','');
						$('#ml_response').remove();
						$('#mailingList').after('<div id="ml_response" class="response"><p>'+data+'</p></div>');
						$('#ml_response').slideDown();
						$('#mailingList img.loader').fadeOut(500,function(){$(this).remove()});
						if(data=='Thank you for signing up.') $('#mailingList').slideUp(800);
					}
				);
				return false;
			}
		});
	}
	
	//Photos Page
	if ($('#thumbs').length ) {
		$('#main').append('<div id="loader"></div>');
		$('#photos li:first-child').attr('id', 'p0');
		$('#thumbs a').click(function(event){
			event.preventDefault();
			var indexNum = $(this).parent().index();
			loadPhoto($(this).attr('href'), indexNum);
		});
		function loadPhoto (path, indexNum) {
			$('#photos li.active').stop(false,true);
			if ($('#p'+indexNum).length)  {
				if (!($('#p'+indexNum).hasClass('active'))) {
					$('#photos li.active').addClass('prev').removeClass('active');
					$('#p'+indexNum).hide().addClass('active').fadeIn(800, function() { $('#photos li.prev').removeClass('prev'); });
				}
			} else {
				$('#loader').fadeIn(100);
				var photo = $('<li id="p'+indexNum+'"></li>');
				$('#photos').append(photo);
				var curr = $("#photos li#p"+indexNum);
				var img = new Image();
				$(img).load(function () {
					$(this).css('display','none');
					$(curr).append(this);
					$('#loader').fadeOut();
					$('#photos li.active').addClass('prev').removeClass('active');
					$(this).addClass('active').fadeIn(800);
				}).error(function () {
					$(curr).remove();
				}).attr('src', path);
			}
		}
	}
	
	// Get IE version
	function msieversion() {
		var ua = window.navigator.userAgent;
		var msie = ua.indexOf ( "MSIE " );
		if ( msie > 0 ) {
			return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
		}
	}
	var ieVer = msieversion();
	// Add before and after divs for ie<=8
	if (ieVer <= 8) {
		//$('body').append('<div class="topStripe"></div><div class="bottomW"></div>');
		$('body').append('<div class="bottomW"></div>');
	}
	if (ieVer <= 7) {
		$('#mainNav ul').prepend('<div class="navBarDB navBarDBL"></div>');
		$('#mainNav ul').append('<div class="navBarDB navBarDBR"></div>');	
	}
	

});
