var bookBoxy;

function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec ) ) / Math.pow(10,dec) );
	return result;
}

function popHotelProfile(hotelHref){
	var ajax = {
            url: hotelHref, type: 'GET', dataType: 'html', cache: false, success: function(html) {
            html = jQuery(html);
            bookBoxy = new Boxy(html, {modal:true,
                                             title:'About the hotel',
                                             unloadOnHide: true,
                                             afterShow: function(){
                                             	$('#accordion').accordion({fillSpace: true, active: roomPriceShowFirst});
                                             	slideShow();
                                             	DD_roundies.addRule('.inputcontainer', '5px', true);
					 	$('.datePicker').datepicker({dateFormat: 'dd/mm/yy',buttonImage: '/images/date.png',showOn: 'both', buttonImageOnly: true, constrainInput: false, maxDate: '+1y', minDate: '+1d'});
                                             }});
            }
        };

        jQuery.ajax(ajax);
}

$(document).ready(function(){

	$('#coupon_input').hide();
	$('.datePicker').datepicker({dateFormat: 'dd/mm/yy',buttonImage: '/images/date.png',showOn: 'both', buttonImageOnly: true, constrainInput: false, maxDate: '+2y', minDate: '+2d'}); 
	DD_roundies.addRule('#searchform', '6px', true);
	DD_roundies.addRule('.inputcontainer', '5px', true);	

	$('a.noDateBookNow').each(function()
	{
		$(this).click(function(e){
			e.preventDefault();
			userSpecMax = $(this).attr('maxDate');
			userSpecMin = $(this).attr('minDate');
			var ajax = {
            url: '/bookAjaxDatePicker.php?root='+$(this).attr('href'), type: 'GET', dataType: 'html', cache: false, success: function(html) { 
            html = jQuery(html);

		if (!userSpecMax){
			userSpecMax = '+4m';
		}else{
			userSpecMax = new Date(userSpecMax);
		}
		if (!userSpecMin){
			userSpecMin = '+2d';
		}else{
			userSpecMin = new Date(userSpecMin);
		}

            bookBoxy = new Boxy(html, {modal:true,
                                             title:'When would you like to go?',
                                             unloadOnHide: true,
                                             afterShow: function(){
                                             	DD_roundies.addRule('.inputcontainer', '5px', true);
											 	$('.datePicker').datepicker({dateFormat: 'dd/mm/yy',buttonImage: '/images/date.png',showOn: 'both', buttonImageOnly: true, constrainInput: false, maxDate: userSpecMax, minDate: userSpecMin});
												$("#ajax_coupon_link").click(function(e){
													$("#ajax_coupon_link").hide();
													$("#ajax_coupon_input").show();
												});												                                             											}});
										 	
            }
        };

        jQuery.ajax(ajax);			
		})
	})

	$('a.venueSearchInquiry').each(function()
	{
		$(this).click(function(e){
			e.preventDefault();
			userSpecMax = $(this).attr('maxDate');
			userSpecMin = $(this).attr('minDate');
			var ajax = {
            url: '/searchAjax.php?root='+$(this).attr('href'), type: 'GET', dataType: 'html', cache: false, success: function(html) { 
            html = jQuery(html);

		if (!userSpecMax){
			userSpecMax = '+12m';
		}else{
			userSpecMax = new Date(userSpecMax);
		}
		if (!userSpecMin){
			userSpecMin = '+2d';
		}else{
			userSpecMin = new Date(userSpecMin);
		}

            bookBoxy = new Boxy(html, {modal:true,
                                             title:'What are the details of the function?',
                                             unloadOnHide: true,
                                             afterShow: function(){
                                             	DD_roundies.addRule('.inputcontainer', '5px', true);
						$('.datePicker').datepicker({dateFormat: 'dd/mm/yy',buttonImage: '/images/date.png',showOn: 'both', buttonImageOnly: true, constrainInput: false, maxDate: userSpecMax, minDate: userSpecMin});

					}});
		}
        };

        jQuery.ajax(ajax);			
		})
	})

	$('a.hotelProfile').each(function()
	{
		$(this).click(function(e){
			e.preventDefault();
			popHotelProfile($(this).attr('href'));
		})
	})

	$('a.requestHelpPopup').each(function()
	{
		$(this).click(function(e){
			e.preventDefault();
			var ajax = {
            url: $(this).attr('href'), type: 'GET', dataType: 'html', cache: false, success: function(html) {
            html = jQuery(html);
            bookBoxy = new Boxy(html, {modal:true,
                                             title:'Help Information',
                                             unloadOnHide: true});
            }
        };

        jQuery.ajax(ajax);			
		})
	})
	
	$("#coupon_link").click(function(e){
		$("#coupon_link").hide();
		$("#coupon_input").show();
	});

	$(".offerWithDate").click(function(e)
	{

		//bookBoxy.hideAndUnload(function(){});
	});

	$("input.roomChoice").click(function(e)
	{
		prfxID = $(this).attr('id');
		$('#pid').val(prfxID);
		$('#purp').val(prfxID);
		$('#summaryPublishTitle').html($("#"+prfxID+"name").val());
		$('#summaryPublishUsual').html($("#"+prfxID+"rack").val());
		$('#summaryPublishDaily').html($("#"+prfxID+"daily").val());
		result = parseFloat($("#"+prfxID+"daily").val()) * parseInt($("#summaryPublishRooms").html()) * parseInt($("#summaryPublishNights").html());
		$('#summaryPublishTotal').html(result);
		saving = roundNumber((1-($("#"+prfxID+"daily").val()/$("#"+prfxID+"rack").val()))*100,0);
		$('#summaryPublishDiscount').html(saving+"% saving!");
	});

	assembleMap(); // Load the google maps if applicable
});


function slideShow() {
	if($('#gallery').length > 0)
	{
	
		//Set the opacity of all images to 0
		$('#gallery a').css({opacity: 0.0});
		
		//Get the first image and display it (set it to full opacity)
		$('#gallery a:first').css({opacity: 1.0});
		
		//Set the caption background to semi-transparent
		$('#gallery .caption').css({opacity: 0.7});
	
		//Resize the width of the caption according to the image width
		$('#gallery .caption').css({width: '299px'});
		
		//Get the caption of the first image from REL attribute and display it
		$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
		.animate({opacity: 0.7}, 400);
		
		//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
		setInterval('gallery()',6000);
	}	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 50px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '40px'},500 );
	
	//Display the content
	$('#gallery .content').html(caption);
	
	
}
