function show_page()
{
    $('viewport').setStyle('display', 'block');
    $('loader').setStyle('display', 'none');
}

function moveto(x,y,blockleft)
{
    if (blockleft == null)
	blockleft = false;
    var ws = window.getSize();
    if (blockleft)
    {
	x = Math.max(x,ws.x/2);
	y = Math.max(y,ws.y/2);
    }
    var dx = -x + ws.x/2;
    var dy = -y + ws.y/2;
    var m = new Fx.Morph('viewport', {duration:1000});
    m.start({left:dx, top:dy});
}

function contents(name)
{
    $('videos').setStyle('display', 'none');
    $('photo').setStyle('display', 'none');
    $('or').setStyle('display', 'none');

    switch(name)
    {
        case "concerts":
            moveto(2320,170);
            break;

        case "chansons":
            moveto(500,2270,true);
            break;

        case "photos":
            moveto(410,1610,true);
	    $('photo').setStyle('display', 'block');
            display_photos();
            break;

        case "videos":
            moveto(2400,2250);
            display_videos();
            break;

        case "groupe":
            moveto(2590,930);
            break;

        case "contact":
            moveto(650,100);
            break;

        case "presse":
            moveto(650,390);
            break;

	case "or":
	    $('or').setStyle('display', 'block');
	    moveto(3450,1300);
            break;

        default:
            moveto(1510,1620);
            break;
    }
}

/* Concerts */

function display_concerts()
{
    var url = "http://www.google.com/calendar/feeds/lagouaille@gmail.com/public/full?alt=json-in-script&callback=display_concerts_cb&orderby=starttime&singleevents=true&sortorder=descending&futureevents=false";

    $('concerts-dates').set('text', 'Chargement des concerts...');
    new Element('script', {'src': url}).inject($('concerts'));
}

