var FamTripperWidget = function () {
  
  var me = this;

  $('.datepicker').datepicker ({
    //showOn: 'button',
    //buttonImage: 'widget/images/calendar-icon.gif',
    //buttonImageOnly: false,
    
    changeMonth: true,
    changeYear: true,
    
    minDate: -5, 
    maxDate: '+12M',
	onSelect: function(dateText, inst){
		selectedDate = new Date(dateText);
		currentForm = $(this).parents("form");
		switch($(this).attr("name")){
			case "chk_in":
				if (currentForm.children("input[name^=doa]").length > 0)
				{
					currentForm.children("input[name=doa_mm]").val(selectedDate.getMonth()+1);
					currentForm.children("input[name=doa_dd]").val(selectedDate.getDate());
					currentForm.children("input[name=doa_yy]").val(selectedDate.getFullYear());			
				}
				break;
			case "chk_out":
				if (currentForm.children("input[name^=dod]").length > 0)
				{
					currentForm.children("input[name=dod_mm]").val(selectedDate.getMonth()+1);
					currentForm.children("input[name=dod_dd]").val(selectedDate.getDate());
					currentForm.children("input[name=dod_yy]").val(selectedDate.getFullYear());				
				}
				break;
			case "leavingDate":
				if(currentForm.children("input[name^=dateLeaving], input[name^=dateReturning]").length > 0)
				{
					currentForm.children("input[name=dateLeavingMonth]").val(selectedDate.getMonth()+1);
					currentForm.children("input[name=dateLeavingDay]").val(selectedDate.getDate());
					currentForm.children("input[name=dateLeavingYear]").val(selectedDate.getFullYear());				
				}
				break;
			case "returningDate":
				if(currentForm.children("input[name^=dateReturning], input[name^=dateReturning]").length > 0)
				{
					currentForm.children("input[name=dateReturningMonth]").val(selectedDate.getMonth()+1);
					currentForm.children("input[name=dateReturningDay]").val(selectedDate.getDate());
					currentForm.children("input[name=dateReturningYear]").val(selectedDate.getFullYear());				
				}
				break;
		}
	}

  });
  
  
  //set click handlers on links in tabs
  $(".tabs li a").click(function() {
    me.selectTab($(this).parent("li"));
    return false;
  });
}


FamTripperWidget.prototype.selectTab = function(tabItem) {
  var me = this;
  
  // remove "on" class and toggle corner images "off" on ALL tabs
  $(".tabs li").removeClass("on");
  $(".tabs li").each(function(i,item) {
    me.setTab(item, false);
  });
  
  // set "on" and toggle corner images "on" for selected tabs
  $(tabItem).addClass("on");
  me.setTab(tabItem, true);
  var contentId = $(tabItem).attr('id').replace("-tab", "-content");
  
  //hide all content and show selected content
  $("#wcontent").children().hide();
  $("#"+contentId).fadeIn();
  
}

/**
 * 
 * toggle the images inside the given tab either "on" or "off"
 * 
 */
FamTripperWidget.prototype.setTab = function(tab, status) {
  var from = status?"-off":"-on";
  var to = status?"-on":"-off";

  $(tab).find("img").each(function(i,item) {
    var newSrc = $(item).attr("src").replace(from, to);
    $(item).attr('src', newSrc);
  });
}

$(document).ready(function() {	

	var from = status?"-off":"-on";
	var to = status?"-on":"-off";

	$('.switchTab').click(function() {
   		$("#wcontent").children().hide();
		$("#packages-content").fadeIn();
		$('#flights-tab').removeClass('on');
		$('#packages-tab').addClass('on');
		$('#flights-tab .l').attr("src", "widget-images/left-off.gif");
		$('#flights-tab .r').attr("src", "widget-images/right-off.gif");
		$('#packages-tab .l').attr("src", "widget-images/left-on.gif");
		$('#packages-tab .r').attr("src", "widget-images/right-on.gif");
	});
	
	$('.switchDate').click(function() {
		if (!$(this).hasClass('selected')) {
			var targetName = $(this).attr("id");
			$('.switchDate').removeClass('selected');
			$(this).addClass('selected');
			$('.dateFieldSwitch').hide();
			$("#" + targetName + "DateFields").fadeIn();
		}
	});
});
