//--------------------------------------------------------------------------------------------------------------------------------
// -- briatexte.js : functions Javascript for ASB
//--------------------------------------------------------------------------------------------------------------------------------



//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Resolution
//------------------------------------------------------------------------------------------------------------------------
// -- Manage resolution
function resolution( widthSelf, phpSelf ) {
	if( widthSelf != screen.width ) {
		document.location = phpSelf + "width=" + screen.width ;
	}
}


//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Bulle
//------------------------------------------------------------------------------------------------------------------------
var IB = new Object ;
var posX = 0 ; posY = 0 ;
var xOffset = 10 ; yOffset = 10 ;

// -- Set bulle
function setBulle( info, first, second ) {
	// -- Table
	var text = "<table border=0 cellspacing=0 cellpadding=" + IB.NbPixel + ">" ;
	text = text + "<tr bgcolor='" + IB.ColContour + "'>" ;
	text = text + "<td>" ;
	text = text + "<table border=0 cellpadding=2 cellspacing=0 bgcolor='" + IB.ColFond + "'>" ;
	
	// -- Information
	text = text + info
	// -- Mail protected against spam
	if( first != '' ) {
		text = text + "<a class='linkWhite' href='mailto:" + first + "@" + second + "'>" + first + "@" + second + "</a></td></tr>" ;
	}

	// -- End table
	text = text + "</table>" ;
	text = text + "</td>" ;
	text = text + "</tr>" ;
	text = text + "</table>&nbsp;" ;
	// -- Print bulle with test generated
	return( PrintBulle( text ) ) ;
}

// -- Print bulle
function PrintBulle( text ) {
	var finalPosX = posX - xOffset ;
	if( finalPosX < 0 ) finalPosX = 0 ;
	// -- Diff by browser version
	if( document.layers ) {
		document.layers[ 'bulle' ].document.write( text ) ;
		document.layers[ 'bulle' ].document.close() ;
		document.layers[ 'bulle' ].top = posY + yOffset ;
		document.layers[ 'bulle' ].left = finalPosX ;
		document.layers[ 'bulle' ].visibility = 'show' ;
	}
	if( document.all ) {
		bulle.innerHTML = text ;
		document.all[ 'bulle' ].style.top = posY + yOffset ;
		document.all[ 'bulle' ].style.left = finalPosX ;
		document.all[ 'bulle' ].style.visibility = 'visible' ;
	}
	else if( document.getElementById ) {
		document.getElementById( 'bulle' ).innerHTML = text ;
		document.getElementById( 'bulle' ).style.top = posY + yOffset ;
		document.getElementById( 'bulle' ).style.left = finalPosX ;
		document.getElementById( 'bulle' ).style.visibility = 'visible' ;
	}
	return( true ) ;
}

// -- Clear bulle
function HideBulle() {
	// -- Diff by browser version
	if( document.layers ) {
		document.layers[ 'bulle' ].visibility = 'hide' ;
	}
	if( document.all ) {
		document.all[ 'bulle' ].style.visibility = 'hidden' ;
	}
	else if( document.getElementById ) {
		document.getElementById( 'bulle' ).style.visibility = 'hidden' ;
	}
	return( true ) ;
}

// -- Intialize bulle
function InitBulle( ColTexte, ColFond, ColContour, NbPixel ) {
	IB.ColTexte = ColTexte ; IB.ColFond = ColFond ; IB.ColContour = ColContour ; IB.NbPixel = NbPixel ;
	// -- Diff by browser version
	if( document.layers ) {
		window.captureEvents( Event.MOUSEMOVE ) ; window.onMouseMove = _getMousePos ;
		document.write( "<layer name='bulle' top=0 left=0 visibility='hide'></layer>" ) ;
	}
	if( document.all ) {
		document.write( "<div id='bulle' style='position:absolute;top:0;left:0;visibility:hidden'></div>" ) ;
		document.onmousemove = _getMousePos ;
	}
	else if( document.getElementById ) {
	        document.onmousemove = _getMousePos ;
		document.write( "<div id='bulle' style='position:absolute;top:0;left:0;visibility:hidden'></div>" ) ;
	}
}


