/**
 * dime.feedload
 * 2011.06.14
 */

if(typeof(dime) == "undefined") var dime = {};

dime.feedload = function(){
	
	////////updateSec
	var updateSec = {
		init:function(){
			this.container = $("#updateSec .updateSecBody");
		},
		onload:function(){
			this.container.empty();
			//console.log(this.container);
			////Asset
			var updateBox_lv1= $('\
			<dl class="updateBox_lv1 clearfix">\
				<dt class="imgborder"><a href="#" class="alpha"><img alt="" width="114" /></a></dt>\
				<dd>\
					<div class="date"></div>\
					<div class="title">\
						<h2><!--[if lte IE 7 ]><table cellpadding="0" cellspacing="0"><tr><td><![endif]-->\
						<a href="#"></a><span class="new">New</span>\
						<!--[if lte IE 7 ]></td></tr></table><![endif]--></h2>\
					</div>\
				</dd>\
			</dl>')
			
			var updateBox_lv2 = $('\
			<div class="updateBox_lv2">\
				<dl class="clearfix">\
					<dt></dt>\
					<dd><a href="#"></a></dd>\
				</dl>\
			</div>');
			//
			var container = this.container
			$.each(dime.feed["recent"].item, function(i){ 
				if(i < 3){
					var box = updateBox_lv1.clone();
					$("dt a", box).attr({ "href":this.link });
					$("dt a img", box).attr({ "src":this.image });
					$(".date", box).append(this.date);
					$(".title h2 a", box).attr({ "href":this.link })
					.append(this.catchcopy ? this.catchcopy: this.title);
					
					
				}else{
					var box = updateBox_lv2.clone();
					$("dt", box).append(this.date);
					$("dd a", box).attr({ "href":this.link })
					.append(this.catchcopy ? this.catchcopy: this.title);
				}
				container.append(box);
			});
		}
		
	}
	////
	////////recomSec
	var recomSec = {
		init:function(){
			this.container = $("#recomSec");
		},
		onload:function(){
			
			////Asset
			var recomBox = $('\
			<dl class="recomBox clearfix">\
				<dt></dt>\
				<dd><a href="#"></a></dd>\
			</dl>');
			//
			
			var container = this.container;
			$.each(dime.feed["recom"].item, function(i){ 
				var box = recomBox.clone();
				$("dt", box).append(this.date);
				$("dd a", box).attr({ "href":this.link })
				.append(this.title);
				container.append(box);
			});
		}
	}
	////
	////////categorySec
	var categorySec = {
		init:function(){
			this.containers = {};
			this.containers["lifestyle"]  =  $("#categorySec .category_lifestyle .container");
			this.containers["newproduct"] =  $("#categorySec .category_newproduct .container");
			this.containers["entame"]     =  $("#categorySec .category_entame .container");
			this.containers["community"]  =  $("#categorySec .category_community .container");
		},
		onload:function(){
			
			////Asset
			var ciBox = $('\
			<dl class="ciBox clearfix">\
				<dt></dt>\
				<dd><a href="#"></a></dd>\
			</dl>');
			//
			
			var containers = this.containers;
			
			$.each(containers, function(key, val){
				$.each(dime.feed[key].item, function(i){ 
					var box = ciBox.clone();
					$("dt", box).append(this.date);
					$("dd a", box).attr({ "href":this.link })
					.append(this.title);
					val.append(box);
				});
			});
			
		}
	}
	////
	
	//init
	updateSec.init();
	recomSec.init();
	categorySec.init();
	
	//load
	$.ajax({
		url:"http://tf.digital-dime.com/feed.js",
		cache:true,
		type:"GET",
		dataType:"script",
		timeout:10000,
		error: function(){
			updateSec.container.empty();
			updateSec.container.html('<p class="loading">データを取得できませんでした</p>');
		},
		success: function(){
			updateSec.onload();
			recomSec.onload();
			categorySec.onload();
		}
	});	
	
	
	////pr_release
	var pr_release = {
		init:function(){
			this.container  =  $("#categorySec .category_pr_release .container");
		},
		onload:function(){
			////Asset
			var ciBox = $('\
			<dl class="ciBox clearfix">\
				<dt></dt>\
				<dd><a href="#"></a></dd>\
			</dl>');
			//
			var container = this.container;
			$.each(dime.feed["pr_release"].item, function(i){ 
				var box = ciBox.clone();
				$("dt", box).append(this.date);
				$("dd a", box).attr({ "href":this.link })
				.append(this.title);
				container.append(box);
			});
			
		}
	}
	////
	//init
	pr_release.init();
	//load
	$.ajax({
		url:"http://tf.digital-dime.com/feed_pr_release.js",
		cache:true,
		type:"GET",
		dataType:"script",
		timeout:10000,
		error: function(){
		},
		success: function(){
			pr_release.onload();
		}
	});	
}

/* execute */
$(function(){
	if($("body#top").length){
		dime.feedload();
	}
});

