﻿  $(function() {
            //$("#ajaxloader").show();            
            $.getJSON(newBloggerURL,
            function(data) {
            document.getElementById("posts").innerHTML = "";
            
            var tempTitle = GetURlTitle();
            var showTitleFlag = false;
                         
                $.each(data.feed.entry, function(i, postentry) {
                
                    var posttitle = postentry.title.$t;
                    var postsummary = postentry.content.$t;                  
                    var postdate = postentry.published.$t;      
                    var month = postdate.substr(5,2);
				    var year = postdate.substr(0,4);
				    var day = postdate.substr(8,2);
				    postdate = convertAtomDateString(postdate);
                   
                    if(tempTitle == "")
                    {                    
                    document.getElementById("posts").innerHTML += "<div class='entry'><b>" + postdate + "</b>";    
                    document.getElementById("posts").innerHTML += "<h2>" + posttitle + "</h2>";    
                    document.getElementById("posts").innerHTML += "<p>" + postsummary + "</p></div>"; 
                    }
                    else
                    {                     
                     if(CompareURLAndPostTitle(tempTitle,posttitle) && !showTitleFlag)
                     {
                        document.getElementById("posts").innerHTML += "<div class='entry'><b>" + postdate + "</b>";    
                        document.getElementById("posts").innerHTML += "<h2>" + posttitle + "</h2>";    
                        document.getElementById("posts").innerHTML += "<p>" + postsummary + "</p></div>";
                        showTitleFlag = true;
                     }                     
                    }
                                           
                });                 
                //$("#ajaxloader").hide("fast");                
            });
        });
        
        
//convert an Atom-formatted date string to a javascript-compatible date string
function convertAtomDateString(str)
{
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
	var weekday= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var year, month, date, dt, wkDay;
	year = str.slice(0,4);	
	month = months[1*str.slice(5,7)];		//Jan-Dec
	date = str.slice(8,10);		//01-31
	dt = month + " " + date + ", " +  year;		
	var d = new Date(dt);
	wkDay = weekday[d.getDay()];
	dt = wkDay + ", " + dt;		
	return dt; 
}

//Used to make JavaScript sort case-insensitive
function caseInsensitiveCompare(a, b) {
    var anew = a.toLowerCase();
    var bnew = b.toLowerCase();
    if (anew < bnew) return -1;
    if (anew > bnew) return 1;
    return 0;
}

function GetTitleForURL(urlTitle)
{
   //Remove Special characters 
   urlTitle =  urlTitle.replace(/[^a-zA-Z 0-9]+/g,'');
   
   //Replace any "-" by ""
   urlTitle = urlTitle.replace(/-/g, "");
   
   //Replace spaces by "-"
   urlTitle = urlTitle.replace(/ /g, "-");
   
   //Replace "--" by "-"
   urlTitle = urlTitle.replace(/--/g, "-");
   
   //alert(urlTitle)
   var i;
   var breakChar=0;
   
   if(urlTitle.length > 50){   
    for(i=45; i< urlTitle.length; i++)
    {
        if(urlTitle[i] == "-")
        {
          breakChar = i;
          break;
        }
    }     

        if(breakChar > 45){                     
        urlTitle = urlTitle.substring(0,breakChar);
        }
        else{
        urlTitle = urlTitle;             
        }
   }
      
   urlTitle = urlTitle.toLowerCase();
   //alert(urlTitle);
   return urlTitle;
}

//Show Recent Posts
ShowRecentPosts();
function ShowRecentPosts(){
     var recentPostUrl;
     recentPostUrl = _yourBlogUrl + "?orderby=updated&alt=json-in-script&callback=recentPostsCallback&max-results=" + _numRecentPosts;
     var script = document.createElement("script");
	 script.setAttribute("type", "text/javascript");
	 script.setAttribute("src", recentPostUrl);
	 document.documentElement.firstChild.appendChild(script);	
     }
     
function GetURlTitle(){
 var tempURL = newBloggerURL;
 var titleStartPos = tempURL.indexOf("&title=");
 var tempTitle = "";
 if (titleStartPos > -1)
 {
    var titleEndPos = tempURL.indexOf("&callback=");
    tempTitle = tempURL.substring(titleStartPos + 7,titleEndPos); 
 }
  return tempTitle;  
}

function CompareURLAndPostTitle(urlTitle,postTitle){
var titleMatchFlag = false;
var tempUrlTitle = urlTitle;
var tempPostTitle = GetTitleForURL(postTitle);
//var compareUrlTitle = tempUrlTitle.substring(0,tempUrlTitle.indexOf("-"));
//var comparePostTitle = tempPostTitle.substring(0,tempPostTitle.indexOf("-"));

var compareUrlTitle = tempUrlTitle.split("-");
var comparePostTitle = tempPostTitle.split("-");

var firstURLWord = compareUrlTitle[0]; 
var firstPostWord = comparePostTitle[0];
var SecondURLWord = compareUrlTitle[1]; 
var SecondPostWord = comparePostTitle[1];

if(firstURLWord.indexOf(firstPostWord) > -1 && SecondURLWord.indexOf(SecondPostWord) > -1){
titleMatchFlag = true;
}
return titleMatchFlag;
}

function RemoveWhiteSpaces(str){
str = str.replace(/\s/g, "");
return str;
}


