relTitle = true;

var    overlayColor = '#000'    ,
     overlayOpacity =  0.7      ,
	       frameColor = '#fff'    ,
	
	
	overlayHeight  = $(document).height(),
	overlayWidth   = $(document).width(),
  viewportHeight = $(window).height() / 2,
	viewportWidth  = $(window).width() / 2,
  
	modifierVar    = 0,
	
    oldRelArray = new Array(),
    newRelArray = new Array();

$('.jqOverlay[rel] a, .jqOverlay a[rel], a[rel].jqOverlay').each(function(){

var relVar = $(this).attr('rel');

oldRelArray.push(relVar);



var relVar = relVar.replace(/[^a-zA-Z 0-9]+/g,'').replace(/ /g,'-');

newRelArray.push(relVar);

});

function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++)
   {
      for(var x = 0, y = r.length; x < y; x++)
      {
         if(r[x]==a[i]) continue o;
      }
      r[r.length] = a[i];
   }
   return r;
}

newRelArray = unique(newRelArray),



oldRelArray = unique(oldRelArray);

for(var i = 0; i < newRelArray.length; i++ )

{

$('body').append('<div id="jqOverlay-' + newRelArray[i] + '" class="jqOverlay-frame"><a class="jqOverlay-close"></a></div>');

$('.jqOverlay[rel="' + oldRelArray[i] + '"] a, .jqOverlay a[rel="' + oldRelArray[i] + '"], a[rel="' + oldRelArray[i] + '"].jqOverlay').children().each(function(){

$(this).clone().appendTo( $('#jqOverlay-' + newRelArray[i] ) ).attr('title', oldRelArray[i] ).hide();

});

}

$('.jqOverlay[rel] a, .jqOverlay a[rel], a[rel].jqOverlay').click(function(e){

e.preventDefault();

var   getRel = $(this).attr('rel'),
      getSrc = $('img', this).attr('src');
	
var getRel = getRel.replace(/[^a-zA-Z 0-9]+/g,'').replace(/ /g,'-');	
	
$imgPath = $('#jqOverlay-' + getRel + ' img[src*="' + getSrc + '"]');			
		
$('.jqOverlay-background').fadeIn('slow');

$('#jqOverlay-' + getRel).css({'top':viewportHeight - frameHeight,'left':viewportWidth - frameWidth}).fadeIn('slow', function(){ 

resizeFrame();

var getCache = $(this).data('cacheSrc');

if ( getCache == getSrc ) { return; }

$('#jqOverlay-' + getRel + ' h1').remove();

$('#jqOverlay-' + getRel + ' p').remove();

var h1Var = $imgPath.attr('title');

var  pVar = $imgPath.attr('alt');

if ( h1Var !== null ) { $imgPath.before('<h1>' + h1Var + '</h1>'); }

if ( pVar !== null ) { $imgPath.after('<p>' + pVar + '</p>');	}

$('#jqOverlay-' + getRel + ' a.jqOverlay-close').fadeOut('slow');



$('img', this).animate({'opacity':0},'slow', function(){  

$(this).hide(); 

var modifierVar  = $('#jqOverlay-' + getRel + ' h1').outerHeight(true);

    modifierVar += $('#jqOverlay-' + getRel + ' p').outerHeight(true);
	
$imgPath.css({'width':'auto', 'height':'auto','opacity':0});

var imgHeight = $imgPath.height() + modifierVar,
		
     imgWidth = $imgPath.width();

if ( imgHeight > $(window).height() ) { imgHeight = ( $(window).height() - 200 ) - modifierVar; $imgPath.css({'height':imgHeight - modifierVar, 'width':'auto'}); imgWidth = $imgPath.width(); } 

if ( imgWidth  > $(window).width()  ) { imgWidth  = ( $(window).width() - 200 ) - modifierVar; $imgPath.css({'width':imgWidth, 'height':'auto'}); imgHeight = $imgPath.height() + modifierVar; }
		 
$('#jqOverlay-' + getRel).data('cacheSrc', getSrc).animate({'width':imgWidth,'height':imgHeight,'marginTop':'-'+imgHeight/2,'marginLeft':'-'+imgWidth/2},'slow', function(){

$imgPath.css({'display':'block','margin':'auto'}).animate({'opacity':1},'slow', function(){$('#jqOverlay-' + getRel + ' a.jqOverlay-close').fadeIn('slow'); 

}); 

});

});

});
	
});






$('body').append('<div class="jqOverlay-background"/>');

$('.jqOverlay-background').css({'width':overlayWidth,'height':overlayHeight,'opacity':overlayOpacity,'backgroundColor':overlayColor}).hide();

if ($.browser.msie && $.browser.version.substr(0,1) < 7) { posVar = 'absolute'; $('body').scrollTop(0); } else { posVar = 'fixed' } 

$('.jqOverlay-frame').css({'borderColor':frameColor,'backgroundColor':frameColor,'position':posVar});

var frameWidth = $('.jqOverlay-frame').width() / 2,
   frameHeight = $('.jqOverlay-frame').height() / 2;

$('.jqOverlay-frame').css({'top':viewportHeight - frameHeight,'left':viewportWidth - frameWidth}).hide();


$('.jqOverlay-frame').hover(function(){

if ( $(this).is(':animated') ) { return; }

$h1 = $('h1', this);
$p = $('p', this); 

$h1.css({'height':'auto'});

$p.css({'height':'auto'});

h1Height = $h1.outerHeight(true) + 10;

pHeight  = $p.outerHeight(true) + 10;

$h1.css({'height':0});

$p.css({'height':0});

$h1.stop().animate({'height':h1Height},'slow');

$p.stop().animate({'height':pHeight},'slow');

}, function(){

$h1.stop().animate({'height':0},'slow');

$p.stop().animate({'height':0},'slow');

})

$('.jqOverlay-frame a.jqOverlay-close, .jqOverlay-background').click(function(){

$('.jqOverlay-frame').fadeOut('slow');

$('.jqOverlay-background').fadeOut('slow');

});

function resizeFrame() {

var $frame     = $('.jqOverlay-frame:visible'),
overlayHeight  = $(document).height(),
overlayWidth   = $(document).width(),
viewportHeight = $(window).height() / 2,
viewportWidth  = $(window).width() / 2,
frameHeight    = $frame.height(),
frameWidth     = $frame.width(),
marginLeft     = parseInt ( $frame.css('margin-left') ) *-1,
marginTop      = parseInt ( $frame.css('margin-top') ) *-1;

if ( viewportHeight < marginTop + 50 ) { viewportHeight = marginTop + 50 } 		

if ( viewportWidth < marginLeft + 50 ) { viewportWidth = marginLeft + 50 }
		
$frame.css({'top':viewportHeight,'left':viewportWidth});


//if ( $(this).height() < frameHeight || $(this).width() < frameWidth ) { $frame.css({'position':'absolute'}); $(this).scrollTop(0); } else { $frame.css({'position':'fixed'}) }



$('.jqOverlay-background').css({'width':overlayWidth,'height':overlayHeight});

}

$(window).resize(function() {

resizeFrame();
		
});

//$('.jqOverlay-frame h1, .jqOverlay-frame p').css({'backgroundColor':frameColor})
