$.datepick.setDefaults({
   	showOn: 'focus',
	firstDay: 6,
	dateFormat: 'dd/mm/yy',
	minDate: '-1y',
	maxDate: '+2y',
	closeAtTop: false,
	changeMonth: false, changeYear: false
});

// Check that the page has loaded
$(document).ready(function() {

	// LODGE APARTMET TABS

			// Start of showing only 4 bed apt
			$('#lodgeApartments .lodgeApartment').hide();
			$('#lodgeApartments .lodge_4').addClass('selected').show();

			// Show correct tab on click
			$('#lodgeApartments .apartmentTabs LI A').click( function(){

		    	// Remove all selected classes
				$('#lodgeApartments .selected').removeClass('selected');

				// Hide all apartments
				$('#lodgeApartments .lodgeApartment').hide();

				// Show and select correct tab
				var apartment = $(this).parent().attr('class');

				$('#lodgeApartments .'+ apartment +'').addClass('selected').show();

				return false;

			});


	// POPUP TABS

		// Start of showing only selected tab
		$('.popup .tabbedContent:not(.selected)').hide();

		// Show correct tab on click
		$('.popup .popupTabs LI A').click( function(){

	    	// Remove all selected classes
			$('.popup .selected').removeClass('selected');

			// Hide all tabbed content
			$('.popup .tabbedContent').hide();

			// Show and select correct tab
			var tab = $(this).parent().attr('class');

			$('.popup .'+ tab +'').addClass('selected').show();

			return false;

		});


	// FAQ

		// start off hidden
		$('.faq .answer').hide();

		// Open/close correct FAQ
		$('.faq .question').click( function(){

            // If open, close and remove 'open' class, If not open, open and add 'open' class
			if( $(this).parent().hasClass('open') ){
	        	$(this).parent().removeClass('open').find('.answer').slideUp();
			} else {
                // Close all others
				$('.faq .answer').slideUp();
				$('.faq').removeClass('open');

				$(this).parent().addClass('open').find('.answer').slideDown();
			}

			return false; 
		});


	// POPUPS

		// transfersArrival popup
		$('A.transfersArrival').click( function(){

			$('#transfersArrivalPopup').fadeIn();

			// set in window pane (adjust for scroll)
			$('#transfersArrivalPopup .popupContainer').css('top', $(window).scrollTop() );

			return false;

		});

		// Open ski lifts window
		$('#transfersArrivalPopup .flying .button INPUT').click( function(){

			window.open('http://www.ski-lifts.com/ridersretreat');

		});


		// payNow popup
		$('A.payNow').click( function(){

			$('#makePaymentPopup').fadeIn();

			$('#makePaymentPopup .popupContainer').css('top', $(window).scrollTop() );

			return false;

		});

		// addPayment popup
		$('A.addPayment').click( function(){

			var id = $(this).attr('href');

			$('#addPaymentPopup').fadeIn( function(){
	        	$(this).find('#key_id').attr('value', id);
			});

			// set in window pane (adjust for scroll)
			$('#addPaymentPopup .popupContainer').css('top', $(window).scrollTop() );

			return false;

		});

		// full height
		$('.popup').height($(document).height());

		// close
		$('.popup .close').click( function(){

			$(this).parents('.popup').fadeOut();

			return false;

		});


	// GALLERY

		// Add lightbox
	    $('A.lightbox').lightBox();
		$('A.lightbox1').lightBox();
		$('A.lightbox2').lightBox();
		$('A.lightbox3').lightBox();
		$('A.lightbox4').lightBox();
		$('A.lightbox5').lightBox();

		// First selected
		$('.gallery').each(function(){

			$(this).find('.thumbnail:first').addClass('selected');

		});

	    // Functionality
	    $('.gallery .thumbnail').click(function() {

			// Control seletect class
			$(this).parent('.thumbnails').find('.thumbnail').removeClass('selected');
			$(this).addClass('selected');

	        // Get which thumnail clicked
	        var href = $(this).attr('href');

	        // Move correct image to top of stack
			$(this).parents('.gallery').find('.mainImgContainer').prepend($(this).parents('.gallery').find('.mainImgContainer A IMG[src*="' + href + '"]').parents('A'));

	        return false

	    });

		// Hover class for mainImg
		$('.gallery .mainImgContainer A').hover(
	    	function(){
	        	$(this).find('.overlay').css('cursor', 'pointer').show();
	    	},
			function(){
	        	$(this).find('.overlay').hide();
	    	}
		);


	// CLEAR/CONTROL FORM FIELDS
		$('.textBox INPUT, .textArea TEXTAREA').focus(function() {

			// Get title(original value) and current value
			var title = $(this).attr('title');
			var value = $(this).val();

	        // Clear if original value only
			if( title==value ){
				$(this).val("");
			}

		});
		$('.textBox INPUT, .textArea TEXTAREA').blur(function() {

			// Get title(original value) and current value
			var title = $(this).attr('title');
			var value = $(this).val();

	        // If empty, re-populate
	        if( value=="" ){
				$(this).val(title);
			}

	   });
	   $('FORM .button INPUT').click(function() {

			$('.textBox INPUT, .textArea TEXTAREA').each(function() {

				// Get title(original value) and current value
				var title = $(this).attr('title');
				var value = $(this).val();

		        // Clear if original value only
				if( title==value ){
					$(this).val("");
				}

			});

		});


	// BIG TARGET
		//Chalet List
		$(".chaletList .chalet A").bigTarget({
		    hoverClass: 'hover', // CSS class applied to the click zone onHover
		    clickZone : '.chalet' // jQuery parent selector
		  });
		$(".chaletList .chalet").css('cursor', 'pointer');


	// DATEPICKER
	    $('.datePick INPUT').datepick();

		//DateRange (arr/dep)
		$('#startDate, #endDate').datepick({beforeShow: customRange});

		function customRange(input) {
		    return {minDate: (input.id == 'endDate' ?
		        $('#startDate').datepick('getDate') : null),
		        maxDate: (input.id == 'startDate' ?
		        $('#endDate').datepick('getDate') : null)};
		}

		//DOB
		$('.dob INPUT').datepick({minDate: '-100y', maxDate: '+1y', yearRange: '1900:2100', changeMonth: true, changeYear: true});


	// CUFON
		Cufon.replace('H1, H2, H3, H4');


	// TWITTER
		$("#twitter").getTwitter({
			userName: "ridersretreat",
			numTweets: 1,
			slideIn: false,
			showHeading: false
		});


	// IE6 (temp)
		if( $('BODY').hasClass('.browserIE6') ){

	    	$('HEAD').prepend('<link rel="stylesheet" href="css/ie6.css" type="text/css" media="screen"  />');

		}

});



