$(document).ready(function() {
	$.jsonp({
		url: $bookURL+'/captaincircle/checkOutage.page?callback=?',
		success: function(outage){
			console.log('Home page - outage check status = '+outage.status);
			switch(outage.status){
				case "OK":
					if( getCookie("LOGIN_UID") != undefined && getCookie("LOGIN_UID") != "" ){
						$(".cruised-with-princess-block").remove();
					}
					loadPorts();
					$.when(loadPorts).then(function(){
						console.log('loadPorts done....');
						$.getScript('/js/share/find/fnc.core.js', function(){
							type = "quick-search";
							$.getScript('/js/share/find/fnc.simple.js');
						});
					});
					loadLastViewed();
					break;
				case "OUTAGE":
				case "ERROR":  
				default:	
					renderOutage();
					break;
			}
		},
		error: function(){ 
			console.log('Home page - outage check failed');	
			renderOutage();				
		}
	});				
	
	function loadPorts(){
		$.getJSON('/find/getEmbarkPortsJSON.do', function(data) {
			$.each(data, function(key, val) {
				$('#port').append('<option id="E'+key+'" value="'+key+'">'+val+'</option>');
			});
		});		
	}
	
	function loadLastViewed(){
		$.getJSON('/find/getLastViewedJSON.do', function(data) {
			if( data ){
				$('#last-viewed-data').empty();
				$('#last-viewed-data').append('<h4><a href=\'/find/cruiseDetails.do?itinCode='+data.ITIN_CODE+'&voyageCode='+data.VYG_CODE+'&resType='+data.RES_TYPE+'\'>'+data.NAME+'</a></h4>');
				$('#last-viewed-data').append('<p>'+data.FROM_TO+' &ndash; <span class=\'ship-name\'>'+data.SHIP_NAME+'</span></p>');
				$('#recently-viewed img').remove();
				$('#recently-viewed').removeClass('none')
			}
			else{
				$('#recently-viewed h3').remove();
				$('#last-viewed-data').remove();
				$('<img title="escape completely" src="/images/global/escape_completely_home.png">').hide().prependTo('#recently-viewed').fadeIn('medium');
			}	
		});			
	}
	
	function initBookingEngineDetails(){
		$.getJSON('/find/getBookingEngineDtlsJSON.do', function(data) {
			bookingEngineDetails = bookingEngineDetails + data.bookingEngineId + ',C,false,' + data.defaultSelectedMeta;
			console.log('bookingEngineDetails = ' + bookingEngineDetails);			
		});		
	}
	
	function renderOutage(){
		var jqxhr = $.get("./quickSearchOutage.html", function(data) { 
	     	$('#quick-search').empty();
	     	$('#quick-search').append(data);
	   	}, "html");				
	}		
		
});
