function removeWarning( ) {
	$('jswarning').hide();
};

function showFirstImage( p ) {
	if ( p == "contact" || p == "index" ) {
		return;	
	}
	
	var el = $(p).select('div');

	createOverlay();
	createZoomBox();
	
	var size = getPageDimensions();

	el.each( function( e ) {
		e.hide();
		if ( p == 'tearsheets' ) {
			$(e).select('a').each( function( a ) {
				var i = new Image;
				i.src = a.down('img').readAttribute('src').replace( "/thumb.php?i=", "" );
				Event.observe( a, 'click', function() {
					imageZoom( i, a.down('span').innerHTML, size );
				});
				Event.observe( a, 'mouseover', function() {
		  			imageHover( a );
				});
				Event.observe( a, 'mouseout', function() {
		  			imageHoverCancel( a );
				});			
			});			
		} else {			
			if ( size[3] <= 768 ) {
				$(e, e.down('img')).invoke('setStyle', 'max-height: 460px');
			}			
		}

	});
	
	$(p).down('div').show();	
};

function doPrevious( p ) {
	var entries = $(p).select('div');
	
	var n = entries.indexOf( entries.find( function( e ) {
		return $(e).visible() == true;
	}) );

	Effect.Fade($(entries[n]), { duration: 0.8 });
	
	if ( entries[n] != entries.first() ) {
		Effect.Appear($(entries[n-1]), { duration: 0.8 });
	} else {
		Effect.Appear($(entries.last()), { duration: 0.8 });
	}
};

function doNext( p ) {
	var entries = $(p).select('div');
	
	var n = entries.indexOf( entries.find( function( e ) {
		return $(e).visible() == true;
	}) );

	Effect.Fade($(entries[n]), { duration: 0.8 });
	
	if ( entries[n] != entries.last() ) {
		Effect.Appear($(entries[n+1]), { duration: 0.8 });
	} else {
		Effect.Appear($(entries.first()), { duration: 0.8 });
	}
};

function imageHover( e ) {
	e.down('img').setOpacity(0.85);
};

function imageHoverCancel( e ) {
	e.down('img').setOpacity(1.0);
};

function imageZoom ( img, caption, size ) {
	$('overlay').show();
	
	if ( !img.complete ) {
		waitForImage(img, caption, size);
	} else {
		var w = ( size[3] <= 768 ) ? img.width * 0.75  : img.width;
		var h = ( size[3] <= 768 ) ? img.height * 0.75 : img.height;
		var l = ( size[2] - 20 - w) / 2;	
		
		$('zoombox').down('img').writeAttribute( { src: img.src, title: 'click to close' } );
		$('zoombox').down('img').setStyle( { width: w + 'px', height: h + 'px' } );
		$('zoombox').setStyle( { width: w + 'px', top: '20px', left: l + 'px' } );
		$('zoombox').down('p').update( caption.replace( / \/ /g, "<br /><br />" ) );
		$('zoombox').show();
	}
};

function createOverlay( ) {
	var e = new Element('div', { 'id': 'overlay' });
	
	e.setStyle({
		display: 'none',
		position: 'fixed',
		top: '0',
		left: '0',
		zIndex: '9',
		width: '100%',
		height: '100%',
		backgroundImage: 'url(/images/overlay.png)'
	});
	
	Event.observe( e, 'click', function() {
		removeOverlay();
	});
	
	Element.insert( $('wrap'), { before : e } );
};

function createZoomBox( ) {
	var e = new Element('div', { 'id': 'zoombox' });
	
	e.setStyle({
		display: 'none',
		position: 'absolute',
		zIndex: '100',
		backgroundColor: '#eee',
		padding: '5px 10px 5px 10px',
		cursor: 'pointer'
	});
	
	Event.observe( e, 'click', function() {
		removeOverlay();
	});
	
	e.update('<span style="float: right; margin-bottom: 5px;"><a href="#" onclick="removeOverlay(); return false;" class="close-x">(x)</a></span><img src="" alt="eriktextorius.com" /><p></p><span style="float:right"><a href="#" onclick="removeOverlay(); return false;">Close</a></span>');
	
	Element.insert( $('overlay'), { after : e } );
}

function removeOverlay( ) {
	$('overlay').hide();
	$('zoombox').hide();
}

function getPageDimensions(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array( pageWidth, pageHeight, windowWidth, windowHeight ); 
	return arrayPageSize;
}

function waitForImage( img, caption, size ) {
	if( !img.complete ) {
		setTimeout( function () { waitForImage( img, caption, size ); }, 50 );
	} else {
		var w = ( size[3] <= 768 ) ? img.width * 0.75  : img.width;
		var h = ( size[3] <= 768 ) ? img.height * 0.75 : img.height;
		var l = ( size[2] - 20 - w) / 2;	
		
		$('zoombox').down('img').setStyle( { width: w + 'px', height: h + 'px' } );
		$('zoombox').setStyle( { width: w + 'px', top: '20px', left: l + 'px' } );
		$('zoombox').down('img').writeAttribute( { src: img.src, title: 'click to close' } );
		$('zoombox').down('p').update( caption.replace( / \/ /g, "<br /><br />" ) );
		$('zoombox').show();
	}
}