var i, n, elem, node;
var updateStage, pickupStage, photoStage, cocollMarcheStage, recommendAStage, recommendBStage, recommendCStage;
var updateNode, pickupNode, photoNode, cocollMarcheNode, recommendANode, recommendBNode, recommendCNode;
var topXml, pickupXml;
var topXmlURL = "/top.xml";
var pickupXmlURL = "/nursery/api/pickup";
//var pickupXmlURL = "/pickup.xml";

jQuery.noConflict();
var j$ = jQuery;


function init(){
	/*
	 * Ajax表示用html要素の指定
	 */
	updateStage = jQuery("#news_Area");
	pickupStage = jQuery("#pickup_Area");
	photoStage = jQuery("#photo_Area");
	cocollMarcheStage = jQuery("#cocollMarche_Area");
	recommendAStage = jQuery("#recommendA_Area");
	recommendBStage = jQuery("#recommendB_Area");
	recommendCStage = jQuery("#recommendC_Area");

	/*
	 * xml読み込み
	 */
	//Ajaxの初期化
	jQuery.ajaxSetup({cache:false});
	//toppage.xmlの読み込み開始
	jQuery.ajax({
		url:topXmlURL,
		dataType:"xml",
		success:onTopXmlLoadSuccess
	});
	//今月のピックアップ保育園の読み込み開始
	jQuery.ajax({
		url:pickupXmlURL,
		dataType:"xml",
		success:onPickupXmlLoadSuccess
	});
	//Flash表示
	var flashvars = {};
	var params = {};
	var attributes = {};
	swfobject.embedSWF("/preloader.swf", "flash_Area", "420", "250", "9.0.0", false, flashvars, params, attributes);
}

/*
 * イベントハンドラ
 */

