// UTILS
var d = [];
function dom(pSel){
	if(typeof(d[pSel]) === 'undefined') d[pSel] = $(pSel);
	return d[pSel];
}
function switchTheme(pTheme){
	dom('head').append('<link rel="stylesheet" media="screen" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/'+pTheme+'/jquery.ui.theme.css" class="ui-theme" />');
	if($('link.ui-theme').size() > 2){
		$('link.ui-theme:first').remove();
	}
}

//UI CONFIG
function loadUI() {
	// DIALOG
	dom('.popup').dialog({autoOpen:false,modal:true,resizable:false,width:400,
		open:function(event, ui) {
			$(this).find('input:first').focus();
			$(this).find('input:password').val('');
		},
        close:function() {
            $(this).children('div').empty().removeClass('ui-state-error ui-state-highlight');
        }
    });
    dom('#embed').dialog('option','width',520);
	
	// ACCORDION
	dom('.accordion').accordion({collapsible:true,autoHeight:false,animated:'bounceslide'});
	
	// TABS
	$('#menu').tabs({selected:0});
	$('#menu').children('.ui-tabs-nav').removeClass('ui-corner-all').addClass('ui-corner-top');
	
	// AUTOCOMPLETE
	dom('#searchbar').autocomplete({
		source: function(pRequest, pResponse) {
			$.ajax({url: 'http://google.com/complete/search', dataType: 'jsonp', data: {hl: 'fr', client: 'hp', q: pRequest.term},
				success: function(pData) {
					var response = [], length = pData[1].length;
					for (var i = 0; i < length; i ++) {
						response[i] = {};
						response[i].label = pData[1][i][0].replace('<b>', '').replace('</b>', '');
						response[i].value = pData[1][i][0].replace('<b>', '').replace('</b>', '');
					}
					pResponse(response);
				}
			})
		},
		focus: function() {return false;},
		minLength: 2
	}).removeClass('ui-widget ui-widget-content ui-corner-all');
	
	// HOVER
	$('.ui-state-default').live('mouseover',
		function() {$(this).addClass('ui-state-hover')}
	).live('mouseout',
		function() {$(this).removeClass('ui-state-hover')}
	);
}



// EVENT DELEGATION
function loadEvent() {
	// POPUPS
	$('form.popup').submit(function() {
		if ($(this).attr('target') == undefined) {
			$.post('ajax/'+this.id+'.php?callback=?&'+$(this).serialize(), {
				id: this.id,
				page: param.page,
				data: $(this).data("data")
			}, callback, 'json');
			return false;
		}
	});	
	// SEARCH
	dom('#searchbar').focusin(
		function(){if($(this).val() == 'Rechercher...') $(this).val('');}
	).focusout(
		function(){if($(this).val() == '') $(this).val('Rechercher...');}
	);
	
	// MENU
	dom('#menu').delegate('a','click',function(){
		var href=$(this).attr('href'),pos = href.indexOf('='),page=href.substring(1,pos);
		if(page=='popup') dom('#'+href.substring(pos+1)).dialog('open');
		else if(page=='playlist') ajaxCall(page,{id:href.substring(pos+1)});
		else if(page=='feed') ajaxCall(page,{url:href.substring(pos+1)});
		else if(page=='live') playWmv(href.substring(pos+1));
		else if(page=='last') ajaxCall(page,{id:href.substring(pos+1)});
		else if(page=='user') ajaxCall(page,{id:href.substring(pos+1)});
		else if(page=='friends') ajaxCall(page,{id:href.substring(pos+1)});
		else if(page=='followers') ajaxCall(page,{id:href.substring(pos+1)});
		return false;
	});
	
	// PLAYLIST
	dom('#playlist ul').delegate('a','click',function(){
		if($(this).hasClass('user')) {
			var href=$(this).attr('href'),pos = href.indexOf('='),page=href.substring(1,pos);
			ajaxCall(page,{id:href.substring(pos+1)});
		}
		else playVideo($(this).prev('input[type=checkbox]').val());
		return false;
	});
	
	// THEMES
	dom('#theme').children('a').click(function(){
		switchTheme($(this).attr('class'));
		return false;
	});
}

