var GFeed={};

GFeed.Date = function(e,param){
	
	var t = new Date(e.publishedDate);
	
	var mo = t.getMonth() + 1;
	
	var d = t.getDate();
	
	var y = t.getFullYear().toString();
	
	var h = t.getHours();
	
	var mi = t.getMinutes();
	
	var g = y.substr(2);
	
	if (d < 10) {
		d = "0" + d;
	}
	if (mo < 10) {
		mo = "0" + mo;
	}
	if (mi < 10) {
		mi = "0" + mi;
	}
	
	this.format1  = d+"."+mo+"."+g+" "+h+":"+ mi;
	this.format2 = "<span>"+d+"."+mo+"."+g+"</span> | "+h+":"+ mi;

	if(param) return this[param];
	else return this.format1;
	
};

GFeed.Node = function(n, p){
	if (typeof(n) == "string") {
		this.n = document.createElement(n);
	}
	else {
		this.n = n;
	}
	if (!p) {
		return this.n;
	}
	for (r in p) {
		if (this.n[r] != null) {
			this.n[r] = p[r];
		}
		else {
			this.n.style[r] = p[r];
		}
	}
	return this.n;
};
GFeed.addAttr = function(n, p){
	this.n = n;
	if (!p) {
		return this.n;
	}
	for (r in p) {
		if (this.n[r] != null) {
			this.n[r] = p[r];
		}
		else {
			this.n.style[r] = p[r];
		}
	}
	return this.n;
};
GFeed.Var = function(e){

	GFeed.blank = GFeed.Node("img", {
		src: "http://www.ligazakon.ua/images/point.jpg",
		width: "5"
	});
	
	GFeed.arrow = GFeed.Node("img", {
		src: "http://www.ligazakon.ua/images/gfx/arrow_gray.gif"
	});
	
	GFeed.div = GFeed.Node("div", {
		className: "pubTitle"
	});
	
	GFeed.date = GFeed.Node("div", {
		className: "pubDate",
		color: "#d20000"
	});
	
	GFeed.descr = GFeed.Node("div", {
		className: "pubDescr",
		innerHTML: e.contentSnippet
	});
	
	GFeed.link = GFeed.Node("a", {
		href: e.link,
		target: "_blank"
	});
	
};
GFeed.Rss = function(url,id,size){

	var feed = new google.feeds.Feed(url);
	
	feed.setNumEntries(size);
	
	feed.load(function(result){
		
		var container = document.getElementById(id);
		
		if (result.error || !container) {
			return;
		}
		if (container.innerHTML) {
			container.innerHTML = "";
		}
		
		for(var i=0;i<result.feed.entries.length; i++){

			var entry = result.feed.entries[i];
			
			GFeed.Var(entry);

			GFeed.date.appendChild(document.createTextNode(GFeed.Date(entry,"format1")));
			
			if (id == "feed-conf"){
				if (entry.categories[0] == "Анонсированная" || entry.categories[0] == "Текущая") {
					GFeed.link.className = "bold";
					GFeed.link.title = "Задавайте вопросы уже сейчас!";
				}
				if (entry.categories[0]=="Текущая"){
					GFeed.div.innerHTML+="<img src='http://conf.ligazakon.ua/_site/src/conference/_pic/online2.gif' style='margin-right:5px;' />";
				}
				if (entry.categories[0]=="Завершенная") {
					GFeed.div.innerHTML+="<img src='http://ligazakon.ua/images/support/flag-close.gif' style='margin-right:5px;padding-bottom:1px;' />";
				}
			}
			GFeed.link.appendChild(document.createTextNode(entry.title));
			GFeed.link.appendChild(GFeed.blank);
			GFeed.link.appendChild(GFeed.arrow);
			GFeed.div.appendChild(GFeed.link);
			if (id == "feed-search-news"){
				GFeed.link.innerHTML = entry.title;
				GFeed.date.innerHTML = GFeed.Date(entry,"format2");
				GFeed.date.removeAttribute("style");
				container.appendChild(GFeed.date);
				container.appendChild(GFeed.link);
			}
			if (id=="feed-laws") {
				container.appendChild(GFeed.descr);
			}
			if (id=="feed-sos") {
				
				GFeed.Node(GFeed.date,{
					className: "",
					color: "#999999"
				});
				
				GFeed.Node(GFeed.div,{
					paddingBottom: "5px"
				});
				container.appendChild(GFeed.date);
				
			}
			
			container.appendChild(GFeed.div);
			
			if (id == "feed-conf") {
			
				container.appendChild(GFeed.date);
				
				container.appendChild(GFeed.descr);
				
			}
			
		}

	});

};

google.load("feeds","1",{nocss:true});