/**
 * @version    2010-02-25 07:46:12
 * @copyright  Copyright (c) 2010, SOFTBANK MOBILE Corp.
 */

(function ($) {
SBM.use('plugin.history');
var self = SBM.extra.services = SBM.plugin.extend({
	title:   document.title,
	hash:    SBM.env.location().hash,
	content: null,
	
	/**
	 */
	ready: function () {
		if (!window.adpsp_track)
			window.adpsp_track = function () {};
		
		this.ghostpower();
		$('p.pagetop a').click(function () {
			self.win.scroll('#pagetop');
			return false;
		});
		
		this.plugin.history.change(function (next, prev) {
			var tab;
			if (next && (tab = $('#switch-contentnavi li.' + next + ' a')).size())
				self.open(tab, true);
			else {
				self.hash = null;
				self.open($('#switch-contentnavi li:first a'), true);
			}
		});
		
		$('#switch-contentnavi li').each(function () {
			var el = $('<div id="' + this.className.split(' ')[0] + '"></div>');
			$('body').prepend(el.css({ padding: 0, margin: 0 }));
		});
		
		var tab;
		if (this.hash && $('#switch-contentnavi li.' + this.hash).size())
			tab = $('#switch-contentnavi li.' + this.hash);
		else {
			tab = $('#switch-contentnavi li:first');
			this.hash = null;
		}
		
		if (tab.size())
			this.open(tab.find('a'));
		
		$('#switch-contentnavi li a').click(function (el) {
			return self.open(this);
		});
		
		this.initEvents();
	},
	
	/**
	 */
	initEvents: function () {
		this.plugin.history.change(function (next, prev) {
			self.display(next || 'function/intro');
		});
		
		this.display(location.hash.replace(/^#/, ''));
		
		$('#intro div').click(function () {
			if (!$(this).find('a:last[href^=#]').size())
				return false;
			
			var hash = $('#contents').attr('class') + '/' + $(this).find('a:last').attr('href').replace(/^.*#/, '');
			self.display(hash);
			self.plugin.history.add(hash);
			self.win.scroll('#pagetop');
			return false;
		});
		
		$('p.inner_switch a, li.inner_switch a, h3.inner_switch a').click(function () {
			var hash = $('#contents').attr('class') + '/' + this.href.replace(/^.*#/, '');
			self.display(hash);
			self.plugin.history.add(hash);
			if (/^select/.test(hash))
				self.scroll('#select-menu ul', function () {}, -100);
			else if (/^wifi\-channel/.test(hash))
				self.scroll('#select-menu ul', function () {});
			else
				self.win.scroll('#pagetop');
			return false;
		});
		
		$('.outer_switch > a').click(function() {
			var hash = $(this).attr('href').replace(/#/, '');
			var el   = $('#switch-contentnavi li.' + hash + ' a');
			if (!el.size())
				el = $('#switch-contentnavi li:first a');
			self.extra.services.open(el);
			self.win.scroll('#pagetop');
			return false;
		});
		
		$('.inner_switch_area').hover(function () {
			$('img', this).css('visibility', 'hidden');
		}, function () {
			$('img', this).css('visibility', 'visible');
		});
	},
	
	/**
	 * @param   {element}  el
	 * @param   {boolean}  [onchange]
	 * @return  {boolean}
	 */
	open: function (el, onchange) {
		el = $(el);
		$('#contents').removeClass();
		if (this.content)
			this.content.hide();
		
		var tab      = el.parent().parent();
		var _hash    = this.hash;
		this.hash    = tab.attr('class').split(' ')[0];
		this.content = $('#contents div.colmun div.page[id=' + this.hash + ']');
		this.content.show();
		$('#contents').addClass(this.hash ? this.hash : 'top');
		setTimeout(function () { document.title = self.title; }, 0);
		
		if (_hash && !onchange) {
			this.display(this.hash, true);
			this.ajax.load('log/' + this.hash + '.html', function () {});
			adpsp_track(this.env.path.replace(/index\.html.*/, '') + '#' + this.hash, '');
			this.plugin.history.add(self.hash);
		}
		
		return false;
	},
	
	/**
	 * @param   {string}   hash
	 * @param   {boolean}  noloop
	 */
	display: function (hash, noloop) {
		(function (t, h) {
			if (!noloop) {
				switch (t) {
					case 'priceplan':
					case 'select':
					case 'wifi-channel':
						self.open($('#switch-contentnavi li.' + t + ' a'), true);
						break;
				}
			}
			
			$('#intro, #case1, #case2, #case3').hide();
			$('#owarailife, #baseball, #international-football, #domestic-football, #martial-arts, #sumo, #entertainment-news, #korean-entertainment, #cosme, #gourmet, #uranai, #movie, #diet, #travel').hide();
			$('#charge, #pc, #mobile, #smartphone, #iphone, #voice').hide();
			$('#paper, #preview, #nicovideo, #rentalvideo, #yahoovideo, #livedelivery').hide();
			switch (t) {
				case 'priceplan':
					switch (h) {
						case 'pc':
						case 'mobile':
						case 'smartphone':
						case 'iphone':
						case 'voice':
							$('#' + h).show(); break;
						default:
							$('#charge').show();
					}
					break;
				case 'select':
					$('#select-menu li').removeClass('current');
					switch (h) {
						case 'baseball':
						case 'international-football':
						case 'domestic-football':
						case 'martial-arts':
						case 'sumo':
						case 'entertainment-news':
						case 'korean-entertainment':
						case 'cosme':
						case 'gourmet':
						case 'uranai':
						case 'movie':
						case 'diet':
						case 'travel':
							$('#' + h).show();
							$('#select-menu a[href=#' + h + ']').parent().addClass('current');
							break;
						default:
							$('#owarailife').show();
							$('#select-menu a[href=#owarailife]').parent().addClass('current');
					}
					break;
				case 'function':
					switch (h) {
						case 'case1':
						case 'case2':
						case 'case3':
							$('#' + h).show(); break;
						default:
							$('#intro').show();
					}
					break;
				case 'wifi-channel':
					$('#select-menu li').removeClass('current');
					switch (h) {
						case 'preview':
						case 'nicovideo':
						case 'rentalvideo':
						case 'yahoovideo':
						case 'livedelivery':
							$('#' + h).show();
							$('#select-menu a[href=#' + h + ']').parent().addClass('current');
							break;
						default:
							$('#paper').show();
							$('#select-menu a[href=#paper]').parent().addClass('current');
					}
					break;
				default:
					$('#intro').show();
			}
		}).apply(null, hash.split('/'));
	},
	
	/**
	 * @param   {string|element}  el
	 * @param   {function}        [fn]
	 * @param   {number}          [margin]
	 * @return  {boolean}
	 */
	scroll: (function () {
		var timer;
		var clear = function () {
			if (timer)
				clearInterval(timer);
			timer = null;
		};
		
		return function (el, fn, margin) {
			clear();
			
			var from = $(window).scrollTop();
			var to   = $(el).position().top + (margin || 0);
			
			timer = setInterval(self.bind(function () {
				var move = Math.floor((from - to) / 3);
				if (!move && (from != to))
					move = (from > to) ? 1 : -1;
				from -= move;
				if (from == to) {
					if (fn)
						fn.call(this);
					clear();
				}
				else
					$(window).scrollTop(from);
			}), 50);
			
			var d = document;
			if (self.env.ua == 'ff')
				d.body.addEventListener('DOMMouseScroll', function () {
					clear();
					this.removeEventListener('DOMMouseScroll', arguments.callee, false);
				}, false);
			else
				$(d).one('mousewheel', clear);
			
			return false;
		};
	})(),
	
	/**
	 */
	ghostpower: function () {
		$('#contents a.gp').click(function () {
			var id     = $(this).attr('href');
			var tab    = null;
			var parent = $(id);
			
			while ((parent = parent.parent()).size()) {
				if (parent.hasClass('page')) {
					tab = parent.attr('id');
					break;
				}
				if (parent.attr('id') == 'contents')
					break;
			}
			
			if (tab) {
				var margin = (function (id) {
					switch (id) {
						case '#friends_mail': return 135;
					}
					return 0;
				})(id);
				
				self.open($('#switch-contentnavi li.' + tab + ' a'), true);
				self.plugin.history.add(self.hash);
				self.scroll(id, function () {
					self.display(self.hash, true);
					self.ajax.load('log/' + self.hash + '.html', function () {});
				}, margin);
			}
			
			return false;
		});
	}
});

$(function () {
	SBM.extra.services.ready();
});
})(jQuery);
