// Initiate Shadowbox
var shadowBoxOptions = { 
	loadingImage: 	base_url+'public/images/rooms/shadowbox_loading.gif',
	overlayColor: 	'#000',
	overlayOpacity:	0.75,
	displayNav: 	false,
	players: 		["iframe"]
}

Shadowbox.init(shadowBoxOptions);

$(document).ready(function() { 
	
	$('#card_type').change(function() {
		
		var type = $(this).attr('value');
		
		if(type == 'MC' || type == 'VISA') {
			$('.total_payable .debit').hide();
			$('.total_payable .credit').show();
		}
		else {
			$('.total_payable .debit').show();
			$('.total_payable .credit').hide();
		}

	});
	
	
	$('#bargains_list li').css({cursor : 'pointer'}).hover(
		function() { $(this).children('a').css({textDecoration : 'underline'}); },
		function() { $(this).children('a').css({textDecoration : 'none'}); }
	);
	
	$('#bargains_list li').click(function() {
		getbargain($(this).attr('rel'));
		return false;
	});
	
	$('#bargains_pagination a').click(function() {
		
		if($(this).parent().hasClass('more')) { 
			window.location.href = $(this).attr('href'); 
			return false;
		}

		var pageNumber = Number($(this).attr('href'));
		var startRow = (pageNumber * 3) - 2;
		
		$('#bargains_list li').hide();
		
		for (i = startRow; i <= (startRow + 2); i ++) {
			$('#offerRow'+i).show();
		}

		return false;
	});
		
	// Submit form
	$('#search_form .footer a').click(function() { 
		$(this).prev().click();
		return false;
	});		

	$('#r_norooms').change(function() {
			
			var rooms = $(this).attr('value');

			if (rooms == 1) {
				$("#occupants_room_2").hide();
				$("#occupants_room_3").hide();				
			}
			
			if (rooms == 2) {
				$("#occupants_room_2").show();
				$("#occupants_room_3").hide();				
			}
			
			if (rooms == 3) {
				$("#occupants_room_2").show();
				$("#occupants_room_3").show();				
			}
			$('.bottom').css("bottom", "0px");

	});

	$('#r_children').change(function() {
			children = $(this).attr('value');
			
			// Show the number of children required 
			if (children > 0) {
				for (var i = 1; i <= children; i++) {
					string = "#rooms_children_ages_child" + i;
					$(string).show();
				}
				while (i <= 8) {
					string = "#rooms_children_ages_child" + i;
					$(string).hide();
					i++;
				}
			}
			else {
				for (var i = 1; i <= 8; i++) {
					string = "#rooms_children_ages_child" + i;
					$(string).hide();
				}
			}

			$('.bottom').css('bottom', '0px');
		});


	$('#r_room2_children').change(function() {
			children = $(this).attr('value');
			// Show the number of children required 
			if (children > 0) {
				for (var i = 1; i <= children; i++) {
					string = "#rooms_children_ages_room2_child" + i;
					$(string).show();
				}
				while (i <= 8) {
					string = "#rooms_children_ages_room2_child" + i;
					$(string).hide();
					i++;
				}
			}
			else {
				for (var i = 1; i <= 8; i++) {
					string = "#rooms_children_ages_room2_child" + i;
					$(string).hide();
				}
			} 
			$('.bottom').css('bottom', '0px');
			
	});

	$('#r_room3_children').change(function() {
			children = $(this).attr('value');
			// Show the number of children required 
			if (children > 0) {
				for (var i = 1; i <= children; i++) {
					string = "#rooms_children_ages_room3_child" + i;
					$(string).show();
				}
				while (i <= 8) {
					string = "#rooms_children_ages_room3_child" + i;
					$(string).hide();
					i++;
				}
			}
			else {
				for (var i = 1; i <= 8; i++) {
					string = "#rooms_children_ages_room3_child" + i;
					$(string).hide();
				}
			} 
			$('.bottom').css("bottom", "0px");
			
	});
	
	
	$('#r_destination').change(function() { 
		var airport_code = $(this).attr('value');

		$('#r_resort').html('<option value="">Updating...</option>').prev().html($('#r_resort option:first').html()); 		


		$.post(site_url+'home/get_room_resorts', { airport_code : airport_code }, function(data) {
			options = '<option value="">please select....</option>';
			options += '<option value="all">all resorts</option>';	
			for(var i = 0; i < data.length; i++ ) {
				options += '<option value="' + data[i]['id'] + '">' + data[i]['resort_name'] + '</option>';
			}
			options += '<option value="all">all resorts</option>';	
			
			$('#r_resort').html(options).prev().html($('#r_resort option:first').html()); 

		}, 'json'); 
	});		
});

//
// Hotel Details Popup
//
function terms_popup(url) {
	Shadowbox.open({ 
			player:		'iframe',
			content:	url,
			height:		500,
			width: 		780
		});
		return false;
}

//
// Hotel Details Popup
//
function hotel_details(url) {
		
	Shadowbox.open({ 
			player:		'iframe',
			content:	url,
			height:		500,
			width: 		780
		});
		return false;
}