function display_concerts_cb(list)
{
    $('concerts-dates').set('text', '');
    var ul = new Element('ul');
    $('concerts-dates').adopt(ul);

    list.feed.entry.each(function(item) {
            var html = "";
            var day = item.gd$when[0].startTime;
            var title = item.title.$t;

            day = day.replace(/(\d+)-(\d+)-(\d+)T(\d+):(\d+).*/,"$3-$2-$1 à $4h$5");

            title = title.replace(/OPA/i,'<a href="http://www.opa-paris.com/" target="_blank">OPA</a>');
            title = title.replace(/Scène Bastille/i,'<a href="http://www.la-scene.com/" target="_blank">Scène Bastille</a>');
            title = title.replace(/Abracadabar/i,'<a href="http://www.abracadabar.fr" target="_blank">Abracadabar</a>');
            title = title.replace(/Gibus/i,'<a href="http://www.gibus.fr" target="_blank">Gibus</a>');
            title = title.replace(/Gary's Café/i,"<a href='http://www.allomatch.com/bar-sportif/paris/gary-s-cafe.html' target='_blank'>Gary's Café</a>");
            title = title.replace(/Kibélé/i,'<a href="http://www.kibele.fr" target="_blank">Kibélé</a>');
            title = title.replace(/Loco/i,'<a href="http://www.laloco.com" target="_blank">Loco</a>');
            title = title.replace(/Chouchou bar/i,'<a href="http://www.chouchoubar.com/" target="_blank">Chouchou bar</a>');
            title = title.replace(/Gambetta/i,'<a href="http://legambetta.free.fr/" target="_blank">Gambetta</a>');
            title = title.replace(/Espace Blues/i,'<a href="http://www.myspace.com/espaceblues" target="_blank">Espace Blues</a>');
            title = title.replace(/Bar du Cirque/i,'<a href="http://www.barducirque.fr/" target="_blank">Bar du Cirque</a>');
            title = title.replace(/Trëma/i,'<a href="http://www.trema-officiel.com/" target="_blank">Trëma</a>');
            title = title.replace(/palais des congrès d'Issy les Moulineaux/i,"<a href=\"http://www.paci.com.fr/\" target=\"_blank\">palais des congrès d'Issy les Moulineaux</a>");

            title = title.replace(/([^.])$/, "$1.");

            html += day + " : ";
            html += title;
            if (item.content.$t)
            html += "<br/>" + "<i>" + item.content.$t + "</i>";
            ul.adopt(new Element('li', {html: html}));
    });
}

/* Chansons */

function rord(){return (Math.round(Math.random())-0.5);}

var autostart = 0;

function play(f) {
    if (f != null)
    {
        var id = 'chanson-'+f;
        $$('.chanson').each(function(e){e.setStyle('display', 'none');});
        $(id).setStyle('display', 'block');
    }

    if (f != null)
        autostart = 1;
    mp3.sort(rord);
    var i = 0;
    var mp3_files = "";
    if (f == null || f == "")
    {
        mp3_files = mp3[0];
        i++
    }
    else
        mp3_files = "mp3/"+f+".mp3";

    for (; i < mp3.length; i++)
    {
        mp3_files += "|"+mp3[i];
    }

    $('player').innerHTML='<object type="application/x-shockwave-flash" data="dewplayer-multi.swf?mp3='+mp3_files+'&amp;autostart='+autostart+'&amp;autoreplay=0&amp;showtime=1" width="200" height="20"><param name="wmode" value="transparent" /><param name="movie" value="dewplayer-multi.swf?mp3='+mp3_files+'&amp;autostart='+autostart+'&amp;autoreplay=0&amp;showtime=1" /></object>';
}

function toggle_play()
{
    autostart = 1 - autostart;
    play();
}

/* Groupe */

function hover_groupe()
{
    var membres = ["alexis", "david", "henri", "louis", "sam"];

    membres.each
        (function (m)
         {
           var hid = "membre-hover-"+m;
           var id = "membre-"+m;
           $(hid).addEvents({
               mouseenter: function(){$(id).setStyle('display', 'block');},
               mouseleave: function(){$(id).setStyle('display', 'none');}
               });
         });
}

/* Photos */

function display_photos (album_id)
{
    var username = 'lagouaille';
    /*
       The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. 
       These images are available as both cropped(c) and uncropped(u) sizes by appending c or u to the size. As an 
       example, to retrieve a 72 pixel image that is cropped, you would specify 72c, while to retrieve the uncropped 
       image, you would specify 72u for the thumbsize or imgmax query parameter values.

       32, 48, 64, 72, 144, 160
       */
    var thumbsize = '64';
    /*
       The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. 
       These images are available as only uncropped(u) sizes by appending u to the size or just passing the size 
       value without appending anything.

       200, 288, 320, 400, 512, 576, 640, 720, 800
       */
    var imgmax = '800';

    var url = (album_id != null && album_id != "") ? 'http://picasaweb.google.com/data/feed/base/user/' + username + '/albumid/'+ album_id +'?category=photo&alt=json&callback=view_photo_list&thumbsize=' + /*thumbsize*/ 64 +'&imgmax='+ imgmax : 'http://picasaweb.google.com/data/feed/base/user/' + username + '?category=album&alt=json&callback=view_album_list&access=public&thumbsize=' + /*thumbsize*/ 160;

    $('photo').set('text', 'Chargement des photos...');
    $('photos-navigate').set('text', '');

    new Element('script', {'src': url}).inject($('photo'));
}

function view_album_list(list) {
    href  = list.feed.author[0].uri.$t;
    user = list.feed.author[0].name.$t;

    $('photo').set('text','');
    $('photos').set('text','');
    $('photos-navigate').set('text','');
    var photo = new Element('div'); //, {'align':'center'});//{'style': 'position: relative; clear:both; float:left; width:100%; overflow:hidden;'});
    $('photo').adopt(photo);
    list.feed.entry.each(function(item, i) {
            var title = item.title.$t;
            var thumb = item.media$group.media$thumbnail[0].url;

            id_begin = item.id.$t.indexOf('albumid/')+8;
            id_end = item.id.$t.indexOf('?');
            var id = item.id.$t.slice(id_begin, id_end);
            var href = 'javascript:display_photos("' + id + '")';
            d = item.published.$t;
            var date = d.substr(8,2) + '-' + d.substr(5,2) + '-' + d.substr(0,4);

	    var div = new Element('div', {'class': 'image-container'});
	    var a = new Element('a', {'href': href});
	    a.adopt(new Element('img', {'src': thumb, 'alt': title}));
	    div.adopt(a);
	    a.adopt(new Element('p', {'html': title, 'style': 'text-align: center;'}));
	    photo.adopt(div);
            });
}

function view_photo_list(list) {
    var album = list.feed.title.$t;
    var href  = 'javascript:display_photos()';
    var photos = {};

    $('photos-navigate').set('text','');
    $('photos-navigate').adopt(new Element('a', {'href': href, 'html': 'Retour à la liste des albums'}));

    $('photo').set('text','');
    $('photo').adopt(new Element('div', {'id': 'photo-show', 'style': 'width: 100%; margin-left: auto; margin-right: auto;'}));
    list.feed.entry.each(function(item, size) {
            var title = item.title.$t;
            size = item.media$group.media$content[0].width;
            var thumb = item.media$group.media$thumbnail[0].url;

            id_begin = item.id.$t.indexOf('albumid/')+8;
            id_end = item.id.$t.indexOf('/photoid');
            var id = item.id.$t.slice(id_begin, id_end);
            var url = item.media$group.media$content[0].url;
            photos[url] = {'caption': title, 'src': url, 'thumbnail': thumb, 'href': href};
	    //$('photo').adopt(new Element('img', {'src': url, 'alt': title, 'width': '540px'}));
            });
    var slideshow = new Slideshow('photo-show', photos, {thumbnails: false, controller: true});
}

function display_image(album, img)
{
    var href = 'javascript:display_photos("' + album + '")';
    $('navigate').set('text','');
    $('navigate').adopt(new Element('a', {'href': href, 'html': "Retour l'album", 'class': 'backlink'}));

    $('photos').set('text','');
    $('photos').adopt(new Element('a', {'href': href}).adopt(new Element('img', {'src': img})));
}

/* Vidéos */

function display_videos()
{
    //var url = "http://gdata.youtube.com/feeds/users/lagouaillefromparis/uploads?alt=json-in-script&format=5&callback=display_videos_cb&orderby=published";
    var url = "http://gdata.youtube.com/feeds/api/playlists/A13D59654D5DFE4A?alt=json-in-script&format=5&callback=display_videos_cb";

    $('videos').setStyle('display', 'block');
    $('videos').set('text', 'Chargement des videos...');
    new Element('script', {'src': url}).inject($('videos'));
}

function display_video(i, url)
{
    var id = "video"+i;
    var video = document.getElementById(id);
    var height = 340;

    url += "&autoplay=1";
    video.set('text','');
    /* var html = '<div class="center"><object width="560" height="340"><param name="movie" value="'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></div>';
    video.innerHTML = html; */
    var div = new Element("div", {"class": "center"});
    video.adopt(div);
    var obj = new Element("object", {"width": "560", "height": height});
    div.adopt(obj);
    obj.adopt(new Element("param", {"name": "movie", "value": url}));
    obj.adopt(new Element("param", {"name": "allowFullScreen", "value": "true"}));
    obj.adopt(new Element("param", {"name": "allowscriptaccess", "value": "always"}));
    obj.adopt(new Element("embed", {"src": url, "type": "application/x-shockwave-flash", "allowscriptaccess": "always", "allowfullscreen": "true", "width": "560", "height": height}));
}

function display_videos_cb(list)
{
    var height = 340;
    $('videos').set('text', '');
    var table = new Element('table');
    var tr = new Element('tr');
    table.adopt(tr);
    $('videos').adopt(tr);

    list.feed.entry.each(function(item, i) {
            var url = item.media$group.media$content[0].url;
            var td = new Element('td');
            var div = new Element('div', {"class": "video"});
            div.innerHTML += '<object width="560" height="'+height+'"><param name="movie" value="'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="'+height+'"></embed></object>';
            td.adopt(div);
            tr.adopt(td);
            var passagers = new Element('td');
            passagers.adopt(new Element('div', {"class": "video-passagers"}));
            tr.adopt(passagers);

            /* MSIE hack since the thumbnail thing doesn't work... */
            /*
            if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
                var url = item.media$group.media$content[0].url;
                $('videos').innerHTML += '<h3>'+item.title.$t+'</h3><div class="center"><object width="560" height="'+height+'"><param name="movie" value="'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="'+height+'"></embed></object></div>';
            }
            else
            {
                var td = new Element('td');
                var id = "video"+i;
                var div = new Element('div', {"id": id, "class": "video"});
                var href = 'javascript:display_video('+i+",'"+item.media$group.media$content[0].url+"')";
                var a = new Element('a', {"href": href});
                var img = new Element('img', {"src": item.media$group.media$thumbnail[3].url});
                div.adopt(a.adopt(img));
                td.adopt(div);
                tr.adopt(td);
            }
            */
    });
}

/* Contact */

function init_contact()
{
    $('sendmail').addEvent('submit', function(e) {
	    this.set('send',
		{
		onComplete: function(ans) {
		    $('name').set('text', 'Merci !');
		    $('email').set('text', 'Merci !');
		    $('message').set('text', 'Merci !');
		}
		});
	    });
}

/* Or */
function set_goldbook_form ()
{
    $('goldbook_form').addEvent('submit', function(e) {
	    e.stop();
	    this.set('send', {onComplete: function(response) {
		$('input_name').value = "";
		$('input_comment').value = "";
		display_goldbook();
		}});
	    this.send();
	    });
}

function display_goldbook()
{
    var url = "or.php";

    $('goldbook').set('text', "Chargement du livre d'or...");
    new Element('script', {'src': url}).inject($('goldbook'));
}

function display_goldbook_cb(list)
{
    $('goldbook').set('text','');

    list.each(function(item) {
        var html = item.comment + '<br/><div class="citation-source">' + item.name + '</div>';
        var p = new Element('p', {'html': html, 'class': "citation"});
        $('goldbook').adopt(p);
      });
}