//------------------------------------------------------------------------------------------------------------------------
// -- Privates functions - Bulle
//------------------------------------------------------------------------------------------------------------------------
function _getMousePos( e ) {
	// -- Diff by browser version
	if( document.all ) {
		posX = event.x + document.body.scrollLeft ;
		posY = event.y + document.body.scrollTop ;
	}
	else {
		posX = e.pageX ;
		posY = e.pageY ;
	}
}


//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Login
//------------------------------------------------------------------------------------------------------------------------
// -- Test form
function testLogin( objUser ) {
	// -- Validate user : not empty
	if( ! objUser.value.length ) {
		alert( "Attention, le nom d'utilisateur est obligatoire !!\n\tVeuillez recommencer la saisie." ) ; objUser.focus() ;
		return( false ) ;
	}
	return( true ) ;
}


//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Informations
//------------------------------------------------------------------------------------------------------------------------
// -- Delete information
function deleteInfo( message ) {
	// -- Confirm to delete information
	var ret = confirm( message ) ;
	return( ret ) ;
}

// -- Test form
function testInfo( objDate, objTitle, objSsTitle ) {
	// -- Validate date : YYYY-MM-DD
	var model = /^\d{4}-\d{2}-\d{2}$/ ;
	if( ! model.test( objDate.value ) ) {
		alert( "Attention, la Date [" + objDate.value + "] n'est pas correct !!\n\tVeuillez recommencer la saisie." ) ; objDate.focus() ;
		return( false ) ;
	}
	// -- Validate title : not empty
	if( ! objTitle.value.length ) {
		alert( "Attention, le Titre est obligatoire !!\n\tVeuillez recommencer la saisie." ) ; objTitle.focus() ;
		return( false ) ;
	}
	// -- Validate sstitle : not empty
	if( ! objSsTitle.value.length ) {
		alert( "Attention, le Sous-titre est obligatoire !!\n\tVeuillez recommencer la saisie." ) ; objSsTitle.focus() ;
		return( false ) ;
	}
	return( true ) ;
}


//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Club
//------------------------------------------------------------------------------------------------------------------------
// -- Save club
function saveClub( message ) {
	// -- Confirm to save club
	var ret = confirm( message ) ;
	return( ret ) ;
}



//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Date
//------------------------------------------------------------------------------------------------------------------------
// -- List count down
var listObj = new Array() ;
var listEven = new Array() ;
var listSec = new Array() ;
var nbObj = 0 ;

// -- Signal
var countdown ;

// -- Calculus size integer
function lenValue( value ) {
	// -- Size integer	
	if( value >= 100 ) {
		return( 3 ) ;
	}
	if( value >= 10 ) {
		return( 2 ) ;
	}
	if( value >= 1 ) {
		return( 1 ) ;
	}
	return( 0 ) ;	
}

// -- Convert nb seconds to date
function formatDate( iiObj ) {
	// -- Initialize attributes
	var nbSec = listSec[ iiObj ] ;
	// -- Count down terminated
	if( nbSec < 0 ) {
		listObj[ iiObj ].value = "L'évenement '" + listEven[ iiObj ] + "' est démarré !!" ;
		listObj[ iiObj ].className = "contenuCountDownOrange" ;
		return( 0 ) ;
	}

	var value = '' ;
	var size = 0 ;

	// -- Days between 2 dates
	var rest = Math.floor( nbSec / 86400 ) ;
	nbSec = nbSec - ( rest * 86400 ) ;
	if( rest ) {
		size = size + lenValue( rest ) + 1 ;
		value = value + rest + 'j ' ;
	}

	// -- Hours between 2 dates
	rest = Math.floor( nbSec / 3600 ) ;
	nbSec = nbSec - ( rest * 3600 ) ;
	if( rest ) {
		size = size + lenValue( rest ) + 1 ;
		value = value + rest + 'h ' ;
	}

	// -- Minutes between 2 dates
	rest = Math.floor( nbSec / 60 ) ;
	nbSec = nbSec - ( rest * 60 ) ;
	if( rest ) {
		size = size + lenValue( rest ) + 1 ;
		value = value + rest + 'm ' ;
	}

	// -- Seconds between 2 dates
	var rest = Math.floor( nbSec ) ;
	if( rest ) {
		size = size + lenValue( rest ) + 1 ;
		value = value + rest + 's' ;
	}

	// -- Count down not terminated
	listObj[ iiObj ].value = "Il reste " + value + " avant l'évenement '" + listEven[ iiObj ] + "'." ;
	return( 0 ) ;
}

