/*
 * Author:  kyori.hu@gmail.com
 * Provide: http://www.maopao.com/
 * Updated: 2011-7-30
 */

//loading emoji..
var oHead = document.getElementsByTagName('head').item(0);
var oScript= document.createElement("script");
oScript.type = "text/javascript";
oScript.src="js/emoji.js";
oHead.appendChild(oScript);

//var mp = {};
//(function(){
var page = 1;
var mp = {
		cache: {},
		load: function(templateName, data, callback){
				//alert(self.cache[template]);
			var self = this;
			if (self.cache[templateName]){
				var html = self.render(self.cache[templateName], data, function(html){
					//alert("cache..");
					callback(html);
				});

			} else {
				self.fetch(templateName, function(template){
					var html = self.render(template, data, function(html){
						callback(html);
					});
				});
			} 
		},
		fetch: function(templateName, callback){
			var self = this;
			$.ajax({
				url: "tpl/" + templateName + ".tpl",
				cache: true,
				//jsonpCallback: +template+"Callback",
				success: function(template){
					self.cache[templateName] = template;
					callback(self.cache[templateName]);
				},
				dataType: "text"
			});

		},
		render: function(template, data, callback){
			$.getScript("js/mustache.js", function(){
				//alert(Mustache.to_html(template, data));
				callback(emoji_ajax(Mustache.to_html(template, data)));
			});
		}
	};
//})();

(function($){
	$.fn.loading = function(options){
		var settings = {
			url: "",
			view: "",
			height: 1000,
			index: 0,
			num: 3,
			loading: true,
			interval: 500,
			goback: false,
			waiting: ""
		};

		var self = this;
		var init = function(){ $(window).scrollTop(0); };
		window.setTimeout(init, 100);
		if(options) $.extend(settings, options);

		$(window).bind("scroll", function(){
			if(settings.goback){
				$(window).scrollTop() > settings.goback ? $("#goback").fadeIn("slow") : $("#goback").fadeOut("slow");
				$("#goback").css({bottom:"40px", right: ($(window).width()-900)/2+190});
			}

			if(self.height() > settings.height){
				$("#waiting").remove();
				return false;
			}
			else if($(window).scrollTop() > self.height() + 1000){
				if(settings.loading){
					page++;
					settings.loading = false;
					$("."+settings.waiting+":last").after("<span id='waiting'></span>");
					settings.index++;
					window.setTimeout(function(){
						$.ajax({
							type: "GET",
							url: settings.url+"&page="+page,
							cache: false,
							success: function(json){
								mp.load(settings.view, json, function(data){
									$("#waiting").remove();
									//for(var i=0; i<settings.num; i++) self.append($(data).eq((settings.index-1)*settings.num+i));
									self.append(data);
									settings.loading = true;
								});
							}
						});
					}, settings.interval);
				}
			}
		});
	}
})(jQuery);

$(function(){
	$(".guide li a").focus(function(){ $(this).blur(); });
	$(".keyword").bind({
		"click": function(){ this.value == this.defaultValue ? this.value='' : this.focus(); },
		"blur": function(){ this.value == '' ? this.value = this.defaultValue : ''; }
	});
	if($(".hotphoto").length) $.getScript("js/lazyload.js", function(){ $(".hotphoto img").lazyload({ placeholder:"img/loading.gif", effect:"fadeIn" }); });
	//if($(".info").length) $("#mp_info").loading({ url:"message", height:2000, num:1, interval:500, goback:800, waiting:"mp_info"});
	$.ajax({
		type: "GET",
		url: "infoCountAjax",
		cache: false,
		success: function(json){
			if(json.count<1){
				$("#count").hide();
			}else{
				$("#count").show();
			}
			$("#count").html(json.count);
			//alert($("#count").text());
		}
	})
});

