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



(function ($) { $(function () {
	var index = {
		init: function() {
			var def = 1;
			index.imgchange(def);
			index.control();
			index.timer();
			index.news();
			index.load();
			index.action();
		},

		
		/* ================= mainvisual ================= */
		
		
		control: function () {
			$('div#control ul li a').click(function () {
				var key = $(this).parent().attr('id').replace('ctl-','');
				
				$('div#control ul li.on').attr('class','');
				$(this).parent().attr('class','on');
				
				index.imgchange(key);
				$('div#control ul').stopTime();
				index.timer();
				
				return false;
			});
		},
		
		
		
		imgchange: function (key) {
			$('div.main-img.mv-current').fadeOut();
			$('div.main-img.mv-current').attr('class','main-img');
			
			$('div.main-img#mv-' + key).fadeIn('slow');
			$('div.main-img#mv-' + key).attr('class','main-img mv-current');
			
		},
		
		
		
		timer: function () {
		
			var ctl_length = $('div#control ul li').size();

			$('div#control ul').everyTime(8000, function() {
				var ctl_id = $('div#control ul li.on').attr('id').replace('ctl-','');
				var key    = (ctl_id == ctl_length) ? 1 : parseFloat(ctl_id) + 1;
				
				$('div#control ul li.on').attr('class','');
				$('div#control ul li#ctl-' + key).attr('class','on');
				
				index.imgchange(key);
			});
		
		},


		
		/* ================= news ================= */
		

		news: function () {
		
			var news_length = parseFloat($('div#headline ul#hl-list li').size());
			
			$('div#headline ul#hl-list li').each(function (i) {
				$(this).attr('id','news-' + i);
			});
			
			$('div#headline ul#hl-list li#news-0').fadeIn();
			$('div#headline ul#hl-list li#news-0').attr('class','news-current');
			
			
			$('div#headline ul#hl-list').everyTime(8000, function() {
				var news_id = $('div#headline ul#hl-list li.news-current').attr('id').replace('news-','');
				var key     = (news_id == (news_length - 1)) ? 0 : parseFloat(news_id) + 1;
				
				$('div#headline ul#hl-list li.news-current').fadeOut();
				$('div#headline ul#hl-list li.news-current').attr('class','');
				
				$('div#headline ul#hl-list li#news-' + key).fadeIn();
				$('div#headline ul#hl-list li#news-' + key).attr('class','news-current');
				
			});
		},
		
				
		/* ================= showcase ================= */


		load: function () {
			$.ajax({
				url     : '/data/shared/xml/plans.xml',
				type    : 'GET',
				dataType: 'xml',
				cache   : false,
				success : function (r) {
					index.output($(r).children());
				}
			});
		},
		
		
		output: function (data) {
			
			$(data).children().each( function (i) {
				var html = [];
				
				if ($(this).get(0).tagName == 'package') {
				
					$(this).children().each( function () {
						html.push(
							'<div class="elm mini ' + $(this).attr('keyword') + '">' + '\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="' + $(this).find('url').text() + '"></a>' + '\n' +
							'<p class="sh-img"><em><img src="' + $(this).find('img_icon').attr('src') + '" alt="" width="60" height="50" /></em><img src="' + $(this).find('img_copy').attr('src') + '" alt="' + $(this).find('img_copy').attr('alt') + '" width="225" height="50" /></p>' + '\n' +
							'<h3>' + $(this).find('title').text() + '</h3>' + '\n' +
							'<p class="sh-conbi">' + $(this).find('spec').text() + '</p>' + '\n' +
							'<p class="sh-price">' + '\n'
						);
						
						if ($(this).find('price_m').attr('type') == 'I') {
							html.push('<span class="left">月額<em>' + $(this).find('price_m').text() +'</em></span>' + '<span class="right">初期<em>' + $(this).find('price_d').text() + '</em></span>' + '\n');
						} else if ($(this).find('price_m').attr('type') == 'N') {
							html.push('<span class="left">月額<em>' + $(this).find('price_m').text() +'</em>円</span>' + '<span class="right">初期<em>' + $(this).find('price_d').text() + '</em>円</span>' + '\n');
						} else {
							html.push('<span class="left">月額<em>' + $(this).find('price_m').text() +'</em>円</span>' + '<span class="right">初期<em>' + $(this).find('price_d').children().eq(0).text() + '</em>円</span> 〜<' + '\n');
						}
						
						html.push(
							'</p>' + '\n' +
							'</div>' + '\n' + '\n'
						);
					});
					
					

				} else {
				
					$(this).children().each( function () {
						html.push(
							'<div class="elm nml ' + $(this).attr('keyword') + '">' + '\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="' + $(this).find('url').text() + '"></a>' + '\n' +
							'<p class="sh-img"><img src="' + $(this).find('img').attr('src') + '" alt="' + $(this).find('img').attr('alt') + '" width="285" height="180" /></p>' + '\n' +
							'<p class="sh-spec"><img src="' + $(this).find('img_spec').attr('src') + '" alt="' + $(this).find('img_spec').attr('alt') + '" width="285" height="60" /></p>' + '\n' +
							'<h3>' + $(this).find('title').text() + '</h3>' + '\n' +
							'<p class="sh-price">' + '\n'
						);
						
						if ($(this).find('price_m').attr('type') == 'I') {
							html.push('<span class="left">月額<em>' + $(this).find('price_m').text() +'</em></span>' + '\n');
						} else {
							html.push('<span class="left">月額<em>' + $(this).find('price_m').text() +'</em>円</span>' + '\n');
						}
						
						if ($(this).find('price_d').attr('type') == 'I') {
							html.push('<span class="right">初期<em>' + $(this).find('price_d').text() + '</em></span>' + '\n');
						} else if ($(this).find('price_d').attr('type') == 'N') {
							html.push('<span class="right">初期<em>' + $(this).find('price_d').text() + '</em>円</span>' + '\n');
						} else {
							html.push('<span class="right">初期<em>' + $(this).find('price_d').children().eq(0).text() + '</em>円 〜</span>' + '\n');
						}
						
						html.push(
							'</p>' + '\n' +
							'</div>' + '\n' + '\n'
						);
					});
					
				}
				
				$('div#svc-sort div.showcasebox div.in').append(html.join(''));
				
				
			});
			
			
			
			common.showcase();
			
		},
		
		
		action: function () {
		
			$('div#sort ul li a').click(function () {
			
				/* var */
				
				var li_class = $(this).parent().attr('class');
				var li_id    = $(this).parent().attr('id');
				var kw       = li_id.replace('st-','kw');
				var st_on    = $('div#sort ul li.st-on').attr('id');
				
				
				/* var display */
				
				var display  = function () {
					
					if(li_id != 'st-0') {
						
						$('div.showcasebox').each(function () {
												
							$(this).children().children('div.elm').css({'display':'none'});
							$(this).children().children('div.elm.' + kw).css({'display':'block'});
														
						});
						
						$('div#svc-all').css({'display':'none'});
						$('div#svc-sort').css({'display':'block'});
						
					} else {
					
						$('div.showcasebox').each(function () {
												
							$(this).children().children('div.elm').css({'display':'block'});
														
						});

						$('div#svc-sort').css({'display':'none'});								
						$('div#svc-all').css({'display':'block'});	
					
					}
				}
				
			
			
				if(li_class != 'st-on') {

					/* sort */
					
					$('div#sort ul li.st-on').attr('class','');
 					$(this).parent().attr('class','st-on');					
					
					
					/* showcase */
					
					if($.browser.msie && $.browser.version <= 7) {
					
						display();							
											
					} else {
										
						$('div#loader').height($('div#showcase').height());
						$('div#loader').show();
						$('div#showcase').hide();
						
						display();
		
						$('div#loader').animate(
							{ height: $('div#showcase').height() }, 'slow'
						).fadeOut('normal', function () {
							$('div#showcase').css({'display':'block'});
						});
					
					}
				
				}
				
				return false;
				
			});
		}
	}

	index.init();
	
})})($);