/**
 * Gallery JavaScript Class
 *
 * author:    Sebastian von Conrad (sebastian[-at-]skylinecreative.net)
 * copyright: Skyline Creative 2009
 * 
 * MAY NOT BE USED, COPIED OR MODIFIED WITHOUT EXPRESS PERMISSION FROM SKYLINE CREATIVE, LLC.
 */

// custom code for dontstopthedance

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;
}

// end custom code

var Gallery = {
	init: function( settings ) {
		Gallery.setDefaultSettings( settings );
				
		if ( Gallery.settings.thumbId ) {
			Gallery.thumbsEvents();		
		}
		
		if ( Gallery.settings.imgContId && !Gallery.allImages  ) {
			Gallery.getImagesFromContainer();		
		}
		
		if ( !Gallery.allImages ) {
			//"Gallery could not be initialized!";
			return;	
		}
		
		Gallery.startPreload();		
		
		if ( Gallery.settings.inlineId && !Gallery.iImage ) {
			Gallery.createInline();
		}
		
		if ( ( Gallery.settings.thumbClick == 'modal' || Gallery.settings.inlineClick == 'modal' ) && !Gallery.mImage ) {
			Gallery.createModal();
		}		
	},
	
	setDefaultSettings : function( settings ) {
		var defaultSettings = $H();
		
		defaultSettings.set( 'modalId',   'gallery-modal' );
		defaultSettings.set( 'inlineId',  '' );
		defaultSettings.set( 'thumbId',   '' );
		defaultSettings.set( 'imgContId', '' );
		defaultSettings.set( 'overlayId', 'overlay' );
		
		defaultSettings.set( 'thumbsPath', '' );
		defaultSettings.set( 'modalPath',  '' );
		defaultSettings.set( 'inlinePath', '' );
		
		defaultSettings.set( 'thumbClick',  null );
		defaultSettings.set( 'inlineClick', null );
		defaultSettings.set( 'thumbOver',   null );
		defaultSettings.set( 'thumbOut',    null );
		defaultSettings.set( 'inlineOver',  null );
		defaultSettings.set( 'inlineOut',   null );
		
		defaultSettings.set( 'draggableModal', true );
		
		defaultSettings.set( 'captionUsage', null );
		defaultSettings.set( 'captionType', 'alt' );
		
		defaultSettings.set( 'closeUsage',      null );
		defaultSettings.set( 'closeText',       '[x]' );
		defaultSettings.set( 'closeTextTop',    '' );
		defaultSettings.set( 'closeTextBottom', '' );
		defaultSettings.set( 'closeCss',        { 'cssFloat' : 'right', 'cursor' : 'pointer' } );
		defaultSettings.set( 'closeCssTop',     '' );
		defaultSettings.set( 'closeCssBottom',  '' );
		
		defaultSettings.set( 'modalCss',  { 'margin' : '25px auto', 'width' : '600px', 'height' : '600px', 'zIndex' : '100', 'backgroundColor' : '#eee', 'padding' : '10px', 'cursor' : 'move' }  );
		defaultSettings.set( 'overlayCss', { 'display' : 'none', 'position' : 'fixed', 'top' : '0', 'left' : '0', 'zIndex' : '99', 'width' : '100%', 'height' : '100%', 'backgroundImage' : 'url(images/overlay.png)', 'cursor' : 'pointer' } );
		
		defaultSettings.each( function( s ) {
			if ( settings[s.key] == undefined ) {
				settings[s.key] = s.value;			
			}
		});
		
		Gallery.settings = settings;
	},
	
	thumbsEvents : function( ) {
		if ( $( Gallery.settings.thumbId ) == null ) {
			return;		
		}
		
		Gallery.allImages = new Array;
		
		$( Gallery.settings.thumbId ).select( 'a' ).each( function( link ) {
			Gallery.allImages.push( link );
			
			if ( Gallery.settings.thumbOver ) {
				Event.observe( link, 'mouseover', function() {
					Gallery.settings.thumbOver( link );
				});
			}
			
			if ( Gallery.settings.thumbOut ) {
				Event.observe( link, 'mouseout', function() {
					Gallery.settings.thumbOut( link );
				});
			}
			
			switch( Gallery.settings.thumbClick ) {
				case null    :
					return;
					break;
				case 'inline':
					Event.observe( link, 'click', function( event ) {
						Gallery.updateInline( link );
						Event.stop( event );
					});
					break;
				case 'modal' :
				default      :
					Event.observe( link, 'click', function( event ) {
						Gallery.updateModal( link );
						Event.stop( event );
					});
					break;
			}
			
		});
	},
	
	startPreload : function() {
		Gallery.image = $H();
		
		Gallery.allImages.each( function( element ) {
			if ( element.tagName == 'IMG' ) {
				var imageName = element.readAttribute( 'src' ).match( /[^\/]*$/i );
			} else {
				var imageName = element.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
			}
			
			if ( Gallery.settings.modalPath ) {
				Gallery.preload( Gallery.settings.modalPath + imageName );
			}
		 	
		 	if ( Gallery.settings.inlinePath && ( Gallery.settings.inlinePath != Gallery.settings.modalPath ) ) {
 				Gallery.preload( Gallery.settings.inlinePath + imageName );
		 	}
		});
	},
	
	preload : function( imagePath ) {
		Gallery.image[imagePath] = new Image;
		Gallery.image[imagePath].src = imagePath;
	},
	
	getImagesFromContainer : function() {
		if ( !$( Gallery.settings.imgContId ) ) {
			return;		
		}
		
		Gallery.allImages = new Array;

		$( Gallery.settings.imgContId ).select( 'img' ).each( function( image ) {
			Gallery.allImages.push( image );
		});
	},
	
	createModal  : function() {
		var overlayId   = Gallery.settings.overlayId;
		Gallery.overlay = new Element( 'div', { 'id' : overlayId } );
		
		var modalId     = Gallery.settings.modalId;
		Gallery.modal   = new Element( 'div', { 'id' : modalId } );
		
		var imageId     = modalId + "-image";
		Gallery.mImage  = new Element( 'img', { 'id' : imageId } );
		
		var contId      = imageId + "-container"
		Gallery.mCont   = new Element( 'div', { 'id' : contId } );
		
		Gallery.overlay.setStyle( Gallery.settings.overlayCss );
		Gallery.modal.setStyle( Gallery.settings.modalCss );
		Gallery.mImage.setStyle( { margin: '0' } );
		
		$$( 'body' ).first().insert( { top : Gallery.overlay } )
		
		Gallery.overlay.insert( { top : Gallery.modal  } );
		Gallery.modal.insert  ( { top : Gallery.mCont  } );
		Gallery.mCont.insert  ( { top : Gallery.mImage } );
		
		if ( Gallery.settings.draggableModal ) {
			new Draggable( modalId, { scroll : window, starteffect : null, endeffect : null } );		
		}		
		
		Event.observe( Gallery.overlay, 'click', function( event ) {
			Gallery.hideModal();
			Event.stop( event );
		});
		
		Event.observe( Gallery.modal, 'click', function( event ) {
			Event.stop( event );
		});
		
		if ( Gallery.settings.captionUsage == 'all' || Gallery.settings.captionUsage == 'modal' ) {
			var captionId = modalId + '-image-caption';
			Gallery.mImage.caption = new Element( 'span', { 'id' : captionId } );
			//Gallery.mImage.caption.setStyle( { cursor : 'text' } );
			Gallery.mCont.insert( { bottom : Gallery.mImage.caption } );
		}
		
		if ( Gallery.settings.closeUsage ) {
			var closeId = modalId + '-image-close';
			
			switch( Gallery.settings.closeUsage ) {
				case 'all' :
					Gallery.mImage.closeTop = new Element( 'span', { 'id' : closeId } );
					Gallery.mImage.closeTop.setStyle( Gallery.settings.closeCss ); 
					Gallery.mImage.closeTop.update( '<a href="#" onclick="Gallery.hideModal(); return false;">' + ( Gallery.settings.closeTextTop ? Gallery.settings.closeTextTop : Gallery.settings.closeText ) + '</a>' );
					Gallery.mImage.closeBot = new Element( 'span', { 'id' : closeId + '-bottom' } );
					Gallery.mImage.closeBot.setStyle( Gallery.settings.closeCss ); 
					Gallery.mImage.closeBot.update( '<a href="#" onclick="Gallery.hideModal(); return false;">' + ( Gallery.settings.closeTextBottom ? Gallery.settings.closeTextBottom : Gallery.settings.closeText ) + '</a>' );
					Gallery.mCont.insert( { top : Gallery.mImage.closeTop } );
					Gallery.mCont.insert( { bottom : Gallery.mImage.closeBot } );
					break;
				case 'top' :
					Gallery.mImage.close = new Element( 'span', { 'id' : closeId } );
					Gallery.mImage.close.setStyle( Gallery.settings.closeCss ); 
					Gallery.mImage.close.update( '<a href="#" onclick="Gallery.hideModal(); return false;">' + ( Gallery.settings.closeTextTop ? Gallery.settings.closeTextTop : Gallery.settings.closeText ) + '</a>' );
					Gallery.mCont.insert( { top : Gallery.mImage.close } );	
					break;
				case 'bottom' :
					Gallery.mImage.close = new Element( 'span', { 'id' : closeId } );
					Gallery.mImage.close.setStyle( Gallery.settings.closeCss ); 
					Gallery.mImage.close.update( '<a href="#" onclick="Gallery.hideModal(); return false;">' + ( Gallery.settings.closeTextBottom ? Gallery.settings.closeTextBottom : Gallery.settings.closeText ) + '</a>' );
					Gallery.mCont.insert( { bottom : Gallery.mImage.close } );
					break;			
			}			
		}
	},
	
	hideModal  : function() {
		Gallery.overlay.hide();
		
		Gallery.mImage.writeAttribute( { 'src' : '', 'width' : '0', 'height' : '0' } );
		
		if ( Gallery.mImage.caption ) {
			Gallery.mImage.caption.update();
		}
		
	},
	
	updateModal  : function( element ) {
		function waitForImage() {
			if( !Gallery.image[imagePath].complete ) {
				setTimeout( function () { waitForImage(); }, 50 );
			} else {
				showImage();
			}
		}
		
		function showImage() {
			Gallery.mImage.writeAttribute( { 'src' : imagePath, 'alt' : imageAlt, 'width' : Gallery.image[imagePath].width, 'height' : Gallery.image[imagePath].height } );
			
			Gallery.mCont.setStyle( { 'width' : Gallery.image[imagePath].width + "px" } );
			
			Gallery.mCont.style.visibility = 'hidden';
			Gallery.mCont.show();
			
			if ( Gallery.mImage.caption ) {
				Gallery.mImage.caption.update( '<br />' + imageCaption );
			}
			
			Gallery.mCont.hide();
			Gallery.mCont.style.visibility = 'visible';
			
			new Effect.Morph( Gallery.modal, {
				style: 'width:' + Gallery.mCont.getWidth() + 'px; height:' + Gallery.mCont.getHeight() + 'px;',
				duration: 0.4,
				queue: 'front'
			});
			
			new Effect.Appear( Gallery.mCont, {
				duration: 0.6,
				queue: 'end'
			});
		}
		
		Gallery.mCont.hide();
		Gallery.overlay.show();
		
		if ( element.tagName == 'IMG' ) {
			var imageName = element.readAttribute( 'src' ).match( /[^\/]*$/i );
			var imageAlt  = element.readAttribute( 'alt' );		
		} else {
			var imageName = element.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
			var imageAlt  = element.down( 'img' ).readAttribute( 'alt' );
		}
		
		var imagePath = Gallery.settings.modalPath + imageName;
		
		if ( !Gallery.image[imagePath] ) {
			Gallery.image[imagePath]  = new Image;
			Gallery.image[imagePath].src  = imagePath;		
		}
		
		if ( Gallery.mImage.caption ) {
			switch ( Gallery.settings.captionType ) {
				case 'alt' :
					var imageCaption = element.down( 'img' ).readAttribute( 'alt' );
					break;
				case 'span':
					if ( element.down( '.caption' ) ) {
						var imageCaption = element.down( '.caption' ).innerHTML;				
					} else {
						var imageCaption = element.next( '.caption' ).innerHTML;				
					}
					break;
				default :
					var imageCaption = '';
					break;
			}
		}
		
		if ( !Gallery.image[imagePath].complete ) {
			waitForImage();
		} else {
			showImage();
		}
	},
	
	createInline : function() {
		Gallery.inline = $( Gallery.settings.inlineId )
		
		// custom code for dontstopthedance
		Gallery.iLink  = false; // Gallery.inline.down( 'a' );
		// end custom code
		if ( Gallery.iLink ) {
			// there's a link in the div, which contains our image
			Gallery.iImage  = Gallery.iLink.down( 'img' );
			
			if ( Gallery.settings.inlineClick ) {
				switch ( Gallery.settings.inlineClick ) {
					case 'modal':
						Event.observe( Gallery.iLink, 'click', function( event ) {
							Gallery.updateModal( Gallery.iLink );
							Event.stop( event );
						});
						break;
					case 'next'	:
						Event.observe( Gallery.iLink, 'click', function( event ) {
							Gallery.nextInline( Gallery.iLink );
							Event.stop( event );
						});
						break;
				}
			}
			
			if ( Gallery.settings.inlineOver ) {
				Event.observe( Gallery.iLink, 'mouseover', function() {
					Gallery.settings.inlineOver( Gallery.iLink );
				});
			}
			
			if ( Gallery.settings.inlineOut ) {
				Event.observe( Gallery.iLink, 'mouseout', function() {
					Gallery.settings.inlineOut( Gallery.iLink );
				});
			}	
		} else {
			// no link, no events
			Gallery.iImage  = Gallery.inline.down( 'img' );

			// custom code for dontstopthedance
			var size = getPageDimensions();
			
			if ( size[3] <= 800 ) {
				var w = Gallery.image[Gallery.iImage.getAttribute( 'src' )].width;
				var h = Gallery.image[Gallery.iImage.getAttribute( 'src' )].height;
				
				while ( h >= 450 ) {
					w = w * 0.95;
					h = h * 0.95;		
				}
				
				Gallery.iImage.writeAttribute( { 'width' : w, 'height' : h } );
				$( 'photoinfo' ).setStyle( { 'width' : ( w + 26 ) + "px" } );
			}
			
                        $('photodiv').show(); 
			// end custom code
		}
		
		if ( Gallery.settings.captionUsage == 'all' || Gallery.settings.captionUsage == 'inline' ) {
			Gallery.iImage.caption = Gallery.inline.down( '.caption' );
		} else if ( Gallery.settings.captionUsage == 'modal' && Gallery.settings.captionType == 'span' ) {
			var captionId = Gallery.settings.inlineId + '-image-caption';
		 	
		 	if ( $( captionId ) ) {
		 		Gallery.iImage.caption = $( captionId );
		 	} else {
		 		Gallery.iImage.caption = new Element( 'span', { 'id' : captionId, 'class' : 'caption' } );
				Gallery.iImage.caption.hide();
				Gallery.iImage.insert( { after : Gallery.iImage.caption } );		 	
		 	}			
		}
	},
	
	nextInline : function( element ) {
		var currentImageName = element.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
		var currentImage;
		
		if ( Gallery.allImages ) {
			Gallery.allImages.each( function( image ) {
				if ( image.tagName == 'IMG' ) {
					var imageName = image.readAttribute( 'src' ).match( /[^\/]*$/i );
				} else {
					var imageName = image.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
				}				
				
				if ( imageName.toString() == currentImageName.toString() ) {
					currentImage = image;
					throw $break;
				}
			});
			
			if ( currentImage == Gallery.allImages.last() ) {
				var nextImage = Gallery.allImages.first();
			} else {
				var nextImage = currentImage.next( currentImage.tagName );			
			}
		}		
				
		Gallery.updateInline( nextImage );
	},
	
	previousInline : function( element ) {
		var currentImageName = element.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
		var currentImage;
		
		if ( Gallery.allImages ) {
			Gallery.allImages.each( function( image ) {
				if ( image.tagName == 'IMG' ) {
					var imageName = image.readAttribute( 'src' ).match( /[^\/]*$/i );
				} else {
					var imageName = image.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
				}				
				
				if ( imageName.toString() == currentImageName.toString() ) {
					currentImage = image;
					throw $break;
				}
			});
			
			if ( currentImage == Gallery.allImages.first() ) {
				var previousImage = Gallery.allImages.last();
			} else {
				var previousImage = currentImage.previous( currentImage.tagName );			
			}
		}
			
		Gallery.updateInline( previousImage );
	},
	
	updateInline : function( element ) {
		function waitForImage() {
			if( !Gallery.image[imagePath].complete ) {
				setTimeout( function () { waitForImage(); }, 50 );
			} else {
				showImage();
			};
		}
		
		function showImage() {
			new Effect.Fade( Gallery.inline, {
				duration: 0.6,
				queue: { 'position': 'front', 'scope': 'updateInline' },
				afterFinish: function() { 
					// custom code for dontstopthedance
					var size = getPageDimensions();
					
					if ( size[3] <= 800 ) {
						var w = Gallery.image[imagePath].width;
						var h = Gallery.image[imagePath].height;
						
						while ( h >= 450 ) {
							w = w * 0.95;
							h = h * 0.95;		
						}
						
						Gallery.iImage.writeAttribute( { 'src' : imagePath, 'alt' : imageAlt, 'width' : w, 'height' : h } );
						$( 'photoinfo' ).setStyle( { 'width' : ( w + 26 ) + "px" } );
					} else {
						Gallery.iImage.writeAttribute( { 'src' : imagePath, 'alt' : imageAlt, 'width' : Gallery.image[imagePath].width, 'height' : Gallery.image[imagePath].height } );
						$( 'photoinfo' ).setStyle( { 'width' : ( Gallery.image[imagePath].width + 26 ) + "px" } );
					}				
					
					var number;
					
					Gallery.allImages.each( function( ccImage ) {
						var ccImageName = ccImage.readAttribute( 'src' ).match( /[^\/]*$/i );
						
						if ( imageName.toString() == ccImageName.toString() ) {
							number = Gallery.allImages.indexOf( ccImage ) + 1;						
						}
					});
					
					$( 'image-count' ).update( number );
					// end custom code
					
					if ( Gallery.iImage.caption ) {
						Gallery.iImage.caption.update( imageCaption );
					}
					
					newInline.update( Gallery.inline.innerHTML );
				}
			});
			
			new Effect.Appear( newInline, {
				duration: 0.6,
				queue: { 'position': 'end', 'scope': 'updateInline' },
				afterFinish: function() { 
					Effect.Queues.get( 'updateInline' ).each( function( effect ) { effect.cancel(); });

					Gallery.inline.remove();
					newInline.writeAttribute( { 'id' : Gallery.settings.inlineId } );
					
					// custom code for dontstopthedance
					Event.observe('arrow-previous', 'click', function( event ) {
						Gallery.previousInline( $('photodiv') );
						event.stop();						
					} );
			
					Event.observe('arrow-next', 'click', function( event) {
						Gallery.nextInline( $('photodiv') );
						event.stop();
					} );

					//if ( Gallery.allImages.first().readAttribute( 'src' ).match( /[^\/]*$/i ).toString() == imageName.toString() ) {
						if ( $('desc') ) {
                                                        $('desc').setStyle({ 'width' : $('photo').getWidth() / 2.3 + 'px' });
                                                        $('desc').setStyle({ 'height' : $('photo').getHeight() - 35 + 'px' });

                                                        Event.observe('photo', 'mouseover', function() {
								$('desc').setStyle({ 'visibility' : 'visible' });
							} );

							Event.observe('photo', 'mouseout', function() {
								$('desc').setStyle({ 'visibility' : 'hidden' });
							} );

							Event.observe('desc', 'mouseover', function() {
								$('desc').setStyle({ 'visibility' : 'visible' });
							} );

							Event.observe('desc', 'mouseout', function() {
								$('desc').setStyle({ 'visibility' : 'hidden' });
							} );
						}
							
					//}
					// end custom code
					
					Gallery.createInline();
				}
			});	
		}
		
		var newInline  = new Element( 'div' );
		
		if ( element.tagName == 'IMG' ) {
			var imageName = element.readAttribute( 'src' ).match( /[^\/]*$/i );
			var imageAlt  = element.readAttribute( 'alt' );		
		} else {
			var imageName = element.down( 'img' ).readAttribute( 'src' ).match( /[^\/]*$/i );
			var imageAlt  = element.down( 'img' ).readAttribute( 'alt' );
		}
		
		var imagePath = Gallery.settings.inlinePath + imageName;
						
		if ( Gallery.iImage.caption ) {
			switch ( Gallery.settings.captionType ) {
				case 'alt' :
					if ( element.tagName == 'IMG' ) {
						var imageCaption = element.readAttribute( 'alt' );
					} else {
						var imageCaption = element.down( 'img' ).readAttribute( 'alt' );
					}
					break;
				case 'span':
					if ( element.tagName == 'IMG' ) {
						var imageCaption = element.next( '.caption' ).innerHTML;
					} else {
						var imageCaption = element.down( '.caption' ).innerHTML;
					}					
					break;
				default :
					var imageCaption = '';
					break;
			}
		}
		
		if ( !Gallery.image[imagePath] ) {
			Gallery.image[imagePath]  = new Image;
			Gallery.image[imagePath].src  = imagePath;		
		}
		
		newInline.hide();
		newInline.clonePosition( Gallery.inline, { 'setWidth' : false, 'setHeight' : false } );
		
		Gallery.inline.insert( { before : newInline } );
		
		if ( !Gallery.image[imagePath].complete ) {
			waitForImage();		
		} else {
			showImage();
		}	
	}	
}