var datas ;
var rss_container_text;
var userProfile;
var tweetBox_json;

$(document).ready(function()
{
    rss_container_text =   $('div.content div.rss div.textContainer div.text') ;
    userProfile = 'ESHaematology' ;
    tweetBox_json = new Object;
    tweetBox_json.tweets = new Array();
}) ;

function slideConference()
{
    current = parseInt($('div.home div.conferences div.leftBox ul.confList li.actif').attr('id').split('conf')[1]) ;
    var next = current + 1 ;
    if (next > 6)
        next = 1 ;
    $('div.home div.conferences div.leftBox ul.confList li#conf'+next).click() ;
    setTimeout('slideConference();', 13000) ;
}

$(function()
{
	/******** Gestion du module de recherche ********/
	$('div.header div.bottom form input.field').focus(function(){
		if($(this).val() == 'Search here') $(this).val('');
	});
	$('div.header div.bottom form input.field').blur(function(){
		if($(this).val() == '') $(this).val('Search here');
	});
		
	
	/******** Gestion des conferences de la page d'accueil ********/
	var confOpen = 'conf1';
	var idConf;
	try
                 {
                        $('div.home div.conferences div.leftBox ul.confList li#conf1')[0].className = 'actif';
                        $('div.home div.conferences div.rightBox div#conf1cont').show();
                        
                        $('div.home div.conferences div.leftBox ul.confList li').click(function(){
                                idConf = $(this)[0].id;
                                $('div.home div.conferences div.leftBox ul.confList li#'+confOpen)[0].className = '';
                                $('div.home div.conferences div.leftBox ul.confList li#'+idConf)[0].className = 'actif';
                                $('div.home div.conferences div.rightBox div#'+confOpen+'cont').slideUp(200, function(){
                                        $('div.home div.conferences div.rightBox div#'+idConf+'cont').slideDown(200);
                                });
                                $.get('calendar', {'start': $('.start_'+idConf).text() , 'end': $('.end_'+idConf).text()}, 
                                    function(data)
                                    {
                                        datas = data ;
                                        $('.calendar_holder').fadeOut(function()
                                            {
                                                $('.calendar_holder').html(datas) ;
                                            }) ;
                                        $('.calendar_holder').fadeIn() ;
                                    })
                                confOpen = idConf;
                                return false;
                        });
                 }catch(err){}
});


var nouvellePosition;

function defilerRSS() {
	nouvellePosition = rss_container_text.position().left-650;
	if(nouvellePosition < -rss_container_text.width()) nouvellePosition = 0;
	rss_container_text.css('left', nouvellePosition);
                  setTimeout(defilerRSS, 5000);
}


function getTweets(){
    var feedData = "";
    $.getJSON("http://api.twitter.com/1/users/show.json?screen_name="+userProfile+"&callback=?", function(tweetdata) {
        tweetBox_json.profile = new Object;
        tweetBox_json.profile.screen_name = tweetdata.screen_name;
        tweetBox_json.profile.avatar = tweetdata.profile_image_url;
        tweetBox_json.profile.name = tweetdata.name;
        tweetBox_json.profile.followers_count = tweetdata.followers_count;
        tweetBox_json.profile.statuses_count = tweetdata.statuses_count;
        tweetBox_json.profile.friends_count = tweetdata.friends_count;
    })
    .complete(function(){
        $.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name="+userProfile+"&count=100&callback=?", function(tweetdata) {
            // For each item returned in tweetdata
            $.each(tweetdata, function(i, tweet) {
                tweetBox_json.tweets[i] = new Object;
                if ( tweet.text.search('http') != -1)
                {
                        var url = 'http'+ tweet.text.split('http')[1].split(' ')[0] ;
                        var nextText = tweet.text.split('http')[1].split(' ')[1] ;
                        tweet.text = tweet.text.split('http')[0] + '<a href="' + url + '">' + url + '</a> ' ;
                        if (nextText)
                            tweet.text += nextText ;
                }
                tweetBox_json.tweets[i].text = tweet.text;//
                tweetBox_json.tweets[i].created_at = dateFormat(new Date(tweet.created_at).toString(), "yyyy mmm dd HH:MM") ;
                tweetBox_json.tweets[i].location = tweet.user.location;
                tweetBox_json.tweets[i].retweeted = tweet.retweeted;
                tweetBox_json.tweets[i].retweet_count = tweet.retweet_count;
            });
        })
    });
}

