$(function () {
  // Tab Nav -------------------------------------------------------------------------------------------------------------------------------- 
	var tabContainers = $('div.tab-container');
	tabContainers.hide().filter(':first').show();

	$('ul.tabs a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('ul.tabs a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
	// Self-labeled form fields --------------------------------------------------------------------------------------------------------------------------------
	$('input[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));	
		}

		$(this).focus(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('').addClass('focused');	
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');	
			}
		});
	});
	
	// Form Validation - http://bassistance.de/jquery-plugins/jquery-plugin-validation/ ---------------------------------------------------------------
	$("#signup-form").validate({
 		rules: {
 			name: "required",
 			email_sign_up: {
 				required: true,
 				email: true
 			}
 		},
 		messages: {
 			name: "Please enter your name",
 			email_sign_up: "Please enter a valid email address"
 		}
 	});
	
	$("#guest-list-form").validate({
 		rules: {
 			name: "required",
 			email_guest_list: {
 				required: true,
 				email: true
 			},
 			confirm_email_guest_list: {
 				required: true,
 				email: true,
 				equalTo: "#email-guest-list"
 			},
 			num_guests: "required",
 			reservation_day: "required",
 			reservation_month: "required",
 			reservation_year: "required"
 		},
 		messages: {
 			name: "Please enter your name",
 			email_guest_list: "Please enter a valid email address",
 			confirm_email_guest_list: {
 				required: "Please enter a valid email address",
 				equalTo: "Please enter the same email as above"
 			},
 			num_guests: "Please enter the number of guests",
 			reservation_day: "Required",
 			reservation_month: "Required",
 			reservation_year: "Required"
 		}
 	});
  	
	$("#booking-form").validate({
    rules: {
    	name: "required",
    	email_booking: {
    		required: true,
    		email: true
    	},
    	confirm_email_booking: {
    		required: true,
    		email: true,
    		equalTo: "#email-booking"
    	},
    	mobile: "required",
    	num_guests: "required",
    	reservation_day: "required",
    	reservation_month: "required",
    	reservation_year: "required",
    	reservation_hour: "required",
    	reservation_minute: "required",
    	reservation_area: "required"
    },
    messages: {
    	name: "Please enter your name",
    	email_booking: "Please enter a valid email address",
    	confirm_email_booking: {
    		required: "Please enter a valid email address",
    		equalTo: "Please enter the same email as above"
    	},
    	mobile: "Please enter a valid mobile number",
    	num_guests: "Please enter the number of guests",
    	reservation_day: "Required",
    	reservation_month: "Required",
    	reservation_year: "Required",
    	reservation_hour: "Required",
    	reservation_minute: "Required",
    	reservation_area: "Required"
    }
  });
	
	// Expanding boxes --------------------------------------------------------------------------------------------------------------------------------
	// Find all columns whose .box-inner content is shorter than it"s .box-outer container and hide the plus-bar
  $(".box-outer").each(function() {
    if ($(this).height() > $(this).find(".box-inner").height() ) {
      $(this).siblings(".plus-bar").hide();
      $(this).height($(this).height() + 40);
      // alert($(this).attr("class"));
    }  
  });
  
  // Now, let"s activate the plus-bar functionality...
  // Attach click functions to the show and hide button divs
  $(".show").click(function() { 
    $(this).siblings(".box-outer").animate({height: $(this).prev().find(".box-inner").height()}, 400); 
    $(this).toggle().next().toggle();
    initHeight = $(this).prev(".box-outer").height();
  });

  $(".hide").click(function() { 
    $(this).siblings(".box-outer").animate({height: initHeight}, 400);
    $(this).toggle().prev().toggle();
  });
  
  // Slideshows - Reviews, Gallery & Flyers --------------------------------------------------------------------------------------------------------------------------------
  $('#reviews').cycle({ 
      fx:      'fade',
      timeout:  30000
  });
  
  $('#gallery-pics').cycle({ 
      fx:      'fade'
  });
  
  $('#flyers').cycle({ 
      fx:      'fade',
      timeout:  10000
  });
  
  // PlanB TV --------------------------------------------------------------------------------------------------------------------------------
  // This is for the jQuery media plugin to load the first youtube video
  $("a.media").media({
    preferMeta:    1,         				                            // true if markup metadata takes precedence over options object 
    autoplay:      0,       				                              // normalized cross-player setting 
    params:        { autoplay:"0", rel:"0", id:"ytapiplayer" },		// added to object element as param elements; added to embed element as attrs      	
    attrs:         { autoplay:"0", rel:"0", id:"ytapiplayer" },		// added to object and embed elements as attrs        			
    flashvars:     { autoplay:"0", rel:"0", id:"ytapiplayer" },   // added to flash content as flashvars param/attr 
    flashVersion:  "7"       				                              // required flash version 
  });
  
  // PlanB TV drop-down menu
  $("ul.menu a").live("click",function(){
    var $href = $(this).attr("href");
    $('#featured-video').empty().append('<a class="media {width:280, height:175, type:\'swf\'}" href="'+$href+'"></a>');
    $("a.media").media({
      params:        { autoplay:"1", rel:"0" },		// added to object element as param elements; added to embed element as attrs      	
      attrs:         { autoplay:"1", rel:"0" },		// added to object and embed elements as attrs        			
      flashvars:     { autoplay:"1", rel:"0" }
    });

    $("#planbtv-media ul.menu").slideToggle("fast");
    $("#planbtv-media .plus-bar").toggleClass("hide");

    return false;
  });
  
  $("#planbtv-media .plus-bar").click(function() {
    $("#planbtv-media ul.menu").slideToggle("fast");
    $(this).toggleClass("hide");
  });
  
  // AJAX loading for calendar ------------------------------------------------------------------------------------------------------------
  $('#calendar thead a').live('click', function() {
    $('#calendar').fadeOut().load($(this).attr('id')+' #calendar' , function(){
      $(this).fadeIn();
    });
    // now let's make sure the page doesn't reload
    return false;
  });
  
  // AJAX loading for listings ------------------------------------------------------------------------------------------------------------
  $("#calendar td a").live('click', function(){ 
    // capture the url to load from the href 
    var urlLoad = "/listing-item/";
    urlLoad += $(this).attr("id");
    
    // slide up the div to hide the swap 
    $("#listing-item").slideUp(function() { 
      // load the second template with AJAX 
      $("#listing-item").load(urlLoad, function() { 
        // once the load is complete, slide back down 
        $("#listing-item").slideDown(); 
      }); 
    }); 
    // just make sure the page doesn’t reload 
    return false; 
  });
  
  // DOM Window calls (replacement for Thickbox modals)
  $('.form-pop').openDOMWindow({ 
    eventType:'click', 
    height: 500, 
    width: 300,
    windowPadding: 0,
    windowBGColor: '#ec008c',
    borderColor: '#ec008c',
    borderSize: 10,
    loader: 1, 
    loaderImagePath:'images/loading.gif', 
    loaderHeight: 13, 
    loaderWidth: 208 
  });
  
  $('.contact-pop').openDOMWindow({ 
    eventType:'click', 
    height: 300, 
    width: 300,
    windowPadding: 0,
    windowBGColor: '#ec008c',
    borderColor: '#ec008c',
    borderSize: 10,
    loader: 1, 
    loaderImagePath:'images/loading.gif', 
    loaderHeight: 13, 
    loaderWidth: 208 
  });
  
  // Load links in new window without the use of target=_blank ----------------------------------------------------------------------------
  $('a.new-window').click(function(){
    window.open(this.href);
    return false;
  });
	
});