/* Author: Dave Folan -  @BigLittleFlan */

//Move to the top of the page skipping out the nav
window.scrollTo(0,0);

///Script to fix orientation zoom bug. As discussed here: webdesignerwall.com/tutorials/iphone-safari-viewport-scaling-bug 
(function(doc) {

	var addEvent = 'addEventListener',
	    type = 'gesturestart',
	    qsa = 'querySelectorAll',
	    scales = [1, 1],
	    meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];

	function fix() {
		meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
		doc.removeEventListener(type, fix, true);
	}

	if ((meta = meta[meta.length - 1]) && addEvent in doc) {
		fix();
		scales = [.25, 1.6];
		doc[addEvent](type, fix, true);
	}

}(document));

//Arbitatry width set in the CSS roughly iphone 4S landscape width
var mobileWidth = 480;

$(document).ready(function(){
	/*=======================================
	 * Enable the dropdown nav
	 * If you select something from the nav go there...that's navigation 101
	 =======================================*/
	$('select.navigation').change(function() {
		window.location = $(this).val();
	});

	/*=======================================
	 * Trigger the open close for hotels
	 =======================================*/
	$('article.hotel header h3').click(function(){
		//Check that they are viewing on a mobile device
		if (viewingMobileLayout()) {
			//Is the element already open?
			if ($(this).parents('article.hotel').hasClass('active')) {
				//If it is slide it up and hide it.
				$('article.hotel.active .body').slideUp(function(){
					$(this).attr('style','');
				});
				$('article.hotel.active').removeClass('active');				
			} else {
				//If it's not close any other and show this one
				$('article.hotel.active .body').slideUp(function(){
					$(this).attr('style','');
				});
				$('article.hotel.active').removeClass('active');

				$(this).parents('article.hotel').addClass('active');
				$('article.hotel.active .body').slideDown();
			}
		}
	});

	/*=======================================
	 * Carousel slider gallery fun
	 =======================================*/
	if ($('.slider').length > 0) {
		$('.slider').flexslider({
			animation: "slide",
			controlNav: false
		});
	}
	if ($('.datepicker').length > 0) {
		var dates = $( '.datepicker' ).datepicker({
			defaultDate: "+1w",
			dateFormat: 'dd/mm/yy',
			onSelect: function( selectedDate ) {
				var option = this.id == "arrive" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});

	}

	/*=======================================
	 * Set phone links for mobile view
	 =======================================*/
	 if ($('.phone').length > 0) {
	 	togglePhoneAttr();
	 	$(window).resize(togglePhoneAttr);
	 }

});

//Check to see if the user is viewing the mobile layout
function viewingMobileLayout() {
	windowWidth = $(window).width();
	if (windowWidth <= mobileWidth) {
		return true;
	} else {
		return false;
	}
}

//If your in mobile layout take the data-phone attribute and update the href attribute
function togglePhoneAttr() {
	if (viewingMobileLayout()) { //If your viewing mobile layout add tel: to the href
		$('.phone').each(function(index){
			phoneNuber = $(this).attr('data-phone');
			$(this).attr('href', 'tel:'+phoneNuber);
			$(this).addClass('mobile-view');
		});
	} else { //If your not viewing the mobile layout set the href to be and arbitary value
		$('.phone').each(function(index){
			$(this).attr('href', '#phone');
			$(this).removeClass('mobile-view');
		});		 		
	}
}





