  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]'));
      }
    }
  };

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));
				});
			});
	});
});