	YAHOO.namespace("YAHOO.util.App");

	var Dom = YAHOO.util.Dom;
	var Evt = YAHOO.util.Event;
	var Fn  = YAHOO.util.Functional;
	var App = YAHOO.util.App;
	var Sel = YAHOO.util.Selector;

  /* Global Utility Functions */

	YAHOO.lang.augmentObject(App, {
	  Component: [],
    logging_enabled: true,

		deferred_component_finder: function (component) {
			return component._defer;
		},

	  /*
	    Components are site-wide widgets that respond to initialize()
	    and expect that method to be called when the DOM is ready
	  */

	  init_components: function () {
			var components = Fn.reject(App.Component, this.deferred_component_finder);
	    this.invoke_component_initializer(components);
	  },

		init_deferred_components: function () {
			var components = Fn.select(App.Component, this.deferred_component_finder);
	    this.invoke_component_initializer(components);
	  },

		invoke_component_initializer: function (components) {
			Fn.each(components, function (component) {
        try {
          component.initialize();
          App.log("Initialized component " + component._name);
        }
        catch (e) {
          App.log("Failed to initialize " + component._name);
          App.log(e);
        }
      });
		},

	  register_component: function (name, definition, defer_initialization) {
  	  YAHOO.lang.augmentObject(YAHOO.namespace(name), definition);
  	  var component = eval(name);
    	App.Component.push(component);
      component._name = name;
			component._defer = defer_initialization ? true : false;
    	return component;
  	},

	  new_element: function (localName, attrs, html, parent) {
		  var el = null;
		  try {
		    el = document.createElement(localName);
		    if (el && parent) {parent.appendChild(el);}
		    if (el && attrs) {for (var a in attrs) {el.setAttribute(a, attrs[a]);}}
		    if (el && html) {el.innerHTML = html;}
		  }
		  catch (e) {}
		  return el;
    },

    remove_element: function (el) {
      if (!el || !Dom.get(el)) { return; }
      Dom.get(el).parentNode.removeChild(Dom.get(el));
    },

    get_data: function (el, name) {
      return Dom.get(el).getAttribute('data-' + name);
    },

    set_data: function (el, name, value) {
      Dom.get(el).setAttribute('data-' + name, value);
      return el;
    },

    format_string: function (message, data) {
      var interpol = new RegExp(/(^|.|\r|\n)(#\{(.*?)\})/);
      while (matches = interpol.exec(message)) {
        message = message.replace(matches[2], data[matches[3]]);
      }
      return message;
    },

    create_xhr_logger: function () {
      return (function (o) { App.log(o.responseText); App.log(o); });
    },

    serialize_form: function (formId) {
      var values = [];

  		Fn.each(['input', 'select'], function (field_type) {
  		  var group = Dom.get(formId).getElementsByTagName(field_type)
  		  Fn.each(group, function (field) {
    		  if (field.disabled) return;
          if (!field.name && !field.id) return;
    		  if (field.type === 'radio' && !field.checked) return;
    		  if (Dom.hasClass(field, 'no_display')) return;
          values.push((field.name || field.id) + '=' + encodeURIComponent(field.value));
    		});
  		});

  		return values.join('&');
  	},

	  show: function (el, fn, displayType) {
  		var dType = "block";
  		if (displayType && typeof displayType === "string") { dType = displayType; }
	    Dom.setStyle(el, 'display', dType);
	    if (typeof fn === "function") { fn.call(Dom.get(el), Dom.get(el)); }
	    return Dom.get(el);
	  },

	  hide: function (el, fn) {
	    Dom.setStyle(el, 'display', 'none');
	    if (typeof fn === "function") { fn.call(Dom.get(el), Dom.get(el)); }
	    return Dom.get(el);
	  },

	  is_hidden: function (el) {
	    return ('none' === Dom.getStyle(el, 'display') || 'hidden' === Dom.getStyle(el, 'visibility'));
	  },

    // Method delegation
	  delegate: function (target, fn, responder, method) {
	    var lambda = responder[method || fn];
	    if (typeof lambda !== "function") { return; }
	    target[fn] = function () {
	     lambda.apply(target, arguments);
	    }
	  },

	  log: function (msg, trace) {
      if (!App.logging_enabled || typeof console !== "object" || !console.log) { return; }
      console.log(msg);
      if (trace) {
        console.log(this.log.caller);
      }
    },

	  create_matcher: function (r) {
      return function (f) { return r.test(f.value); };
    },

    create_inverse_matcher: function (r) {
      return function (f) { return !r.test(f.value); };
    },

	  // Toggle any block-level element by setting its display style
	  toggleDisplay: function (el, onshow, onhide) {
	    if (this.is_hidden(el)) {
	      this.show(el, onshow);
	    }
	    else {
	      this.hide(el, onhide);
	    }
	  },

	  keypress: function (target, keys, lambda) {
      (new YAHOO.util.KeyListener(target, {keys: keys}, lambda, YAHOO.util.KeyListener.KEYUP)).enable();
	  },

	  r2: {
	  	secure_url: function () {
	  		return 'https://' + App.get_data('ncl', 'r2-secure-host');
	  	},

	  	non_secure_url: function() {
	  		return 'http://' + App.get_data('ncl', 'r2-host');
	  	},

	  	base_url: function() {
	  		return (document.location.protocol == 'https:') ? App.r2.secure_url() : App.r2.non_secure_url();
	  	}
	  }
	});

	YAHOO.lang.augmentObject(YAHOO.namespace("YAHOO.util.App.Ajax"), {
	  create_loading_image: function (parent) {
			if (App.Ajax.is_loading()) return;
	    return App.new_element('img', {
	      'src': NCLUtils.createURL('/images/framework/drop-it-like-its-hot.gif'),
	      'alt': 'loading',
	      'id': 'loading_graphic'
	    }, null, Dom.get(parent));
	  },

	  remove_loading_image: function () {
	    App.remove_element('loading_graphic');
	  },
		is_loading: function () {
			return !!Dom.get('loading_graphic');
		}
	});

  /* "Shake" Animation Effect */

	// Credit: Dustin Diaz - http://www.dustindiaz.com/ajax-contact-updated/
	YAHOO.util.App.effects = function() {
	  return {
	    /*
	      @param {String/HTMLElement} | oEl : Accepts a string to use as an ID for getting a DOM reference, or an actual DOM reference
	      @param {Int} | iOffset : The unit (in 'px') that the element will be shaken 'by'
	      @param {Int} | iNum : The number of times the motion will iterate
	      @param {Int} | iSpeed : The speed at which the motion will animate
	    */

	    shake : function(oEl, iOffset, iNum, iSpeed) {
	      var xy = YAHOO.util.Dom.getXY(oEl);
	      var left = xy[0]-iOffset;
	      var right = xy[0]+iOffset;
	      (function(type, args, count) {
	        if ( count >= iNum ) {
	          var a = {
	            points : {
	              to : xy
	            }
	          };
	          var anim = new YAHOO.util.Motion(oEl, a, iSpeed);
	          anim.animate();
	          return;
	        }
	        else if ( count % 2 ) {
	          var c = count+1;
	          var a = {
	            points : {
	              to : [right, xy[1]]
	            }
	          };
	          var anim = new YAHOO.util.Motion(oEl, a, iSpeed);
	          anim.onComplete.subscribe(arguments.callee, c);
	          anim.animate();
	        }
	        else {
	          var c = count+1;
	          var a = {
	            points : {
	              to : [left, xy[1]]
	            }
	          };
	          var anim = new YAHOO.util.Motion(oEl, a, iSpeed);
	          anim.onComplete.subscribe(arguments.callee, c);
	          anim.animate();
	        }
	      })(null, null, 1);
	    }
	  };
	}();

  /* Video Player Component API */

	YAHOO.lang.augmentObject(YAHOO.namespace("YAHOO.util.App.VideoPlayer"), {
	  players: {},

	  find_player: function (el) {
	    if (typeof YAHOO.util.App.VideoPlayer.players[el] !== "object") {
	      var container = Dom.get(el);
	      YAHOO.util.App.VideoPlayer.players[el] = new SWFObject('/nclweb/flash/jwplayer/4.4/player-licensed.swf', 'mpl', container.clientWidth, container.clientHeight, '9');
	      YAHOO.util.App.VideoPlayer.players[el].addParam('allowscriptaccess','always');
	      YAHOO.util.App.VideoPlayer.players[el].addParam('allowfullscreen','true');
				YAHOO.util.App.VideoPlayer.players[el].addParam('wmode','opaque');
	    }
	    return YAHOO.util.App.VideoPlayer.players[el];
	  },

	  create: function (el, params) {
	    var container = Dom.get(el);
	    if (typeof container !== "object") { return; }

	    var flashvars = '&duration=' + params.duration + '&file=' + params.file + '&image=' + params.image;

			if (typeof params.controls === "string") {
	      flashvars += '&controlbar=' + params.controls;
	    }

			if (typeof params.streamer === "string") {
				flashvars += "&streamer=" + params.streamer;
			}

	    var so = YAHOO.util.App.VideoPlayer.find_player(el);
	    so.addParam('flashvars', flashvars);
	    so.write(el);
	  }
	});

	/* Integration Cookie */
	YAHOO.lang.augmentObject(YAHOO.namespace("YAHOO.util.App.IntCookie"), {
	   get: function () {
		   var cookieVal = YAHOO.util.Cookie.get("NCLWEBInt");
  		 if (cookieVal) {
  		   try {
  	       return YAHOO.lang.JSON.parse(cookieVal);
  		   }
  		   catch (e) {
  	       App.log("Invalid cookie value to parse!");
  	     }
  		 }
  		 App.log("No integration cookie found!");
	   },

	   set: function (jsonObj) {
	     var cookieVal = null;
	     try {
	       cookieVal = YAHOO.lang.JSON.stringify(jsonObj);
	       YAHOO.util.Cookie.set("NCLWEBInt", cookieVal, {
	         path: "/",
	    	   domain: ".ncl.com",
	    	   expires: Date.today().addDays(1).toUTCString()
	       });
	     }
	     catch (e) {
	       App.log("Invalid cookie object to parse!");
	     }
	   },


    get_integer_value: function (value) {
      var cookie = YAHOO.util.App.IntCookie.get();
      if (!cookie || !cookie[value]) { return 0; }
      try {
        return parseInt(cookie[value]);
      }
      catch (ex) {
        return 0;
      }
    },

    has_updated_vacations: function () {
      return this.get_integer_value('seen_vacations') === 1;
    },

    has_updated_alerts: function () {
      return this.get_integer_value('seen_alerts') === 1;
    },

    update_saved_vacations: function (count) {
  	  var cookie = YAHOO.util.App.IntCookie.get();
  	  if (!cookie) { return; }
  	  cookie.seen_vacations = 1;
  	  if (count) { cookie.vac = count; }
  	  YAHOO.util.App.IntCookie.set(cookie);
  	},

  	update_alerts: function (count) {
  	  YAHOO.util.App.IntCookie.set_alert_count(count);
  	  var cookie = YAHOO.util.App.IntCookie.get();
  	  if (!cookie) { return; }
  	  cookie.seen_alerts = 1;
  	  if (count) { cookie.alts = count; }
  	  YAHOO.util.App.IntCookie.set(cookie);
  	},

    alerts: function () {
      return YAHOO.util.App.IntCookie.get_integer_value('alts');
    },

    bookings: function () {
      return YAHOO.util.App.IntCookie.get_integer_value('res');
    },

    saved_items: function () {
      return YAHOO.util.App.IntCookie.get_integer_value('vac');
    },

    initialize: function () {
      this.set_alert_count(YAHOO.util.App.IntCookie.alerts());
      this.set_saved_count(YAHOO.util.App.IntCookie.saved_items());
      this.set_booked_count(YAHOO.util.App.IntCookie.bookings());
      this.bind_booked();
    },

    set_alert_count: function (count) {
      $j('#user_alerts a.value').html(count);
      if (0 < parseInt(count)) {
        $j('#user_alerts').addClass('hasCount').removeClass('noCount');
      }
      else {
        $j('#user_alerts').removeClass('hasCount').addClass('noCount');
      }
    },

    set_saved_count: function (count) {
      $j('#user_saved_vac a.value').html(count);
    },

    set_booked_count: function (count) {
      $j('#user_booked_vac a.value').html(count);
    },

    bind_booked: function () {
  	  $j('#user_booked_vac').click(function () {
        document.location.href = $j(this).find('a').attr('href');
  	  });
  	}
	});

  /* Sitewide Promo Component API */

	YAHOO.lang.augmentObject(YAHOO.namespace("YAHOO.util.App.SitewidePromos"), {
	  promoId: '',
	  height: 100,
	  width: '',
	  backgroundColor: '',
	  expiration: 0,

	  buttons: {
	    plus: '/nclweb/images/framework/open-button.png',
	    minus: '/nclweb/images/framework/close-button.png'
	  },

	  attach_events: function () {
	    if (Dom.get('sitewide_promo_toggle')) {
	      Evt.on('sitewide_promo_toggle', 'click', function () {
	        (App.is_hidden('sitewide_promo') ? App.SitewidePromos.open : App.SitewidePromos.close)();
	      });
	    }
	  },

	  initialize: function () {
	    App.log("Initializing SitewidePromos");
      var selected_promotion = this.randomize();
      if (!selected_promotion) { return false; }

      this.set_message(Dom.get('sitewide_message'), selected_promotion);
        this.set_banner(Dom.get('sitewide_promo_link'), selected_promotion);
      this.set_link(Dom.get('sitewide_promo_link'), selected_promotion);
        this.set_banner_height(selected_promotion);
        this.set_promo_id(selected_promotion);
        this.set_expiration(selected_promotion);
        this.set_backgroundColor(Dom.get('sitewide_promo'), selected_promotion);

      this.attach_events();
        if (!this.should_be_closed(App.SitewidePromos.promoId)) { this.open(); }
      },

      get_new_promos: function(candidates) {
        return Fn.select(candidates, function (promotion) {
          if (!promotion || !promotion.id) { return false; }
    	  return null == YAHOO.util.Cookie.getSub('sitewide_promo_closed', promotion.id);
        });
    },

	  randomize: function () {
	    if (!this.promotions || 0 == this.promotions.length) { return null; }
	    var candidates = this.filter(this.promotions);
	    if (!candidates || 0 == candidates.length) { return null; }
	    var brandNewPromos = this.get_new_promos(candidates);
	    if (brandNewPromos && 0 > brandNewPromos.length) {
	      candidates = brandNewPromos;
	    }
	    return candidates[parseInt((Math.random() * 100)) % candidates.length];
	  },

	  // Don't display the promotion if the current url matches any excluded pattern
	  should_display: function (promotion) {
	    if (!promotion || !promotion.exclude) { return false; }
	    if (Fn.detect(promotion.exclude, function (exclusion) {
	      return document.location.href.match(exclusion);
	    })) { return false; }
	    return true;
	  },

	  filter: function (promos) {
	    return Fn.select(promos, function (promotion) {
	      return App.SitewidePromos.should_display(promotion);
	    });
	  },

	  set_message: function (el, promotion) {
	    if (!el || !promotion || !promotion.button_text) { return; }
	    el.innerHTML = promotion.button_text;
	  },

	  set_link: function (el, promotion) {
	    if (!el || !promotion || !promotion.link) { return; }
	    if (promotion.link.target) { el.target = promotion.link.target; }
	    if (promotion.link.href)   { el.href   = promotion.link.href; }
	    if (promotion.link.title)  { el.title  = promotion.link.title; }
	  },

	  set_static_banner: function (el, promotion) {
		el.innerHTML = "<img id=\"sitewide_promo_banner\" src=\"" + escape(promotion.banner.href) + "\" alt=\"" + promotion.banner.title + "\" />";
	  },

	  set_dynamic_banner: function (promotion) {
		if (!promotion.banner.width || !promotion.banner.height) { return; }
	    var flashvars = false;
		var params = {
	      menu: "false",
	      wmode: "transparent",
	      flashvars: this.set_flash_vars(promotion)
		};
		var attributes = {};
        swfobject.embedSWF(promotion.banner.href, "sitewide_promo_banner", promotion.banner.width, promotion.banner.height, "9.0.0", null, flashvars, params, attributes);
	  },

	  set_flash_links: function (promotion) {
		if (!promotion || !promotion.link || !promotion.link.href) { return; }
		if (typeof promotion.link.href == "string") {
		  promotion.link.href = [ promotion.link.href ];
		}
		var values = Fn.collect(promotion.link.href, function(href, i) { return encodeURIComponent(href); });
		var keys = Fn.collect(values, function(href, i) { return "link" + i; });
		return Fn.collect(Fn.zip(keys, values), function(value, index) { return value.join("="); }).join("&");
	  },

	  set_flash_parameters: function (promotion) {
	    if (!promotion || !promotion.banner || !promotion.banner.flashvars) { return; }
	    var values = Fn.collect(promotion.banner.flashvars, function(flashvar,i) { return encodeURIComponent(flashvar.value); });
	    var keys = Fn.collect(promotion.banner.flashvars, function(flashvar,i) { return encodeURIComponent(flashvar.name); });
	    return Fn.collect(Fn.zip(keys, values), function(value, index) { return value.join("="); }).join("&");
	  },

	  set_flash_vars: function (promotion) {
        var links = this.set_flash_links(promotion);
        var parameters = this.set_flash_parameters(promotion);
        if (links && parameters) { return links + "&" + parameters; }
        else if (links) { return links; }
        else { return parameters; }
	  },

	  set_banner: function (el, promotion) {
		if (!el || !promotion || !promotion.banner || !promotion.banner.href) { return; }
		if ((/\.swf$/i).test(promotion.banner.href)) { this.set_dynamic_banner(promotion); }
		else { this.set_static_banner(el, promotion); }
	  },

	  set_banner_height: function (promotion) {
		if (!promotion || !promotion.banner || !promotion.banner.height) { return; }
		this.height = promotion.banner.height;
	  },

	  set_backgroundColor: function (el, promotion) {
		if (!el || !promotion || !promotion.banner || !promotion.banner.backgroundColor) { return; }
		el.style.backgroundColor = promotion.banner.backgroundColor;
	  },

	  set_promo_id: function (promotion) {
	    if (!promotion && !promotion.id) { return; }
	    this.promoId = promotion.id;
	  },

	  set_expiration: function (promotion) {
		if (!promotion && !promotion.expiration) { return; }
	    this.expiration = promotion.expiration;
	  },

	  open: function () {
	    App.show('sitewide_promo');
      var animation = new YAHOO.util.Anim('sitewide_promo', {
        height: { from: 0, to: App.SitewidePromos.height }
      }, 1, YAHOO.util.Easing.easeOut);

      animation.onComplete.subscribe(function () {
        Dom.get('sitewide_promo_toggle_img').src = App.SitewidePromos.buttons.minus;
          App.SitewidePromos.closed("false", App.SitewidePromos.promoId);
      });

      animation.animate();
	  },

		show: function () {
			this.open();
		},

	  close: function () {
	    var animation = new YAHOO.util.Anim('sitewide_promo', {
        height: { from: App.SitewidePromos.height, to: 0 }
      }, 1, YAHOO.util.Easing.easeIn);

      animation.onComplete.subscribe(function () {
        Dom.get('sitewide_promo_toggle_img').src = App.SitewidePromos.buttons.plus;
        App.hide('sitewide_promo');
          App.SitewidePromos.closed("true", App.SitewidePromos.promoId);
      });

      animation.animate();
	  },

		hide: function () {
      this.close();
    },

		toggle: function () {
			App.is_hidden('sitewide_promo') ? App.SitewidePromos.show() : App.SitewidePromos.hide();
		},

	  midnight: function () {
	    var dt = new Date();
	    dt.setHours(23);
	    dt.setMinutes(59);
	    dt.setSeconds(59);
	    return dt;
	  },

	  should_expire_when: function () {
	    if (App.SitewidePromos.expiration && App.SitewidePromos.expiration > 0) {
	      var dt = new Date();
	      dt.setMinutes(dt.getMinutes() + App.SitewidePromos.expiration);
	      return dt;
	    } else {
	      return this.midnight();
	    }
	  },

	  closed: function (value, promoId) {
	    YAHOO.util.Cookie.setSub('sitewide_promo_closed', promoId, value, {
	      path: "/",
	      expires: this.should_expire_when(),
	      domain: ".ncl.com"
	    });
	  },

	  should_be_closed: function (promoId) {
	    if (Dom.get("home")) { return true; }
	    var cookie_value = YAHOO.util.Cookie.getSub('sitewide_promo_closed', promoId);
	    if (null == cookie_value) { return false; }
	    return ("true" == cookie_value);
	  }
	});

	YAHOO.namespace('YAHOO.util.App.TabViewMixin');
  YAHOO.util.App.TabViewMixin = {
    tabs: function () {
      return this.get("tabs");
    },

    length: function () {
      return this.tabs().length;
    },

    active_index: function () {
      return this.get("activeIndex");
    },

    active_tab: function () {
      return this.get("activeTab");
    },

    active_content: function () {
      return this.active_tab().get("contentEl");
    },

    next_content: function () {
      return this.next_tab().get("contentEl");
    },

    first_tab: function () {
      return this.getTab(0);
    },

    last_tab: function () {
      return this.getTab(this.length() - 1);
    }
  };

	App.register_component("YAHOO.util.App.TabView", {
	  /* Automatically wireup any tabs on the page! */
	  initialize: function () {
	    this.widgets = {};
	    var tabsets = Fn.reject(Dom.getElementsByClassName('yui-navset', 'DIV', 'contentbody'), function (set) {
	      return Dom.hasClass(set, 'disabled');
	    });
	    Fn.each(tabsets, function (item) {
        try {
          YAHOO.util.App.TabView.widgets[item.id] = new YAHOO.widget.TabView(item);
          YAHOO.lang.augmentObject(YAHOO.util.App.TabView.widgets[item.id], YAHOO.util.App.TabViewMixin);
          YAHOO.util.App.TabView.selectFromUrl(YAHOO.util.App.TabView.widgets[item.id]);
        }
	      catch (e) {
	        App.log("Failed to create tabview");
	      }
	    });
	  },

	  /*
	    Returns a reference to a TabView instance that is indexed by the element id
	  */

	  find: function (id) {
	    return this.widgets[id];
	  },

	  /* Helper for subscribing to a TabView's events */
	  subscribe: function(id, event_name, block) {
	    this.find(id).addListener(event_name, block);
	  },

		/*
		 * Source: http://blog.davglass.com/files/yui/tab7/
		 *
		 * example:
		 *
		 * http://www.ncl.com/nclweb/destination/details.html?destinationCode=ALASKA#stories
		 *
		 * would set the stories tab as active if the stories tab's 'a href' were defined as follows:
		 *
		 * <li id="tabStories"><a href="#stories">Stories</a></li>
		 *
		 * It does nothing if no '#' value is specified.
		 */
		selectFromUrl: function(tab_view) {
	    if (-1 == Fn.indexOf(document.location.href, '#')) {return; }
	    var url = location.href.split('#');
	    if (!url[1]) { return; }

	    var tabHash = url[1];
      var tabs = tab_view.get('tabs');
      for (var i = 0; i < tabs.length; i++) {
        if (tabs[i].get('href') == '#' + tabHash) {
          tab_view.set('activeIndex', i);
          break;
        }
      }
		}
	});

	YAHOO.util.App.TabView.on = YAHOO.util.App.TabView.addListener = YAHOO.util.App.TabView.subscribe;

	//Hide social-network icons for 'Learn&Connect' tab
	$j.HideIcons = {
		initialize: function() {
			$j('#tab_learn_connect').click(function(){
		    	$j(".social-network-icons").css("display","none");
			});
		    $j('#tab_sitemap').click(function(){
		    	$j(".social-network-icons").css("display","block");
			})
		}
	}

	// ViewPort Resizing Functionalities.
  $j.Resize = {
	initialize: function () {
		$j.Resize.resizeit();
  		$j(window).resize(function(){
  			$j.Resize.ResizeIt();
  		})
  	},
  resizeit: function(){
		var width = parseInt($j(window).width());
		if ( width < 950 ) {
			$j('#nav-main-menu').width(970);
			$j('#content-container').width(970);
			$j('#footer-container').width(970);
		}
	  }
  }

	// Site Search Function Definition
  $j.SiteSearch = {
  	initialize: function () {
  	  var input = $j.SiteSearch.element().get(0);
  	  input.form.action = App.r2.base_url() + '/search';

  	  if ($j("#site_search").length == 0) { return; }
        $j.SiteSearch.element().focus(function() {
  	    $j.SiteSearch.select();
  	  });

  	  $j.SiteSearch.element().click(function() {
  	    if (!$j.SiteSearch.has_data()) {
  		  $j.SiteSearch.clear();
  		}
  	  });

  	  $j.SiteSearch.element().blur(function() {
  	    if (!$j.SiteSearch.has_data()) {
  		  $j.SiteSearch.reset();
  		}
  	  });

  	  $j.SiteSearch.reset();
  	  $j.SiteSearch.blur();

      var ac_url = App.r2.base_url() + "/search_complete";

      $j.SiteSearch.element().autocomplete(ac_url, {
        minChars: 3,
        max: 10,
        scrollHeight: 220,
        selectFirst: false,
        dataType: 'jsonp'
      });
  	},

  	is_blank: function () {
  	 return (0 == $j.SiteSearch.value().length);
  	},

  	element: function () {
  	 return $j("#site_search_query");
  	},

  	value: function () {
  	 return $j.SiteSearch.element().val();
  	},

  	has_changed: function () {
  	 return !(/^Search the entire site for[\.]{3}/i).test($j.SiteSearch.value());
  	},

  	has_data: function () {
  	 return (!$j.SiteSearch.is_blank() && $j.SiteSearch.has_changed());
  	},

  	blur: function () {
  	 $j.SiteSearch.element().blur();
  	},

  	select: function () {
  	 $j.SiteSearch.element().select();
  	},

  	reset: function () {
  	 $j.SiteSearch.element().val("Search the entire site for...");
  	},

  	clear: function () {
  	 $j.SiteSearch.element().val("");
  	},

  	submit: function () {
  	 if (!$j.SiteSearch.has_data()) { return; }
  	 $j("#site_search").submit();
  	}
  };


  /* Footer Tab Component API */
  App.register_component("YAHOO.util.App.FooterTabs", {
	  initialize: function () {
	    App.FooterTabs.activate(App.FooterTabs.first());
	    Evt.on(App.FooterTabs.all(), 'click', App.FooterTabs.toggle);
	  },

    components: function (el) {
      return el.id.match(/tab_(.+)/);
    },

    deactivate: function (el) {
      Dom.removeClass(App.FooterTabs.components(el), 'active');
    },

    activate: function(el) {
      Dom.addClass(App.FooterTabs.components(el), 'active');
    },

    is_active: function (el) {
      return Dom.hasClass(el, 'active');
    },

    all: function (el) {
      if (App.FooterTabs.elements === "object") { return App.FooterTabs.elements; }
      App.FooterTabs.elements = Dom.getElementsByClassName('tab', 'LI', 'footer_tabs');
      return App.FooterTabs.elements;
    },

    first: function (el) {
      return App.FooterTabs.all()[0];
    },

    find_targets: function () {
      var tabs = Fn.partition(App.FooterTabs.all(), function (el) {
        return App.FooterTabs.is_active(el);
      });
      return { previous: tabs.matches[0], next: tabs.rejects[0] };
    },

    toggle: function(e) {
      Evt.stopEvent(e);
      var el = Evt.getTarget(e);

      if (App.FooterTabs.is_active(el)) { return; }
      var tabs = App.FooterTabs.find_targets();

      App.FooterTabs.deactivate(tabs.previous);
      App.FooterTabs.activate(tabs.next);
    }
	});

  /* Call to Action */
  YAHOO.lang.augmentObject(YAHOO.namespace("YAHOO.util.App.CallToAction"), {
	  Defaults: {
	    modal: true,
	    fixedcenter: true,
	    visible: false
	  },

	  initialize: function (el, panel) {
	    App.log("Initializing CallToAction");
	    Evt.on(el, 'click', function (e) {
	      Evt.stopEvent(e);
	      var modal = new YAHOO.widget.Panel(panel, YAHOO.util.App.CallToAction.Defaults);
        modal.render();
        YAHOO.util.App.CallToAction.reset_display(panel);
	      Dom.get('cta_frame').src = App.CallToAction.callback_url();
	      modal.show();
	    });
	  },

	  callback_url: function () {
  		return App.r2.base_url() + '/help/leads?nclweb_refer=YES';
	  },

	  reset_display: function (el) {
		document.domain="ncl.com";
	    Dom.setStyle(el, "display", "block");
	    //App.log("Showing the cta iframe....");
	    App.show("cta_frame");
	  }
  });

	App.register_component("YAHOO.util.App.AutoModal", {
	  initialize: function () {
      this.init_auto_signup();
      this.init_auto_callback();
	  this.init_auto_legacy_modal();
	  },

	  init_auto_signup: function () {
	    Dom.getElementsByClassName('signup_modal', 'A', 'container', function (link) {
	      App.log("Attaching signup modal");
	      Evt.on(link, 'click', function (e) {
	        open_signup_window(e);
	      });
	    });
	  },

	  init_auto_callback: function () {
	    Dom.getElementsByClassName('callback_modal', 'A', 'container', function (link) {
	      App.log("Attaching callback modal");
	      Evt.on(link, 'click', function (e) {
	        open_callback_window(e);
	      });
	    });
	  },

	init_auto_legacy_modal: function () {
		Dom.getElementsByClassName('legacy_modal', 'A', 'container', function (link) {
	      App.log("Attaching legacy modal");
	      Evt.on(link, 'click', function (e) {
	        open_legacy_window(e);
	      });
	    });
      }
	});

	App.register_component('YAHOO.util.App.Menu', {
	  initialize: function () {
	    var menu = new YAHOO.widget.MenuBar("menu", {
        autosubmenudisplay: true, lazyload: false,
  			showdelay: 0, hidedelay: 0, zindex: 20
      });
      menu.render();
  		Dom.getElementsByClassName('yuimenu', 'DIV', 'menu', function (el) {
  			Dom.setStyle(el, 'display', 'block');
  		});
	  }
	});

	App.register_component('YAHOO.util.App.Alerts', {
	  initialize: function () {
      $j('#user_alerts').qtip({
	      content: {
	        prerender: !YAHOO.util.App.IntCookie.has_updated_alerts(),
	        text: '',
	        title: { 'text': ' ' }
	      },
	      show: { delay: 1, when: { event: 'click' }},
	      hide: { delay: 0, when: { event: 'click' }},
	      position: {
	        target: $j('#user_alerts'),
	        corner: { 'target': 'bottomRight', 'tooltip': 'topRight' },
	        adjust: { x: -1, y: 0}
	      },
	      style: {
	        tip: false,
	        width: 425,
	        color: 'black',
	        title: { 'background-color': 'transparent' },
	        border: { width: 4, color: '#81C8E8' },
	        classes: {
	          title: 'alerts_title',
	          content: 'alerts_content',
	          tooltip: 'alerts_modal'
	        }
	      },
	      api: {
	        beforeContentUpdate: YAHOO.util.App.Alerts.before_content_update,
	        beforeHide: YAHOO.util.App.Alerts.before_hide,
	        beforeShow: YAHOO.util.App.Alerts.before_show
	      }
	    });
    },

    before_hide: function () {
      $j('.alerts_modal').css('display', 'none');
      $j('#user_alerts').css({'background-color': '#1C4865', 'color': '#81C8E8'});
      return true;
    },

    before_show: function () {
      $j('.alerts_modal').css('display', 'inline');
      $j('#user_alerts').css({'background-color': '#81C8E8', 'color': 'white'});
      return true;
    },

    before_content_update: function () {
      $j.getJSON(
        YAHOO.util.App.Alerts.alerts_url(),
        YAHOO.util.App.Alerts.process_json
      );
      return '<span class="load">Loading&#8230;</span>';
    },

    alerts_url: function () {
      return App.r2.base_url() +
        '/alerts/json?format=json&jsoncallback=?';
    },

    process_json: function (data) {
      YAHOO.util.App.IntCookie.update_alerts(data.length);
      var results = YAHOO.util.App.Alerts.create_results(data);
      $j('.alerts_content').html(results);
    },

    create_results: function (data) {
      var list = document.createElement('ul');
      if (0 == data.length) {
        var item = document.createElement('li');
        $j(item).html('There are currently no alerts.');
        $j(list).append(item);
      }
      else {
        _.each(data, function (alert) {
          var item = document.createElement('li');
          $j(item).attr('id', 'alert-' + alert.nid);
          $j(list).append(item);
          $j(item).addClass(alert.node_type);
          $j(item).addClass(alert.node_data_field_alert_type_field_alert_type_value);

          var hd = document.createElement('h6');
          $j(hd).html(alert.node_title);
          $j(item).append(hd);

          var p = document.createElement('p');
          $j(p).html(alert.node_revisions_body);
          $j(item).append(p);

          if (alert.node_data_field_link_field_link_url) {
            var absolute = (/^http/).test(alert.node_data_field_link_field_link_url);
            var prefix = absolute ? "" : App.r2.base_url();
            $j(item).append($j('<a/>').attr('href', prefix + alert.node_data_field_link_field_link_url).html('View More'));
          }
        });
      }
      return list;
    }
	});

  /* Initialize all Ajax components */
  Evt.onDOMReady(function (e) {
	document.domain="ncl.com";
    App.init_components();
    App.CallToAction.initialize("cta_link", "cta_container");
    App.IntCookie.initialize();
    $j.SiteSearch.initialize();
    $j.Resize.initialize();
    $j.HideIcons.initialize();
  });


  App.log("Application Initialized");
	YAHOO.register("application", App, {version: "1.2", build: "0"});

