var http_request = false;

function makePOSTRequest( url, parameters ) {
	http_request = false;
	if ( window.XMLHttpRequest ) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType ) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType( 'text/xml' );
			http_request.overrideMimeType( 'text/html' );
		}
	} else if ( window.ActiveXObject ) { // IE
		try {
			http_request = new ActiveXObject( 'Msxml2.XMLHTTP' );
		} catch (e) {
			try {
				http_request = new ActiveXObject( 'Microsoft.XMLHTTP' );
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert( 'Cannot create XMLHTTP instance' );
		return false;
	}
      
	http_request.onreadystatechange = alertContents;
	http_request.open( 'GET', url, true );
//	http_request.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
//	http_request.setRequestHeader( 'Content-length', parameters.length );
	http_request.setRequestHeader( 'Connection', 'close' );
	http_request.send( parameters );
}

function alertContents() {
	if ( http_request.readyState == 4 ) {
		if ( http_request.status == 200 ) {
			//alert(http_request.responseText);
			pageTracker._trackEvent('REFRESH', document.domain, 'page refresh');
			result = http_request.responseText;
			document.getElementById( 'columnMain' ).innerHTML = result;
			updatePictures();
		} else {
			alert( 'There was a problem with the request.' );
		}
	}
}
   
function refresh( obj ) {
	makePOSTRequest( '/web/content.html', '' );
}

var secs = 0;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var allServers = {
        'keynote1.mac4ever.com' : '1',
        'keynote2.mac4ever.com' : '2',
        'keynote3.mac4ever.com' : '3',
        'keynote.mac4ever.org' : '4'
};
                        
var servNbr = 4;
                        
function InitializeTimer() {
	var forceReload = document.getElementById( 'forceReload' );
	if ( forceReload != null && forceReload.innerHTML == 'true' ) {
		window.location.reload( true );
	}
	 
/*	var servs = document.getElementById( 'servers' );
	if ( servs != null ) {
		var ok = eval( servs.innerHTML );
		var toHide = new Array();
		var toShow = new Array();	
		for( key in allServers ) {
			var i = 0;
			var done = false;
			while( i < ok.length && !done ) {
				if ( key == ok[i] ) {
					toShow.push( allServers[key] );
				}
				i++;
			}
			if ( !done ) {
				toHide.push( allServers[key] );
			}
		}
			 
		for( var i = 0; i < toHide.length; i++ ) {
			document.getElementById( 'ttrserv' + toHide[i] ).style.display = 'none';
			document.getElementById( 'mtrserv' + toHide[i] ).style.display = 'none';
			document.getElementById( 'btrserv' + toHide[i] ).style.display = 'none';
		}
		for( var i = 0; i < toShow.length; i++ ) {
			document.getElementById( 'ttrserv' + toShow[i] ).style.display = '';
			document.getElementById( 'mtrserv' + toShow[i] ).style.display = '';
			document.getElementById( 'btrserv' + toShow[i] ).style.display = '';
		}
	}
	
	for ( key in allServers ) {   
		if ( document.domain.indexOf( key ) != -1 ) {
			document.getElementById( 'serv' + allServers[key] ).style.visibility = 'visible';
		}	   
		else {
			document.getElementById( 'serv' + allServers[key] ).style.visibility = 'hidden';
		}
	}
*/		

	// Set the length of the timer, in seconds 
	secs = 21;
	StopTheClock();
	refresh( '' );
	StartTheTimer();
}


function StopTheClock() {
    if( timerRunning )
        clearTimeout( timerID );
    timerRunning = false;
}

function StartTheTimer() {
    if ( secs == 0 ) {
        InitializeTimer();
    }
    else {
        document.getElementById( 'displayTimer' ).innerHTML = ( secs - 1 );
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout( 'StartTheTimer()', delay );
    }
}

var availablePicts = 0;
var firstPict = 0;

var picts = null;
var links = null;
			
var nextDiv = null;
var prevDiv = null;

var nextCont = null;
var prevCont = null;
			
function initPictures() {
	picts = [
		document.getElementById( 'pict1' ),
		document.getElementById( 'pict2' ),
		document.getElementById( 'pict3' ),
		document.getElementById( 'pict4' ),
		document.getElementById( 'pict5' )
	];

	links = [
		document.getElementById( 'link1' ),
		document.getElementById( 'link2' ),
		document.getElementById( 'link3' ),
		document.getElementById( 'link4' ),
		document.getElementById( 'link5' )
	];
				
	nextCont = document.getElementById( 'contnext' );
	prevCont = document.getElementById( 'contprev' );
	
	nextDiv = document.getElementById( 'next' );
	prevDiv = document.getElementById( 'prev' );
	
	nextDiv.onmouseover = function() {
		this.className = 'nextbHover';
	};
	nextDiv.onmouseout = function() {
		this.className = 'nextb';
	};
	prevDiv.onmouseover = function() {
		this.className = 'prevbHover';
	};
	prevDiv.onmouseout = function() {
		this.className = 'prevb';
	};
	
	return true;
}

function updatePictures() {
//	setTimeout( 'updatePicturesDelayed()', 30000 );
	updatePicturesDelayed();
}

function updatePicturesDelayed() {
	var lastPict = 0;
	if ( document.getElementById( 'lastPict' ) ) {
		lastPict = parseInt( document.getElementById( 'lastPict' ).innerHTML );
	}
	else {
		lastPict = 0;
	}
	
	if ( lastPict == 0 ) {
		availablePicts = 0;
		document.getElementById( 'pictures' ).style.display = 'none';
		document.getElementById( 'textPictures' ).style.display = 'none';
		document.getElementById( 'page_content_border' ).style.background = 'url(/images/css/page_border_bg2.gif) no-repeat';
		return;
	}
	
	if ( lastPict > 0 && availablePicts == 0 ) {
		document.getElementById( 'pictures' ).style.display = 'block';
		document.getElementById( 'textPictures' ).style.display = 'block';
		document.getElementById( 'page_content_border' ).style.background = 'transparent';
	}
	
	if ( availablePicts < lastPict ) {
		availablePicts = lastPict;
		if ( availablePicts < 5 ) {
			firstPict = 1;
		}
		else {
			firstPict = availablePicts - 4;
		}
		showPictures();
	}
}

		
function showPictures() {
	var n = 5;
	if ( availablePicts < 5 ) {
		n = availablePicts;
	}
				
	for( var i = 0; i < n; i++ ) {
		var p = 'img' + ( firstPict + i ) + '.jpg';
		//picts[i].setAttribute( 'src', 'http://keynotemac4ever.xserve.fr/images/keynote/thumbnails/' + p );
		//links[i].setAttribute( 'href', 'http://keynotemac4ever.xserve.fr/images/keynote/fullsize/' + p );
		picts[i].setAttribute( 'src', '/images/keynote/thumbnails/' + p );
		links[i].setAttribute( 'href', '/images/keynote/fullsize/' + p );
	}
	if ( availablePicts > 5 ) {
		if ( firstPict == availablePicts - 4 ) {
			nextCont.style.visibility = 'hidden';
		}
		else {
			nextCont.style.visibility = 'visible';
		}
					
		if ( firstPict <= 1 ) {
			prevCont.style.visibility = 'hidden';
		}
		else {
			prevCont.style.visibility = 'visible';
		}
	}
	else {
		nextCont.style.visibility = 'hidden';
		prevCont.style.visibility = 'hidden';
		for( var i = n; i < 5; i++ ) {
			picts[i].setAttribute( 'src', '/images/blank.gif' );
			links[i].setAttribute( 'href', '#' );
		}
	}
}

function next() {
	if ( firstPict + 1 <= availablePicts - 4 ) {
		firstPict += 1;
		showPictures();
	}
}
			
function prev() {
	if ( firstPict - 1 >= 0 ) {
		firstPict -= 1;
		showPictures();			
	}
}
