var doc = jQuery(document);

   doc.ready(function () {
   ident.useInwardEdges = true
   doc.bind('ident:update', loadContent);

   ident.search('http://friendfeed.com/nathanielban');
});
     /*
     *  Copyright 2009, Glenn Jones
     *  Licensed under the MIT license.
     *  http://identengine.com/license/
     *  More information: http://identengine.com/
     */
    
    // Page functions
    // -------------------------------------------------
   
    var days = 1; // Number of days of content to show

    // Adds click event to form when page loads
    var doc = jQuery(document);
    doc.ready(function () {
    
        // Wire update notice to render page content
        doc.bind('ident:update', statusUpdate);
        doc.bind('ident:update', headingUpdate);
        doc.bind('ident:error', displayError);
        
        webAddress.elt = "#webaddress";
        webAddress.startAnimate();
        
         
                // Adds onclick event to form button
        jQuery("#subbutton").click(function () {
        	startDemo()            
        });
        
        // Add on enter keyup event
        jQuery("#urlinput").keyup(function(e) {
			if(e.keyCode == 13) {
				startDemo();
			}
		}); 

      });  
     
     
         function startDemo(){
         ident.reset();
        	resetAllContent();
        	webAddress.stopAnimate();    
        
          	jQuery('<h2 id="search-heading">Searching for your content</h2>').appendTo('#results');
        	jQuery('<div id="searching"><img class="spinner" src="images/ajax-loader.gif" /> Looking across the web to find your content.</div>').appendTo('#results');
            
            var url = jQuery("#urlinput").attr("value");
            ident.search(url);
	}

     
     
     
    function headingUpdate(){
        jQuery('#search-heading').html("Searching for your profiles - Found " + ident.identities.length);
    }
    
    
    var alertTimerId;
     
     
    function statusUpdate(e, type){
        if(type != null){
            if(type == 'content-added')
                renderContent();
                    
        }
        clearTimeout (alertTimerId);
        alertTimerId = setTimeout ("delayedStatusUpdate()", 500);
    } 
    
    
    function delayedStatusUpdate(){
         if(ident.isSearching()){
            jQuery('#searching').html('<img class="spinner" src="images/ajax-loader.gif" /> Looking across the web to find your content...');
        }else{
           renderProfiles();
           jQuery('#search-heading').html("Loading content...");
           loadContent();
        }
    }
    
    
    function loadContent(){ 
        // A number of data services that should make a good lifestream
        ident.findContent('twitter.com', 'Status', 'hAtom' );
        ident.findContent('flickr.com', 'Images', 'Atom' ); 
        ident.findContent('del.icio.us', 'Bookmarks', 'Rss'); 
        ident.findContent('last.fm', 'Activity', 'Rss' );
        ident.findContent('ffffound.com', 'Images', 'Rss' );
        ident.findContent('tumblr.com', 'Lifestream', 'Rss' );
        ident.findContent('vimeo.com', 'Video', 'Rss' );
    }
     
     
    function renderProfiles(){
    
     
        jQuery('#profiles').html('');  
            
        var hCard = ident.combinedProfile;

        var profile = jQuery('<div class="profile"></div>').appendTo('#profiles');
        var titlegroup = jQuery('<div class="title-group"></div>').appendTo(profile); 
        
        if( hCard.photo ) 
            if(hCard.photo.length > 0 )
                renderPhoto(profile,hCard.photo[0].value); 
          
        // Use top username
        if(ident.topFormattedName() != '')
            jQuery('<div class="fn">' + ident.topFormattedName() +  '</div>').appendTo(titlegroup);
        else
            jQuery('<div class="fn">' + hCard.fn +  '</div>').appendTo(titlegroup); 
                        
        if( hCard.title ) 
            if(hCard.title.length > 0 )
                jQuery('<div class="title">' + hCard.title[0].value +  '</div>').appendTo(titlegroup); 
            
        if( hCard.org ) 
            if(hCard.org.length > 0 )
                if(hCard.org[0]['organization-name'])
                    jQuery('<div class="organization-name">' + hCard.org[0]['organization-name'] +  '</div>').appendTo(titlegroup); 


        // Render first array item of note - only first 5 sentences of that item    
        if( hCard.note ){  
          if( hCard.note.length > 0 ){
               jQuery('<div class="note">' + ident.sentenceTruncate(hCard.note[0].value,5) +  '</div>').appendTo(profile);  
          }
        }
          
        if( hCard.adr ) 
        {
            var adr = jQuery('<div class="adr"></div>').appendTo(profile);                
            for (var x = 0; x < hCard.adr.length; x++) {
                var address = hCard.adr[x];
                if( address['extended-address'] )
                    renderArray(adr,address['extended-address']);
                    
                if( address['street-address'] )
                    renderArray(adr,address['street-address']);
                    
                if( address.locality )
                    jQuery('<div class="locality">' + address.locality +  '</div>').appendTo(adr); 
                    
                if( address.region )
                    jQuery('<div class="region">' + address.region +  '</div>').appendTo(adr); 
                    
                if( address['postal-code'] )
                    jQuery('<div class="postal-code">' + address['postal-code'] +  '</div>').appendTo(adr); 
                    
                if( address['country-name'] )
                    jQuery('<div class="country-name">' + address['country-name'] +  '</div>').appendTo(adr);                  
            }
        }  
              
   
        if( hCard.tel )
            renderItemArray(profile,hCard.tel);   
                       
        if( hCard.email )
            renderItemArray(profile,hCard.email); 
              
        if(ident.topDeclaredProfileUrl() != '')
            jQuery('<div class="url"><a href="' + ident.topDeclaredProfileUrl() +  '">' + ident.topDeclaredProfileUrl() +  '</a></div>').appendTo(profile); 
        else 
            renderItemArray(profile,hCard.url);
                      
  
        if(ident.identities.length > 0){
             var ul = jQuery('<ul class="profile-list"></ul>').appendTo('.profile'); 
             for (var x = 0; x < ident.identities.length; x++) {
                 if(ident.identities[x].name != '')
                    jQuery('<li class="floatclear"><a href="' + ident.identities[x].profileUrl  + '"><div class="icon ' + ident.identities[x].spriteClass +  '"></div> <span class="icon-label">' + ident.identities[x].name + '</span></a> <span class="username">(' + ident.identities[x].username + ')</span></li>').appendTo(ul);   
                else
                    jQuery('<li class="floatclear"><a href="' + ident.identities[x].profileUrl  + '"><div class="icon ' + ident.identities[x].spriteClass +  '"></div> <span class="icon-label">' + ident.identities[x].domain + '</span></a></li>').appendTo(ul); 
               }
         } 
                 
    }
    
    
    // Sorts objects into order using the name property
    ident.sortByDate = function(a, b) {
        var x = a['published-datetime'];
        var y = b['published-datetime'];
        return ((x > y) ? -1 : ((x < y) ? 1 : 0));
    };
    
    
    function renderContent(){
   
        jQuery('#results').html('');
        jQuery('#searching').css({'display' : 'none'});
        jQuery('#search-heading').css({'display' : 'none'});
        
        /*
        // Refresh button for content - needs more work
        
        var refresh = jQuery('<input id="refresh" type="button" value="Refresh lifestream" />').appendTo('#results');
        jQuery(refresh).click(function () {
            // Forced clear down of content data
            jQuery('#results').html('');
            ident.entries = new Array();
            ident.events = new Array();
            ident.relCollection = new Array();
            ident.history = new Array();
            loadContent();
        });
        */

        // Sort the collection of domains into order  
        ident.entries.sort(ident.sortByDate);    

        // Create of cutoff date
        var cutoff = new Date();
        cutoff.setDate(cutoff.getDate()-days);
       
         for (var i = 0; i < ident.entries.length; i++) {
            
             if(ident.entries[i]['published-datetime'] > cutoff)
             {

                var div = jQuery('<div class="item"></div>').appendTo('#results');
                jQuery('<div><img width="40" title="' + ident.entries[i].name + '" src="public/images/icons/' + ident.entries[i].name.replace(/[.,-, ]/g,'')  +  '.png" /></div>').appendTo(div);   
                var item = ident.entries[i];

                switch (item.type){
                    case 'Status':
                        renderStatus( item, div );
                        break;
                    case 'Images':
                        renderItem( item, div );
                        break;
                    case 'Bookmarks':
                        renderBookmarks( item, div );
                        break;    
                    case 'Events':
                        renderEvents( item, div );
                        break;
                   case 'Activity':
                        renderActivity( item, div );
                        break;

                    default:
                        renderItem( item, div )    
                }
            }
        }
    }
    
    
    // Renders a standard feed item
    function renderItem( item, div ){
        jQuery(div).addClass( 'standard' );
    
        if(item['entry-title']){
            if(item.link)
                jQuery('<div class="entry-title"><a href="' + item.link + '">' + item['entry-title'] + '</a></div>').appendTo( div );
            else
                jQuery('<div class="entry-title">' + item['entry-title'] + '</div>').appendTo( div );
        }
        
        if(item['entry-content']){
            if(item['entry-title'] != item['entry-content'])
                jQuery('<div class="entry-content">' + addHyperlinks(item['entry-content']) + '</div>').appendTo( div );
        }
        
        if(item['published-datetime'])
            jQuery('<div class="published">' + item['published-datetime'].toLocaleDateString() + ' - ' + item['published-datetime'].toLocaleTimeString() + '</div>').appendTo(div);
         
    }
    
     
    // Renders a status item Twitter / Identi.ca etc
    function renderStatus( item, div ){
        jQuery(div).addClass( 'status' );
    
        var body = '' 
        if(item['entry-title'])
            body = item['entry-title'];
        else
           body = item['entry-content'];    
        
        body = addHyperlinks( body );   
        body = addStatusAccountLinks( body, 'http://' + item.domain + '/' );
          
        
        jQuery('<div class="entry-title">' + body + '</div>').appendTo( div );
       
        if(item['published-datetime'])
            jQuery('<div><a class="published" href="' + item.bookmark.link + '">' + item['published-datetime'].toLocaleDateString() + ' - ' + item['published-datetime'].toLocaleTimeString() + '</a></div>').appendTo(div);
         
    }
    
    
    function renderBookmarks( item, div ){
        renderItem( item, div );
        jQuery(div).addClass( 'bookmark' );
    }
    
    
    function renderEvents( item, div ){
        renderItem( item, div );
        jQuery(div).addClass( 'event' );
    }
    
    
   function renderActivity( item, div ){
        renderItem( item, div );
        jQuery(div).addClass( 'activity' );
    }
       

    function addHyperlinks( text ){
        if(ident.isArray(text)){
            if(text.length > 0){
                text = text[0];
            }else{
                text= '';
            }
        }
           
        if( text != ''){
            parts = text.split(' ');
            for(var x = 0; x <= parts.length-1; x++){
                if(parts[x].indexOf('http://') == 0)
                    parts[x] = '<a href="' + parts[x] + '">' + parts[x] + '</a>';
            }
            text = parts.join(' ');
        }
        return text;
    }
    
    
    function addStatusAccountLinks( text, url ){
        if(ident.isArray(text))
            text = text[0];
            
        if( text != ''){
            parts = text.split(' ');
            for(var x = 0; x <= parts.length-1; x++){
                if(parts[x].indexOf('@') == 0){
                    if( parts.length > x ){
                        parts[x+1] = '<a href="'+ url + parts[x+1] + '">' + parts[x] + parts[x+1] + '</a>';
                        parts[x] = '';
                    }
                }
            }
            text = parts.join(' ');
        }
        return text;
    }
    
    
    function renderArray( element, array, prefix, classname ){
        if(ident.isArray(array)){
            for (var i = 0; i < array.length; i++) {
                if( prefix )
                    jQuery('<div class="' + classname +'">' + prefix + ': ' + array[i] +  '</div>').appendTo(element); 
                else
                    jQuery('<div class="' + classname +'">' + array[i] +  '</div>').appendTo(element);   
            }
        }
     }
     
     
     function renderItemArray( element, array, prefix, classname ){
        for (var i = 0; i < array.length; i++) {
            var type = '';
            if(array[i].type != '')
                type = ' (' + array[i].type + ')';
            
            if( prefix )
                jQuery('<div class="' + classname +'">' + prefix + ': ' + array[i].value  + type + '</div>').appendTo(element); 
            else
                jQuery('<div class="' + classname +'">' + array[i].value + type + '</div>').appendTo(element);   
        }
     }
     
     
    function renderPhoto( element, url ){
        jQuery('<div><img class="photo" src="' + url +  '" /></div>').appendTo(element); 

    }
    
    
    function displayError(e, type){
        resetAllContent();
        jQuery('<h2 id="search-heading"></h2>').appendTo('#results');
        jQuery('<div id="searching"></div>').appendTo('#results');
        
        jQuery('#searching').html('<h2 class="error">' + type + '</h2>');
        jQuery('#searching').css({'display' : 'block'});
        jQuery('#search-heading').css({'display' : 'none'});
     }

     

    // Clear the debug html
    function resetContent(){
        //jQuery('#results').html('');
    }
    
        // Clear the debug html
    function resetAllContent(){
        jQuery('#profiles').html('');
        jQuery('#results').html('');
    }