// -- Add count down to the list
function addCountDown( obj, year, month, day, hour, minute, second, even ) {
	// -- Times
	dateTo = new Date( year, month - 1, day, hour, minute, second ) ;
	dateFrom = new Date() ;

	// -- Store Objects
	listObj[ nbObj ] = obj ;
	// -- Store even
	listEven[ nbObj ] = even ;
	// -- Store seconds
	listSec[ nbObj ] = Math.floor( ( dateTo.getTime() - dateFrom.getTime() ) / 1000 ) ;
	nbObj++ ;
}

// -- Start count down
function countDown() {
	// -- Print rest
	for( ii = 0 ; ii < nbObj ; ii++ ) {
		listSec[ ii ]-- ;
		formatDate( ii ) ;
	}

	// -- Start timer
	countdown = setTimeout( "countDown()", 1000 ) ;
}



//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Image
//------------------------------------------------------------------------------------------------------------------------
var objTitle ;			// -- Object image title
var rateTitle = 0 ;		// -- Image rate
var width_max = 0 ;		// -- Image width
var height_max = 0 ;		// -- Image heigh
var width_increase = 0 ;	// -- Increase width
var height_increase = 0 ;	// -- Increase height

// -- Image flip flop
function flipflop( obj, rate ) {
	// -- Object
	objTitle = obj ;
	rateTitle = rate * 50 ;

	// -- Image width
	width_max = objTitle.width ;
	width_increase = width_max / 10 ;
	// -- Heigth
	height_max = objTitle.height ;
	height_increase = height_max / 10 ;

	setTimeout( "first_flip()", rateTitle ) ;
}

// -- Image flip
function first_flip() {
	// -- Increase image width
	var width_image = objTitle.width ;
	width_image = width_image - width_increase ;
	objTitle.width = width_image ;

	// -- Image width is on the min
	if( width_image <= 0 ) {
		width_increase = -width_increase ;
	}
	// -- Image width is on the max
	else if( width_image >= width_max ) {
		// -- Change the direction
		width_increase = -width_increase ;
		setTimeout( "second_flip()", 1000 ) ;
		return( true ) ;
	}
	
	setTimeout( "first_flip()", rateTitle ) ;
}

// -- Image flop
function second_flip() {
	// -- Increase image height
	var height_image = objTitle.height ;
	height_image = height_image - height_increase ;
	objTitle.height = height_image ;

	// -- Image width is on the min
	if( height_image <= 0 ) {
		height_increase = -height_increase ;
	}
	// -- Image width is on the max
	else if( height_image >= height_max ) {
		// -- Change the direction
		height_increase = -height_increase ;
		setTimeout( "first_flip()", 1000 ) ;
		return( true ) ;
	}
	
	setTimeout( "second_flip()", rateTitle ) ;
}



// -- Popup image
function PopupImage( image, title ) {
	w = open( '', 'image', 'width=400,height=400,toolbar=no,scrollbars=no,resizable=yes' ) ;
	w.document.write( "<html><head><title>" + title + "</title></head>" ) ;
	w.document.write( "<script language=javascript>function checksize() { if( document.images[0].complete ) {" ) ;
	w.document.write( "window.resizeTo( document.images[0].width + 12, document.images[0].height + 30 ) ; window.focus() ; }" ) ;
	w.document.write( "else { setTimeout( 'check()', 250 ) } }</script>" ) ;
	w.document.write( "<body onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>" ) ;
	w.document.write( "<img src='" + image + "' border='0'>" ) ;
	w.document.write( "</body></html>" ) ;
	w.document.close() ;
}