var typeplayer = null;
function switchPlayer(a) {
    if (typeplayer != a) {
        typeplayer = a;
        if (typeplayer == "flv") {
            flvplayerready == null && createflvplayer();
            playerwmvready && wmvplayer.sendEvent("STOP")
        }
        if (typeplayer == "wmv") {
            playerwmvready == null && createwmvplayer();
            flvplayerready && flvplayer.sendEvent("STOP")
        }
        dom("#object").children().not("#" + a + "player").hide();
	//.css("visibility", "hidden");
        dom("#object").children().filter("#" + a + "player").show();
	//css("visibility", "visible")
    }
}
var flvplayer = null,
flvplayerready = null;
function createflvplayer() {
    if (typeof swfobject == "undefined") $.getScript("https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js", createflvplayer);
	else swfobject.embedSWF("static/flash/player-5.7.swf", "flvplayer", "100%", "300", "9.0.98", "static/flash/expressInstall.swf", {
        repeat: "list",
        playlist: "over"
    },
    {
        allowfullscreen: "true",
        allowscriptaccess: "always",
	wmode: "opaque"
    },
    {
        id: "flvplayer",
        name: "flvplayer"
    })
}

function playerReady(a) {
    flvplayerready = true;
    flvplayer = dom("#flvplayer").get(0);
    flvplayer.addControllerListener("ITEM", "itemListener")
}
var wmvplayer = null,
playerwmvready = null;
function createwmvplayer() {
    if (!window.Silverlight) $.getScript("static/js/silverlight.js", createwmvplayer);
	else if (typeof jeroenwijering == "undefined") $.getScript("static/js/wmvplayer.js", createwmvplayer);
    else {
        var a = dom("#wmvplayer").get(0);
        wmvplayer = new jeroenwijering.Player(a, "static/xml/wmvplayer.xaml", {
            height: "300",
            width: "400",
            autostart: "true",
            showstop: "true"
        });
        wmvPlayerReady()
    }
}
function wmvPlayerReady() {
    if (wmvplayer.configuration.sender) playerwmvready = true;
    else setTimeout(wmvPlayerReady, 100)
}
function playWmv(a) {
    switchPlayer("wmv");
    if (playerwmvready != null) playerwmvready == true && wmvplayer.sendEvent("LOAD", a);
    else setTimeout(function() {
        playWmv(a)
    },
    100)
}
var currentChannel = 0,
playingChannel = 0,
currentpage = 0,
currentPlaylist;
function playVideo(a) {
    switchPlayer("flv");
    if (playingChannel != currentChannel || param.page != currentpage) {
		currentpage = param.page;
        playingChannel = currentChannel;
        flvplayer.sendEvent("LOAD", currentPlaylist)
    }
    flvplayer.sendEvent("ITEM", a);
    itemListener({
        index: a
    })
}
function itemListener(a) {
    var g = flvplayer.getPlaylist()[a.index];
    dom("#title").html(g.title);
    dom("#description").html(g.description);
    dom("#videoDownload").attr("href", g.file);
    if (playingChannel == currentChannel) {
        dom("#playlist>ul").children("li").removeClass("ui-state-highlight");
        dom("#playlist>ul").children("li:eq(" + a.index + ")").addClass("ui-state-highlight");
    }
    videoLink = g.id != 0 ? "http://katchup.tv/?video=" + g.id: "http://katchup.tv/?feed=" + g.link;
    playlistLink = test.link;
    file = test.swf;
	dom("#menudlembed").html('<a href="#" onmousedown="addthisvideo('+g.index+');" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-plus"></span><span class="ui-button-text">Ajouter</span></a> <a href="'+g.link+'" onclick="window.open(this.href);return false;" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-link"></span><span class="ui-button-text">Voir sur '+g.author+'</span></a> <a href="#" onmousedown="ajaxCall(\'feed\',{\'url\':\''+g.link+'\'});" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-heart"></span><span class="ui-button-text">Related</span></a> <a href="#" onmousedown="dom(\'#embed\').dialog(\'open\');" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-carat-2-e-w"></span><span class="ui-button-text">Exporter</span></a>');
    dom("#embed>fieldset").html('<p><label>Exporter :</label><a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;pubid=xa-4e2ea65726bdb07c" onmouseover="return addthis_open(this, \'\', \'' + videoLink + '\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></p><p><label>Url de la vidéo :</label><input type="text" class="ui-state-default" style="width:350px" value="' + videoLink + '" onclick="this.select();"/></p><p><label>Url de la playlist :</label><input type="text" class="ui-state-default" style="width:350px" value="' + playlistLink + '" onclick="this.select();" /></p><p><label>Code Embed :</label><textarea class="ui-state-default" style="width:350px" rows="4" onclick="this.select();"><object id="player" data="' + file + '" name="player" type="application/x-shockwave-flash" width="330" height="425"><param name="movie" value="' + file + '" /><param value="true" name="allowfullscreen" /><param value="always" name="allowscriptaccess" /><param value="opaque" name="wmode" /><param value="repeat=list&autostart=false&playlist=bottom&playlistsize=125" name="flashvars" /></object></textarea /></p>')
}
var type = '';
var param = {};
function ajaxCall(a, data) {
    if(a != 'user' && a != 'unfollow' && a != 'follow') {
		type = a, param = data;
		dom("#playlist>ul").html('<br /><p style="text-align:center;"><img src="static/img/loading.gif" alt="Loading" title="Loading" /><br />Loading...</p>');
	}
    $.getJSON("rss/" + a + ".php?format=json&callback=?", data, callback)
}
function prevPage() {
	if(param.page > 1) param.page = param.page-1;
	else param.page = 1;
	ajaxCall(type, param);
}
function nextPage() {
	if(param.page) param.page = param.page+1;
	else param.page = 2;
	ajaxCall(type, param);
}
function addvideo() {
    var a = [];
    dom("#playlist>ul").find("input:checked").each(function(g) {
	a[g] = currentPlaylist[$(this).val()];
    });
    dom("#addvideo").dialog("open").data("data", JSON.stringify(a));
}
function addthisvideo(pId) {
	var a = [];
	a[0] = currentPlaylist[pId];
	dom("#addvideo").dialog("open").data("data", JSON.stringify(a));
}
function deleteVideo() {
    var a = [];
    dom("#playlist>ul").find("input:checked").each(function(g) {
        a[g] = currentPlaylist[$(this).val()].id
    });
	playingChannel = "";
    dom("#deletevideo").dialog("open").data("data", JSON.stringify(a));
}
function deletePlaylist(a) {
    dom("#deleteplaylist").dialog("open").data("data", a)
}
function deleteFeed(a) {
    dom("#deletefeed").dialog("open").data("data", a)
}
function logout() {
    $.getJSON("ajax/logout.php?callback=?", {}, callback)
}
function callback(a) {
    if (a.popup) {
        var g = dom("#" + a.popup.id);
        if (a.popup.success == true) {
            g.children("div").attr("class", "ui-state-highlight").html('<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span>' + a.popup.message + "</p>");
            setTimeout(function() {
                g.dialog("close")
            },
            1E3)
        } else g.children("div").attr("class", "ui-state-error").html('<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>' + a.popup.message + "</p>")
    }
    if (a.header != undefined) {
        var d = [],
        b = 0;
    }
    if (a.profile) {
        d = [];
        b = 0;
        if (a.profile.id) {
			dom("#header>div").html('<a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-error ui-corner-all" onmousedown="logout();"><span class="ui-button-icon-primary ui-icon ui-icon-power"></span><span class="ui-button-text">Logout</span></a>');
			
            dom("#playlist>div").html('<a href="#" onmousedown="addvideo();" class="ui-button ui-button-icon-only ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-plus"></span><span class="ui-button-text">Ajouter</span></a> <a href="#" onmousedown="deleteVideo();" class="ui-button ui-button-icon-only ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-trash"></span><span class="ui-button-text">Supprimer</span></a>Cocher : <a href="#" title="Sélectionner toutes les vidéos" onmousedown="$(\'#playlist>ul :checkbox\').attr(\'checked\', \'checked\');">Tout</a> <a href="#" title="Sélectionner aucune vidéo" onmousedown="$(\'#playlist>ul :checkbox\').removeAttr(\'checked\');">Aucun</a>');
			
			if($("#tabs-playlist").length === 0) $("#menu").tabs("add", "#tabs-playlist", "PLAYLIST", 1);
			
			$("#tabs-playlist").accordion('destroy').empty().html('<h3><a href="#">Mon Profil</a></h3><div id="tabs-playlist-profile"></div><h3><a href="#">Mes Playlists ('+a.profile.playlists.length+')</a></h3><ul id="tabs-playlist-playlist"><li></li></ul><h3><a href="#">Mes Abonnements ('+a.profile.feeds.length+')</a></h3><ul id="tabs-playlist-feed"><li></li></ul>').accordion({collapsible:true, autoHeight:false});
			
			//$("#tabs-playlist-profile").html('<br /><p style="text-align:center;"><img src="images/loading.gif" alt="Loading" title="Loading" /><br />Loading...</p>');
			d[b++] = '<a href="?popup=avatar"><img src="';
			d[b++] = a.profile.image;
			d[b++] = '" width="80" height="80" alt="';
			d[b++] = a.profile.pseudo;
			d[b++] = '" title="';
			d[b++] = a.profile.pseudo;
			d[b++] = '" style="float:left;" /></a>';
			d[b++] = '<div style="margin-left:85px;">';
			d[b++] = '<h2>';
			d[b++] = a.profile.pseudo;
			d[b++] = '</h2>';
			d[b++] = '<em>';
			d[b++] = a.profile.nbVideos;
			d[b++] = ' Vidéos</em><br />';
			d[b++] = '<br />';
			d[b++] = 'E-mail : <a href="mailto:';
			d[b++] = a.profile.email;
			d[b++] = '">';
			d[b++] = a.profile.email;
			d[b++] = '</a><br />';
			d[b++] = 'Site Web : <a href="';
			d[b++] = a.profile.web;
			d[b++] = '">';
			d[b++] = a.profile.web;
			d[b++] = '</a><br />';
			d[b++] = '</div><div style="clear:left;text-align:center;font-weight:bold;margin:20px;"><a href="?last=';
			d[b++] = a.profile.id;
			d[b++] = '">Dernières vidéos ajoutées</a></div>';
			if(a.profile.friends) {
				friends = a.profile.friends.length;
				d[b++] = '<hr /><div><h2 class="ui-priority-secondary">Utilisateurs suivis (';
				d[b++] = friends;
				d[b++] = ')</h2>';
				for (var e = 0; e < friends; e++) {
					d[b++] = '<a href="?user=';
					d[b++] = a.profile.friends[e].id;
					d[b++] = '"><img src="';
					d[b++] = a.profile.friends[e].image;
					d[b++] = '" title="';
					d[b++] = a.profile.friends[e].pseudo;
					d[b++] = '" style="margin:1px 2px;"/></a>';
				}
				d[b++] = '</div><div style="clear:left;text-align:center;font-weight:bold;margin:20px;"><a href="?friends=';
				d[b++] = a.profile.id;
				d[b++] = '">Leur actualité</a></div>';
			}
			if(a.profile.followers) {
				followers = a.profile.followers.length;
				d[b++] = '<hr /><div><h2 class="ui-priority-secondary">Utilisateurs abonnés (';
				d[b++] = followers;
				d[b++] = ')</h2>';
				for (var e = 0; e < followers; e++) {
					d[b++] = '<a href="?user=';
					d[b++] = a.profile.followers[e].id;
					d[b++] = '"><img src="';
					d[b++] = a.profile.followers[e].image;
					d[b++] = '" title="';
					d[b++] = a.profile.followers[e].pseudo;
					d[b++] = '" style="margin:1px 2px;"/></a>';
				}
				d[b++] = '</div><div style="clear:left;text-align:center;font-weight:bold;margin:20px;"><a href="?followers=';
				d[b++] = a.profile.id;
				d[b++] = '">Leur actualité</a></div>';
			}
            $("#tabs-playlist-profile").html(d.join(''));
			$("#menu").tabs({selected: '#tabs-playlist'});
        } else {
			dom("#header>div").html('<a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all" onmousedown="dom(\'#signup\').dialog(\'open\');"><span class="ui-button-icon-primary ui-icon ui-icon-arrowreturnthick-1-e"></span><span class="ui-button-text">Inscription</span></a> <a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-highlight ui-corner-all" onmousedown="dom(\'#login\').dialog(\'open\');"><span class="ui-button-icon-primary ui-icon ui-icon-power"></span><span class="ui-button-text">Login</span></a>');
			
			dom("#playlist>div").empty();
			
            if($("#tabs-playlist").length === 1) $("#menu").tabs("remove", '#tabs-playlist');
        }
    }
    if (a.profile != undefined && a.profile.playlists != undefined) {
        var f = [],
        c = [],
        h = b = 0;
        d = a.profile.playlists.length;
        if (a.profile.playlists) {
            c[h++] = '<li><a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all" onmousedown="dom(\'#addplaylist\').dialog(\'open\');"><span class="ui-button-icon-primary ui-icon ui-icon-video"></span><span class="ui-button-text">Ajouter une playlist</span></a></li>';
            for (var e = 0; e < d; e++) {
                f[b++] = '<option value ="';
                f[b++] = a.profile.playlists[e].id;
                f[b++] = '">';
                f[b++] = a.profile.playlists[e].title;
                f[b++] = "</option>";
                c[h++] = '<li><a href="#" onmousedown="deletePlaylist(';
                c[h++] = a.profile.playlists[e].id;
                c[h++] = ');" class="ui-button ui-button-icon-only ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-trash"></span><span class="ui-button-text">Supprimer cette playlist</span></a><img src="static/img/icons/';
                c[h++] = a.profile.playlists[e].image;
                c[h++] = '" alt="';
                c[h++] = a.profile.playlists[e].title;
                c[h++] = '" /><a href="#" onmousedown="ajaxCall(\'playlist\', {id:';
                c[h++] = a.profile.playlists[e].id;
                c[h++] = '});">';
                c[h++] = a.profile.playlists[e].title;
                c[h++] = "</a><br /><em>";
                c[h++] = a.profile.playlists[e].description;
                c[h++] = "</em></li>"
            }
        } else {
            f[b++] = "";
            c[h++] = "Inscris toi pour créer tes propres playlists !"
        }
        dom("#addvideo_playlist").html(f.join(""));
        $("#tabs-playlist-playlist").html(c.join(""))
    }
    if (a.profile != undefined && a.profile.feeds != undefined) {
        f = [];
        b = 0;
        d = a.profile.feeds.length;
        if (a.profile.feeds) {
            f[b++] = '<li><a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all" onmousedown="dom(\'#addfeed\').dialog(\'open\');"><span class="ui-button-icon-primary ui-icon ui-icon-signal-diag"></span><span class="ui-button-text">Ajouter un abonnement</span></a></li>';
            for (e = 0; e < d; e++) {
                f[b++] = '<li><a href="#" onmousedown="deleteFeed(';
                f[b++] = a.profile.feeds[e].id;
                f[b++] = ');" class="ui-button ui-button-icon-only ui-widget ui-state-default ui-corner-all"><span class="ui-button-icon-primary ui-icon ui-icon-trash"></span><span class="ui-button-text">Supprimer cet abonnement</span></a><img src="static/img/icons/';
                f[b++] = a.profile.feeds[e].image;
                f[b++] = '" alt="';
                f[b++] = a.profile.feeds[e].title;
                f[b++] = '" /><a href="#" onmousedown="ajaxCall(\'feed\', {url:\'';
                f[b++] = a.profile.feeds[e].link;
                f[b++] = "'});\">";
                f[b++] = a.profile.feeds[e].title;
                f[b++] = "</a><br /><em>";
                f[b++] = a.profile.feeds[e].description;
                f[b++] = "</em></li>"
            }
        } else f[h++] = "Inscris toi pour t'abonner à des flux !";
        $("#tabs-playlist-feed").html(f.join(""))
    }
    if (a.playlist != undefined) {
        c = [];
        b = 0;
        d = a.playlist.item.length;
		for (e = 0; e < d; e++) {
            c[b++] = '<li class="ui-state-default"><input type="checkbox" value="';
            c[b++] = e;
            c[b++] = '" title="Sélectionner cette vidéo" />';
			if (a.playlist.item[e].id != 0) {
                c[b++] = '<a href="?video=';
                c[b++] = a.playlist.item[e].id
            } else {
                c[b++] = '<a href="?feed=';
                c[b++] = a.playlist.item[e].link
            }
			c[b++] = '"><div><img class="thumbnail" src="';
			c[b++] = a.playlist.item[e].image;
            c[b++] = '" alt= "';
            c[b++] = a.playlist.item[e].title;
            c[b++] = '" width="80" height="60" /><img class="favicon" src="static/img/favicon/';
			c[b++] = a.playlist.item[e].author;
            c[b++] = '.ico" alt="';
            c[b++] = a.playlist.item[e].author;
			c[b++] = '" width="16" height="16" /><span>';
			var time=new Date(0);
			if(a.playlist.item[e].duration == -1) time.setSeconds(0);
			else time.setSeconds(a.playlist.item[e].duration);
			c[b++] = time.toGMTString().substring(20,25)
			c[b++] = '</span></div>';
            c[b++] = a.playlist.item[e].title;
            c[b++] = "</a>"
			if(a.playlist.item[e].users_id) {
				c[b++] = '<br /><br /><strong class="ui-priority-secondary">Par </strong><a href="?user=';
				c[b++] = a.playlist.item[e].users_id;
				c[b++] = '" class="user">';
				c[b++] = a.playlist.item[e].pseudo;
				c[b++] = '</a>';
			}
			c[b++] = '</li>';
        }
        if (d == 0) {
            c[0] = '<br /><li style="text-align:center;">Aucune vidéo trouvée</li>';
            dom("#playlist>h1").html("Playlist (0)")
        } else dom("#playlist>h1").html('<a href="' + a.playlist.rss + '" onclick="window.open(this.href);return false;"><img src="static/img/icons/feed.png" alt="' + a.playlist.rss + '" width="16" height="16" /></a> ' + a.playlist.title + " (" + d + ")");
        dom("#playlist>ul").html(c.join("")).sortable();
        playPlaylist(a)
    }
	
	
	
	
	
	
	// USER
    if (a.user) {
        d = [];
        b = 0;
		// LOGIN
        if (a.user.id) {
			if($("#tabs-user").length === 0) $("#menu").tabs("add", "#tabs-user", "USER", 2);
			
			$("#tabs-user").accordion('destroy').empty().html('<h3><a href="#">Son Profil</a></h3><div id="tabs-user-profile"></div><h3><a href="#">Ses Playlists ('+a.user.playlists.length+')</a></h3><ul id="tabs-user-playlist"><li></li></ul><h3><a href="#">Ses Abonnements ('+a.user.feeds.length+')</a></h3><ul id="tabs-user-feed"><li></li></ul>').accordion({collapsible:true, autoHeight:false});
			
			
			//$("#tabs-user-profile").html('<br /><p style="text-align:center;"><img src="images/loading.gif" alt="Loading" title="Loading" /><br />Loading...</p>');
			
			
			if (a.user.friend != undefined) {
				if (a.user.friend) {
					d[b++] = '<a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all" onmousedown="ajaxCall(\'unfollow\', {id:';
					d[b++] = a.user.id;
					d[b++] = '});"><span class="ui-button-icon-primary ui-icon ui-icon-person"></span><span class="ui-button-text">Ne plus suivre</span></a><br /><br />';
				}
				else {
					d[b++] = '<a href="#" class="ui-button ui-button-text-icon-primary ui-widget ui-state-default ui-corner-all" onmousedown="ajaxCall(\'follow\', {id:';
					d[b++] = a.user.id;
					d[b++] = '});"><span class="ui-button-icon-primary ui-icon ui-icon-person"></span><span class="ui-button-text">Suivre</span></a><br /><br />';
				}
			}
			d[b++] = '<img src="';
			d[b++] = a.user.image;
			d[b++] = '" width="80" height="80" alt="';
			d[b++] = a.user.pseudo;
			d[b++] = '" title="';
			d[b++] = a.user.pseudo;
			d[b++] = '" style="float:left;" /><div style="margin-left:85px;"><h2>';
			d[b++] = a.user.pseudo;
			d[b++] = '</h2><em>';
			d[b++] = a.user.nbVideos;
			d[b++] = ' Vidéos</em><br /><br />E-mail : <a href="mailto:';
			d[b++] = a.user.email;
			d[b++] = '">';
			d[b++] = a.user.email;
			d[b++] = '</a><br />Site Web : <a href="';
			d[b++] = a.user.web;
			d[b++] = '">';
			d[b++] = a.user.web;
			d[b++] = '</a><br /></div><div style="clear:left;text-align:center;font-weight:bold;margin:20px;"><a href="?last=';
			d[b++] = a.user.id;
			d[b++] = '">Dernières vidéos ajoutées</a></div>';
			if(a.user.friends) {
				friends = a.user.friends.length;
				d[b++] = '<hr /><div><h2 class="ui-priority-secondary">Utilisateurs suivis (';
				d[b++] = friends;
				d[b++] = ')</h2>';
				for (var e = 0; e < friends; e++) {
					d[b++] = '<a href="?user=';
					d[b++] = a.user.friends[e].id;
					d[b++] = '"><img src="';
					d[b++] = a.user.friends[e].image;
					d[b++] = '" title="';
					d[b++] = a.user.friends[e].pseudo;
					d[b++] = '" style="margin:1px 2px;"/></a>';
				}
				d[b++] = '</div><div style="clear:left;text-align:center;font-weight:bold;margin:20px;"><a href="?friends=';
				d[b++] = a.user.id;
				d[b++] = '">Leur actualité</a></div>';
			}
			if(a.user.followers) {
				followers = a.user.followers.length;
				d[b++] = '<hr /><div><h2 class="ui-priority-secondary">Utilisateurs abonnés (';
				d[b++] = followers;
				d[b++] = ')</h2>';
				for (var e = 0; e < followers; e++) {
					d[b++] = '<a href="?user=';
					d[b++] = a.user.followers[e].id;
					d[b++] = '"><img src="';
					d[b++] = a.user.followers[e].image;
					d[b++] = '" title="';
					d[b++] = a.user.followers[e].pseudo;
					d[b++] = '" style="margin:1px 2px;"/></a>';
				}
				d[b++] = '</div><div style="clear:left;text-align:center;font-weight:bold;margin:20px;"><a href="?followers=';
				d[b++] = a.user.id;
				d[b++] = '">Leur actualité</a></div>';
			}
			
			$("#tabs-user-profile").html(d.join(''));
			$("#menu").tabs({selected: "#tabs-user"});
        }
		// LOGOUT
		else {
            if($("#tabs-user").length === 1) $("#menu").tabs("remove", '#tabs-user');
		}
    }
	// PLAYLISTS
    if (a.user != undefined && a.user.playlists != undefined) {
        var f = [],
        c = [],
        h = b = 0;
        d = a.user.playlists.length;
        if (a.user.playlists) {
            for (var e = 0; e < d; e++) {
                f[b++] = '<option value ="';
                f[b++] = a.user.playlists[e].id;
                f[b++] = '">';
                f[b++] = a.user.playlists[e].title;
                f[b++] = "</option>";
                c[h++] = '<li><img src="static/img/icons/';
                c[h++] = a.user.playlists[e].image;
                c[h++] = '" alt="';
                c[h++] = a.user.playlists[e].title;
                c[h++] = '" /><a href="#" onmousedown="ajaxCall(\'playlist\', {id:';
                c[h++] = a.user.playlists[e].id;
                c[h++] = '});">';
                c[h++] = a.user.playlists[e].title;
                c[h++] = "</a><br /><em>";
                c[h++] = a.user.playlists[e].description;
                c[h++] = "</em></li>"
            }
        } else {
            f[b++] = "";
            c[h++] = "Inscris toi pour créer tes propres playlists !"
        }
        //$("#addvideo_playlist").html(f.join(""));
        $("#tabs-user-playlist").html(c.join(""))
    }
	// FEEDS
    if (a.user != undefined && a.user.feeds != undefined) {
        f = [];
        b = 0;
        d = a.user.feeds.length;
        if (a.user.feeds) {
            for (e = 0; e < d; e++) {
                f[b++] = '<li><img src="static/img/icons/';
                f[b++] = a.user.feeds[e].image;
                f[b++] = '" alt="';
                f[b++] = a.user.feeds[e].title;
                f[b++] = '" /><a href="#" onmousedown="ajaxCall(\'feed\', {url:\'';
                f[b++] = a.user.feeds[e].link;
                f[b++] = "'});\">";
                f[b++] = a.user.feeds[e].title;
                f[b++] = "</a><br /><em>";
                f[b++] = a.user.feeds[e].description;
                f[b++] = "</em></li>"
            }
        } else f[h++] = "Inscris toi pour t'abonner à des flux !";
        $("#tabs-user-feed").html(f.join(""))
    }
}
var test = [];
function playPlaylist(a) {
    if (flvplayerready) {
        currentChannel = a.playlist.link;
        currentPlaylist = a.playlist.item;
        test.rss = a.playlist.rss;
        test.link = a.playlist.link;
        test.swf = a.playlist.embed;
        playingChannel == 0 && playVideo(0)
    } else setTimeout(function() {
        playPlaylist(a)
    },
    100)
}
$(document).ready(function() {
    if (typeof JSON == "undefined") $.getScript("static/js/json2.js");
	if (!window._ate) $.getScript("http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e2ea65726bdb07c");
    loadUI();
	loadEvent();
    switchPlayer("flv");
});

