/* HOMEVIEW */
/* Overlay Functions
----------------------------------------------------------------------------- */


	var $j = jQuery.noConflict();
	var ie6 = (navigator.userAgent.match('MSIE 6') ? true : false);


/* TRIGGERS
----------------------------------------------------------------------------- */


	$j(document).ready(function() {

		$j(window).resize(positionPanels);

		$j('a.overlay').click(function() {
	
			var $this = $j(this);
			openOverlay($this);
			return false;
		
		});
		
	});

	
/* OPEN + POSITION OVERLAY PANELS
----------------------------------------------------------------------------- */


	function openOverlay($this) {

		if ($this.toString().match('#')) {
		
			var $this = $this;
		
		} else {
		
			var $this = $this.attr('href');
		
		}
				
		if (ie6) {
		
			$j('#overlay').css('top', document.documentElement.scrollTop + 'px');
			$j($this).css('top', document.documentElement.scrollTop + 'px');
		
		}
		
		if ($j('body').hasClass('opened')) {

			$j('div.overlay').fadeOut(function() {

				$j('body').addClass('overlay');
				positionPanels($this);
				$j($this).fadeOut();
				$j('body').removeClass('overlay');
				
			});
		
		} else {

			$j('#overlay').show().animate({ opacity: '0.48' }, 910);
			$j($this).addClass('opened').fadeIn(function() {

				$j('body').addClass('overlay');
				positionPanels($this);
	
			});
		
		}	

	}

	function positionPanels($this) {

		$j($this).children('div').children('div.panel').each(function() {
		
			var width = parseFloat($j(this).width());
			var height = parseFloat($j(this).height());

			if ($j(this).children('a.close').length == 0) {

				$j('<a class="close" href="#" title="Close">Close</a>').prependTo(this);

				function closeOverlay() {

					$j('div.overlay').fadeOut(function() {
	
						$j('body').removeClass('overlay');
	
					});	

					$j('#overlay').animate({ opacity: '0' }, 'normal', function() { $j(this).hide(); });

					return false;
				
				}

				$j(this).find('img').click(closeOverlay);
				$j(this).find('a.close').click(closeOverlay);
	
			}

		});

	}