function displayLastTweet()
{
    if (!tweetBox_json.tweets.length)
    {
            setTimeout(displayLastTweet, 1000) ;
            return false ;
    }
    $('.twitter .lastTweet').html(tweetBox_json.tweets[0].text+'<br/>'+tweetBox_json.tweets[0].created_at+' from '+tweetBox_json.tweets[0].location) ;
    return true;
}

function displayLatestTweets(nb)
{
    if (!tweetBox_json.tweets.length)
    {
            setTimeout("displayLatestTweets("+nb+");", 1000) ;
            return false ;
    }
    $('.twitter_news').html("<div class='title_feed'><b>LATEST TWEETS</b></div>");
    for (var i = 0 ; i < nb ; i++)
    {
        $('.twitter_news').append(
                    '<div class="tweet_feed">' +
                           '<table>' +
                           '<tr>' +
                            '<td valign="top">' +
                                '<img src="'+ tweetBox_json.profile.avatar  +'" />' +
                            '</td>' +
                            '<td valign="top">' +
                                '<b>@'+userProfile+'</b><br/>' + tweetBox_json.tweets[i].text +
                                '<br/><span class="tweet_time">' + tweetBox_json.tweets[0].created_at+' from '+tweetBox_json.tweets[0].location +'</span>'+
                            '</td>' +
                           '</tr>' +     
                    '</table>' +
                    '</div>' ) ;
    }
    return true ;
}


BookmarkApp = function () {
    var isIEmac = false; /*@cc_on @if(@_jscript&&!(@_win32||@_win16)&& 
(@_jscript_version<5.5)) isIEmac=true; @end @*/
    var isMSIE = (-[1,]) ? false : true;
    var cjTitle = document.title;
    var cjHref = location.href;

    function hotKeys() {
        var ua = navigator.userAgent.toLowerCase();
        var str = '';
        var isWebkit = (ua.indexOf('webkit') != - 1);
        var isMac = (ua.indexOf('mac') != - 1);

        if (ua.indexOf('konqueror') != - 1) {
            str = 'CTRL + B'; // Konqueror
        } else if (window.home || isWebkit || isIEmac || isMac) {
            str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D'; // Netscape, Safari, iCab, IE5/Mac
        }
        return ((str) ? 'Press ' + str + ' to bookmark this page.' : str);
    }

    function isIE8() {
        var rv = -1;
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null) {
                rv = parseFloat(RegExp.$1);
            }
        }
        if (rv > - 1) {
            if (rv >= 8.0) {
                return true;
            }
        }
        return false;
    }

    function addBookmark(a) {
        try {
            if (typeof a == "object" && a.tagName.toLowerCase() == "a") {
                a.style.cursor = 'pointer';
                if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
                    window.sidebar.addPanel(cjTitle, cjHref, ""); // Gecko
                    return false;   
                } else if (isMSIE && typeof window.external == "object") {
                    if (isIE8()) {
                        window.external.AddToFavoritesBar(cjHref, cjTitle); // IE 8                    
                    } else {
                        window.external.AddFavorite(cjHref, cjTitle); // IE <=7
                    }
                    return false;
                } else if (window.opera) {
                    a.href = cjHref;
                    a.title = cjTitle;
                    a.rel = 'sidebar'; // Opera 7+
                    return true;
                } else {
                    alert(hotKeys());
                }
            } else {
                throw "Error occured.\r\nNote, only A tagname is allowed!";
            }
        } catch (err) {
            alert(err);
        }

    }

    return {
        addBookmark : addBookmark
    }
}();
