/*
 *
 */
(function($) {
  $.lightbox = function(data) {
    $.isFunction(data) ? data.call() : $.lightbox.popup(null, data);
  }

  $.fn.lightbox = function(settings) {
    $.lightbox.popup($(this), settings);

    return this;
  }

  $.extend($.lightbox,
  {
    active: null,
    ie6: false,
    ie7: false,
    inited: false,
    target: null,
    target_parent: null,

    init: function() {
      if (this.inited) {
	return true;
      } else {
	this.inited = true;
      }

      if ($.browser.msie) {
	this.ie6 = (parseInt($.browser.version) <= 6);
	this.ie7 = (7<= parseInt($.browser.version));
      }

      var files = [ 'lb_overlay.png', 'lb.gif', 'loading_big_w.gif',
	'loading_big_g.gif', 'lb_close_w.gif', 'lb_close_g.gif'];
      var preload = new Array(6);

      for (var i = 0; i < 6; i++) {
	preload[i] = new Image();
	preload[i].src = '/img/' + files[i];
      }
    },

    visible: function() {
      return (0 < $('#lightbox_overlay:visible').length);
    },

    popup: function(data, settings) {
      this.settings = $.extend({
	gray: false,
	loading: false,
	modal: false,
	max_width: 1100,
	close_callback: null
      }, settings);

      if (this.ie6) {
	$('#lightbox_overlay').css({
	  'background-image': 'none',
	  'background-color': '#000000',
	  'opacity': '0.5'
	});

	var l = parseInt($("body").css("padding-left"));
	var r = parseInt($("body").css("padding-right"));
	$('#lightbox_overlay').width($("html body").width() + l + r);
      }

      $('#lightbox_overlay').show();

      if (!this.settings.modal)
	$('#lightbox_overlay').click(this.close)

      var oc = $('#lightbox .close');
      var ocn = $('#lb_content');
      var occ = ocn.parent();

      if (this.settings.gray) {
	occ.addClass('cc-g');
	oc.removeClass('close_w');
	oc.addClass('close_g');
	if (this.settings.loading) {
	  ocn.removeClass('lb_content_w lb_content_loading_w');
	  ocn.addClass('lb_content_g lb_content_loading_g');
	} else {
	  ocn.removeClass('lb_content_w lb_content_loading_w lb_content_loading_g');
	  ocn.addClass('lb_content_g');
	}
      } else {
	occ.removeClass('cc-g');
	oc.removeClass('close_g');
	oc.addClass('close_w');
	if (this.settings.loading) {
	  ocn.removeClass('lb_content_g lb_content_loading_g');
	  ocn.addClass('lb_content_w lb_content_loading_w');
	} else {
	  ocn.removeClass('lb_content_g lb_content_loading_w lb_content_loading_g');
	  ocn.addClass('lb_content_w');
	}
      }

      if (data) {
	this.target = data;
	this.target_parent = data.parent();

	data.appendTo(ocn);
	data.show();
      }

      oc.click(this.close).show();

      this.active = $('#lightbox');
      this.active.show();

      this.resizeOverlay();
      this.repositionBox();

      $(document).bind('keydown.lightbox', function(e) {
	if (e.keyCode == 27) {
	  $.lightbox.close();
	}
      });
      
      $(window).bind('resize.lightbox', function() {
	$.lightbox.resizeOverlay();
	$.lightbox.repositionBox();
      });

      if (this.ie6) {
	$(window).bind('scroll.lightbox', function() {
	  $.lightbox.repositionBox();
	});
      }
    },

    close: function() {
      var obj = ((typeof this) == "function") ? this : $.lightbox;

      if (obj.target) {
        obj.target.hide();
	obj.target.appendTo(obj.target_parent);
	obj.target = null;
      }

      $(document).unbind('keydown.lightbox');
      $(window).unbind('resize.lightbox');
      $(window).unbind('scroll.lightbox');

      $('#lb_content').width('');
      $('#lb_content').empty();
      $('#lightbox').width('');
      $('#lightbox').hide();
      $('#lightbox_overlay').hide();

      if (obj.settings.close_callback)
	obj.settings.close_callback();

      return false;
    },

    replace: function(c) {
      var ocn = $('#lb_content');

      if (this.target) {
	this.target.hide();
	this.target.appendTo(this.target_parent);
	this.target = null;
      }

      if (typeof(c) == 'object') {
        this.target = c;
        this.target_parent = c.parent();
      }
      
      ocn.empty();
      ocn.append(c);

      if (typeof(c) == 'object') {
	this.target.show();
      }

      if (this.ie7) {
	var w = ocn.children(":first").width();
	ocn.width(w);
      }

      this.repositionBox();
    },

    resizeOverlay: function() {
      var pageSize = this.getPageSize();

      $('#lightbox_overlay').css({
	height: pageSize.largestHeight
      });
    },

    repositionBox: function() {
      this.active.css('width', '');

      var w = this.active.width();
      var pageSize = this.getPageSize();
      var pageScroll = this.getPageScroll();

      for (var i = 0; i < 3; i++) {
	var m = this.active.width();

	if (this.settings.max_width < m)
	  m = this.settings.max_width;

	var t = (pageSize.windowHeight - this.active.height()) / 2.5;
	var l = (pageSize.windowWidth - m) / 2;

	if (this.ie6) {
	  t += pageScroll.yScroll;
	  l += pageScroll.xScroll;
	}

	this.active.css({
	  top: t,
	  left: l
	});

	var nw = this.active.width();

	if (this.settings.max_width < nw) {
	  this.active.css('width', this.settings.max_width + 'px');
	}
	else if (nw == w)
	  break;
      }
    },

    getPageSize: function() {
      var xScroll, yScroll;
      
      if (window.innerHeight && window.scrollMaxY) {	
	xScroll = window.innerWidth + window.scrollMaxX;
	yScroll = window.innerHeight + window.scrollMaxY;
      } else if (document.body.scrollHeight > document.body.offsetHeight){
	// all but Explorer Mac
	xScroll = document.body.scrollWidth;
	yScroll = document.body.scrollHeight;
      } else {
	// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and
	// Safari
	xScroll = document.body.offsetWidth;
	yScroll = document.body.offsetHeight;
      }
      
      var windowWidth, windowHeight;
      
      if (self.innerHeight) {
	// all except Explorer
	if(document.documentElement.clientWidth){
	  windowWidth = document.documentElement.clientWidth; 
	} else {
	  windowWidth = self.innerWidth;
	}
	windowHeight = self.innerHeight;
      } else if (document.documentElement
		 && document.documentElement.clientHeight) {
	// Explorer 6 Strict Mode
	windowWidth = document.documentElement.clientWidth;
	windowHeight = document.documentElement.clientHeight;
      } else if (document.body) {
	// other Explorers
	windowWidth = document.body.clientWidth;
	windowHeight = document.body.clientHeight;
      }	
      
      // for small pages with total height less then height of the viewport
      if(yScroll < windowHeight){
	pageHeight = windowHeight;
      } else { 
	pageHeight = yScroll;
      }
      
      // for small pages with total width less then width of the viewport
      if(xScroll < windowWidth){	
	pageWidth = xScroll;		
      } else {
	pageWidth = windowWidth;
      }
      
      var largestWidth;
      var largestHeight;
      var smallestWidth;
      var smallestHeight;

      if ( pageWidth >= windowWidth )
      {	largestWidth = pageWidth; smallestWidth = windowWidth;	}
      else
      {	largestWidth = windowWidth; smallestWidth = pageWidth;	}

      if ( pageHeight >= windowHeight )
      {	largestHeight = pageHeight; smallestHeight = windowHeight;	}
      else
      {	largestHeight = windowHeight; smallestHeight = pageHeight;	}
      
      var arrayPageSize = {
	'pageWidth' : pageWidth,
	'pageHeight' : pageHeight,
	'windowWidth' : windowWidth,
	'windowHeight' : windowHeight,
	'largestWidth' : largestWidth,
	'largestHeight' : largestHeight
      };

      return arrayPageSize;
    },
		
    getPageScroll: function () {
      var xScroll, yScroll;

      if (self.pageYOffset) {
	yScroll = self.pageYOffset;
	xScroll = self.pageXOffset;
      } else if (document.documentElement
		 && document.documentElement.scrollTop) {
	// Explorer 6 Strict
	yScroll = document.documentElement.scrollTop;
	xScroll = document.documentElement.scrollLeft;
      } else if (document.body) {// all other Explorers
	yScroll = document.body.scrollTop;
	xScroll = document.body.scrollLeft;	
      }
      var arrayPageScroll = {
	'xScroll' : xScroll,
	'yScroll' : yScroll
      };

      return arrayPageScroll;
    }
  });
})(jQuery);

jQuery(document).ready(function() {
  jQuery.lightbox.init();
});
