

jQuery(function($) {


	google.load("feeds", "1",{"callback" : initializeBR});
	//initializeBR();

			/**
			 * printf() for Javascript
			 */
			function sprintf() {
                if( sprintf.arguments.length < 2 ) {
                    return;
                }
                var data = sprintf.arguments[ 0 ];
                for( var k=1; k<sprintf.arguments.length; ++k ) {
                    switch( typeof( sprintf.arguments[ k ] ) ){
                        case 'string':
                            data = data.replace( /%s/, sprintf.arguments[ k ] );
                            break;
                        case 'number':
                            data = data.replace( /%d/, sprintf.arguments[ k ] );
                            break;
                        case 'boolean':
                            data = data.replace( /%b/, sprintf.arguments[ k ] ? 'true' : 'false' );
                            break;
                            default:
                           /// function | object | undefined
                        break;
                    }
                }
                return(data);
            }
            if( !String.sprintf ) {
            	String.sprintf = sprintf;
            }


            /**
             * generate text showing the age of the bookmark's latest post
             */
            function getAge(days, hours){
                if (days > 1) {
                    return String.sprintf("%d days ago", days);
                } else if (days==1){
                	return "yesterday ";
                } else if (days< 1 && hours > 1) {
                	return String.sprintf("%d hrs ago", hours);
                } else if (days< 1 && hours == 1) {
                	return "in the last hour";
                } else if (days < 1 && hours < 1) {
                	return "just recently";
                }
            }
            /* add age and other info to bookmark*/
            function addAge(feed, bookmark, anchor, divID, previewtoggle, last_posttitle){
                var $li =$(bookmark),
                 $a =$(anchor),
                 $toggle =$(previewtoggle),
                 $title =$(last_posttitle),
                 now = (new Date()).getTime(),
                 then = (new Date()).getTime(),
                 ageInDays,
                 ageInHours,
                 randomAge,
                 ageMsg=[],
                 $snippet,
                 entry;
                entry = feed.entries[0]
                /*,entry1= feed.entries[1] */;
                try {
                    then = new Date(entry.publishedDate).getTime();
                } catch (dateException) {
                    // do noting
                }
                
                ageInDays =  Math.floor((now-then)/(1000*60*60*24)),
                ageInHours = Math.floor((now-then)%(1000*60*60*24)/3600000);

                try {
                                         $entryTitle =$("<p></p>").html(entry.title);
                                        $a.attr({title: $entryTitle.html()});
                } catch (titleException) {
                    $a.attr({title: ""});
                }
				if (!isNaN(then)){
					// insert age into list-item to allow sorting by age of post
                    $li.attr({age:then});
				} else {
		            /* add "very old" age to bookmarks with no feed-url */
					randomAge = Math.floor(Math.random()*1000001);
					$li.attr({age:randomAge});
				}

                                if (!(isNaN(ageInDays) || isNaN(ageInHours))){
                    ageMsg.push(getAge(ageInDays, ageInHours));
					           
                                        $toggle.html(ageMsg.join('')).attr({feedurl:feed.link});
                }
                                			$title.html(entry.title);
			$title.attr( {
				href  : entry.link,
				title : $("<p></p>").html(entry.contentSnippet).html(),
				rel   : "external",
				rev   : "bookmark"
			});

			/*$title.parent().append($("<a></a>").attr({
					href : entry1.link,
					title: $("<p></p>").html(entry1.contentSnippet).html(),
					rel   : "external",
					rev   : "bookmark"
			}).html(entry1.title));*/
                                return false;
            }
            /* add "very old" age to bookmarks with no feed-url */
            function addZeroAge(zeroAgeBookmark){
                var $zali =$(zeroAgeBookmark),
                randomAge = Math.floor(Math.random()*1000001);
                if ($zali !== null ) {
                    $zali.attr({age:randomAge});
                }
                return false;
            }

            /* add jQuery-oberserver to enable mouse-clicks*/
            function addFeedControl(preview, feed, name){
                    var feedControl = new google.feeds.FeedControl();
                    feedControl.addFeed(feed, name);
                    feedControl.draw($(preview).get(0));
                    return false;
            }

			/*
			 *
			 */
            function feedreading_limit_display(){ 
            	var
                 liArray2 = $("#feedreading_blogroll_2 > li") ;	        				for (var i=0; i < liArray2.length; i++) { 
	        					if(i > -1){
	                				$(liArray2[i]).css("display","none");
	        					}
	        				}
	        								return false;
        	}
        	
            /*
             * Sort the blogroll with tsort()
             */
            function feedreading_automatic_sort() { 			var $allCompleted ,
			            				$hasCompleteAge2=true, isComplete2=false, $liSortArray2 = $("#feedreading_blogroll_2>li") 
          				;            		for (var i=0; i < $liSortArray2.length; i++) {
						var $age2 = $($liSortArray2[i]).attr("age");
						if ($age2 === null || $age2 == "" || isNaN($age2)) {
							$hasCompleteAge2 = false;
						}
            		}
					if ($hasCompleteAge2 && !isComplete2) {
						$("#feedreading_blogroll_2>li").tsort({order:"desc",attr:"age"});
						isComplete2=true;
					}
          								$allCompleted =
									            				$hasCompleteAge2          				;				
				if ($allCompleted){
				clearInterval(myInterval);
												}
								return false;
            }

	function feedreading_rolling(){
						var
				         							
						$blogroll_2 = $('#feedreading_blogroll_2'), $blogroll_2_size=$blogroll_2.find('li').size() , $blogroll_2_limit = ($blogroll_2_size>5)? (5): ($blogroll_2_size-1) 
									;
			
						
				         							
				if ($blogroll_2_size > 1) {
					$("#feedreading_blogroll_2 > li").css("display","none");
					$blogroll_2.feedReadingBlogrollSpy($blogroll_2_limit,4000);
				}
						return false;
	}

            /*
             * add observer to blogroll()
             */
	function feedreading_category_observer() {
	    		$("ul.feedreading_blogroll_bookmarklist").bind("mouseenter",function(event){
			var $eventTarget =$(event.target);
			$eventTarget.parents(".feedreading_blogroll_bookmarklist").addClass("mouseover");
		});
		$("ul.feedreading_blogroll_bookmarklist").bind("mouseleave",function(event){
			var $eventTarget =$(event.target);
			$eventTarget.parents(".feedreading_blogroll_bookmarklist").removeClass("mouseover");
		});
		return false;
	}
			
      function initializeBR() { 						google.feeds.lookupFeed("http://alldolldup.typepad.com/", function() {
							var url10= this.url, feed10 = new google.feeds.Feed(this.url);
                            feed10.load(function(result_10){
                                if (!result_10.error) {
                                addAge(result_10.feed, "#feedreading_bookmark_10", "#feedreading_anchor_10", "#feedreading_info_10", "#feedreading_previewtoggle_10", "#frbl_last_posttitle_10");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_10");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://barbieobsession.blogspot.com/", function() {
							var url12= this.url, feed12 = new google.feeds.Feed(this.url);
                            feed12.load(function(result_12){
                                if (!result_12.error) {
                                addAge(result_12.feed, "#feedreading_bookmark_12", "#feedreading_anchor_12", "#feedreading_info_12", "#feedreading_previewtoggle_12", "#frbl_last_posttitle_12");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_12");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://barbierelatedstuff.blogspot.com/", function() {
							var url13= this.url, feed13 = new google.feeds.Feed(this.url);
                            feed13.load(function(result_13){
                                if (!result_13.error) {
                                addAge(result_13.feed, "#feedreading_bookmark_13", "#feedreading_anchor_13", "#feedreading_info_13", "#feedreading_previewtoggle_13", "#frbl_last_posttitle_13");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_13");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://brooklynstars-forever.blogspot.com/", function() {
							var url14= this.url, feed14 = new google.feeds.Feed(this.url);
                            feed14.load(function(result_14){
                                if (!result_14.error) {
                                addAge(result_14.feed, "#feedreading_bookmark_14", "#feedreading_anchor_14", "#feedreading_info_14", "#feedreading_previewtoggle_14", "#frbl_last_posttitle_14");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_14");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://terrigoldphoto.blogspot.com/", function() {
							var url15= this.url, feed15 = new google.feeds.Feed(this.url);
                            feed15.load(function(result_15){
                                if (!result_15.error) {
                                addAge(result_15.feed, "#feedreading_bookmark_15", "#feedreading_anchor_15", "#feedreading_info_15", "#feedreading_previewtoggle_15", "#frbl_last_posttitle_15");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_15");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://www.dollsofcolor.org/blog/", function() {
							var url8= this.url, feed8 = new google.feeds.Feed(this.url);
                            feed8.load(function(result_8){
                                if (!result_8.error) {
                                addAge(result_8.feed, "#feedreading_bookmark_8", "#feedreading_anchor_8", "#feedreading_info_8", "#feedreading_previewtoggle_8", "#frbl_last_posttitle_8");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_8");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://www.bebarbie.net/", function() {
							var url16= this.url, feed16 = new google.feeds.Feed(this.url);
                            feed16.load(function(result_16){
                                if (!result_16.error) {
                                addAge(result_16.feed, "#feedreading_bookmark_16", "#feedreading_anchor_16", "#feedreading_info_16", "#feedreading_previewtoggle_16", "#frbl_last_posttitle_16");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_16");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://www.masterstrokecanada.com/dressmakerdetails/weblog/", function() {
							var url21= this.url, feed21 = new google.feeds.Feed(this.url);
                            feed21.load(function(result_21){
                                if (!result_21.error) {
                                addAge(result_21.feed, "#feedreading_bookmark_21", "#feedreading_anchor_21", "#feedreading_info_21", "#feedreading_previewtoggle_21", "#frbl_last_posttitle_21");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_21");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://thedollcafe.blogspot.com", function() {
							var url9= this.url, feed9 = new google.feeds.Feed(this.url);
                            feed9.load(function(result_9){
                                if (!result_9.error) {
                                addAge(result_9.feed, "#feedreading_bookmark_9", "#feedreading_anchor_9", "#feedreading_info_9", "#feedreading_previewtoggle_9", "#frbl_last_posttitle_9");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_9");
                  			}
                                });

						});

                                          						google.feeds.lookupFeed("http://fashiondollreview.blogspot.com/", function() {
							var url17= this.url, feed17 = new google.feeds.Feed(this.url);
                            feed17.load(function(result_17){
                                if (!result_17.error) {
                                addAge(result_17.feed, "#feedreading_bookmark_17", "#feedreading_anchor_17", "#feedreading_info_17", "#feedreading_previewtoggle_17", "#frbl_last_posttitle_17");
                  			} else {
                      			addZeroAge("#feedreading_bookmark_17");
                  			}
                                });

						});

                                          
																																feedreading_category_observer();
				return false;
                }


      $.fn.feedReadingBlogrollSpy = function (limit, interval) {
    	    limit = limit || 4;
    	    interval = interval || 4000;

    	    return this.each(function () {
    	        // 1. setup
    	            // capture a cache of all the list items
    	            // chomp the list down to limit li elements
    	        var $list = $(this),
    	            items = [], // uninitialised
    	            currentItem = limit,
    	            total = 0, // initialise later on
    	            height = $list.find('li:first').height();

    	        // capture the cache
    	        $list.find('li').each(function () {
    	            //items.push('<li>' + $(this).html() + '</li>');
    	            items.push($(this));
    	        });
    	        /*for (var i=0; i < li_items.length; i++){
    	            //items.push('<li>' + $(this).html() + '</li>');
    	            items.push($(this));
    	        } */

    	        total = items.length;

    	        $list.wrap('<div class="spyWrapper" />').parent().css({ height : height * (limit + 2) });

    	        $list.find('li').filter(':gt(' + (limit - 1) + ')').remove();

    	        // 2. effect
    	        function spy() {
			if(!$list.hasClass("mouseover")){
			    // insert a new item with opacity and height of zero
			    var $insert = $(items[currentItem]).css({
				height : 0,
				opacity : 0,
				display : 'none'
			    }).prependTo($list);

			    // fade the LAST item out
			    $list.find('li:last').animate({ opacity : 0}, 1000, function () {
				// increase the height of the NEW first item
				$insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);

				// AND at the same time - decrease the height of the LAST item
				// $(this).animate({ height : 0 }, 1000, function () {
				    // finally fade the first item in (and we can remove the last)
				    $(this).remove();
				// });
			    });

			    currentItem++;
			    if (currentItem >= total) {
				currentItem = 0;
			    }
			}
			setTimeout(spy, interval);
    	        }

    	        spy();
    	    });
    	};
});
            
                    