
function setupProbar() {

	var cookieName = 'probar';
	var cookieOptions = { expires: null, path: '/' };
	var collapsedState = { marginTop: "-93px" }; // , paddingBottom: "93px" }; 
	var collapsedClass = 'probar_collapsed';
	var openState = { marginTop: "0px", paddingBottom: "0px" }; 
	var $container = $("#content_container"); 

	if($.cookie(cookieName) == 'collapsed') $container.addClass("probar_collapsed").css(collapsedState); 

	$("#hidethis").click(function() {

		if(!$container.is('.' + collapsedClass)) {
			$.cookie(cookieName, 'collapsed', cookieOptions); 	
			$container.animate(collapsedState).addClass(collapsedClass);
		} else {
			$.cookie(cookieName, 'open', cookieOptions); 	
			$container.animate(openState).removeClass(collapsedClass); 
		}

		return false; 
	}); 
}

function setupVillaLists() {

	$(".villa_list_table li.villa_item").click(function() {
		var url = $(this).find("li.villa_title a").attr('href'); 
		window.location = url;
	}); 

	$(".featured_villa_list li").click(function() {
		var url = $(this).find("a.villa_title").attr('href'); 
		window.location = url;
	}); 
	
}

function setupVillaImages() {
	$("a.villa_image").hover(function() {
		var $div = $("<div class='villa_image_magnify'></div>"); 
		var $t = $(this); 
		var $img = $t.children("img"); 
		$div.css('width', ($img.width()-6) + "px"); 
		$div.css('height', ($img.height()-6) + "px"); 
		$t.append($div); 
	}, function() {
		$(this).children("div.villa_image_magnify").remove();
	}); 
}

function setupInquiryForm(cookieOptions) {

        var cookieOptions = { expires: 30, path: '/' };
	var $form = $("#inquiry_form"); 

	// setup the default values for any fields so that they show when the field is blank
	// but disappear when the user has populated the field

	$("input.default").focus(function() {
		var $t = $(this);
		if($t.val() == $t.data('default')) $t.val(''); 
		$t.addClass('focused'); 
		
	}).blur(function() {
		var $t = $(this);
		if($t.val().length < 1) $t.val($t.data('default')).removeClass('focused'); 
		
	}).each(function() {
		var $t = $(this);
		var $label = $form.find("label[for=" + $t.attr('id') + "]"); 
		var text = $label.text();
		$t.data('default', text); 
		$t.val(text); 
	}); 

	// make it show 'child' rather than 'children' when the number '1' is entered
	var updateChildren = function() {
		if($("#inquiry_children").val() == 1) $("#inquiry_children_label").text('child'); 
			else $("#inquiry_children_label").text('children'); 
	}; 
	$("#inquiry_children").change(updateChildren); 

	// setup the submit action to post via ajax and then store result in cookie
	// so that user doesn't have to enter everything again on subsequent requests

	var cookieName = 'villa_inquiry';
	var saved = $.cookie(cookieName);

	if(saved != null) {
		// repopulate saved info from cookie
		saved = $.json.decode(saved);
		$(":input[name!=villa_id]", $form).each(function() {
			var $t = $(this);
			var name = $t.attr('name');
			if(name == 'inquiry_type') return; 
			if(saved[name]) $t.val(saved[name]).addClass('focused'); 
		});
		updateChildren();
	}

	$form.submit(function() {
		// executed when user submits villa inquiry form

		var errors = '';
		var fields = { ajax: 1 };

		$(":input", $form).each(function() {

			var $t = $(this);
			var val = $.trim($t.val());
			var name = $t.attr('name');
			var label = name.replace(/[^a-zA-Z0-9]/, ' '); 
			var id = $t.attr('id'); 

			if(name == 'submit_inquiry') return; 
			if(val == $t.data('default')) val = '';

			fields[name] = val;

			if($t.is(".required")) {
				if(val.length < 1) {
					if(name == 'children') errors += "Please enter # of children traveling or 0 if none\n";
						else if(name == 'date_from' || name == 'date_to') errors += "Please complete both travel dates (if you don't know your travel dates, please approximate them).\n";
						else errors += "Please enter a value for " + label + "\n";
				} else if(name == 'email' && (val.indexOf('@') < 1 || val.indexOf('.') < 1)) {
					errors += "Please enter a valid email address\n";
				} else if((name == 'adults' || name == 'children') && val.length > 0) {
					if(!/^\d+$/.test(val)) errors += "Please enter only digits for " + label + "\n";	
				}
			} 
			if((name == 'date_from' || name == 'date_to') && val.length > 0) {
				if(!/^\s*\d{1,2}\s*[-\/.]\s*\d{1,2}\s*[-\/.]\s*\d{2,4}\s*$/.test(val) && !/^\w+\s*\d{1,2}\s*[, ]+\d\d\d\d\s*$/.test(val)) {
					errors += "Please enter dates like: 'mm/dd/yy' or 'Month dd, yyyy'";
				}
			}
		});

		if(errors.length) {
			alert(errors); 
			return false;
		}

		$form.slideUp("normal", function() {
			$.post($form.attr('action'), fields, function(data) {
				$form.html(data);
				$form.slideDown("normal");
				fields.ajax = undefined; 	
			}, 'html');
		});

		$.cookie(cookieName, $.json.encode(fields), cookieOptions);

		return false;
	});

	// everything below this doesn't play well with IE7 and earlier, so exit
	if($.browser.msie && $.browser.version < 8) return;

	// setup the optional inquiry comments field
	$("#inquiry_comments_wrap").hide();
	var inquiryComments = $("#inquiry_add_comments").click(function() {
		$i = $("#inquiry_comments_wrap"); 
		$t = $(this);
		if($i.is(":visible")) {
			$i.slideUp();
			$t.text($t.text().replace(/hide /, 'add ')); 
		} else { 
			$i.slideDown(); 
			$t.text($t.text().replace(/add /, 'hide ')); 
		}
		return false; 
	}).val();

	if($("#inquiry_comments").size() > 0 && $("#inquiry_comments").val().length > 0) $("#inquiry_add_comments").click();
}


$(document).ready(function() {

	$("#select_villa").change(function() {
		var url = $(this).parents("form").attr('action') + $(this).val() + '/';
		window.location = url; 
	}); 

	$("#select_location").change(function() {
		var url = $(this).parents("form").attr('action') + $(this).val() + '/';
		window.location = url; 
	}); 

	$(".villa_list_sort_select").change(function() {
		$(this).parent("form").submit();
	}); 

	/*
	$("#showgrid").click(function() {
		$("#grid").toggle();
		return false; 
	}); 
	*/

	/*
	var rsidebarHeight = $("#rsidebar").height()+350;
	var containerHeight = $("#content_container").height();
	var contentHeight = $("#content").height();

	if(rsidebarHeight > containerHeight) {
		$("#content").height(rsidebarHeight / 2); 
	}
	*/
	

	setupVillaLists();
	setupVillaImages();


}); 

var addthis_config = {
	ui_cobrand: "Island Hideaways",
	ui_offset_top: -46, 
	ui_header_color: "#ffffff", 
	ui_header_background: "#4474bc"
}


