/**
 * @author matt
 */

var border = RUZEE.ShadedBorder.create({ corner:8, shadow:0,  border:0 });

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

$(document).ready(function() {
	// set the #content min-height so the sidebar is contained
  var minheight = $('#sidebar').height();
  $('#content').css('min-height',minheight + 310);
  
  var defaultArrival = $('#bookingArrival').val();
	
	$('#navigation li.active a, #navigation li.activeParent a').wrapInner("<span></span>");
	
	$('h1').wrapInner("<span></span>");
	
	$('.images-control li a').click(function() {
		$('#main').css('background-image', 'url(/site/images/photo_main'+ $(this).attr('href') +'.jpg)');
		$(this).parent().siblings().removeClass('current');
		$(this).parent().addClass('current');
		
		return false;
	});
	$('.images-control li a:first').click();
	
	$('.unit-gallery') 
    .after('<div class="links-container"><ul id="images-links"></ul></div>') 
    .cycle({ 
        fx:     'fade', 
        prev:   '#prev', 
        next:   '#next', 
        speed:  1000, 
        timeout: 0, 
        pager:  '#images-links'
    });
	
	/* if only one image, the cycle won't start. Show it manually and disable the image controls */
	if ($('body#unit-page .unit-gallery').children().length <= 1 ) {
		$('#images-links').hide();
		$('.unit-gallery').children('img').show();
	}
	
	$('ul#images-links').prepend("<li class=\"images-text\"><p>Images</p></li>");
	//$('ul#images-links').prepend("<li class=\"images-text\"><a href=\"\">Images</a></li>");
	
	$('a[rel=new]').click(function() {
		window.open($(this).attr('href'));
        return false;
	});
	
	border.render('content');
	$('.png-fix').ifixpng();
	
	$('#bookingArrival').datepicker({dateFormat: 'MM/dd/yy', mandatory: true, showAnim: 'fadeIn', speed: 'medium'}); 
	
	// handle booking form submission
	$('#bookingFormFull, #bookingFormSmall').submit(function() {
		var fdate = $('#bookingArrival').val().split("/");
		
		// Check for a valid arrival date
		if ((!isNumeric(fdate[1]) || fdate[1].length > 2 || fdate[1] > 31) 
			|| (!isNumeric(fdate[2]) || fdate[2].length> 4 || fdate[2] > 2018))
		{
			alert("Please enter a valid Arrival Date into the booking form");
			$('#bookingArrival').focus();
			return false;
		}
		
		$('#hiddenMonth').val(fdate[0]);
		$('#hiddenDay').val(fdate[1]);
		$('#hiddenYear').val(fdate[2]);
		
		$('#hiddenAdults').val($('#bookingGuests').val());
		
		if ($('#bookingClientCode').val().toUpperCase() == "REP")
			$('#hiddenRate').val("05");
	});
	
	$('#bookingArrival').focus(function() {
		if ($(this).val() == defaultArrival)
			$(this).val("");
	}).blur(function() {
		if ($(this).val() == "")
			$(this).val(defaultArrival);
	});
	
	// Accommodation page property details switcher
    /*$('.accommodation-property').mouseover(function() {        
        $(this).parent().parent().next('.accommodation-details')
            .html($(this).html())
            .prepend("<h3>"+ $(this).find('a:first').html() +"</h3>")
            .append("<p><a href=\""+ $(this).find('a:first').attr('href') +"\">Read more and book now!</a></p>")
            .find('a:first').remove();
        $(this).siblings().removeClass("active").end().addClass("active");
    }).mouseout(function() {
	});*/
	
	$('.accommodation-property').bind("mouseenter", function() {        
        $(this).parent().parent().next('.accommodation-details')
            .html($(this).html())
            .prepend("<h3>"+ $(this).find('a:first').html() +"</h3>")
            .append("<p><a href=\""+ $(this).find('a:first').attr('href') +"\">Read more and book now!</a></p>")
            .find('a:first').remove();
        $(this).siblings().removeClass("active").end().addClass("active");
    });
    
    // Load the first property details
	$('.accommodation-main > div').each(function() {
		$(this).children(':first')
				.addClass("first")
			.siblings(':last')
				.addClass('last');
	});
    $('.accommodation-property.first').trigger('mouseenter');
	
	// Add CSS classes to the details boxes for the unit pages left bar
	$('#sidebar #details table:eq(1) tbody tr').each(function() {
		$(this).children(':first').addClass("heading");
	});
	$('#sidebar #details table tbody > tr:last').addClass("last");
});