//Direct URL Loading Tab Switching Functionality
$(document).ready(function(){
  listFbLike();
	jQuery("input[@name=metaCode]:checked").closest('.stateroom-type').addClass('selected');   //Highlighting Stateoom selection
	//Tab Switching Functionality
	jQuery('#tabs a').click(function(event){
		event.preventDefault(); 		
		jQuery('#tabs a').removeClass('active');		
		jQuery(this).addClass('active');
		var tabId=jQuery(this).attr('id');
		jQuery("input[type=hidden][name=currentPage]").val(jQuery(this).attr('href'));	
		var url = jQuery(this).attr('href');
		if( jQuery(this).attr('href').indexOf('flightsDetails.do?') == -1 ){
        	url += "?"+ getDynamicUserSelections();	
		}		
		
		ajaxCallback(url,'tab-content',c, 'cruiseDetails');		
		function c(){//callback
			if(tabId=='CompareDatesAndFares'){
				var elementId=jQuery("input[@name=metaCode]:checked").attr('id');
				jQuery('.pricing-details-table .'+elementId ).addClass('highlighted-meta');
				compareDatesBorderForIE();
			}
			if(tabId=='exList'){
				no_excursions_selected();
			}						
			var destination = $('#'+'sign-in-continue').offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100, function() {			
				window.location.hash = url;
			});		
	  	}		 
	   return false;
	});
	
	//Highlighting Stateoom selection
	jQuery('.stateroom-type input').click(function(){
		jQuery('.stateroom-type').removeClass('selected');
		jQuery(this).closest('.stateroom-type').addClass('selected');
		
		jQuery('.pricing-details-table col').removeClass('highlighted-meta');
		var elementId=jQuery(this).attr('id');
		jQuery('.pricing-details-table .'+elementId ).addClass('highlighted-meta');
		jQuery('#selectedMeta').val(jQuery('#'+elementId).val());
	});
	
	adjustStateRoomTypeHeight();
	//Open Sign in box next to the clicked element
	jQuery('.sign-in-register-link').live('click',function() {
		var elemOffsetPosition = $(this).offset();
		var elemWidth = $(this).outerWidth();
		jQuery('#sign-in-box').fadeIn('medium');
		jQuery('#iframe-page').attr('src', $bookURL + '/captaincircle/renderQLForm.page');
		jQuery('#sign-in-box').offset({
			'left': elemOffsetPosition.left + elemWidth + 20,
			'top': elemOffsetPosition.top
		});
		return false;
	});

});


function adjustStateRoomTypeHeight(){
	// Adjusting height of Stateroom selection box
	var maxHeight = 0;		
	jQuery('.stateroom-type').each(function(){		
		maxHeight = Math.max(maxHeight, $(this).height());
	});
	jQuery('.stateroom-type').css('height',maxHeight);	
}

function ajaxCallback(urls,elemId,func,frmName){
  var formData='';
  if(frmName != null && frmName.length >0){
  	formData=$("#"+frmName).serialize();
  }
  jQuery.ajax({ 
    type: "POST", 
    url:urls, 
    dataType:"html", 
    data:formData, 
	global:false,
    success:function(s){   
      //document.getElementById(elemId).innerHTML=s;     
      jQuery("#"+elemId).html(s);   
      func();
    }, error: 
      function(e) { 
        document.getElementById(elemId).innerHTML= "Unable to process now. Please try later.";
      } 
  });
}

function openLegalTerms(){
  window.open(getWWWUrl(getUrl())+'legal/passage_contract/index.jsp', 'legal_terms');
}

function openUBELegalTerms(bId,isBestFare,country){
  if(isBestFare == 'true') {
    window.open(getWWWUrl(getUrl())+'legal/passage_contract/index.jsp', 'legal_terms');
  } else {
    window.open(getWWWUrl(getUrl()) + 'legal/passage_contract/passage_contract_'+bId+'.jsp?con='+country, 'ta_rates_footer');
  }
}

function compareDatesBorderForIE(){
	if(jQuery.browser.msie && jQuery.browser.version <= 7) {
		/* IE7 Hack : <col> border not supported by IE */	
		jQuery('#compare-cruises-table').find('td:nth-child(3n):not(.itinerary-table td)').css({'border-right': 'solid 1px #CCC', 'border-left': 'solid 1px #CCC'});
		/* IE7 Hack : border for stateroom heading cells  */	
		jQuery('#compare-dates-fares table:first').find('th:nth-child(1), th:nth-child(2)').css({'border-right': 'solid 1px #fff', 'border-left': 'solid 1px #fff'});
		jQuery('#compare-dates-fares table:first').find('th:last-child').css({'border-right': 'solid 1px #fff'});
		
	}
	
	if(jQuery.browser.msie && jQuery.browser.version == 8) {
		/* IE8 Hack : border for stateroom heading cells  */	
		jQuery('#compare-dates-fares table:first').find('th:nth-child(1),th:last-child').css({'border-right': 'solid 1px #fff', 'border-left': 'solid 1px #fff'});
	}
}

jQuery('.show-tooltip').live('mouseover mouseout',
	function(event){
		if( event.type == 'mouseover' ){
		jQuery('.tooltip').css({'left':'-2000px','top':'-2000px'});
		jQuery('.tooltip').removeClass().addClass('tooltip');
		var $this = jQuery(this);
		var thisHeight =0;
		// If the element has children, calculate that height too.
		if($this.children().length){
			thisHeight= $this.contents().outerHeight();
		}
		else{
			thisHeight= 10;
			}
			
		var tooltipColor= $this.attr('data-tooltip');
		jQuery('.tooltip').addClass(tooltipColor+'-tooltip');
		var tooltipContent =$this.attr('data-tooltip-content');
		tooltipContent = "#"+tooltipContent;
		var tooltipHeading= $(tooltipContent +' h2').text();
		jQuery('#tooltip-content').prev('h2').text(tooltipHeading);
		tooltipContent = $(tooltipContent +" .tooltip-inner-content").html();
		
		jQuery('#tooltip-content').html(tooltipContent);				
		var elemOffsetPosition = $this.offset();
		var elemWidth = $this.outerWidth();
		var tooltipWidth= jQuery('.tooltip').outerWidth();
		var tooltipHeight= jQuery('.tooltip').outerHeight();
		jQuery('.tooltip').delay(100).show(function(){
			 jQuery('.tooltip').offset({
				'left': elemOffsetPosition.left - tooltipWidth + elemWidth + 5,
				'top': elemOffsetPosition.top - tooltipHeight - thisHeight
			});
			var tooltipOffset = jQuery('.tooltip').offset();
			var contentDivOffset = jQuery('#content').offset();
			//Reset the Tooltip Position
			jQuery('.tooltip .pointer-arrow').css({'left':'auto', 'right':'5px'});	
			//Re-position Tooltip if it is bleeds beyond the content area
			if(contentDivOffset.left > tooltipOffset.left){
				jQuery('.tooltip').offset({
				'left': contentDivOffset.left + 10
			});
				jQuery('.tooltip .pointer-arrow').css({'left':'10px', 'right':'auto'});
				}

			jQuery('.tooltip').hover(function(){
				$(this).show();
			},
			function(){
				$(this).hide();
				}
			);
		});
	}
	else{
			jQuery('.tooltip').stop(true, true).hide();	 
		}
	}
);
	