//
function onTopXmlLoadSuccess(data,dataType){
	var _img, _ul, _ol, _li, _p, _title, _href;
	//XMLデータの受け取り
	topXml = data;
	//データノードの指定
	updateNode = jQuery("update",topXml);
	photoNode = jQuery("photo",topXml);
	cocollMarcheNode = jQuery("cocollMarche",topXml);
	recommendANode = jQuery("recommendA",topXml);
	recommendBNode = jQuery("recommendB",topXml);
	recommendCNode = jQuery("recommendC",topXml);

	//更新情報
	updateStage.empty();
	_ul = jQuery("<ul>");
	updateStage.append(_ul);
	jQuery("item",updateNode).each(function(idx){
		_href = jQuery(this).attr("href");
		switch(_href){
			case "#":
				_li = jQuery("<li>"+jQuery(this).text()+"</li>");
				break;
			default:
				_li = jQuery("<li><a href='"+jQuery(this).attr("href")+"' target='"+jQuery(this).attr("target")+"'>"+jQuery(this).text()+"</a></li>");
				break;
		}
		_ul.append(_li);
	});
	//今週の一枚
	photoStage.empty();
	jQuery("item",photoNode).each(function(idx){
		_li = jQuery("<li class='photo_pt0"+(idx + 1)+" mr19'>");
		photoStage.append(_li);
		_href = jQuery(this).attr("href");
		switch(_href){
			case "#":
				_img = jQuery("<div><img src='"+jQuery(this).attr("src")+"' alt='' width='96' height='96' /></div>");
				_p = jQuery("<p class='center'>"+jQuery(this).text()+"</p>");
				break;
			default:
				_img = jQuery("<div><a href='"+jQuery(this).attr("href")+"' target='"+jQuery(this).attr("target")+"'><img src='"+jQuery(this).attr("src")+"' alt='' width='96' height='96' /></a></div>");
				_p = jQuery("<p class='center'><a href='"+jQuery(this).attr("href")+"' target='"+jQuery(this).attr("target")+"'>"+jQuery(this).text()+"</a></p>");
				break;
		}
		_li.append(_img);
		_li.append(_p);
	});
	jQuery("li:last",photoStage).removeClass("mr19");
	
	//ココルマルシェ売れ筋ランキング
	cocollMarcheStage.empty();
	_ul = jQuery("<ul>");
	cocollMarcheStage.append(_ul);
	jQuery("item",cocollMarcheNode).each(function(idx){
		_li = jQuery("<li>");
		_ul.append(_li);
		_href = jQuery(this).attr("href");
		switch(_href){
			case "#":
				_img = jQuery("<img src='"+jQuery(this).attr("src")+" width='60' height='60' border='0'/>");
				_name = jQuery(jQuery("name",this).text()+'<br />'+jQuery("price",this).text());
				break;
			default:
				_img = jQuery("<a href='"+jQuery(this).attr("href")+"'><img src='"+jQuery(this).attr("src")+"' width='60' height='60' border='0'/></a>");
				_name = jQuery("<a href='"+jQuery(this).attr("href")+"'>"+jQuery("name",this).text()+'<br />'+jQuery("price",this).text()+"</a>");
				break;
		}
		_li.append(_img);
		_li.append(_name);
	});


	//見逃せない！注目コンテンツ
	recommendAStage.empty();
	_ul = jQuery("<ul id='osusumeContents'>");
	recommendAStage.append(_ul);
	jQuery("item",recommendANode).each(function(idx){
		_li = jQuery("<li>");
		_ul.append(_li);
		_href = jQuery(this).attr("href");
                switch(_href){
			case "#":
				_img = jQuery("<div><img src='"+jQuery(this).attr("src")+"' width='120' height='60' /></div>");
				_title = jQuery("<h3>"+jQuery("title",this).text().replace(/^\s+/,"").substring(0,18)+"</h3>");
				_p = jQuery("<p>"+jQuery("text",this).text().replace(/^\s+/,"").substring(0,38)+"</p>");
				break;
			default:
				_img = jQuery("<div><a href='"+jQuery(this).attr("href")+"'><img src='"+jQuery(this).attr("src")+"' width='120' height='60' /></a></div>");
				_title = jQuery("<h3><a href='"+jQuery(this).attr("href")+"'>"+jQuery("title",this).text().replace(/^\s+/,"").substring(0,18)+"</a></h3>");
				_p = jQuery("<p>"+jQuery("text",this).text().replace(/^\s+/,"").substring(0,38)+"</p>");
				break;
		}
		_li.append(_img);
		_li.append(_title);
		_li.append(_p);
	});

	recommendBStage.empty();
	_ul = jQuery("<ul id='osusumeContents'>");
	recommendBStage.append(_ul);
	jQuery("item",recommendBNode).each(function(idx){
		_li = jQuery("<li>");
		_ul.append(_li);
		_href = jQuery(this).attr("href");
		switch(_href){
			case "#":
				_img = jQuery("<div><img src='"+jQuery(this).attr("src")+"' width='120' height='60' /></div>");
				_title = jQuery("<h3>"+jQuery("title",this).text().replace(/^\s+/,"").substring(0,18)+"</h3>");
				_p = jQuery("<p>"+jQuery("text",this).text().replace(/^\s+/,"").substring(0,38)+"</p>");
				break;
			default:
				_img = jQuery("<div><a href='"+jQuery(this).attr("href")+"'><img src='"+jQuery(this).attr("src")+"' width='120' height='60' /></a></div>");
				_title = jQuery("<h3><a href='"+jQuery(this).attr("href")+"'>"+jQuery("title",this).text().replace(/^\s+/,"").substring(0,18)+"</a></h3>");
				_p = jQuery("<p>"+jQuery("text",this).text().replace(/^\s+/,"").substring(0,38)+"</p>");
				break;
		}
		_li.append(_img);
		_li.append(_title);
		_li.append(_p);
	});

	recommendCStage.empty();
	_ul = jQuery("<ul id='osusumeContents'>");
	recommendCStage.append(_ul);
	jQuery("item",recommendCNode).each(function(idx){
		_li = jQuery("<li>");
		_ul.append(_li);
		_href = jQuery(this).attr("href");
		switch(_href){
			case "#":
				_img = jQuery("<div><img src='"+jQuery(this).attr("src")+"' width='120' height='60' /></div>");
				_title = jQuery("<h3>"+jQuery("title",this).text().replace(/^\s+/,"").substring(0,18)+"</h3>");
				_p = jQuery("<p>"+jQuery("text",this).text().replace(/^\s+/,"").substring(0,38)+"</p>");
				break;
			default:
				_img = jQuery("<div><a href='"+jQuery(this).attr("href")+"'><img src='"+jQuery(this).attr("src")+"' width='120' height='60' /></a></div>");
				_title = jQuery("<h3><a href='"+jQuery(this).attr("href")+"'>"+jQuery("title",this).text().replace(/^\s+/,"").substring(0,18)+"</a></h3>");
				_p = jQuery("<p>"+jQuery("text",this).text().replace(/^\s+/,"").substring(0,38)+"</p>");
				break;
		}
		_li.append(_img);
		_li.append(_title);
		_li.append(_p);
	});
}
function onPickupXmlLoadSuccess(data, dataType){
	var _img, _div, _p, _title, _href;
	//XMLデータの受け取り
	pickupXml = data;
	pickupStage.empty();
	jQuery("item",pickupXml).each(function(idx){
		if(idx == 0){
			_div = jQuery("<div class'pickupTxt'>");

			_href = jQuery(this).attr("href");
			switch(_href){
				case "#":
					_img = jQuery("<div class='pickupImg'><img src='"+jQuery(this).attr("src")+"' alt='' width='120' height='75' /></div>");
					_title = jQuery("<h3 class='contentsLink'>"+jQuery("title",this).text()+"</h3>");
					break;
				default:
					//_img = jQuery("<div class='pickupImg'><a href='"+jQuery(this).attr("href")+"' target='"+jQuery(this).attr("target")+"'><img src='"+jQuery(this).attr("src")+"' alt='' width='120' /></a></div>");
					//_title = jQuery("<h3 class='contentsLink'><a href='"+jQuery(this).attr("href")+"' target='"+jQuery(this).attr("target")+"'>"+jQuery("title",this).text()+"</a></h3>");
					_img = jQuery("<div class='pickupImg'><a href='"+jQuery(this).attr("href")+"' target='_self'><img src='"+jQuery(this).attr("src")+"' alt='' width='120' /></a></div>");
					_title = jQuery("<h3 class='contentsLink'><a href='"+jQuery(this).attr("href")+"' target='_self'>"+jQuery("title",this).text()+"</a></h3>");
					break;
			}
			
			var pickupTxt = jQuery("text",this).text();
			if(pickupTxt.length > 90){
				pickupTxt = pickupTxt.substr(0, 90) + "...";
			}
			_p = jQuery("<p>"+pickupTxt+"</p>");

			pickupStage.append(_img);
			pickupStage.append(_div);
			_div.append(_title);
			_div.append(_p);
		}
	});
}


jQuery(document).ready(init);

