  GalleryViewer = function(id, options) {
    this.id = '#' + id;
    this.galleries;
    this.max;
    this.cIndex;
    this.cGallery;
    
    if (options != undefined) {
      jQuery.extend(this, options);
      this.init();
    }
  };
  
  GalleryViewer.prototype = {
    updateCurrentImage: function(img) {
      if (img.image == undefined) {
        img.image = jQuery('<img/>').attr('src', img.url + '360.240.jpg');
      }
      
      jQuery(this.id + ' .gallery_image').attr('src', img.image.attr('src'));
      jQuery(this.id + ' .gallery_image_caption').text(img.caption);
    },
    
    updateCurrentGallery: function(thumb) {
      var index = thumb.attr('rel');
      if (index >= 0 && index < this.max) {
        this.removeSelected();
        this.cIndex = index;
        this.cGallery = this.galleries[this.cIndex];
      
        jQuery(this.id + ' .gallery_title').text(this.cGallery.title);
        jQuery(this.id + ' .gallery_headline').text(this.cGallery.headline);
        jQuery(this.id + ' .gallery_text').html(this.cGallery.text);
        thumb.addClass('selected');
      
        if (this.cGallery.images.length > 0) {
          this.updateCurrentImage(this.cGallery.images[0]);
        }
      
        var gIndexe = jQuery(this.id + ' .gallery_index').empty();
        for (i = 0; i < this.cGallery.images.length; i++) {
          if (i > 0) {
            gIndexe.append('&nbsp;|&nbsp;');
          }
          
          var current = this;
          gIndexe.append(
            jQuery('<a/>').text(i + 1).attr('href', '#').attr('rel', i).click(function(e) {
              e.preventDefault();
              current.updateCurrentImage(current.cGallery.images[jQuery(this).attr('rel')]);
            })
          );
        }
      }
    },

    removeSelected: function() {
      jQuery(this.id + ' .thumb').removeClass('selected');
    },
    
    init: function() {
      this.max = Math.min(this.max, this.galleries.length);
      
      var current = this;
      jQuery(this.id + ' .thumb').click(function(e) {
        e.preventDefault();
        current.updateCurrentGallery(jQuery(this));
      });

      if (this.galleries.length > 0) {
        this.updateCurrentGallery(jQuery(this.id + ' .thumb[rel=0]'));
      }
    }
  };

