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



(function ($) {
	var package = {
		init: function() {
			package.load();
		},

		load: function () {
			$.ajax({
				url     : '/data/shared/xml/plans.xml',
				type    : 'GET',
				dataType: 'xml',
				cache   : false,
				success : function (r) {
					package.output($(r).children());
				}
			});
		},
		
		
		output: function (data) {
			var plan = $('body').attr('id');
			var flag = $('body').attr('class');
			
			var path = document.URL.split('/');
			var root = '/' + path[3] + '/' + path[4] + '/' + path[5];
			
			$(data).children().each( function () {
			
				if ($(this).get(0).tagName == 'package') {
				
					$(this).children().each( function () {

						var html          = [];
						var url           = $(this).find('url').text();
						var icon          = $(this).find('img_icon').attr('src');
						var copy          = $(this).find('img_copy').attr('src');
						var copy_alt      = $(this).find('img_copy').attr('alt');
						var title         = $(this).find('title').text();
						var spec          = $(this).find('spec').text();
						var txt           = $(this).find('txt').text();
						var price_m       = $(this).find('price_m').text();
						var price_m_type  = $(this).find('price_m').attr('type');
						var price_d       = $(this).find('price_d').text();
						var price_d_type  = $(this).find('price_d').attr('type');						
						
						
						/* ================= index ================= */
						
						
						if (flag == 'package-index') {
							if ($(this).attr('plan') == plan) {
								if (txt.length > 76) {
									var str  = txt.substr(0,75);
									var txt  = str + '…';
								}
	
								html.push(
									'<div class="elm pack">' + '\n' +
									(($(this).find('badge').text() != '') ? '<p class="sh-badge"><img class="png" src="/data/shared/img/badge_' + $(this).find('badge').text() + '.png" alt="" width="60" height="64" /></p>' : '') + '\n' +
									'<a class="block" href="' + url + '"></a>' + '\n' +
									'<p class="sh-img"><em><img src="' + icon + '" alt="" width="60" height="50" /></em><img src="' + copy + '" alt="' + copy_alt + '" width="225" height="50" /></p>' + '\n' +
									'<h3><em>' + title + '</em>' + spec + '</h3>' + '\n' +
									'<p class="sh-txt">' + txt + '</p>' + '\n' +
									'<p class="sh-price">' + '\n' +
									'<span class="left">月額<em>' + price_m + ((price_m_type == 'I') ? '</em></span>' : '</em>円</span>') + '\n'
								);
								
								if (price_d_type == 'N') {
									html.push('<span class="right">初期<em>' + price_d + '</em>円</span>' + '\n');
								} else if (price_d_type == 'I') {
									html.push('<span class="right">初期<em>' + price_d + '</em></span>' + '\n');
								} else {
									html.push('<span class="right">初期<em>' + $(this).find('price_d').children('price').eq(0).text() + '</em></span>' + '\n');
								}
								
								html.push(
									'</p>' + '\n' +
									'</div>' + '\n'
								);
								
								$('div#showcase div.in').append(html.join(''));
							}
							
							
						/* ================= detail ================= */
						
							
						} else if (flag == 'package-detail') {
						
							if (root == url) {
						
								if ($(this).find('badge').text() != '') $('div#mainvisual').prepend('<img width="700" height="180" alt="" src="/data/shared/img/badge_' + $(this).find('badge').text() + '_mv.png" id="mv-badge" class="png">');
								$('h1').prepend('<img src="' + icon + '" alt="" width="60" height="50" />');
								$('div#discription p').append(txt);
								
								/* price */
								
								var html_price = [];
								
								if (price_d_type == 'N' || price_d_type == 'I') {
									html_price.push(
										'<table border="0" cellspacing="0" cellpadding="0" width="670" class="tbl-price">' + '\n' +
										'<thead>' + '\n' +
										'<tr>' + '\n' +
										'<th>月額費用（税込）</th>' + '\n' +
										'<th>初期費用（税込）</th>' + '\n' +
										'</tr>' + '\n' +
										'</thead>' + '\n' +
										'<tbody>' + '\n' +
										'<tr>' + '\n' +
										'<td id="price-m" class="price-top"><em>' + price_m + ((price_d_type == 'N') ? '</em>円</td>' : '</em></td>') + '\n' +
										'<td id="price-d"><em>' + price_d + ((price_d_type == 'N') ? '</em>円</td>' : '</em></td>') + '\n' +
										'</tr>' + '\n' +
										'</tbody>' + '\n' +
										'</table>' + '\n'
									);
								} else {
									html_price.push(
										'<table border="0" cellspacing="0" cellpadding="0" width="670" class="tbl-price">' + '\n' +
										'<thead>' + '\n' +
										'<tr>' + '\n' +
										'<th>月額費用（税込）</th>' + '\n' +
										'<th colspan="3">初期費用（税込）</th>' + '\n' +
										'</tr>' + '\n' +
										'</thead>' + '\n' +
										'<tbody>' + '\n' +
										'<tr>' + '\n' +
										'<td rowspan="2" class="price-top"><em>' + price_m + ((price_m_type == 'N') ? '</em>円</td>' : '</em></td>') + '\n' +
										'<th class="tbl-thead">12ヶ月契約</th>' + '\n' +
										'<th class="tbl-thead">6ヶ月契約</th>' + '\n' +
										'<th class="tbl-thead">1ヶ月契約</th>' + '\n' +
										'</tr>' + '\n' +
										'<tr>' + '\n' +
							            '<td><em>' + $(this).find('price_d').children('price').eq(0).text() + '</em>円</td>' + '\n' +
							            '<td><em>' + $(this).find('price_d').children('price').eq(1).text() + '</em>円</td>' + '\n' +
							            '<td><em>' + $(this).find('price_d').children('price').eq(2).text() + '</em>円</td>' + '\n' +
										'</tr>' + '\n' +
										'</tbody>' + '\n' +
										'</table>' + '\n'
									);								
								}
								
								$('div#price').append(html_price.join(''));
								
							}
							
							if ($(this).attr('plan') == plan) {
									
								$('div#showcase div.in').append([
									'<div class="elm pack thumb">' + '\n',
									(($(this).find('badge').text() != '') ? '<p class="sh-badge"><img class="png" src="/data/shared/img/badge_' + $(this).find('badge').text() + '.png" alt="" width="60" height="64" /></p>' : '') + '\n',
									'<a class="block" href="' + url + '"></a>' + '\n',
									'<p class="sh-img"><em><img src="' + icon + '" alt="" width="60" height="50" /></em><img src="' + copy + '" alt="' + copy_alt + '" width="225" height="50" /></p>' + '\n',
									'<h3><em>' + title + '</em>' + spec + '</h3>' + '\n',
									'</div>' + '\n'
								].join(''));
							}
						
						}
						
					});			
				}
				
			});
			
			common.showcase();
			
		}
	}



	$(function () {
		package.init();
	});
	
})($);
