/* VM functions */
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});
                                             	slideShow();
                                             	DD_roundies.addRule('.inputcontainer', '5px', true);
					 	$('.datePicker').datepicker({dateFormat: 'dd/mm/yy',buttonImage: '/pxf.php?f=/images/date.png',showOn: 'both', buttonImageOnly: true, constrainInput: false, maxDate: '+1y', minDate: '+1d'});
                                             }});
            }
        };

        jQuery.ajax(ajax);
}

function popQueryForm(fullHref, userSpecMin, userSpecMax){
	var ajax = {
            url: '/getQuote.php?root='+fullHref, 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: '/pxf.php?f=/images/date.png',showOn: 'both', buttonImageOnly: true, constrainInput: false, maxDate: userSpecMax, minDate: userSpecMin});

					}});
		}
        };

        jQuery.ajax(ajax);
}

$(document).ready(function(){

	$('#coupon_input').hide();
	$('.datePicker').datepicker({dateFormat: 'dd/mm/yy',buttonImage: '/pxf.php?f=/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.venueSearchInquiry').each(function()
	{
		$(this).click(function(e){
			e.preventDefault();
			userSpecMax = $(this).attr('maxDate');
			userSpecMin = $(this).attr('minDate');
			var ajax = {
            url: '/getQuote.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: '/pxf.php?f=/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);
	
	
}

//------
function emptyList( box ) {
	// Set each option to null thus removing it
	while ( box.options.length ) box.options[0] = null;
}

function fillList( box, arr ) {
	// arr[0] holds the display text
	// arr[1] are the values
	for ( i = 0; i < arr[0].length; i++ ) {
		// Create a new drop down option with the
		// display text and value from arr
		option = new Option( arr[0][i], arr[1][i] );
		// Add to the end of the existing options
		box.options[box.length] = option;
	}
	// Preselect option 0
	box.selectedIndex=0;
}

function changeList( box, targetBox ) 
{
	// Isolate the appropriate list by using the value
	list = lists[box.options[box.selectedIndex].value];
	emptyList( targetBox );
	fillList( targetBox, list );
}

//-----
var inMotion = false; //only allow the httpHandler to do something when this is true
var tgtObj = null; //the DIV to change the contents of
var vswurl="/getQuoteProcess.php?action=";
var pageComm = getHTTPObjectVSW();
function ajaxVSEmail(vref, btnObj, seating, capacity, city){
	btnObj.src="/images/ajax-vsLoad.gif";
	sendMessage(vref, seating, capacity, city);
}
function showRecaptcha(element, themeName) {
Recaptcha.create("6LchnwgAAAAAACtVAjtjHK9ZXQeEp8kkGg8cDTBQ", element, {
	theme: themeName,
	tabindex: 0,
	callback: Recaptcha.focus_response_field
});
}
function getHTTPObjectVSW() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmlhttp = false;
	}
}
@else
	xmlhttp = false;
@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false;
	}
	}
return xmlhttp;
}
function processMailReq() {
	if (pageComm.readyState == 4) {
		//Refresh the briefcase display
		var res =  pageComm.responseText.split('@@');
		tgtObj.innerHTML=res[0];
		if (res[1]=="recap"){
			showRecaptcha('recapbox', 'red');
		}
		inMotion = false;
	}
}
function prepWeb(userStr){
	var cleanStr = userStr.split("\n").join("--");
	cleanStr = cleanStr.split("&").join("and");
	cleanStr = cleanStr.split("+").join("[plus sign]");
	cleanStr = cleanStr.split("%").join("[percentage sign]");
	cleanStr = cleanStr.split(" ").join("%20");
	return cleanStr;
}
function performTrans(urlToDo){
	if (inMotion){
		pageComm.open("GET", urlToDo, true);
		pageComm.onreadystatechange = processMailReq;
		pageComm.send(null);
	}
}