//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Cup
//------------------------------------------------------------------------------------------------------------------------
// -- List match cup
var listMatchCup = new Array() ;
var iiMatchCup = 0 ;
var listIdMatchCup = new Array() ;
var iiIdMatchCup = 0 ;

// -- List cup
function addCup( id, text ) {
	listIdMatchCup[ iiIdMatchCup ] = id ;
	listMatchCup[ iiIdMatchCup ] = text ;
	iiIdMatchCup++ ;
}

// -- Get cup
function getCup( id ) {
	iiMatchCup = id ;
	for( ii = 0 ; ii < iiIdMatchCup ; ii++ ) {
		if( listIdMatchCup[ ii ] == iiMatchCup ) {
			return( listMatchCup[ ii ] ) ;
		}
        }
	return( 'nbsp;' ) ;
}

// -- Get first cup
function getFirstCup() {
	return( listMatchCup[0] ) ;
}



//------------------------------------------------------------------------------------------------------------------------
// -- Publics functions - Sponsor rotation
//------------------------------------------------------------------------------------------------------------------------
// -- List sponsor rotation
var listSponsor = new Array() ;
var iiSponsor = 0 ;
var saveSponsor = -1 ;

// -- List sponsor
function addSponsor( href, bulle, src ) {
	var code = "<a  href='" + href + "' target='_blank' " ;
	code = code + "onmouseover=\"setBulle( '" + bulle + "', '', '' )\" onmouseout=\"HideBulle()\">" ;
	code = code + "<img src='" + src + "' border='0'></a>" ;
	listSponsor[ iiSponsor ] = code ;
	iiSponsor++ ;
}

// -- Set linear rotation
function getSponsor() {
	// -- Photo 1
	var jjSponsor = saveSponsor + 1 ;
	if( jjSponsor >= iiSponsor ) {
		jjSponsor = 0 ;
	}
	saveSponsor = jjSponsor ;
	document.getElementById( "sponsor1" ).innerHTML = listSponsor[ jjSponsor ] ;

	// -- Photo 2
	jjSponsor++ ;
	if( jjSponsor >= iiSponsor ) {
		jjSponsor = 0 ;
	}
	document.getElementById( "sponsor2" ).innerHTML = listSponsor[ jjSponsor ] ;

	// -- Photo 3
	jjSponsor++ ;
	if( jjSponsor >= iiSponsor ) {
		jjSponsor = 0 ;
	}
	document.getElementById( "sponsor3" ).innerHTML = listSponsor[ jjSponsor ] ;
	window.setTimeout( "getSponsor()", 5000 ) ;
}

// -- Set random rotation
function getSponsor_random() {
	var jjSponsor = Math.floor( Math.random() * iiSponsor ) ;
	// -- Not the same
	if( jjSponsor == saveSponsor ) {
		jjSponsor += 1 ;
		if( jjSponsor >= iiSponsor ) {
			jjSponsor = 0 ;
		}
	}
	saveSponsor = jjSponsor ;
	document.getElementById( "sponsor1" ).innerHTML = listSponsor[ jjSponsor ] ;

	// -- Photo 2
	jjSponsor = jjSponsor + 1 ;
	if( jjSponsor >= iiSponsor ) {
		jjSponsor = 0 ;
	}
	document.getElementById( "sponsor2" ).innerHTML = listSponsor[ jjSponsor ] ;

	// -- Photo 3
	jjSponsor = jjSponsor + 1 ;
	if( jjSponsor >= iiSponsor ) {
		jjSponsor = 0 ;
	}
	document.getElementById( "sponsor3" ).innerHTML = listSponsor[ jjSponsor ] ;
	window.setTimeout( "getSponsor_random()", 5000 ) ;
} 