(function($) {
  if ($.template) {
	  $.template('menu', '{{if children}}<ul>{{tmpl(children) "menu-item"}}</ul>{{/if}}');
	  $.template('menu-item', '<li><a href="${href}" {{if popup}}class="popup"{{/if}} rel="${href}">${title}</a>{{tmpl "menu"}}</li>');
	  $.template('simple-link', '<a href="${url}">${text}</a>');
  }

  $.fn.menu = function(config) {
	var timeouts = new Array();
    var timeout = 0;
    var conf = $.extend({
      delay: 200,
      url: null,
      menuClass: 'menu',
      topClass: 'top',
      submenuClass: 'submenu',
      firstClass: 'first-submenu',
      hiddenClass: 'hidden',
      hoverClass: 'hover',
      onClass: 'on'
    }, config);

    var $this = this;

    var init = function() {
      $this.addClass(conf.menuClass)
      $this.children('li').addClass(conf.topClass);
      $this.children('li:first').addClass('first');
      $this.find('ul').addClass(conf.submenuClass + ' ' + conf.hiddenClass);
      $this.children('li').children('ul').addClass(conf.firstClass);
    };

    if (conf.url) {
      $.getJSON(conf.url, function(data) {
        for (var i = 0; i < data.length; i++) {
          var link = data[i];
          var par = $('a[rel=' + link.label + ']', $this).parent();
          $.tmpl('menu', link).appendTo(par);
        }

        init();
      });
    } else {
      init();
    }

    this.delegate('li', 'mouseenter', function(e) {
      if (timeouts.length > 0) {
		do {
			clearTimeout(timeouts.pop());
		} while (timeouts.length > 0);
      }

      if (this != e.target && $(this).hasClass(conf.hoverClass)) {
        return;
      }

      var par = $(this).parent();

      // close all submenus
      par.find('.' + conf.submenuClass).addClass(conf.hiddenClass);

      // remove all hover states
      par.find('li').removeClass(conf.hoverClass);
      par.find('a').removeClass(conf.onClass);

	  if ($.browser.msie && $(this).data('pos') !== true) {
		  $(this).children('.' + conf.submenuClass).not('.' + conf.firstClass).css({
			top: $(this).position().top
		  });
		  $(this).data('pos', true);
	  }

      // made this element hover and show its submenu
      $(this).addClass(conf.hoverClass).children('.' + conf.submenuClass).removeClass(conf.hiddenClass);
      $(this).children('a').addClass(conf.onClass);
    });

	var top = this[0];
    this.mouseout(function(e) {
      var $this = $(this);

	  var inTree = false;
	  
	  $(e.relatedTarget).parents().each(function() {
	  	inTree |= (this == top);
	  });

	  if (inTree) {
		return;
	  }
	
      //timeout =
	  timeouts.push(setTimeout(function(e) {
        // hide all submenus
        $this.find('.' + conf.submenuClass).addClass(conf.hiddenClass);

        // remove all hover states on the menu
        $this.find('.' + conf.hoverClass).removeClass(conf.hoverClass);
      }, conf.delay));
    });

    if ($.browser.msie && $.browser.version < 7) {
      this.addClass('ie6');
      
      $('.' + conf.submenuClass).bgiframe()
    }
    
    return this;
  };

  $.fn.poll = function() {
    this.each(function() {
      var $form = $(this);

      $form.delegate(':image', 'click', function(e) {
        e.preventDefault();
        var data = $form.serialize() + '&answer=' + $(this).val();
        $form.parent().load($form.attr('action'), data);
      });
    });

    return this;
  };

  $.fn.bottomNav = function(config) {
    var conf = $.extend({
      base: null
    }, config);

    this.delegate('a', 'mouseenter', function() {
      var rel = $(this).attr('rel').replace(/\//g, '+');
      if (rel) {
        var url = conf.base + '.nav_bottom_sublist.' + rel + '.json' + window.location.search;

        $.getJSON(url, function(data, status) {
          if (status === 'success') {
            $.tmpl('simple-link', data).appendTo($('#footswap').empty());
          }
        });
      }
    });

	return this;
  };

  $.fn.featureList = function() {
    var $this = this;
    
    this.delegate('.feature a, .thumbs a', 'click', function(e) {
      var $a = $(this);
      $('.feature', $this).fadeOut('fast');

      $.getJSON($a.attr('href'), function(data, status) {
        if (status !== 'success') return;
        
        $('#feature-tmpl').tmpl(data).appendTo($('.features', $this).empty());
        $('.thumbs', $this).fadeIn('fast');
      });
      
      e.preventDefault();
    });
    
    return this;
  };
})(jQuery);

jQuery(document).ready(function($) {
	$('.port .loginLink').click(function() {
		var form = $(this).next('.loginBox');
		var iw = form.outerWidth();
		var ih = form.outerHeight();
		var ww = $(window).width();
		var wh = $(window).height();
		var st = $(document).scrollTop();
		var sl = $(document).scrollLeft();
		var oh = form.parent().offset().top;
		var ol = form.parent().offset().left;
		var pos = form.position();
		var ph = form.parent().outerHeight();

		var top = 0;
		if (ih > ph) {
			top = (ih - ph) * -1;
		}

		form.css({
			top: 'auto',
			bottom:'0px'
		});

		form.stop().animate({
			height:'show', 
			opacity: 'show'}, 750, 'swing');

		return false;
	});

	$('.port .closeX').click(function() {
		$(this).parent().stop().fadeOut('fast');
		return false;
	});

	$('.b2b').each(function() {
		var b2b = $(this);
		$('.data', b2b).load('/b2b/index.jsp form input', 
			function(data, textStatus, xhr) {
				$('input:text, input:password,',b2b).each(function() {
					var name = $(this).attr('name');
					var label = '';
					if (name == 'BUSINESS_ENTITY<>loginName') {
						label = 'Account Number';
					} else if (name == 'LOGIN<>userid') {
						label = 'Username';
					} else if (name == 'LOGIN<>password') {
						label = 'Password';
					}
					$('<label for="' + name + '">' + label + '</label>').insertBefore($(this));
					$('<br class="clear"/>').insertAfter($(this));
				});
			});
	});

	/***** FAQ *****/
	$('.shim-faqs .answer_popup').hide();

	$('.shim-faqs').delegate('a', 'mouseenter', function() {
		$(this).parents('.shim-faqs').removeData('disable-leave');
		$('.answer_popup').hide();

		$(this).next()
			.css({
				width: $(this).parent().width() - 5
			})
			.show()
			.position({
				my: 'left top',
				at: 'left bottom',
				of: this,
				offset: '5'
			});
	}).delegate('a', 'mouseleave', function() {
		if (!$(this).parents('.shim-faqs').data('disable-leave')) {
			$(this).next().hide();
		}
	}).delegate('a', 'click', function(e) {
		$(this).parents('.shim-faqs').data('disable-leave', true);
		e.preventDefault();
	});
	/***** End FAQ *****/
	

	/***** Article Browser *****/
	$('.article-browser').accordion({
	    fillSpace: true,
	    event: 'mouseover'
  	});
	/***** End Article Browser *****/

	/***** Product Listing *****/
	$('.product-list').accordion({
	    event: 'mouseover'
  	});
	/***** End Product Listing *****/

	/***** Poll *****/
	$('.poll.v1').poll();
	/***** End Poll *****/

	/***** Feature List *****/
	$('.featurelist').featureList();
	/***** End Feature List *****/

	/***** Page Include *****/
	$('.page-include').each(function() {
		var url = $(this).attr('href');
		$(this).wrap('<div/>').parent().load(url);
	});
	/***** End Page Include *****/

	/***** Article AIM *****/
	$('.articlebrowse-aim').each(function() {
		$(this).parent().prev()
			.addClass("relative")
			.append($(this))
			.append($(this).children('.articlebrowse-aim-bg'));
	});
	/***** End Article AIM *****/

	/***** Content Listing *****/
	$('.contentlist').each(function() {
		var pages = new Array();
		var $this = $(this);
		var $nav = $this.next('.navigation');
		var $prev = $nav.children('.prev');
		var $next = $nav.children('.next');
		var index = -1;
		
		var loadContent = function(i) {
			index = i;
			$this.load(pages[index]);
        
			if (index == 0) $prev.hide();
			else $prev.show();
			if (index >= pages.length - 1) $next.hide();
			else $next.show();
		};

		$this.find('.item .title a').each(function(i) {
			$(this).data('index', i);
			pages.push($(this).attr('href').replace(/\.html/, '.content.-file-embeded.html') + window.location.search);
		}).click(function(e) {
			e.preventDefault();
			loadContent($(this).data('index'));
		});

		$this.children('.simple').remove();

		$prev.click(function(e) {
			if (index < 0) return;

			e.preventDefault();
			loadContent(index - 1);
		});

		$next.click(function(e) {
			if (index < 0) return;

			e.preventDefault();
			loadContent(index + 1);
		});
    });
	/***** End Content Listing *****/

	$(".accordion").accordion();

	/***** FAQ Modal *****/
	$('.faq').delegate('a.modal', 'click', function(e) {
		e.preventDefault();
		$('<div/>').load($(this).attr('href'), function(data, status) {
			if (status == 'success') {
				$(this).dialog({
					modal: true,
					width: 550,
					height: 400,
					resizable: false,
					draggable: false,
					title: $(this).find('.headline').remove().text()
				});
			}
		});
	});
	/***** End FAQ Modal *****/

	
    /***** Image *****/
    $('.media > img').click(function(e) {
      var src = $(this).attr('src').replace(/(\.[0-9]+){2}/g, '').replace(/thumbImage/, 'image');
      
      var $img = $('<img/>', {src: src}).css({
        position: 'absolute', left: -10000
      }).load(function() {

	    $(this).css({
          position: 'static', left: 'auto'
        }).dialog({
          modal: true,
          width: 'auto'
        });

      }).appendTo($('body'));

    }).css({cursor: 'pointer'});
	/***** End Image *****/
});