//
// Hotel Sorting Function - called php to re-sort the result in a given order
//
function hotel_sorting() {
	selected = $('#room_sort_options').val();
	
	$.get(site_url + "rooms/loadingpage", function(data){

		document.getElementById('rooms_results_area').innerHTML = data;  

	});

	$.get(site_url + "rooms/sortby/" + selected, function(data){

		document.getElementById('rooms_results_area').innerHTML = data;  

	});		
}

//
// Paging Function for Rooms, 
//
function room_paging(url) {	  
    
	$.get(site_url + "rooms/loadingpage", function(data){

		document.getElementById('rooms_results_area').innerHTML = data;  

	});
	
	$('html, body').animate({scrollTop:0}, 'normal'); 

	$.get(url, function(data){

		document.getElementById('rooms_results_area').innerHTML = data;  

	});	
}

function validate_rooms_payments() {

	var validationStatus = true;

	$('#rooms_payment_step4 .error').removeClass('error');

	notEmptySelectors = new Array('#card_holder_name', '#card_number', '#card_postcode');

	if(empty($('#card_type').attr('value'))) {
		var $addErrorTo = $('#card_type').parent().parent().parent();
		$addErrorTo.addClass('error');
	}
		
	if(empty($('#expiry_date_mm').attr('value')) || empty($('#expiry_date_yyyy').attr('value'))) {
		var $addErrorTo = $('#expiry_date_mm').parent().parent().parent();
		$addErrorTo.addClass('error');
	}
		
	if(empty($('#cv2_number').attr('value'))) {
		var $addErrorTo = $('#cv2_number').parent();
		$addErrorTo.addClass('error');
	}

	if(checkValuesEntered != null) checkValuesEntered(notEmptySelectors);


	$('#rooms_payment_step4 .error').each(function() {
		if($(this).length) validationStatus = false;
	});
	
	if (validationStatus == true) { 
		$('#rooms_payment_step4').submit();
	}
	
	else { 
		return false;	 
	}
	
}

function checkValuesEntered(jquery) {	
	for (selector in jquery)
	{
		if(empty($(jquery[selector]).attr('value'))) {
			var $addErrorTo = $(jquery[selector]).parent();
			$addErrorTo.addClass('error');
		}
	}
}


function empty( mixed_var ) {  
       
    if (mixed_var === ""   
        || mixed_var === 0     
        || mixed_var === "0"  
        || mixed_var === null    
        || mixed_var === false  
        || mixed_var === undefined      
        || ((typeof mixed_var == 'array' || typeof mixed_var == 'object') && mixed_var.length === 0) ){  
        return true;  
    }  
      
	return false;  
}

function printSpecial(divID)
{
    if (document.getElementById != null)
    {
		var gAutoPrint = true;
		
        var html = '<HTML>\n<HEAD>\n';

        if (document.getElementsByTagName != null)
        {
            var headTags = document.getElementsByTagName("head");
            if (headTags.length > 0)
                html += headTags[0].innerHTML;
        }
       
        html += '\n</HE' + 'AD>\n<BODY>\n';
       
        var printReadyElem = document.getElementById(divID);
       
        if (printReadyElem != null)
        {
                html += printReadyElem.innerHTML;
        }
        else
        {
            alert("Could not find the printReady section in the HTML");
            return;
        }
           
        html += '\n</BO' + 'DY>\n</HT' + 'ML>';
       
        var printWin = window.open("","printSpecial");
        printWin.document.open();
        printWin.document.write(html);
        printWin.document.close();
        if (gAutoPrint)
            printWin.print();
            printWin.close();
    }
    else
    {
        alert("Sorry, the print ready feature is only available in modern browsers.");
    }
}

function getbargain(bargainId)
{
	removebargain();
	
	var title = $('#bargains_list li[rel='+bargainId+'] .title').html()+'<br/>'+$('#bargains_list li[rel='+bargainId+'] .price').html()+'<br/>'+$('#bargains_list li[rel='+bargainId+'] .dates').html()+'<br/>'+$('#bargains_list li[rel='+bargainId+'] .nights').html();
	var description = $('#bargains_list li[rel='+bargainId+'] .description').html();
	
	$('#bargains_list, #bargains_pagination').hide();
	
	$('#bargains_list').before('<div id="bargainDescriptionPanel" class="customScrollerDark"><div class="scroller"><h5>'+title+'</h5>'+description+'</div></div>').after('<a href="#" id="bargainBackButton" onclick="removebargain(); return false;">Return</a>');
	
	var height = 0;
	$('#bargainDescriptionPanel div *').each(function() { height += Number($(this).height()); });
	
	if(height > 255) { $('#bargainDescriptionPanel .scroller').jScrollPane({showArrows:true, scrollbarWidth: 30, arrowSize: 12}); }
	else { $('#bargainDescriptionPanel .scroller').css({overflow: 'hidden'}); }
}

function removebargain() 
{
	$('#bargainDescriptionPanel, #bargainBackButton').remove();
	$('#bargains_list, #bargains_pagination').show();
}