﻿// JScript File

// ==========================================================================
/**
 * Class that represents the blogger.
 */
var Blogger = Class.create();
Blogger.prototype = {
	uri: "",	
	entries: {},
	initialize: function(u,close){
		if(u){this.uri = u;}
		if(close){this.close = close;}
	},	
	buildURL: function(){
		var str = "http://"+this.uri+".blogspot.com/feeds/posts/default?alt=json-in-script&callback=window.activeBlogger.parseFeed";		
		return str;
	},	
	loadFeed: function(u,close){
	
	    if ($("wait")) $("wait").show();
	    if(u){this.uri = u;}
        if(!(close==null)){this.close = close;}
		window.activeBlogger = this;
		var headTag = document.getElementsByTagName('head')[0]; 
		var script = document.createElement("script");
		script.src = this.buildURL();
		script.language = "JavaScript";
		headTag.appendChild(script);
	},	
	parseFeed: function(json){
		this.entries = {};
		e = json.feed.entry;
		for(var i=0;i < e.length;i++){
			this.entries[i] = new BlogEntry(e[i]);
		}
		this.feedURL = json.feed.link[1].href;
		try {
			this.onsuccess(this);
      } catch (e) {}
	},	
	onsuccess: function(c){
	    $('content').innerHTML = "<div class='right pointer'><a href="+this.feedURL+" target='_blank'><img src='user_images/feed-icon.gif'></a></div> <div class='right pointer' onclick='$$(\".what\").each(function(item){item.show()});'> open </div><div class='right'>/</div><div class='right pointer' onclick='$$(\".what\").each(function(item){item.hide()});'> close </div>";	    
	    var months = {0: '01',1:'02',2:'03',3:'04',4:'05',5:'06',6:'07',7:'08',8:'09',9: '10',10:'11',11:'12'};
	    for(k in c.entries){
            try{
		        var entry = c.entries[k];		    
		        var eventDiv = $('content');
		        var div = document.createElement("div");	
		        var str = "";
		        str += "<div class='title2 pointer' onclick='javascript:javascript:new Effect.toggle($(\"blog_content"+k+"\"),\"blind\",{duration:0.3});'>" + rep(entry.title) + "</div>";
		        if(entry.content)
			        str += "<div class='what' id='blog_content"+k+"'>" 
			                    + rep(entry.content) 
			                    + "<div class='additionalInformation'>Updated: " 
			                    + DateConverter.JSDateToText(entry.updated)
			                    + "</div>" 
			                    + "</div>";		   
		        div.innerHTML = str;
		        eventDiv.appendChild(div);
		        $("wait").hide();	
		        if (this.close) $("blog_content"+k).toggle();
	        }catch(e){
		        alert(e.description);
	        }
	    }
        Element.hide(eventDiv);
        new Effect.Appear(eventDiv);
	}
};


// ==========================================================================
var BlogEntry = Class.create();
BlogEntry.prototype = {
	title: null,
	content: null,
	initialize: function(entry){
		this.title = entry.title.$t;
		this.content = entry.content.$t;		
		this.published = DateConverter.rfc3339toDate(entry.published.$t);
		this.updated = DateConverter.rfc3339toDate(entry.updated.$t);
	}
};