ImageSet = function( dirName, imgName, dimy, dimx, key, ordinal) {
	this.dirName = dirName;
	this.imgName = imgName;
	this.dimy = dimy;
	this.dimx = dimx;
	this.path = this.dirName + this.imgName;
	if(!isNull(key ) ){
		this.key = key;
	}
	if(!isNull( ordinal ) ){
		this.ordinal = ordinal;
	}
//	this.preload = IMG({ 'src' : this.path , 'class' : 'invisible'} );
}

ImageManipulator = {}

im = ImageManipulator;

update( ImageManipulator, {
	

	init : function(  ){
	//	log( repr( __images__ ) );
		var self = this;
		this.buildControlBox();
		
		
		appendChildNodes( 'imgwrap', IMG( { 'src' : this.__images__[0].path, 'id' : 'excelsior'} ) );
		
		appear( 'excelsior', { duration : .4 } );
		this.preloadImage( this.__images__[1].path );
		this.preloadImage( this.__images__[this.__images__.length - 1].path );
		

/*		
		YOU NEED TO LEARN HOW TO PROGRAM IN A WAY THAT DOESN'T MAKE SAFARI BREAK.
		Why always 'Object (result of expression x) does not allow calls.'?
		
		x often == func.apply
		
		it's something mochikit does for me in firefox but not in safari.
		
*/		
	//	alert( typeof( self.flipleft ) + "\n" + typeof( self.flipright) + "\n" + typeof(self.slideshow) );
		connect( 'flipleft', "onmousedown", ImageManipulator, 'flipleft' );
		connect( 'flipright', "onmousedown", ImageManipulator, 'flipright' );
		connect( 'slideshow', "onmousedown", ImageManipulator, 'slideshow' );

		
	},
	
	detect : function(){
		var detect = navigator.userAgent.toLowerCase();
		this.ie = false;
		this.safari = false;
		if ( detect.indexOf( "msie" ) != -1){
			this.ie = true;
		}
		if( detect.indexOf( 'safari') != -1 ){
			this.safari = true;
 		}
	},
	
	slideshow : function( e ){
		var self = im;
		addElementClass( 'slideshow', 'playing');
		connect( 'slideshow', "onmouseout", function(){
			addElementClass( this, 'off' );
		});
		connect( 'slideshow', "onmouseover", function(){
			removeElementClass( this, 'off');
		});
		
		self.flipright();
		self.slideshowInt = setInterval( ImageManipulator.flipright, 3500 );
		
		disconnectAll( 'slideshow', "onmousedown" );
		connect( 'slideshow', "onmousedown", function(){ 
			clearInterval( self.slideshowInt );
			removeElementClass( 'slideshow', 'playing' );
			removeElementClass( 'slideshow', 'off' );
			disconnectAll( 'slideshow' );

			self.buildControlBox();
			connect( 'slideshow', "onmousedown", self.slideshow );
		});
	},
	
	flipleft : function( e ){
		var self = this;
		var currentImage = $('excelsior');//self.firstChildElement( 'imgwrap' );
		var imageIndex = parseInt(currentImage.src.match( /(\d+)\.jpg$/)[1], 10);
		log("index: " + imageIndex );
		self.index = parseInt( imageIndex, 10 );
		
		fade( 'imgwrap', { duration : 0.6, afterFinish : function(){
			var self = im;
				removeElement( 'excelsior' );
				var plate = IMG( { 'src' : self.__images__[self.decrementIndex()].path, 'id' : 'excelsior' } );
				appendChildNodes( 'imgwrap', plate );
				appear( 'imgwrap', { duration : 0.1 } );
				self.preloadImage( self.__images__[self.decrementIndex(true)].path );
				if(im.ie ){
					setStyle( 'excelsior', { 'visibility' : 'visible'} );
				}
			}
		});
	},
	
	flipright : function( e ){
		var self = this;
		var currentImage = $('excelsior');//self.firstChildElement( 'imgwrap' );
		var imageIndex = parseInt(currentImage.src.match( /(\d+)\.jpg$/)[1], 10);
		
		
		log( 'image: ' + currentImage.src + '   index: ' + currentImage.src.match( /(\d+)\.jpg$/)[1] );
		log("index: " + imageIndex );
		self.index = parseInt( imageIndex, 10 );
		
		fade( 'imgwrap', { duration : 0.6, afterFinish : function(){
				removeElement( 'excelsior' );
				var self = im;
				log( 'self: ' + typeof(self) );
				log('init: ' + typeof(self.init));
				log( 'self.incrementIndex(): ' + typeof(self.incrementIndex));
				log( self.incrementIndex( true ));
				var plate = IMG( { 'src' : self.__images__[self.incrementIndex()].path, 'id' : 'excelsior' } );
				appendChildNodes( 'imgwrap', plate );
		
				appear( 'imgwrap', { duration : 0.1 } );
				self.preloadImage( self.__images__[self.incrementIndex(true)].path );
				if(im.ie ){
					setStyle( 'excelsior', { 'visibility' : 'visible'} );
				}
			}
		});
	},
	
	insertImage : function( ){
		//var opposite = id == 'leftpage' ? 'rightpage' : 'leftpage';
		var plate = IMG( { 'src' : this.__images__[this.decrementIndex()].path } );
		//updateNodeAttributes( 'rightpage', { 'id' : 'leftpage' } );
		insertSiblingNodesAfter( 'leftpage', DIV( { 'id' : 'rightpage', 'style' : 'display:none;'}, plate ) );
		var imgHeight = self.__images__[self.index].dimy;
		log( "new image dims: " + imgHeight );
		if( imgHeight < 350 ){
			setStyle( plate, { 'margin-top' : ( 350 - imgHeight ) / 2 + 'px' } );
		}
	},
	
	incrementIndex : function( byVal ){
		log("index before: " + this.index );
		
		if( !byVal ){
			if( this.index == this.__images__.length - 1 ){
			
				this.index = 0;
			} else {
				this.index++;
			}
		} else {
			
			if( this.index == this.__images__.length - 1 ){
				log("index unchanged returning 0");
				return 0;
			} else {
				var x = parseInt( this.index, 10 );
				x++;
				log("index unchanged returning " + x);
				return parseInt( this.index, 10 ) + 1;
				
			}
		}
		log( "index after: " + this.index );
		return this.index;
	},
	decrementIndex : function( byVal ){
		log("index before DEcrement: " + this.index );
		if(! byVal ){
			if( this.index == 0){
				this.index = this.__images__.length - 1;
			} else {
				this.index--;
			}
		} else {
			if( this.index == 0){
					log("DEC index unchanged returning " +  (this.__images__.length -1));
				return this.__images__.length -1;
			} else {
				log("DEC index unchenged returning " +  (this.index -1));
				return this.index -1;
			}
		} 
		log( "index after DEcrement: " + this.index );
		return this.index;
	},

	
	leftPad : function( n ){
		return n < 10 ? '0' + n : n;
	},
	
	elementNode : function( el ) {
        return el.nodeType == 1;
    },
    
    firstChildElement : function( el ){
		if(typeof( el ) == 'string'){
			el = $(el);
		}
        return filter( this.elementNode, el.childNodes )[0];
    },
	
	preloadImage : function( file ){
		log( 'PRELOADING: ' + file );
		var img = IMG( { 'src' : file, 'class' : 'invisible' } );
	},
	/*
	
		move this to the HTML
	*/
	buildControlBox : function(){

		
		var buttons = getElementsByTagAndClassName( 'div', 'navbutton');
		forEach( buttons, function( button ){
			disconnectAll( button, "onmouseover" );
			disconnectAll( button, "onmouseout" );
			connect( button, "onmouseover", function(){ addElementClass( this, "moused" ); });
			connect( button, "onmouseout", function(){ removeElementClass( this, "moused"); });
		});
	},
	popup : function( page, pageTitle ){
        window.open( page, pageTitle, "width=400, height=400, toobar=no, scrollbars=yes, resizable=yes");
    },
	
	closeWin : function(){
		window.close();
	}
});
