///////////////////////////////////////////////////////
// xWinLib Gallery, written by SM Repetti
// (c) 2008 by SM Repetti, All Rights Reserved
//

// initialize xWinLib
xlib.init( XINIT_ALL );

var xGallery;
var GALLERY_THUMBNAIL	= 1;

function pageLoad( ) {
	xGallery = new xGalleryObj( );
	
	xGallery.imgSize = { width: 640, height: 480 };
	xGallery.thumbSize = { width: 180, height: 135 };

	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/YellowDayLily.jpg", "Yellow Day Lily..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/TigerSwallowtail-1.jpg", "Tiger Swallowtail..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/RainLily.jpg", "Rain Lily..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/BlackSwallowtail-3.jpg", "Black Swallowtail..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/Canna.jpg", "Canna..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/BlackSwallowtail.jpg", "Black Swallowtail 2..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/YellowRose.jpg", "Yellow Rose..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/Tortoise.jpg", "Tortoise..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/Stinger.jpg", "Stinger..." );
	xGallery.addpic( "http://s253.photobucket.com/albums/hh51/RockinGrandma/Flower%20Garden/Mushroom.jpg", "Mushroom..." );

	xGalleryDisplay( );
}

function xGalleryDisplay( layout ) {
	switch ( layout ) {
	  case  1: xGallery.colWidth = 7; xGallery.thumbSize = { width: 90, height: 68 };   break;
	  case  2: xGallery.colWidth = 1; xGallery.thumbSize = { width: 50, height: 38 };   break;
	  case  3: xGallery.colWidth = 2; xGallery.thumbSize = { width: 400, height: 300 }; break;
	  default: xGallery.colWidth = 4; xGallery.thumbSize = { width: 180, height: 135 };
	}
	xGallery.display( GALLERY_THUMBNAIL );
}

function xGalleryObj( imgSize, thumbSize ) {
	this.imgSize = imgSize;
	this.thumbSize = thumbSize;
	this.defaultPos = "";

	this.topMargin = 60;
	this.colWidth = 4;
	this.margin = 20;

	this.win;
	this.onav = domObject( "navtop" );

	this.aPics = [];
	this.currentPic = 0;

	this.init( );
}

xGalleryObj.prototype = {

	init: function( ) {
		xlib.fx.bgFade( document, "FFFFFF", "000000", 100, 10 );
		xlib.fx.opacity( this.onav, 0 );
		this.onav.style.visibility = "visible";
		xlib.fx.opacityFade( this.onav, DISP_FADE_IN );
	},

	addpic: function( url, title, desc ) {
		var w= xlib.objGenerate( null, OTYPE_IMAGE, "xgal_" + this.aPics.length, 0, 0, this.imgSize.width, this.imgSize.height, { img: url, title: title, desc: desc } )
		w.style &= ~WS_NORMAL;
		w.borderColor = "white";
		w.displayMode = DISP_FADE_IN;
		w.create( );
		this.aPics[ this.aPics.length ] = [ w, url, ( title || "" ), ( desc || "" ) ];
	},

	clickHandler: function( win, e ) {
		if ( win.width >= xGallery.imgSize.width ) {
			win.position( win.origPos.left, win.origPos.top, xGallery.thumbSize.width, xGallery.thumbSize.height );
			win.style &= ~WS_MODAL;
			xlib.winModalLayer( 0 );
		}
		else {
			win.style |= WS_MODAL;
			pt = xlib.winCalcCenter( xGallery.imgSize.width, xGallery.imgSize.height );
			win.position( pt.x, pt.y, xGallery.imgSize.width, xGallery.imgSize.height );
		}
		win.display( WS_NORMAL );
	},
	
	display: function( dtype ) {
		var a, cnt, x, y, w, h, i, win, pt;

		switch ( dtype ) {
		  case GALLERY_THUMBNAIL:
		  	a = this.aPics;
		  	x = this.margin; y = this.topMargin; w = this.thumbSize.width; h = this.thumbSize.height;
		  	cnt = len( a );
		  	for ( i=0, col=1; i<cnt; i++, col++ ) {
				if ( col > this.colWidth ) {
					col = 1;
					x = this.margin;
					y += this.margin + h;
				}
				win = a[i][0];
				win.eClick = win.eDblClick = function( win, e ) { xGallery.clickHandler( win, e ); };
				win.origPos = { left: x, top: y };
				win.position( x, y, w, h );
				win.display( WS_NORMAL );
				x += this.margin + w;
		  	}
		  	this.defaultPos = this.positionSave( );
		  	break;
		}
	},	

	positionSave: function( ) {
		return( xlib.desktop.save( DESKTOP_LAYOUT ) );
	},

	positionRestore: function( data ) {
		if ( ! data ) data = this.defaultPos;
		return( xlib.desktop.winMove( data, MOVE_ANIMATE ) );
	},

	slideshow: function( startFlag, delay ) {
		if ( this.slideshowFunc ) xlib.intervalClear( this.slideshowFunc );
		
		if ( ! startFlag ) {
			if ( xGallery.currentPic ) {
				xGallery.clickHandler( xGallery.aPics[ xGallery.currentPic - 1 ][0], null );
			}
			xGallery.display( GALLERY_THUMBNAIL );
			return;
		}

		this.currentPic = 1;
		xGallery.aPics[0][0].eClick = function( ) { xGallery.slideshow( 0 ); };
		xGallery.clickHandler( xGallery.aPics[0][0], null );
		
		this.slideshowFunc = xlib.intervalSet( 
			function( ) {
				var win;
				
				if ( xGallery.currentPic ) {
					xGallery.clickHandler( xGallery.aPics[ xGallery.currentPic - 1 ][0], null );
				}
				xGallery.currentPic++;
				
				if ( xGallery.currentPic > xGallery.aPics.length  ) {
					xGallery.currentPic = 1;
				}
				win = xGallery.aPics[ xGallery.currentPic - 1 ][0]
				win.eClick = function( ) { xGallery.slideshow( 0 ); };
				xGallery.clickHandler( win, null );
			},
			delay
		);
	}	
}
