/**
 * @version  1.00
 * @updated  2008/03/18
 */


CaseUsers = {
	init: function () {
		this.data = {};
		this.load();
	},



	load: function () {
		jQuery.ajax({
			url     : '/data/shared/xml/case_users.xml',
			type    : 'GET',
			dataType: 'xml',
			cache   : false,
			success : function (xml) {
				var data = CaseUsers.toArray(xml.lastChild);
				
				if(jQuery('#rt-top-users').get(0)) CaseUsers.index(data);
				if(jQuery('#rt-rightnavi-users').get(0)) CaseUsers.nav(data);
				if(jQuery('#list-users').get(0)) CaseUsers.list(data);
				if(jQuery('dl#catalog-users').get(0)) CaseUsers.catalog(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;
	},
	
	
	
	index: function (data) {
		var keys = jQuery('#rt-top-users').html();
		var key  = (keys != 'all') ? keys.split(',') : 'all';
		var html = [];

		if (key != 'all') {
			for (i=0; i<key.length; i++) {
				jQuery(data.user).each(function () {
					if (key[i] == this.id.text) {
						html.push('<div class="user-link" id="iu_' + this.id.text + '">');
						html.push('<em><a href="' + this.link.text + '">' + this.label.text + '</a></em>');
						html.push('<img src="' + this.imgs.text + '" alt="" />');
						html.push('<p>' + this.head.text + '</p>');
						html.push('</div>');
					}
				});
			}
		} else {
			jQuery(data.user).each(function () {
				html.push('<div class="user-link" id="iu_' + this.id.text + '">');
				html.push('<em><a href="' + this.link.text + '">' + this.label.text + '</a></em>');
				html.push('<img src="' + this.imgs.text + '" alt="" />');
				html.push('<p>' + this.head.text + '</p>');
				html.push('</div>');		
			});		
		}
						
		jQuery('#rt-top-users').html(html.join(''));
		
		func_users();
		jQuery('#rt-top-users').css({'visibility':'visible'});

	},

	
		
	nav: function (data) {
		var keys = jQuery('#rt-rightnavi-users').html();
		var key  = (keys != 'all') ? keys.split(',') : 'all';
		var html = [];
		
		html.push('<h3>ユーザー事例</h3>');
		if (key != 'all') {
			for (i=0; i<key.length; i++) {
				jQuery(data.user).each(function () {
					if (key[i] == this.id.text) {
						html.push('<div class="link" id="nu_' + this.id.text + '">');
						html.push('<h4><a href="' + this.link.text + '">' + this.label.text + '</a></h4>');
						html.push('<p>');
						html.push('<img src="' + this.imgs.text + '" alt="" />' + this.head.text + '<a href="' + this.link.text + '">さらに詳しく</a>');
						html.push('</p>');
						html.push('</div>');
					}
				});
			}
		} else {
			jQuery(data.user).each(function () {
				html.push('<div class="link" id="nu_' + this.id.text + '">');
				html.push('<h4><a href="' + this.link.text + '">' + this.label.text + '</a></h4>');
				html.push('<p>');
				html.push('<img src="' + this.imgs.text + '" alt="" />' + this.head.text + '<a href="' + this.link.text + '">さらに詳しく</a>');
				html.push('</p>');
				html.push('</div>');
			});		
		}
		
		jQuery('#rt-rightnavi-users').html(html.join(''));
		jQuery('#right-navi div.vis').css({'visibility':'visible'});
	},

	
	list: function(data) {
		var html = [];
			
		html.push('<div class="pack col">');
		jQuery(data.user).each(function (n) {
			html.push('<div' + ((n % 2) ? ' class="right"' : ' class="left"') + 'id="lu_' + this.id.text + '">');
			html.push('<h3><a href="' + this.link.text + '">' + this.label.text + '</a></h3>');
			html.push('<div class="tbn"><img src="' + this.imgs.text + '" alt="" /></div>');
			html.push('<p class="s">');
			html.push('<strong>' + this.head.text + '</strong>');
			html.push(this.description.text + '<span class="link"><a href="' + this.link.text + '">さらに詳しく</a></span>');
			html.push('</p>');
			html.push('</div>');
			if (n % 2) html.push('<div class="hr"></div>');
		});
		html.push('</div>');
		
		jQuery('#list-users').html(html.join(''));
		
		var elm_n = jQuery(data.user).length;
		var hr_n = jQuery('#list-users .hr').length;

		jQuery('#list-users .hr').get(hr_n-1).style.display = 'none';
		if (elm_n % 2) jQuery('#list-users .hr').get(hr_n-1).style.display = '';
	},
	
	
	
	catalog: function(data) {
		var html = [];
		
		html.push('<dt>ユーザー事例一覧</dt>');
		jQuery(data.user).each(function () {
			html.push('<dd><a href="' + this.link.text + '">' + this.label.text + '</a></dd>');		
		});
		
		jQuery('dl#catalog-users').html(html.join(''));
	}	
}