// move to separate (full screen) window
var sepWindow;

function checkIfOpen() {
	if ( !sepWindow || sepWindow.closed || !sepWindow.document || 
									!sepWindow.document.title ) {
		if ( sepWindow ) sepWindow.close();
		altMakeFullScreen();
	} else {
		sepWindow.focus();
//		window.close();
	}
}

function altMakeFullScreen() {
	window.moveTo( 0, 0 );
	window.resizeTo( screen.width, screen.height );
}

function makeFullScreen() {

	if ( navigator.userAgent.search( /Opera/ ) != -1 )
		altMakeFullScreen();

	else if ( ( sepWindow && sepWindow.closed ) ||
		( !window.menubar && !window.toolbar ) ||
		( ( window.menubar && window.menubar.visible ) || 
		( window.toolbar && window.toolbar.visible ) ) ) {

		if ( location.search.search( /[&?]sep=/ ) == -1 ) {
			var url;
	
			if ( location.search.length > 0 )
				url = location.href + "&sep=";
			else
				url = location.href + "?sep=";


			sepWindow = window.open( url, "RBMainWindow", 
						"dependent=no,width=" + screen.availWidth +
						",height=" + screen.availHeight + 
						",left=0,top=0,location=no," +
						"menubar=no,resizable=no,status=no,toolbar=no,scrollbars=yes" );

			window.setTimeout( "checkIfOpen()", 2000 );

		} else
			altMakeFullScreen();

	} else
		if ( window.outerWidth && window.outerHeight && 
				( ( window.outerWidth < screen.width ) || 
				( window.outerHeight < screen.height ) ) )
			altMakeFullScreen();
}

function refocus() {
	if ( location.search.search( /[&?]sep=/ ) == -1 ) {
		if ( sepWindow && !sepWindow.closed )
			sepWindow.focus();
		else
			makeFullScreen();
	} else {
		if ( location.search.search( /[&?]admin=?/ ) != -1 )
			location.href = "/src/admin.php";
		else
			location.href = "/src/gallery.php?cat=0";
	}
}

function anchor() {
	var s = new Array( "roy", ".bo", "tt|", "fot", "o-g", "raf", "ic.", "com" );
	var d = "";

	for ( i = 0; i < s.length; i++ )
		d = d + s[i].replace( /\|/, "@" );

	document.write( "<a href=\"mailto:" + d + "\">" + d + "</a>" );
}


var currentThumbHighlightIndex = -1;


function SingleImage() {
	this.loaded = false;
	this.image  = new Image;
	this.index  = -1;
	this.isAlt  = 0;
}

SingleImage.prototype.onload = function() {
	this.wrapper.loaded = true;

	if ( currentThumbHighlightIndex >= 0 ) {
		if ( ( currentThumbHighlightIndex == this.wrapper.index ) && ( this.wrapper.isAlt == 0 ) ) {
			img = document.getElementById( 'THUMB' + this.wrapper.index );
			if ( img && img.src && img.width && img.height )
				img.src = this.wrapper.image.src;
		}
	}
}

SingleImage.prototype.preload = function( category, index, isAlt ) {
	this.index = index;
	this.isAlt = isAlt;

	this.image.onload = SingleImage.prototype.onload;
	this.image.wrapper = this;
	this.image.src = "/src/getimage.php?cat=" + category + "&idx=" + index + 
						"&thumb=" + ( ( isAlt != 0 ) ? "y" : "Y" ) + "&img=y";
}



var Thumbs;
function ImagePreloader( category, count ) {
	this.category = category;
	this.count    = count;

	this.imagesNormal = new Array;
	this.imagesAlt    = new Array;


	var indexPart = location.search.match( /[&?]idx=[0-9]+([&?]|$)/ );
	if ( indexPart && indexPart[0] ) {
		var index = indexPart[0].match( /[0-9]+/ );
		if ( index && index[0] )
			currentThumbHighlightIndex = parseInt( index );
	} else
		currentThumbHighlightIndex = 0;


	for ( var i = 0; i < count; i++ ) {
		this.preload( category, i );
	}
}

ImagePreloader.prototype.preload = function( category, index ) {
	var iNormal = new SingleImage;
	var iAlt    = new SingleImage;

	this.imagesNormal.push( iNormal );
	this.imagesAlt.push( iAlt );

	iNormal.preload( category, index, 0 );
	iAlt.preload( category, index, 1 );
}


function altThumb( image, index ) {
	if ( !Thumbs || !image )
		return;

	if ( !Thumbs.imagesAlt || ( Thumbs.imagesAlt.length <= index ) )
		return;
	if ( !Thumbs.imagesAlt[index].loaded )
		return;

	if ( !Thumbs.imagesNormal || ( Thumbs.imagesNormal.length <= index ) )
		return;
	if ( !Thumbs.imagesNormal[index].loaded )
		return;


	image.src = Thumbs.imagesAlt[index].image.src;
}

function normalThumb( image, index ) {
	if ( !Thumbs || !image )
		return;
	if ( !Thumbs.imagesNormal || ( Thumbs.imagesNormal.length <= index ) )
		return;
	if ( !Thumbs.imagesNormal[index].loaded )
		return;

	image.src = Thumbs.imagesNormal[index].image.src;
}


function imageLoad( idx, count, url ) {
	var i;
	var img;

	for ( i = 0; i < count; i++ ) {
		img = document.getElementById( 'THUMB' + i );
		if ( img.src && img.width && img.height )  {
			if ( i != idx )
				altThumb( img, i );
			else
				normalThumb( img, i );
		}
	}

	ImageView.location.href = url;
}

