var $tigr_links = '';

function getSearchLinks()
{
    
    try{
    var $tigr_blog_id = '';
    if( TPToolbar && TPToolbar.src )
    {
        var src = TPToolbar.src ;
        $tigr_blog_id = src.substring(src.indexOf('blog_id') + 8, src.indexOf('&'));
    }
    else
    {
        $tigr_blog_id = $tigr('[rel=EditURI]').attr('href');
        if( $tigr_blog_id == undefined || !$tigr_blog_id )
        {
            if( blog_xid && blog_xid.length > 0 )
            {
                $tigr_blog_id = blog_xid;
            }
            else
            {
                return ('<li>Blog id missing</li>');
            }
        }
        else
        {
            var idx = $tigr_blog_id.indexOf('rsd/')
            if( idx < 0 )
            {
                return ('<li>Blog id missing from ' + loc + ' </li>');
            }
            else
                $tigr_blog_id = $tigr_blog_id.substring(idx + 4, $tigr_blog_id.length);
        }
    }
    var doInit = true;
    //*** on first time page load $tigr_links will have length 0, then only do init
    if($tigr_links.length >0)
      	doInit = false;
    var mineURL = 'http://api.typepad.com/blogs/' + $tigr_blog_id + '/post-assets.js';
    
    $tigr.ajax({
        dataType: 'jsonp',
        url: mineURL,
	error : function (xhr, textStatus, thrownError) { 
		$tigr('#yolink_popover_results').empty();
		var msg = "%0A from:Typepad account"+ "%0A siteURL: "+encodeURIComponent(window.location.href) +"%0A ajaxURL: "+encodeURIComponent(mineURL)+" %0A statusCode: "+ xhr.status +"%0A statusText: "+ textStatus+ "%0A Error: "+ thrownError;
		tigr.yolink.Widget._oops(msg,"Typepad");
	},
        success: function ( feed )
        {
            var arrHtm = [];
            try{
            $tigr.each(feed.entries, function( i, data )
            {
                var title = data.title;
                var url = data.permalinkUrl;
                if( title == null )
                {
                    title = data.filename;
                    title = title.replace(/-/g, ' ');
                    if( title.length > 20 )
                    {
                        title = (title.substring(0, 20) + '....');
                    }
                }
                arrHtm.push('<li><a href=' + url + ' >' + title + '</a></li>');
            }
                );
            $tigr_links = arrHtm.join('');
	    if(doInit)
	    {
		    $tigr_init();		    
            }
           
           if(feed.entries.length == 0 )
	   {               
		$tigr('#yolink_popover_results').empty();
		var msg = "%0A from: Typepad Account" +"%0A siteURL: "+  encodeURIComponent(window.location.href) +  "%0A ajaxURL: " + encodeURIComponent(mineURL) + "%0A message:feed from jsonp call empty" ;            	
		tigr.yolink.Widget._oops(msg,"Typepad");	             
           }else  
           if($tigr_links.length == 0 )
	   {               
		$tigr('#yolink_popover_results').empty();
		var msg = "%0A from: Typepad Account" +"%0A siteURL: "+  encodeURIComponent(window.location.href) +  "%0A ajaxURL: " + encodeURIComponent(mineURL) + "%0A message:feed from jsonp call empty" ;            	
		tigr.yolink.Widget._oops(msg,"Typepad");	             
           }
           
           
         }catch(e){
		$tigr('#yolink_popover_results').empty();
		var msg = "%0A from:Typepad account"+ "%0A siteURL: "+encodeURIComponent(window.location.href) +"%0A jsErr: "+e.message;
		tigr.yolink.Widget._oops(msg,"Typepad");
    
   	}
     
      }
    });    
    return $tigr_links;
   }catch(e){
		$tigr('#yolink_popover_results').empty();
		var msg = "%0A from:Typepad account"+ "%0A siteURL: "+encodeURIComponent(window.location.href) +"%0A jsErr: "+e.message;
		tigr.yolink.Widget._oops(msg,"Typepad");
    
   }
   
} 
;


function $tigr_init()
{

tigr.yolink.Widget.initialize(
{
	display			: 'form',
	getSearch		: function(){  return getSearchLinks(); },                
	base    	        : 'http://www.sixapart.com/typepad/',
	popover			: true,
	showTools   	        : 'result',
	selectAll               : true,
	share                   : 'local',
	googledocs              : 'local',
	fblike                  : 'local',
	tweet                   : 'local',
	preview                 : 'tab',
	auto                    : false,
	apikey                  :  $tigr_apikey,
	location                : '#yolink_searchbox',
	showHide                : true
} );

        
}

$tigr(document).ready(function()
{
	getSearchLinks();    
});

