/******************************************************************************
 * vim:fdl=0:fdo=insert:
 *
 * Copyright (C), 2008-2009, iHao.Net.
 *
 * Project: iSite
 *
 * Filename: core.js
 *
 * Description: Just mash it up, Just for fun
 *
 * Needs: google api, jQuery 1.2.6
 *
 * History:
 *   1. Date: 2009.01.07 20:04:01
 *      Author: iHao
 *      Modification: Scaffold basic functions
 *   2. Date: 2009.01.08 02:00:00
 *      Author: iHao
 *      Modification: add 'now' page [load from fanfou feed]
 *                    add C.F.getnow()
 *   3. Date: 2009.01.08 18:01:00
 *      Author: iHao
 *      Modification: add 'blog' page [load from blogger feed]
 *                    add C.F.getblog()
 *                    C.F.load() changed
 *   4. Date: 2009.01.08 21:01:00
 *      Author: iHao
 *      Modification: add 'photo' page [load from flickr json]
 *                    add C.F.getphotos()
 *   5. Date: 2009.06.05 11:01:00
 *      Author: iHao
 *      Modification: remove blog and photo mashup cause of 'GFW'
 *                    Change 'C.F.getblog'	=> 'C.F.getblogd'
 *                    Change 'C.F.getphotos'	=> 'C.F.getphotosd'
 *                    Change 'C.T.blog'		=> 'C.T.blogd'
 *                    Change 'C.T.photos'	=> 'C.T.photosd'
 *                    Add    'C.D.blog'
 *                    Add    'C.D.photos'
 *                    Add    'C.T.blog'
 *                    Add    'C.T.photos'
******************************************************************************/
var C = {
	O : { //{{{ options
		t : null
	},//}}}
	I : function (options) {//{{{init
		$.extend(C.O,options);
		C.F.init();
	},//}}}
	F : {
		init : function () {//{{{init
			location.hash ? (function(C){C.F.load(location.hash);})(C) : (function(){C.F.load('#now');})(C);
		},//}}}
		load : function (n,obj) {//{{{load
			!obj&&(obj = $('a[href='+n+']'));
			obj.blur();
			if ($(obj).hasClass('current')) return;
			C.F.showLoading();
			n = n.substring(1);
			$(obj).siblings().removeClass('current');
			$(obj).addClass('current');
			try {
				eval('C.F.get'+n)();
			} catch (e) {
				C.F.writeContent(C.F.getContent(n));
				C.F.hideLoading();
			} finally {
				n = obj = null;
			}
			return;
		},//}}}
		writeContent : function () {//{{{writeContent
			var obj = arguments[1]?arguments[1]:'.block';
			$(obj)[0].innerHTML = arguments[0];
			arguments[2]&&(C.F.hideLoading());
		},//}}}
		showLoading : function () {//{{{showLoading
			C.O.t == null ? C.O.t = null : clearTimeout(C.O.t);
			$('a.run').show();
		},//}}}
		hideLoading : function () {//{{{hideLoading
			C.O.t == null ? C.O.t = null : clearTimeout(C.O.t);
			C.O.t = setTimeout(function(){$('a.run').hide();},1000);
		},//}}}
		getContent : function (n) {//{{{ getContent
			var t = C.F.getTpl(n);
			var d = C.F.getData(n);
			try {
				return C.F.render(t,d);
			} catch (e) {
				return e||'<var>ERROR:Shit happend!</var>';
			} finally {
				t = d = n = null;
			}
		},//}}}
		getTpl : function (t) {//{{{ getTpl
			try {
				return eval('C.T.'+t)||'<var>ERROR:No template named '+t+' !</var>';
			} finally {
				t = null;
			}
		},//}}}
		getData : function (d) {//{{{ getData
			try {
				return eval('C.D.'+d)||'<var>ERROR:No data width '+d+' !</var>';
			} finally {
				d = null;
			}
		},//}}}
		render : function (t,d) {//{{{ render
			try {
				var o = t.replace(/###head###/g,d.head);
				o = o.replace(/###time###/g,d.time);
				o = o.replace(/###data###/g,d.data);
				return o||'<var>ERROR:Rending template with data error!</var>';
			} finally {
				t = d = o = null;
			}
		},//}}}
		getnow : function () {//{{{ getnow
			//get contents from http://blog.ihao.net/words/
			C.F.writeContent(C.T.now);
			var url = "http://blog.ihao.net/feeds/words/";
			var o = ['<ul>'];
			var feed = new google.feeds.Feed(url);
			feed.setNumEntries(20);
			feed.load(function(result) {
				if (!result.error) {
					$.each(result.feed.entries, function(i, entry) {
						var cls = (i%2==0)?'even':'odd';
						var num = ((i+1)<10)?('0'+''+(i+1)):(i+1);
						o[o.length] = '<li class="'+cls+'" onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)">';
						o[o.length] = '<i>'+num+'.</i>';
						o[o.length] = '<a class="now_link" href="'+entry.link+'" target="_blank">';
						o[o.length] = entry.title.replace('ihao','<strong>ihao</strong>');
						o[o.length] = '</a>';
						o[o.length] = '<span class="now_date">';
						o[o.length] = C.F.toDate(entry.publishedDate);
						o[o.length] = '</span>';
						o[o.length] = '</li>';
						cls = num = null;
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading NOW data error!</var></li>';
				}
				o[o.length] = '</ul>';
				C.F.writeContent(o.join(''),'.nowlist',true);
				url = o = null;
			});
		},//}}}
		//reborn
		getblog : function () {//{{{getblog
			//get contents from http://blog.ihao.net/
			C.F.writeContent(C.T.blog);
			var url = "http://blog.ihao.net/feeds/rss/";
			var o = ['<ul>'];
			var feed = new google.feeds.Feed(url);
			feed.setNumEntries(20);
			feed.load(function(result) {
				if (!result.error) {
					$.each(result.feed.entries, function(i, entry) {
						var cls = (i%2==0)?'even':'odd';
						var num = ((i+1)<10)?('0'+''+(i+1)):(i+1);
						o[o.length] = '<li class="'+cls+'" onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)" onclick="C.F.showorhide(this)">';
						o[o.length] = '<i>'+num+'.</i>';
						//o[o.length] = '<strong>'+entry.author+'</strong>';
						o[o.length] = '<a class="now_link" href="'+entry.link+'" target="_blank">';
						o[o.length] = entry.title;
						o[o.length] = '</a>';
						o[o.length] = '<span class="now_date">';
						o[o.length] = C.F.toDate(entry.publishedDate);
						o[o.length] = '</span>';
						o[o.length] = '<div class="hides">'+entry.content+'</div>';
						o[o.length] = '</li>';
						cls = num = null;
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading BLOG data error!</var></li>';
				}
				o[o.length] = '</ul>';
				C.F.writeContent(o.join(''),'.bloglist',true);
				url = o = null;
			});
		},//}}}
		getshare : function () {//{{{getshare
			//get contents from http://www.google.com/reader/shared/03155843597492348954
			C.F.writeContent(C.T.share);
			var url = "http://www.google.com/reader/public/atom/user%2F03155843597492348954%2Fstate%2Fcom.google%2Fbroadcast";
			var o = ['<ul>'];
			var feed = new google.feeds.Feed(url);
			feed.setNumEntries(20);
			feed.load(function(result) {
				if (!result.error) {
					$.each(result.feed.entries, function(i, entry) {
						var cls = (i%2==0)?'even':'odd';
						var num = ((i+1)<10)?('0'+''+(i+1)):(i+1);
						o[o.length] = '<li class="'+cls+'" onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)" onclick="C.F.showorhide(this)">';
						o[o.length] = '<i>'+num+'.</i>';
						o[o.length] = '<strong>'+entry.author+'</strong>';
						o[o.length] = '<a class="now_link" href="'+entry.link+'">';
						o[o.length] = entry.title;
						o[o.length] = '</a>';
						o[o.length] = '<span class="now_date">';
						o[o.length] = C.F.toDate(entry.publishedDate);
						o[o.length] = '</span>';
						o[o.length] = '<div class="hides">'+entry.content+'</div>';
						o[o.length] = '</li>';
						cls = num = null;
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading SHARE data error!</var></li>';
				}
				o[o.length] = '</ul>';
				C.F.writeContent(o.join(''),'.bloglist',true);
				url = o = null;
			});
		},//}}}
		//GFWed
		//so change getphotos to getphotosd
		getphotos : function () {//{{{getphotos
			//get contents from flickr
			C.F.writeContent(C.T.photos);
			var url = 'http://api.flickr.com/services/feeds/photos_public.gne?id=69772409@N00&lang=en-us&format=json&jsoncallback=?';
			var o = ['<ul>'];			
			$.getJSON(url,function(data) {
				if (data) {
					$('li.comments').html('Last Update:'+data.modified.substring(0,10));
					$.each(data.items, function(i, item) {
						o[o.length] = '<li onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)" onclick="C.F.showorhide(this)">';
						o[o.length] = '<a class="now_link" target="_blank" href="'+item.media.m.replace('_m','_b')+'" style="z-index:'+(20-i)+'">';
						o[o.length] = '<img src="'+item.media.m.replace('_m','_s')+'" />';
						o[o.length] = '<strong>'+item.title+'</strong>';
						o[o.length] = '</a>';
						o[o.length] = '</li>';
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading PHOTO data error!</var></li>';
				}
				o[o.length] = '<div class="clear"></div></ul>';
				C.F.writeContent(o.join(''),'.photoslist',true);
				if (data) {$('.photoslist ul li a.now_link').lightBox();}
				url = o = null;
			});
		},//}}}
		showorhide : function (obj) {//{{{showorhide
			($(obj).find('.hides:hidden').length>0)?(function(o){
				$(o).find('.hides').slideDown();
			})(obj):(function(o){
				$(o).find('.hides').slideUp();
			})(obj);
		},//}}}
		toDate : function (d) {//{{{toDate
			return new Date(d).toLocaleString();
		},//}}}
		liOver : function (obj) {//{{{liOver
			$(obj).addClass('hover');
		},//}}}
		liOut : function (obj) {//{{{liOut
			$(obj).removeClass('hover');
		}//}}}
	},
	D : {//{{{Datas
		'blogd' : {
			'head' : 'F**K GFW!',
			'data' : 'F**K GFW, my blog is GFWed.',
			'time' : '09.05.16'
		},
		'photosd' : {
			'head' : 'F**K GFW!',
			'data' : 'F**K GFW, my album is GFWed tooooooo.',
			'time' : '09.06.5'
		},
		'comments' : {
			'head':'Give me five',
			'data':'"COMMENTS" page is in Construction... or send to <a href="http://blog.ihao.net/">here</a>',
			'time':'09.01.07'
		},
		'about' : {
			'head':'About this guy',
			'data':'MacVIM, ZOC6, Cornerstone [K], MindNode, VIM, TotalCommand, SecureCRT, Subversion, Filezilla, Firefox, Firebug, Photoshop, Freemind, Axure, Vmware and <a href="http://wakoopa.com/ihao" target="_blank">MORE</a><br /><hr />Python, Perl, Xhtml, CSS, JavaScript, jQuery, PHP5, MYSQL, SQLite3<br /><hr />Snow Leopard, Ubuntu, Archlinux, FreeBSD<br /><hr />HangZhou',
			'time':'10.02.01'
		}
	},//}}}
	T : {//{{{Templates
		'blog' : '<h2>Fresh blog\'s entries</h2><ul class="blockmenu"><li class="time"><a href="http://blog.ihao.net/" target="_blank">read more</a></li><li class="comments">Last Update:-</li></ul><div class="datas bloglist">loading blog\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://blog.ihao.net/" target="_blank">read more</a></li></ul>',
		'photos' : '<h2>Yes, It\'s me</h2><ul class="blockmenu"><li class="time"><a href="http://www.flickr.com/photos/ihao/" target="_blank">read more</a></li><li class="comments">Last Update:-</li></ul><div class="datas photoslist">loading photos\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://www.flickr.com/photos/ihao/" target="_blank">read more</a></li></ul>',
		'now' : '<h2>Words from live</h2><ul class="blockmenu"><li class="time"><a href="http://blog.ihao.net/words/" target="_blank">read more</a></li><li class="comments">Last Update:-</li></ul><div class="datas nowlist">loading now\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://blog.ihao.net/words/" target="_blank">read more</a></li></ul>',
		'share' : '<h2>My google reader\'s shares</h2><ul class="blockmenu"><li class="time"><a href="http://www.google.com/reader/shared/03155843597492348954" target="_blank">read more</a></li><li class="comments">Last Update:-</li></ul><div class="datas bloglist">loading share\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://www.google.com/reader/shared/03155843597492348954" target="_blank">read more</a></li></ul>',
		'comments' : '<h2>###head###</h2><ul class="blockmenu"><li class="time"></li><li class="comments">Last Update:###time###</li></ul><div class="datas">###data###</div><ul class="blockmenu"><li class="tags"></li><li class="more"></li></ul>',
		'about' : '<h2>###head###</h2><ul class="blockmenu"><li class="time"></li><li class="comments">Last Update:###time###</li></ul><div class="datas">###data###</div><ul class="blockmenu"><li class="tags"></li><li class="more"></li></ul>',
		'blogd' : '<h2>###head###</h2><ul class="blockmenu"><li class="time"></li><li class="comments">Last Update:###time###</li></ul><div class="datas">###data###</div><ul class="blockmenu"><li class="tags"></li><li class="more"></li></ul>',
		'photosd' : '<h2>###head###</h2><ul class="blockmenu"><li class="time"></li><li class="comments">Last Update:###time###</li></ul><div class="datas">###data###</div><ul class="blockmenu"><li class="tags"></li><li class="more"></li></ul>'
	}//}}}
};
//{{{other
var loadBlogbuzz = function() {
	$.getJSON(
		'http://pipes.yahoo.com/pipes/pipe.run?_id=Ys9dPOlw3RG5hXDXPxJ3AQ&_render=json&_callback=?',
		function(data) {
		  var container = $('#blogbuzz ul');
		  container.html('');
		  $.each(data.value.items, function(i, item) {
			  var li = $('<li/>').appendTo(container);
			  var a = $('<a/>').attr({
					'href' : item.link,
					'title' : item.title
				  }).text(item.title).appendTo(li);
			  $('<p/>').attr({
					'class' : 't-snippet'
				  }).html(item.description.replace('ihao','<span class="b-highlight">ihao</span>')).appendTo(li);
		  });
	});
};
function loadSearch() {
	var options = new GsearcherOptions();
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
	var searchControl = new google.search.SearchControl();
	searchControl.addSearcher(new google.search.WebSearch(),options);
	//searchControl.addSearcher(new google.search.BlogSearch(),options);
	searchControl.draw(document.getElementById("resoult"));
	searchControl.execute("ihao press");
};
//}}}
