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



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

Services = {
	init: function () {
		this.services = [];	
		this.data = {};
		this.load('housings');
		this.load('basics');
		this.load('options');
		this.load('managements');
		this.load('applications');
/* 		this.load('opensources'); */
	},
	
	
	
	load: function (xml) {
	
		jQuery.ajax({
			url     : '/data/service/shared/xml/' + xml + '.xml',
			type    : 'GET',
			dataType: 'xml',
			cache   : false,
			success : function (xml) {
				var data = Services.toArray(xml.lastChild);
				
				Services.list(data);
				Services.feat(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 = [];
		jQuery(node).children().each(function () {
			if (!r[this.nodeName])
				r[this.nodeName] = [], tags.push(this.nodeName);
			
			var data = { text: jQuery(this).text() };
			for (var attr, i = 0; attr = this.attributes[i]; i++)
				data[attr.name] = this.getAttribute(attr.name);
			
			r[this.nodeName].push(jQuery(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;
	},
	
	

	list: function (data) {
		var html = [];
		$(data.group).each(function(){
			$(this.item).each(function(){
				Services.services.push(this);
				html.push('<dd><a href="#service-popup" onclick="Services.popup(' + (Services.services.length - 1)+ '); return false;" rel="facebox">' + this.label.text + '</a></dd>');
			});
		});	
		
		$('dl#list-' + data.flag.text).append(html.join(''));
		$('div#servicelist a[rel*=facebox]').facebox();
	},
	
	
	
	popup: function (i) {
		var elm  = this.services[i];	
		var html = [];
		
		html.push('<div id="popup-box" class="cnt">');
		html.push('<div class="popup-title"><a href="' + elm.url.text + '">' + elm.label.text + '</a></div>');
		html.push('<h2>特徴</h2>');
		if (elm.feature_icon) {
			html.push('<p class="popup-txt">' + '<img src="' + elm.feature_icon.text + '" class="left" />' + elm.feature_txt.text + '</p>');
		} else {
			html.push('<p class="popup-txt">' + elm.feature_txt.text + '</p>');
		}
		html.push('<div id="popup-price"><h2>料金</h2></div>');		
		html.push('<p id="popup-detail"><a href="' + elm.url.text + '" class="link al-r">さらに詳しく</a></p>');
		html.push('</div>');
		
		$('div#service-popup').html(html.join(''));

		if (elm.has_price.text == 'Y') { 
			Services.pricetable(elm.label.text,elm.pricelist,1);
		} else if (elm.has_price.text == 'I') {
			$('div#popup-price').append(elm.price_table.text);
		}		
	},
	


	feat: function (data) {
		var url = location.href.replace(/\?.*$|#.*$/, '').replace(/^.+\/service\//, '/service/');
		$(data.group).each(function () {
			$(this.item).each(function () {
				if (this.url.text == url) {
					if (this.feature_txt) {
						if (this.feature_icon) {
							$('div#cnt-feature div.feature-txt p').html('<img src="' + this.feature_icon.text + '" class="left" />' + this.feature_txt.text);
						} else {
							$('div#cnt-feature div.feature-txt p').html(this.feature_txt.text);
						}
					
					}
					
					if (this.feature_img) {
						$('div#cnt-feature div.feature-img p').html('<img src="' + this.feature_img.text + '" />');
					} else {
						$('div#cnt-feature div.feature-img p').css({'display':'none'});
						$('div#cnt-feature div.feature-txt').css({'width':'650px'})
					}
					
					if (this.has_price.text == 'Y') { 
						Services.pricetable(this.label.text,this.pricelist,2);
					} else if (this.has_price.text == 'I') {
						$('div#cnt-price').append(this.price_table.text);
					}
					
					$('div#cnt-price').append('<p class="tbl-caption"><a href="/service/price.html" class="link al-r">全ての料金表を見る</a></p>');
					return false;
				}
			});
		});
	},
	
		
	
	pricetable: function (name,data,flag) {
		var name = name;
		var rows = $(data.set).size();
		var html = []
		
		html.push('<table cellpadding="0" cellspacing="0" border="0" class="list" width="649">');
		
		html.push('<thead>');
		html.push('<tr><th style="width:198px;">&nbsp;</th><td style="width:224px;">初期費用</td><td style="width:224px;">月額費用</td></tr>')
		html.push('</thead>');
		
		if(data.has_label.text == 'Y') {
			$(data.set).each(function () {
				html.push('<tr>');
				html.push('<th>' + this.label.text + '</th>');
				$(this.price).each(function (n) {
					html.push('<td>' + this.text + '</td>');
				});
				html.push('</tr>');
			});
		} else {
			$(data.set).each(function (i) {
				if(i == 0) {
					html.push('<tr>');
					html.push('<th rowspan="' + rows + '">' + name + '</th>');
					$(this.price).each(function (n) {
						html.push('<td>' + this.text + '</td>');
					});
					html.push('</tr>');
					
				} else {
					html.push('<tr>');
					$(this.price).each(function (n) {
						html.push('<td>' + this.text + '</td>');
					});
					html.push('</tr>');
				}
			});
		}
				
		html.push('</table>');
		
		
		if (flag == 1) {
			$('div#popup-price').append(html.join(''));		
		} else if (flag == 2) {
			$('div#cnt-price').append(html.join(''));
		}
	}

};