/*======================================================================
 * 
 * Synthetic Pictures
 *
 * Copyright (C) 2006,  Ben Schwartz
 *                      benschw@gmail.com
 *                      http://www.benhschwartz.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *=====================================================================*/

function makeRequest(id, page) {

	if(window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		request = new ActiveXObject("MSXML2.XMLHTTP");
	}
	var url = '/thumbs.php?id=' + id + '&page=' + page;

	sendRequest(url);
}

function sendRequest(url) {

	request.onreadystatechange = onResponse;
	request.open("POST", url, true);
	request.send(null);
}

function checkReadyState(obj) {

	if(obj.readyState == 0) { document.getElementById('directorthumbs').innerHTML = '<br /><div align="center">Requesting Videos...</div>'; }
	if(obj.readyState == 1) { document.getElementById('directorthumbs').innerHTML = '<br /><div align="center">Loading Videos...</div>'; }
	if(obj.readyState == 2) { document.getElementById('directorthumbs').innerHTML = '<br /><div align="center">Videos Loaded...</div>'; }
	if(obj.readyState == 3) { document.getElementById('directorthumbs').innerHTML = '<br /><div align="center">Videos Ready...</div>'; }
	if(obj.readyState == 4)
	{
		if(obj.status == 200) {
			return true;
		} else if(obj.status == 404) {
			// trouble retrieving xml file... treat as unknown error 
			document.getElementById('directorthumbs').innerHTML = "Error. Please try Reloading the page.";
		} else {
			// other error
			document.getElementById('directorthumbs').innerHTML = "Error. Please try Reloading the page.";
		}
	}
}

function onResponse() {
	
	if( checkReadyState( request ) ) {
		
		var response = request.responseXML.documentElement;

		var xmlData = new Array();
		
		// get id of director, current page number, next page number (wraps to "1")
		var curPage	= response.getElementsByTagName( 'curpage' )[0].firstChild.data;
		var id   	= response.getElementsByTagName( 'directorid' )[0].firstChild.data;
		var next 	= response.getElementsByTagName( 'nextpage' )[0].firstChild.data;

		// draw to page next (">") link
		if( next != curPage )
			document.getElementById('nnavnext').innerHTML = '<a href="#" onclick="changePics(\'' + id + '\', \'' + next + '\');">></a>';

		// reset thumb block
		document.getElementById('directorthumbs').innerHTML = '';

		// populate thumb block, linking thumbs to videos.php with params: video id, director id, current page
		for( i = 0; i < response.getElementsByTagName( 'item' ).length; i++ ) {
			
			id  	= response.getElementsByTagName( 'item' )[i].getElementsByTagName( 'id'    )[0].firstChild.data;
			img 	= response.getElementsByTagName( 'item' )[i].getElementsByTagName( 'img'   )[0].firstChild.data;
			title = response.getElementsByTagName( 'item' )[i].getElementsByTagName( 'title' )[0].firstChild.data;
			
			document.getElementById('directorthumbs').innerHTML += '<a onMouseOver="thumbMouseOver( \'' + title + '\', \'' + i + '\' )" onMouseOut="thumbMouseOut( \'' + i + '\' );" href="/v/' + id + '/' + curPage + '"><img border="0" style="visibility:hidden" id="thumb' + i + '" src="/thumbs/' + img + '" /></a>';
			//document.getElementById('directorthumbs').innerHTML += '<a onMouseOver="thumbMouseOver( \'' + title + '\', \'' + i + '\' )" onMouseOut="thumbMouseOut( \'' + i + '\' );" href="videos.php?page=' + curPage + '&id=' + id + '"><img border="0" style="visibility:hidden" id="thumb' + i + '" src="thumbs/' + img + '" /></a>';
			fadeInImage('thumb' + i );
		}
	}
}

function thumbMouseOver( title, i ) {
	handle = document.getElementById( 'thumb' + i );
	setOpacity( handle, 50 );
	
	if( i <= 3 )
		margin = i * 175;
	else
		margin = (i - 4) * 175;

	document.getElementById('videopreview').style.marginLeft = margin + 'px';
	document.getElementById('videopreview').innerHTML = title;
}
function thumbMouseOut( i ) {
	handle = document.getElementById( 'thumb' + i );
	setOpacity( handle, 100 );

	document.getElementById('videopreview').innerHTML = '';
}

// entry point for director page: id == director id, page = preview page to get data for
function changePics(id, page ) {
	makeRequest( id, page);
}


function fadeInImage(imageId) {
  	image = document.getElementById( imageId );
  	setOpacity( image, 0 );
  	image.style.visibility = 'visible';
	fadeIn( imageId, 0 );
}


function fadeIn( objId, opacity ) {
	if( document.getElementById ) {
		handle = document.getElementById( objId );
		if( opacity <= 100 ) {
			setOpacity( handle, opacity );
			opacity += 10;
			window.setTimeout( "fadeIn( '" + objId + "', " + opacity + " )", 100 );
		}
	}
}

function setOpacity(obj, opacity) {
	opacity 						= (opacity == 100)?99.999:opacity;
	
	obj.style.filter 			= "alpha(opacity:"+opacity+")";	// IE/Win
	obj.style.KHTMLOpacity 	= opacity / 100;  					// Safari<1.2, Konqueror
	obj.style.MozOpacity 	= opacity / 100;  					// Older Mozilla and Firefox
	obj.style.opacity 		= opacity / 100;  					// Safari 1.2, newer Firefox and Mozilla, CSS3

}


