var APHRSS={
    api:"http://www.aphonda.co.th/honda2008/rssadmin/",
    feed:function(opt){
        var _this=this;
        var option = $.extend({
            id:0,
            firstPage:true
        },opt);
         return [
            _this.api,
            "ctrl/web/rsscontent/loadrss.ashx?",
            option.firstPage?"parentid":"rssgroupid",
            "="+option.id+"&sort=",
            option.firstPage?"Priority":"StartDate&dir=desc",
            "&start=0",//+(option.start||0),
            "&limit=50"//+(option.limit||10)
        ].join("");
    },
    load:function(opt){
        var _this=this;
        var option = $.extend({
            id:0,
            firstPage:true
        },opt);
        var url=[
            _this.api,
            "ctrl/web/rsscontent/loadrss.ashx?dataType=json&",
            option.firstPage?"parentid":"rssgroupid",
            "="+option.id+"&sort=",
            option.firstPage?"Priority":"StartDate&dir=desc",
            "&start="+(option.start||0),
            "&limit="+(option.limit||10)
        ].join("");
        $.ajax({
			url:url,
			type:"get",
			dataType:"jsonp",
			success:function(data){
			    if(option.container){
			        var ct=$(option.container);
			        ct.html("");
			        $.each(data.items,function(i,o){
			            var conf = {
			                blankImage:_this.api+"xresources/images/s.gif",
			                serviceImage:_this.api+"ctrl/web/rsscontent/loadrssimage.ashx?id="
			            };
			            var tpl = (option.tpl||_this.tpl)(i,o,data,option);
			            ct.append(_this.bind(tpl,o,conf,option));
			        });
			    }
			    if(typeof(option.callback)=="function")
			        option.callback(option,data);
			}
		});
    },
    tpl:function(index,obj,result,option){
        return [
             "<div class='rss-item'>"
            ,"  <h1>{title}</h1>"
            ,"  <img src='{rss:blankImage}' style='background-image:url({rss:serviceImage}{guid});' width='50' height='50' />"
            ,"  <p>{description}</p>"
            ,"  <a href='{link}' class='more' target='_blank'>more</a>"
            ,"</div>"
        ].join("");
    },
    bind:function(tpl,o,conf,option){
        if(conf){
            for(var key in conf){
                tpl=tpl.replace(eval("/{rss:"+key+"}/gi"),conf[key]);
            }
        }
        if(option){
            var funcRx = eval("/{([a-z,A-Z,0-9,_]+):([^}]+)}/gi");
            for(var mt = funcRx.exec(tpl);mt!=null;mt = funcRx.exec(tpl)){
                tpl=tpl.replace(eval("/"+mt[0]+"/gi"),(typeof(option[mt[1]])=="function")?option[mt[1]](o[mt[2]]):mt[0]);
            }
        }
        for(var key in o){
            tpl=tpl.replace(eval("/{"+key+"}/gi"),o[key]);
        }
        return tpl;
    }
};
