$(document).ready(function() {
	$('#gwb_login_submit').click(function () {
		$('#gwb_login_form').submit();
	});
	$('#masthead_slideshow').cycle({
		fx: 'fade',
		speed: 350,
		next: '#masthead_slideshow',
		pause: 1,
		timeout: 3500
	});
	$("#gallery").jcarousel({
		'scroll' : 6
	});
	if ($('#hot-deals').length > 0) {
		$('#hot-deals').css('display', 'block');
		$('#hot-deals').cycle({ 
			fx: 		 'scrollLeft',
	    	speed:       200, 
	    	timeout:     3500, 
	    	pager:      '#hot-deals-pager', 
	   		pagerEvent: 'mouseover', 
	    	pauseOnPagerHover: true  
		});
		$('#hot-deals-pager').css('z-index', '20');
		$('#hot-deals').hover (
			function() { 
				$('#hot-deals').cycle('pause');
			}, 
			function() {
				$('#hot-deals').cycle('resume');
			}
		);
	}
	$("#ratefinder_datepicker").datepicker({ 
		dateFormat: 'D d M yy',
		showOn: 'both',
		buttonImage: '/assets/img/calendar-icon.gif',
		buttonImageOnly: true,
		minDate: '-0d',
		maxDate: '+365d',
		altField: '#ratefinder_arrival_date',
		altFormat: 'yy-mm-dd',
		onSelect: function() {
		loadRates($("#ratefinder_datepicker").datepicker("getDate"));
		_gaq.push(['_trackEvent', 'Properties', 'Select Check In Date']);
		}
	});
	$("#rates_prev_button").click(function() {
		var prev7Date = new Date(currentDate.getTime() - (1000 * 60 * 60 * 24 * 7));
		loadRates(prev7Date);
		_gaq.push(['_trackEvent', 'Properties', 'Button Click - Week <']);
		return false;
	});
	$("#rates_next_button").click(function() {
		var next7Date = new Date(currentDate.getTime() + (1000 * 60 * 60 * 24 * 7));
		loadRates(next7Date);
		_gaq.push(['_trackEvent', 'Properties', 'Button Click - Week >']);
		return false;		
	});
	$("#property_location_map_link").fancybox({
		'overlayShow'	: false,
		'hideOnContentClick'	: false,
		'onComplete' : function() {
		resizegmap();
		_gaq.push(['_trackEvent', 'Properties', 'Show Map from Top Link']);
		}
	});
	$("#small_map_link").fancybox({
		'overlayShow'	: false,
		'hideOnContentClick'	: false,
		'onComplete' : function() {
		resizegmap();
		_gaq.push(['_trackEvent', 'Properties', 'Show Map from Thumbnail']);
		}
	});
	$("#incentive_details_link").fancybox({
		'overlayShow'	: true,
		'hideOnContentClick'	: false,
		'onComplete' : function() {
		_gaq.push(['_trackEvent', 'Properties', 'Show Booking Incentive Details']);
		}
	});
	$("#gwb-properties li:first-child").addClass("selected");
	$("#gwb-properties li:first-child").children(".select-lodge").css('display', 'block');
	$("#gwb-properties li").click(function() {
	  	$(this).addClass("selected").siblings().removeClass("selected");
	  	$(this).addClass("selected").siblings().children(".select-lodge").css('display', 'none');
	  	$(this).children(".select-lodge").css('display', 'block');
	});
	$("#gwb-datepicker").datepicker({ 
		dateFormat: 'DD d M yy',
		minDate: '-0d',
		maxDate: '+365d',
		altField: '#arrivalDate',
		altFormat: 'yy-mm-dd'
	});
	$("#gwb-book-now").click(function() {			  
		var propertyId = $(".selected > .propertyId").attr("value");
		var propertyURI = propertyURI = 'https://www.thebookingbutton.com.au/properties/' + propertyId;
		$("#gwb-booking-form").attr("action", propertyURI);
		$("#gwb-booking-form").submit();
	});
	$(".rt-thumb", "#room_types_container").fancybox({
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	$(".roomtype-toggle", "#room_types_container").click(function() {
		var ref = $(this).attr("href");
		$(ref).toggle();
		_gaq.push(['_trackEvent', 'Properties', 'Toggle Room Type Details']);
		return false;
	});
	$(".galleryimg_zoomable", "#gallery").fancybox({
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	$("a.trackable", "#content").click(function() {
		var trackableUrl = $(this).attr('href');
		_gaq.push(['_link', trackableUrl]);
		return false;
	});
});
function gmap_createSLMarker(gmap, markerTitle, markerLatLong, infoWindowContent) {
	var markerLatLongArray = markerLatLong.split(", ");	
	var markerPosition = new google.maps.LatLng(markerLatLongArray[0],markerLatLongArray[1]);
	var contentString = infoWindowContent;

	var infowindow = new google.maps.InfoWindow({
	content: contentString
	});

	var image = new google.maps.MarkerImage(
	  '/assets/img/gmap/image.png',
	  new google.maps.Size(52,70),
	  new google.maps.Point(0,0),
	  new google.maps.Point(26,70)
	);

	var shadow = new google.maps.MarkerImage(
	  '/assets/img/gmap/shadow.png',
	  new google.maps.Size(90,70),
	  new google.maps.Point(0,0),
	  new google.maps.Point(26,70)
	);

	var shape = {
	  coord: [32,1,35,2,38,3,40,4,40,5,42,6,43,7,44,8,45,9,45,10,47,11,47,12,48,13,48,14,48,15,49,16,49,17,49,18,50,19,50,20,50,21,50,22,50,23,50,24,50,25,50,26,50,27,50,28,50,29,50,30,50,31,50,32,50,33,49,34,49,35,48,36,48,37,47,38,47,39,46,40,46,41,45,42,44,43,43,44,42,45,41,46,39,47,38,48,36,49,34,50,34,51,33,52,32,53,31,54,30,55,30,56,29,57,29,58,28,59,28,60,27,61,27,62,27,63,27,64,27,65,27,66,26,67,26,68,25,68,24,67,24,66,24,65,24,64,24,63,24,62,23,61,23,60,23,59,22,58,22,57,21,56,21,55,20,54,19,53,18,52,18,51,16,50,15,49,14,48,12,47,10,46,9,45,8,44,7,43,6,42,5,41,5,40,4,39,4,38,3,37,2,36,2,35,2,34,1,33,1,32,1,31,1,30,1,29,1,28,1,27,1,26,1,25,1,24,1,23,1,22,1,21,1,20,1,19,1,18,2,17,2,16,2,15,3,14,3,13,4,12,5,11,5,10,6,9,7,8,8,7,9,6,10,5,12,4,13,3,16,2,18,1,32,1],
	  type: 'poly'
	};

	var marker = new google.maps.Marker({
		position: markerPosition,
		icon: image,
		map: gmap,
		shadow: shadow,
		shape: shape,
		title: markerTitle
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(gmap,marker);
	});
}
var ratefinderProperty;
var roomTypes;
var checkInDate;
var currentDate;
var reservationsURI;

function initRoomTypeRates(channelCode, roomTypeIds, date, url) {
	ratefinderProperty = channelCode;
	roomTypes = roomTypeIds;
    checkInDate = date;
	currentDate = date;
	reservationsURI = url + '/' + ratefinderProperty;
	loadRates(currentDate);
}
function loadRates(startDate) {
	if (startDate) {
		ajaxRequestUrl = "/rates/" + ratefinderProperty + "/" + dateToString(startDate) + "/";
	} else {
		ajaxRequestUrl = "/rates/" + ratefinderProperty + "/";
	}
	  var jqxhr = $.get(ajaxRequestUrl, function(data) {
		$("#rates_data").html(data);
	  })	
	  .success(function() {
	
		$("#rates_header").empty();
	  	$('#rates_header').html($('#rates_datesdata'));
	    
		for (i=0;i<roomTypes.length;i++)
		{
			$('#' + roomTypes[i] + '_rates').empty();
			$('#' + roomTypes[i] + '_rates').html($('#' + roomTypes[i] + '_ratesdata').html());
		
			if (startDate) {
				function updateLink() {
					var booknowLink = $('#' + roomTypes[i] + '_booknow').attr('href');
					booknowLink = reservationsURI + '/' + roomTypes[i] + '?start_date=' + dateToString(startDate);
					$('#' + roomTypes[i] + '_booknow').attr('href', booknowLink);
			    }
				updateLink();
			}
		}

	})
	  .error(function() { 
		//alert("error");
		})
	  .complete(function() {
		currentDate = startDate;
		$("#ratefinder_datepicker").datepicker("setDate", currentDate);
		});
		
		$("#rates_data").empty();
	  jqxhr.complete(function(){ 
		//alert("second complete");
		});
}
function dateToString(date) {
    var tempDate = date;
    var dateStr = padStr(tempDate.getFullYear()) + '-' +
                  padStr(1 + tempDate.getMonth()) + '-' +
                  padStr(tempDate.getDate());
	function padStr(i) {
	    return (i < 10) ? "0" + i : "" + i;
	}
	return dateStr;
}
function bindTracking(link) {
	var trackableUrl = $(link).attr('href');
	_gaq.push(['_link', trackableUrl]);
	return false;
}
