$(function() {

	
	function consolelog(item) {
		if (window.console && window.console.firebug) {
	  		console.log(item);
		} 	
	}

	$("ul.tabs").tabs("#venue-tabbedinfo div.panes > div");

	
	$("table.sm_calendar td").hover( function () {
		$(this).children("div.events_today").fadeIn("fast");
		}, 
		function () {
		$(this).children("div.events_today").fadeOut("fast");
		}
	);
	$("#page_container a[rel='gallery']").colorbox({
		slideshow:true,
		transition:'fade', 
		slideshowSpeed:5000,
		slideshowStart: "Start",
		slideshowStop: "Stop"
	});
	//
	//standardise heights of columns
	//
	function equalHeights(domcollection) {
		var maxHeight = 0;
		domcollection.each(function() {
			if( $(this).height() > maxHeight) {
				maxHeight = $(this).height();
			}
		});
		/*domcollection.each(function() {
			if(!$(this).is(':empty') ) {
				$(this).height(maxHeight);
			}
		}); */
		domcollection.height(maxHeight);
	}
	equalHeights($("ul.search_results > li"));
	equalHeights($("ul.venue_info > li:lt(3)"));
	equalHeights($("ul.venue_info > li:gt(2)"));
	equalHeights($("div#page_footer > div"));
	equalHeights($("ul.delegate-services div.box"));
	equalHeights($("#venue-at-a-glance, #venue-includes-this"));
	
	var mainH = $("div.venue_article").height()-307;
	var sideH = $("div.venue_aside").height();
	if(mainH > sideH){
		$("div.venue_aside").height(mainH-7);
	}
	else {
	//	doing this would be bad
	//	$("div.venue_article").height(sideH+7);
	}
	

	var weddingMainHeight = $("div.article").outerHeight();
	var weddingSideHeight = $("div.nav").height();
	if(weddingMainHeight > weddingSideHeight ){
		$("div.nav").height(weddingMainHeight-10);
	}
	
	// "did you know?" box
	if($("div.articlesearch").length > 0) {
		if( $("div.articlesearch").height() > 980 ){
			consolelog("main height: "+$("div.articlesearch").height());
			consolelog("side height: "+$("div.aside").height());
			var diff =  $("div.articlesearch").height() - $("div.aside").height();
			$("div.dyk_landing").height( $("div.dyk_landing").height() + ( $("div.articlesearch").height() - 980 ) );
		}
	}	
	
	
	if( $("form.quick_enquiry").length == 1 ) {
		var articleHeight = $("div.article").innerHeight();
		var imagesHeight = $("div.side_images").innerHeight();
		var asideHeight = $("div.side_images").innerHeight() + $("form.quick_enquiry").innerHeight();
		if( articleHeight > asideHeight) {
			$("form.quick_enquiry").height(articleHeight-495);
		}
	}
	
	//legends: gimme something we can use
	$("div#contour fieldset legend").each(function() {
		$(this).replaceWith("<p class='legend'>" + $(this).text() + "</p>");
	});
	
	// homepage cycle banners
	$('ul#home_banners').cycle({ 
		timeout:  7000, 
		speed:  500
	}); 

	//homepage - make list items clickable
	//homepage - make list items clickable
	$("ul.home_boxes li, ul#home_banners li").each(function() {
		if( $(this).find("a").length == 1 ) {
			$(this).css("cursor", "pointer");
		}
	});
	
	$("ul.home_boxes li, ul#home_banners li").click(function() {
		if( $(this).find("a").length == 1 ) {
			window.location = $(this).find("a").attr("href");
		}
	});
	
	
	// Venue A-z sorting
	$("#venue_list").tablesorter(); 
	
	// placeholder labels
	$("fieldset.site_search label").hide();
	//$("fieldset.site_search input#search").labelify(); 
	
	//search form - jump page when appropriate event type picked
	$("select#acc_eventtype").change(function() {
		var goHere = $(this).find("option:selected").val();
		if(goHere != '') window.location = $(this).find("option:selected").val();
	});
	
	//datepickers
	$('input.enqDateFrom').datepicker({
        constrainInput: true,
	dateFormat: 'dd-mm-yy',
	onSelect: function(dateText, inst) { $(".enqDateFrom_li input").val(dateText) }
     	});
	$('input.enqDateTo').datepicker({
        constrainInput: true,
	dateFormat: 'dd-mm-yy',
	onSelect: function(dateText, inst) { $(".enqDateTo_li input").val(dateText) }
     	});

	// enable hidden js back button
	$("a.button_back").show();

	$("div#map").hide();
	$('a.venue_map').toggle(function() {
		$("div#map").slideDown();
		$(this).html("Close <br/>location map");
	}, function() {
		$("div#map").slideUp();
		$(this).html("Open <br/> location map");
	});
	
	//	Enquiry form - send from xsl form to doc2form for node creation/mail sending
	function sendToDocForm(context) {
		$("div.Doc2Form li."+ context.attr("class").split(" ")[0] +"_li input, div.Doc2Form li."+ context.attr("class").split(" ")[0] +"_li textarea").val(context.val());	
	}
	// run doc2form function on each page load, and on blur from inputs
	$("div#enquiry_form input:text, div#enquiry_form textarea").each(function() {
 		sendToDocForm($(this));
	});
	$("div#enquiry_form input:text, div#enquiry_form textarea").blur(function() {
		sendToDocForm($(this)); 	
	});
	
	// "interested in these venues" checkbox list to textarea conversion
	function convertCheckBoxes() {
		var target = $("div.Doc2Form li.enqWhichFacilities_li textarea");
		target.val("");
		$("div#enquiry_form ul.checkbox_list li input:checked").each(function(){
			target.val(target.val()+$(this).val()+"\n");			
		});
	}
	//run on page load, and when checkbox a value is changed
	convertCheckBoxes();
	$("div#enquiry_form ul.checkbox_list li input").change(function() {
		convertCheckBoxes();
	});
	
	//hu
	function convertEnqType() {
		$("li.enqEnquiryType_li input").val($("select#enqEnquiryType option:selected").text());
	}
	convertEnqType();
	
	$("select#enqEnquiryType").change(function() {
		convertEnqType();	
	});


	function convertCountryCode() {
		$("li.enqCountry_li input").val($("select#enqCountry option:selected").text());
	}
	convertCountryCode();
	
	$("select#enqCountry").change(function() {
		convertCountryCode();	
	});
	
	
	function convertRoomLayout() {
		$("li.enqRoomLayout_li input").val($("select#enqRoomLayout option:selected").text());
	}
	convertRoomLayout();
	$("select#enqRoomLayout").change(function() {
		convertRoomLayout();	
	});
	
	
	function convertBreakouts() {
		$("li.enqBreakoutRooms_li input").val($("select#enqBreakoutRooms option:selected").text());
	}
	convertBreakouts();
	$("select#enqBreakoutRooms").change(function() {
		convertBreakouts();	
	});
	
	function convertReferral() {
		$("li.enqWhereDidYouHear_li input").val($("select#enqWhereDidYouHear option:selected").val());
	}
	$("select#enqWhereDidYouHear").change(function() {
		convertReferral();	
	});
	
	$("ul.search_results li[rel=endrow]").css("margin-right", "0px");
	
	
	//virtual tours
	
	// - set first item as active
	var $firstTour = $('.vt-nav a:first');
	$firstTour.addClass('active');
	
	$('#virtual-tour').flash({
		src: $firstTour.attr('href'),
		width: 495,
		height: 400
	});

	$('.vt-nav a').click(function() {
		$('.vt-nav a').removeClass('active');
		$(this).addClass('active');
		
		$('#virtual-tour').empty().flash({
			src: $(this).attr('href'),
			width: 495,
			height: 400
		});
		
		return false;
		
	});
	
	
	
	
});
