var inlinePageAppended = false;
var overlayAppended = false;
var popupPhotoAppeded = false;

var mylightboxAppended = false;

function showInlinePage( loc, caption ) {
  var pos = {
    sTop : function() {
      return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
    },
    sLeft : function() {
      return window.pageXOffset || document.documentElement && document.documentElement.scrollLeft ||	document.body.scrollLeft;
    },    
    wHeight : function() { 
      return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
    },
    wWidth : function() {
        return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
    }
  };  
  
  //create overlay
  if( !overlayAppended ) {
    $('body').append( '<div id="overlay"></div>' );
    overlayAppended = true;
  }
  
  //create div
  if( !inlinePageAppended ) {
    $('body').append( '<div id="inline-page"></div>' );
    inlinePageAppended = true;
  }

  $('#overlay').css({
    	backgroundColor:	'#000',
    	opacity: 0.6
    }).fadeIn();
  
  var elHeight = $('#inline-page').height();
  var elWidth = $('#inline-page').width();
  
  var elTop  = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
  var elLeft = pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2);
  
  //add ?inlinePage=1 to loc parameters
  if( loc.indexOf( '\?' ) > 0 )
    loc+= '&inlinePage=1';
  else
    loc+= '?inlinePage=1';
  
  
  if( typeof( closeButton ) == 'undefined' )
    closeButton = '/if/popup_close.gif';
  
  var html = '<div id="inline-page-closer"><span class="title">' + caption + '</span><span class="closebtn"><a href="javascript:closeInlinePage();"><img src="' + closeButton + '" width="102" height="27" border="0" alt="close" /></a></span></div>';
  html+= '<iframe id="inline-page-frame" src="' + loc + '" frameborder="0" height="450" width="630"></iframe>';
  
  $('#inline-page').html( html );
  $('#inline-page').css( {top: elTop, left: elLeft} );
  $('#inline-page').fadeIn();
  
  return false;

}

function closeInlinePage() {
  $('#inline-page').fadeOut( "fast" );
  $('#overlay').fadeOut( "slow" );  
}

$(document).ready( function() {
  $( "a.inline-popup" ).bind( "click", function() { showInlinePage( this.href, this.title ); return false; } );
  //$( "a.popup_image" ).bind( "click", function() { showPopupPhoto( this.href ); return false; } );
  //$('a.popup_image').attr( 'rel', 'prettyOverlay' );
});


function submitOnEnter( formID, e ) {
  var keyc;
  if( window.event )
    keyc = window.event.keyCode;
  else if( e )
    keyc = e.which;
  else
    return true;
    
  if( keyc == 13 ) {
    $('#' + formID ).submit();  
  }
} 

function enlargePhoto( loc ) {
  $('#large_photo').attr( "src", loc );
  return false;
}

function showPopupPhoto( loc ) {
  createMyLightbox();
    
  //create overlay
  if( !overlayAppended ) {
    $('body').append( '<div id="overlay"></div>' );
    overlayAppended = true;
  }
  if( !popupPhotoAppeded ) {
    $('#mylightbox-content').append( '<div id="photo_popup"><img id="popup_image" /></div>' );
    popupPhotoAppeded = true;
  }
  
  //load image
  var imgPreloader = new Image();
  imgPreloader.onload = function() {
    $('#popup_image').attr('src',loc);
    
    var pos = {
      sTop : function() {
        return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
      },
      sLeft : function() {
        return window.pageXOffset || document.documentElement && document.documentElement.scrollLeft ||	document.body.scrollLeft;
      },    
      wHeight : function() { 
        return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
      },
      wWidth : function() {
          return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
      }
    };     
    
    var elWidth = imgPreloader.width;
    var elHeight = imgPreloader.height;

    var elTop  = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
    var elLeft = pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2);
    
    $('#mylightbox').css( {width: ( imgPreloader.width + 20 ), height: ( imgPreloader.height + 20 ), top: elTop, left: elLeft} );
    $('#mylightbox').fadeIn();    
  	
  	//	clear onLoad, IE behaves irratically with animated gifs otherwise
  	imgPreloader.onload = function(){};
  }
  imgPreloader.src = loc;  
  

  $('#overlay').css({
    	backgroundColor:	'#000',
    	opacity: 0.6
    }).fadeIn();
  
  
  return false;
}


function createMyLightbox() {
  if( !mylightboxAppended ) {
    $('body').append( '<div id="mylightbox"><div id="mylighbox-head"><div id="mylightbox-head-left"></div><div id="mylightbox-head-right"></div></div><div id="mylightbox-close-container"></div><div id="mylightbox-content-container"><div id="mylightbox-content"></div></div><div id="mylighbox-foot"><div id="mylightbox-foot-left"></div><div id="mylightbox-foot-right"></div></div></div></div>' );
    $('#mylightbox-close-container').append( '<div id="mylightbox-closer"><span class="closebtn"><a href="javascript:closeMyLightbox();"><img src="/if/popup_close.gif" width="102" height="27" border="0" alt="close" /></a></span></div>' );
    mylightboxAppended = true;  
  }  
}

function closeMyLightbox() {
  $('#mylightbox').fadeOut();
  $('#overlay').fadeOut();
}

function fadeToggle( what ) {
  if( $(what).is(':visible') )
    $(what).fadeOut();
  else
    $(what).fadeIn();  
}
/*
			isIE=document.all;
			isNN=!document.all&&document.getElementById;
			isN4=document.layers;

			if (isIE||isNN)
			{
				document.oncontextmenu=checkV;
			}
			else
			{
				document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);
				document.onmousedown=checkV;
			} 

			function checkV(e)
			{
				if (isN4)
				{
					if (e.which==2||e.which==3)
						{
						return false;
						}
					}
				else
				{
					return false;
				}
			}
*/