nAnimDuration = 0.5;
bEditMode = false;

oThickboxHTTP = getHTTPObject( );
aThickboxItems = [ ];

function setBackground( sType, oLocation ) {
	switch ( sType ) {
		case "item": window.frames[ 'background' ].setBackground( "item", oLocation );break;
		default: window.frames[ 'background' ].setBackground( sType, oLocation );break;
	}
}

function openThickbox( ) {
	document.getElementById( "thickbox" ).style.display = "";
	document.getElementById( "blocker" ).style.display = "";
}

function closeThickbox( ) {
	document.getElementById( "thickbox" ).style.display = "none";
	document.getElementById( "blocker" ).style.display = "none";
}

function dimensionsReceived( ) {
	if ( oThickboxHTTP.readyState == 4 ) {
		var aParts = oThickboxHTTP.responseText.split( "|" );

		var sURL = aParts[ 0 ];
		var iWdth = parseInt( aParts[ 1 ] );
		var iHght = parseInt( aParts[ 2 ] );

		var iWndWdth = document.body.clientWidth - 44;
		var iWndHght = document.body.clientHeight - 44;
		if ( aThickboxItems.length > 1 ) iWndHght -= 15;

		if ( iWdth > iWndWdth || iHght > iWndHght ) {
			if ( iWdth / iHght >= iWndWdth / iWndHght ) {
				iHght = Math.round( ( iHght * iWndWdth ) / iWdth );
				iWdth = iWndWdth;
			} else {
				iWdth = Math.round( ( iWdth * iWndHght ) / iHght );
				iHght = iWndHght;
			}
		}

		if ( isNaN( iWdth ) || isNaN( iHght ) ) { iWdth = 400; iHght = 300; }

		var iBoxWdth = iWdth;
		var iBoxHght = iHght;
 
		if ( aThickboxItems.length > 1 ) {
			iBoxHght += 30;

			document.getElementById( "thickbox_index" ).innerHTML = ( iThickboxItemIndex + 1 ) + " / " + aThickboxItems.length;
			document.getElementById( "thickbox_nav" ).style.display = "";

			var iOffsetH = 5;
		} else {
			iBoxHght += 20;

			document.getElementById( "thickbox_nav" ).style.display = "none";

			var iOffsetH = 2.5;
		}

		var oThickbox = document.getElementById( "thickbox" );
		oThickbox.style.width = iBoxWdth;
		oThickbox.style.height = iBoxHght;
		oThickbox.style.marginLeft = - iBoxWdth / 2;
		oThickbox.style.marginTop = - iBoxHght / 2 - iOffsetH;

		var oMediaItem = document.getElementById( "thickbox_content" );
		oMediaItem.innerHTML = '<a href="javascript:;" onClick="this.blur(); closeThickbox();"><img src="' + sURL +'" width="' + iWdth + '" height="' + iHght + '" border="0" /></a>';

		openThickbox( );
	}
}

function nextMediaItem( ) {
	iThickboxItemIndex++;
	if ( iThickboxItemIndex >= aThickboxItems.length ) 
		iThickboxItemIndex = 0;

	displayMediaItem( iThickboxItemIndex, aThickboxItems );
}

function previousMediaItem( ) {
	iThickboxItemIndex--;
	if ( iThickboxItemIndex < 0 ) 
		iThickboxItemIndex = aThickboxItems.length - 1;

	displayMediaItem( iThickboxItemIndex, aThickboxItems );
}

function displayMediaItem( iItemIndex, aMediaItems ) {
	if ( aMediaItems == undefined ) aMediaItems = [ ];

	aThickboxItems = aMediaItems;
	iThickboxItemIndex = iItemIndex;

	for ( var i=0; i<aMediaItems.length; i++ )
		if ( typeof( aMediaItems[ i ] ) == "object" ) 
			aMediaItems[ i ] = aMediaItems[ i ].url;

	oThickboxHTTP.open( "GET", "_ssi/image_getdimensions.php?url=" + aMediaItems[ iItemIndex ], true );
	oThickboxHTTP.onreadystatechange = dimensionsReceived;		 
	oThickboxHTTP.send( null );
}

function displayPage( iId, bIgnoreHash ) {
	if ( document.getElementById( 'div_page' ).style.display == "none" ) {
		Effect.BlindDown( "div_page", { duration:nAnimDuration } );

		var oTrackFrame = window.frames.tracks;
		if ( oTrackFrame.hideTagList != undefined )
			oTrackFrame.hideTagList( );

		if ( oTrackFrame.checkMenuIcons != undefined )
			oTrackFrame.checkMenuIcons( );
		
		oTrackFrame.goBack( );	
	}

	window.hash = "#pageid=" + iId;
	window.location.hash = "pageid=" + iId;

	window.frames.page.location.replace( "content_page.php?id=" + iId );
}

function displayTracks( ) {
	if ( document.getElementById( 'div_page' ).style.display == "" )
		Effect.BlindUp( "div_page", { duration:nAnimDuration } );

	var oMenuFrame = window.frames.menu;
	if ( oMenuFrame != undefined && oMenuFrame.selectPageButton != undefined ) 
		oMenuFrame.selectPageButton( );
}


var sSizeData = "";
function isResized( ) {
	var sCurrData = document.body.offsetWidth + " " + document.body.scrollWidth + " " + document.body.clientWidth
			+ " " + document.body.offsetHeight + " " + document.body.scrollHeight + " " + document.body.clientHeight;

	if ( sSizeData != sCurrData ) {
		sSizeData = sCurrData;

		return true;
	} else return false;
}
function resizeIframe( ) {
	if ( isResized( ) ) {
		var iFrameHeight = document.body.clientHeight - 49 - 20 - 10;
		if ( bEditMode ) iFrameHeight -= 20;

		document.getElementById( 'div_page' ).style.height = ( iFrameHeight - 60 ) + "px";
		document.getElementById( 'div_tracks' ).style.height = iFrameHeight + "px";
	}
};


var hash = location.hash;
if ( hash != "" ) hash = "";
function checkHash( ) {
    if ( location.hash != hash ) {
		
		if ( location.hash == "" ) {
			window.frames.tracks.location.replace( "content_tracks.php" );
			displayTracks( );
		} else {
			var aHashParts = location.hash.substr( 1 ).split( "=" );
			var sHashType = aHashParts[ 0 ];
			var iHashId = parseInt( aHashParts[ 1 ] );

			switch ( sHashType ) {
				case "pageid":
					var oMenuFrame = window.frames.menu;
					oMenuFrame.selectPageButton( iHashId );

					break;

				case "chapterid":
					window.frames.tracks.location.replace( "content_tracks.php?type=chapter&id=" + iHashId );
					displayTracks( );

					break;

				case "itemid":
					window.frames.tracks.location.replace( "content_tracks.php?type=item&id=" + iHashId );
					displayTracks( );

					break;
			}
		}

        hash = location.hash;
    }
}

setInterval( checkHash, 100);
