var providers = {
    google: { // ok
        // http://code.google.com/intl/fr/apis/accounts/
	name:	'Google',
	url:	'https://www.google.com/accounts/o8/id',
        url1:    'https://www.gmail.com/',
        url2:   'http://www.google.com/profiles/{username}'
    },

    yahoo: { //ok
	name:	'Yahoo',      
	url:	'http://yahoo.com/'
    },    
   
    aol: { //ok
	name:	'AOL',     
	prompt:	'Entrez votre screenname AOL : ',
	url:	'http://openid.aol.com/{username}/'
    },

    blogger: { // ok (blog-name pas user-name)
	name:	'Blogger',
	prompt:	'Entrez le nom de votre Blog : ',
	url:	'http://{username}.blogspot.com/'
    },


    myspace: { // ok
	// par defaut le username est un numero
	// mais l'utilisateur peut se choisir son username
	name:	'mySpace',     
	prompt:	'Entrez votre username pour myspace : ',
	url:	'http://www.myspace.com/{username}'
    },

    livejournal: { // ok
	name:	'LiveJournal',
	prompt:	'Enter votre username pour LiveJournal : ',
	url:	'http://{username}.livejournal.com/'
    },

    wordpress: { // ok (il faut d'abord se loger a wp)
	name:	'Wordpress',
	prompt:	'Entrez votre username pour Wordpress.com : ',
	url:	'http://{username}.wordpress.com/'
    },

    verisign: {  // ok (il faut d'abord se loger a pip)
	name:	'Verisign',
	prompt:	'Entrez votre username pour Verisign : ',
	url:	'http://{username}.pip.verisignlabs.com/'
    },

    myopenid: { // ok
	name:	'MyOpenID',
	prompt:	'Entrez votre username pour myOpenID : ',
	url:	'http://{username}.myopenid.com/'
    },

    claimid: {
	name:	'ClaimID',
	prompt:	'Entrez votre username pour ClaimID : ',
	url:	'http://claimid.com/{username}'
    },

    orange: {
	name:	'orange',     
	prompt:	'Entrez votre username pour Orange : ',
	url:	'http://openid.orange.fr/{username}'
    },

    openid: {
	name:	'OpenID',     
	prompt:	'Entrez votre URL OpenID : ',
	url:	'{username}'
    }


    /****************************************************
    flickr: { // idem yahoo
	name:	'Flickr',	
	prompt2:	'Enter your Flickr username: ',
	url:    'http://flickr.com/',
	url2:	'http://flickr.com/{username}/'
    },

    liveid: {  // pas d'url valide
	name:	'liveid',
        url:    'http://login.live.com/login.srf',
	url1:	'https://login.live-int.com/'
    },

    twitter: { // marche pas
	name:	'twitter',     
	prompt:	'Enter your twitter ID: ',
	url:	'http://twitter.com/{username}'
    },

    facebook: {  // accepte openid mais n'est pas provider
	name:	'facebook',     
	prompt:	'Enter your facebook ID: ',
	url:	''
    },

    technorati: {
	name:	'Technorati',
	prompt:	'Enter your Technorati username: ',
	url:	'http://technorati.com/people/technorati/{username}/'
    },

    vidoop: {
	name:	'Vidoop',
	prompt:	'Enter your Vidoop username: ',
	url:	'http://{username}.myvidoop.com/'
    },
    *************************************************/

};

var openid = {

	cookie_expires: 6*30,	// 6 months.
	cookie_name: 'openid_provider',
	cookie_path: '/',
	
	img_path: '/my/img/',
	input_id: null,
	provider: null,

    init: function(input_id) {
	
	var openid_btns = $('#openid_btns');
	
	this.input_id = input_id;
	
	$('#openid_choice').show();
	$('#openid_input_area').empty();
	
	// add box for each provider
	for (id in providers) {
	   	openid_btns.append(this.getProviderBox(providers[id]));
	}
	
	$('#openid_form').submit( function() {openid.do_submit();} );
	
	var box_id = this.readCookie();
	if( box_id ) this.do_select(box_id);
    },

    getProviderBox: function(provider) {
	var box_id = provider["name"].toLowerCase();
	return '<a title="' 
	     + provider["name"] + ": " + provider["url"]
	     + '" href="javascript: openid.signin(\'' + box_id + '\');"' 
	     + ' style="background: #FFF url(' 
	     + this.img_path + box_id + ".png" 
	     + ') no-repeat center center" ' 
	     + 'class="' + box_id 
	     + ' openid_btn"></a>';
    },

    /* select a provider */
    do_select: function(box_id) {

	if( this.provider ){
		// unselect the curent provider
		$('.'+this.provider['id']).css("background-color", "white");
	}

    	this.provider = providers[box_id];
  	if( !this.provider ) return;
	this.provider['id'] = box_id;

	$('.'+box_id).css("background-color", "red");
	this.setCookie(box_id);
		
	// prompt user for input?
	if( this.provider['prompt'] ){
		this.setInputBox();
	} else {
		this.removeInputBox();
	}
    },

    /* here when user click on a provider's logo */
    signin: function(box_id) {
	this.do_select(box_id);
	if( this.provider && !this.provider['prompt'] ){
		$('#openid_form').submit();
	}
    },

    do_submit : function() {
	if( !this.provider || !this.provider['url'] ) return false;
        document.body.style.cursor = 'wait';

	var url= this.provider['url'];
        var un = $('#openid_username');
    	if( un ){
    		url = url.replace('{username}', un.val());
    	}
        
	$('#' + this.input_id).remove();
        $('#openid_form').append('<input type="hidden" id="' + this.input_id 
                                                + '" name="' + this.input_id 
						+ '" value="' + url+'"/>');
	if( un ){
		this.removeInputBox();
	}
    	return true;
    },

    // remove the input box and the submit button
    removeInputBox: function() {
	return $('#openid_input_area').empty();
    },

    // create an input box and a submit button
    setInputBox: function() {
	this.removeInputBox().append(
		'<p>' + this.provider['prompt'] +
		' <input id="openid_username" type="text" />' +
		' <input id="openid_submit" type="submit" />' +
		'</p>'
	);
	$('#openid_username').focus();
    },

    setCookie: function(value) {
	var date = new Date();
	date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
    },

    readCookie: function() {
	var nameEQ = this.cookie_name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while( c.charAt(0)==' ' ) c = c.substring(1,c.length);
		if( c.indexOf(nameEQ) == 0 ) return c.substring(nameEQ.length,c.length);
	}
	return null;
    }

};

