(function($) {
  $.facebox = function(data) {
    $.facebox.init()
    $.facebox.loading()
    $.isFunction(data) ? data.call() : $.facebox.reveal(data)
  }

  $.facebox.settings = {
    loading_image : '/images/template/loading.gif',
    close_image   : '/images/template/closelabel.gif',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    facebox_html  : '\
	  <div id="facebox" style="display:none;"> \
		<div class="popup"> \
		  <div class="body"> \
		  	<div class="header"> \
				<div class="leftCorner"></div> \
				<div class="middle"></div> \
				<a href="#" class="close" title="Close">Close</a> \
			</div> \
		    <div class="content"> \
		    </div> \
			<div class="footer"> \
				<div class="leftCorner"></div> \
				<div class="rightCorner"></div> \
				<div class="middle"></div> \
			</div> \
			<div class="clearing"></div> \
		  </div> \
		</div> \
	  </div>'
  }
  
  /*<div class="footer"> \
	  <a href="#" class="close"> \
		<img src="" title="close" class="close_image" /> \
	  </a> \
	</div> \
	*/	

  $.facebox.loading = function() {
    if ($('#facebox .loading').length == 1) return true

    $('#facebox .content').empty()
	
	$('#overlay').remove(); /*TD inserted to remove overlay if there was one*/
	
	if(jQuery.browser.msie && jQuery.browser.version == 6.0) {
		$('select').hide();
	}
	
	$('body').append('<div id="overlay"></div>')
	$('#overlay').css("opacity","0.5");
	
	var arrayPageSize = $.facebox.getPageSize();
	$('#overlay').css('height',arrayPageSize[1]+10);
    $('#facebox .body').children().hide().end().
      append('<div class="loading"><img src="'+$.facebox.settings.loading_image+'"/></div>')

    var pageScroll = $.facebox.getPageScroll()
    $('#facebox').css({
      top:	pageScroll[1] + ($.facebox.getPageHeight() / 10),
      left:	pageScroll[0]
    }).show()

    $(document).bind('keydown.facebox', function(e) {
      if (e.keyCode == 27) $.facebox.close()
    });
    
    /*$('#overlay, #facebox .popup').bind('click',function(c) {
    	//$('#facebox .body').unbind('click',c);
    	$.facebox.close();
    });
    $('#facebox .body').bind('click',function(cl) {
    	return false;
    });*/
  }

  $.facebox.reveal = function(data, klass, boxType, boxWidth, boxCaption, windowWidth) {
    if (klass) $('#facebox .content').addClass(klass)
	if(boxType == 'image') {
		boxWidth = boxWidth + 58;
		$('#facebox .body').css('width',boxWidth);
	} else {
		if(windowWidth != '') {
			windowWidth = parseInt(windowWidth) + 58;
			$('#facebox .body').css('width',windowWidth);
		} else {
			$('#facebox .body').css('width',710);
		}
	}
	
	//$('#facebox .content').empty();
	//$('#facebox .content').append(data);
	$('#facebox .content').html(data);
	if(boxCaption != '') {
		$('#facebox .content').append('<p class="caption">'+boxCaption+'</p>');
	}
    $('#facebox .loading').remove()
    $('#facebox .body').children().fadeIn('normal')
	
	var arrayPageSize = $.facebox.getPageSize();
	if(jQuery.browser.msie) {
		$('#overlay').css('height',arrayPageSize[1]);
	}
	
	
	/* Temporarily hiding this - was causing problems with the modal window
	$('a[rel*=lightbox]').facebox(); //7/3/2008 Trevor inserted to call modalbox from another modalbox
	jQuery.each($('a[rel*=lightbox]'),function() {
		var curUrl = $(this).attr('href');
		if(curUrl.match('modal=true') == '') {
			if(curUrl.indexOf('?') != -1) {
				$(this).attr('href',curUrl+'&modal=true');
			} else {
				$(this).attr('href',curUrl+'?modal=true');	
			}
		}
	});*/
	
	/* 8/5/2008 Trevor inserted for portfolio navigation */
	$('ul#screenshotNav a.current').css('opacity',0.5);
	$('ul#screenshotNav a').click(function() {
		var curChild = $(this).text();
		var curElem = $(this);
		
		$('#screenshots img.current').fadeOut('fast',function() {
			$(this).removeClass('current');
			$('ul#screenshotNav a.current').css('opacity',1.0).removeClass('current');
			$('#screenshots img:nth-child('+curChild+')').fadeIn('normal').addClass('current');
			$(curElem).addClass('current').css('opacity',0.5);
		});
		return false;
	});
  } /*END PORTFOLIO MODAL WINDOW*/

  $.facebox.close = function() {
    $(document).unbind('keydown.facebox')
    $('#facebox').fadeOut(function() {
      $('#facebox .content').removeClass().addClass('content');
	  $('#facebox .body').css('width',100);
	  $('#overlay').remove();
	  $('#facebox .caption').remove();
	  $('#facebox .content').empty();
	  
	  if(jQuery.browser.msie && jQuery.browser.version == 6.0) {
		$('select').show();
	  }
	  
    })
    return false
  }

  $.fn.facebox = function() {
    $.facebox.init()

    var image_types = $.facebox.settings.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')

    function click_handler() {
      $.facebox.loading(true)

      // support for rel="facebox[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/facebox\[\.(\w+)\]/)
      if (klass) klass = klass[1]

		var queryString = $(this).attr('href').replace(/^[^\?]+\??/,'');
		var params = $.facebox.parseQuery(queryString);
		var windowWidth = params['width'];

      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0]
        var target = this.href.replace(url,'');
		var linkTitle = this.title;
        $.facebox.reveal($(target).clone().show(), klass, 'text', '', linkTitle, windowWidth)

      // image
      } else if (this.href.match(image_types)) {
        var image = new Image()
		var linkTitle = this.title;
        image.onload = function() {
          $.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass, 'image', image.width, linkTitle, windowWidth)
        }
        image.src = this.href

      // ajax
      } else {
		var linkTitle = this.title;
		
		
        $.get(this.href, function(data) { $.facebox.reveal(data, klass, 'text', '', linkTitle, windowWidth) })
      }

      return false
    }

    this.click(click_handler)
    return this
  }

  $.facebox.init = function() {
    if ($.facebox.settings.inited) {
      return true
    } else {
      $.facebox.settings.inited = true
    }

    $('body').append($.facebox.settings.facebox_html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.facebox.settings.close_image
    preload[1].src = $.facebox.settings.loading_image

    $('#facebox .close').click($.facebox.close)
	$('#overlay').click($.facebox.close);
    $('#facebox .close_image').attr('src', $.facebox.settings.close_image)
  }
  
  $.facebox.parseQuery = function(query) {
	   var Params = {};
	   if ( ! query ) {return Params;}// return empty object
	   var Pairs = query.split(/[;&]/);
	   for ( var i = 0; i < Pairs.length; i++ ) {
		  var KeyVal = Pairs[i].split('=');
		  if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
		  var key = unescape( KeyVal[0] );
		  var val = unescape( KeyVal[1] );
		  val = val.replace(/\+/g, ' ');
		  Params[key] = val;
	   }
	   return Params;
	}

  // getPageScroll() by quirksmode.com
  $.facebox.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;	
    }
    return new Array(xScroll,yScroll) 
  }

  // adapter from getPageSize() by quirksmode.com
  $.facebox.getPageHeight = function() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }
  
  $.facebox.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;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
	}
	
})(jQuery);