/**
 * @version  1.00
 * @updated  2009/04/01
 */



if ($.browser.safari && $.browser.version < 500) {
	$(window).load(function () {
		Price.init();
	});
}
else {
	$(document).ready(function () {
		Price.init();
	});
}



Price = {
	init: function () {
		this.data = {};
		this.load('housings');
		this.load('basics');
		this.load('options');
		this.load('managements');
		this.load('applications');
/* 		this.load('opensources'); */
	},
	
	
	
	load: function (xml) {
		$.ajax({
			url     : '/data/service/shared/xml/' + xml + '.xml',
			type    : 'GET',
			dataType: 'xml',
			cache   : false,
			success : function (xml) {
				
				var data = Price.toArray(xml.lastChild);
				Price.html(data);
			}
		});
	},
	
	
	
	toArray: function (node, self) {
		var r    = {};
		var self = self || this.toArray;
		for (var attr, i = 0; attr = node.attributes[i]; i++)
			r[attr.name] = node.getAttribute(attr.name);
		
		var tags = [];
		$(node).children().each(function () {
			if (!r[this.nodeName])
				r[this.nodeName] = [], tags.push(this.nodeName);
			
			var data = { text: $(this).text() };
			for (var attr, i = 0; attr = this.attributes[i]; i++)
				data[attr.name] = this.getAttribute(attr.name);
			
			r[this.nodeName].push($(this).children().size() ? self(this, self) : data);
		});
		
		for (var tag, i = 0; tag = tags[i]; i++) {
			if (r[tag].length == 1) {
				for (var key in r[tag][0])
					r[tag][key] = r[tag][0][key];
			}
		}
		
		return r;
	},
	

	
	html: function (data) {
		var html = []
		
		$(data.group).each(function() {
			if (this.label.text != '') 	html.push('<h3>' + this.label.text + '</h3>');
			html.push('<table cellpadding="0" cellspacing="0" border="0" class="list tbl-small" width="649">');
			html.push('<thead>');
			html.push('<tr><th colspan="2" style="width:398px;">サービス名</th><td style="width:124px;">初期費用</td><td style="width:124px;">月額費用</td></tr>');
			html.push('</thead>');
			html.push('<tbody>');
			$(this.item).each(function(){
				var name = this.label.text;
				var url  = this.url.text;
				
				if (this.has_price.text == 'Y') {
					var num = $(this.pricelist.set).size();
					if (this.pricelist.has_label.text == 'N') {						
						$(this.pricelist.set).each(function(i) {
							if (i == 0) {
								html.push('<tr>');
								html.push('<th colspan="2" rowspan="' + num + '"><a href="' + url + '">' + name + '</a></th>');
								html.push('<td class="al-r">' + this.price[0].text + '</td>');
								html.push('<td class="al-r">' + this.price[1].text + '</td>');
								html.push('</tr>');
							} else {
								html.push('<tr>');
								html.push('<td class="al-r">' + this.price[0].text + '</td>');
								html.push('<td class="al-r">' + this.price[1].text + '</td>');
								html.push('</tr>');								
							}
						});
					} else {
						$(this.pricelist.set).each(function(i) {
							if (i == 0) {
								html.push('<tr>');
								html.push('<th rowspan="' + num + '"><a href="' + url + '">' + name + '</a></th>');
								html.push('<th>' + this.label.text + '</th>')
								html.push('<td class="al-r">' + this.price[0].text + '</td>');
								html.push('<td class="al-r">' + this.price[1].text + '</td>');
								html.push('</tr>');
							} else {
								html.push('<tr>');
								html.push('<th>' + this.label.text + '</th>')
								html.push('<td class="al-r">' + this.price[0].text + '</td>');
								html.push('<td class="al-r">' + this.price[1].text + '</td>');
								html.push('</tr>');								
							}
						});					
					}
				} else if (this.has_price.text == 'I') {
					html.push('<tr><th colspan="2"><a href="' + url + '">' + name + '</a></th><td colspan="2"><a href="' + url + '">詳細ページにてご覧になれます</a></tr>')
				}
			});


		
			html.push('</tbody>');
			html.push('</table>');
		});
				
		$('div#' + data.flag.text + '-box').append(html.join(''));
		
	}
};

