/* 
 * 
 * sports jQuery Plugin
 * charset euc-jp
 *
*/
(function(jQuery){

	$.sports = $.sports || {};
	$.fn.globalNavi = function(o) {
		return this.each(function() {
			new $.sports.glovalNavi(this, o);
		});
	};
	$.fn.scoreBoard = function(o) {
		return this.each(function() {
			if($.isFunction($.ui.tabs)) new $.sports.scoreBoard(this, o);
		});
	};
	$.fn.slideShow = function(o) {
		return this.each(function() {
			new $.sports.slideShow(this, o);
		});
	};

	/* 
	 * 
	 * gloval navi
	 *
	*/
	$.sports.glovalNavi = function(el, o) {

		var options = {
			"other_button"   : $('#other'),
			"other_list"     : $('#other_list'),
			"other_close"    : $('#other_close'),
			"slide_duration" : 500
		};

		var o = o || {}; $.extend(options, o);

		options.other_list.width( $(el).width() );
		$(window).resize(function(){
			options.other_list.width( $(el).width() );
		});
		$(options.other_button).click(function(){
			if( options.other_list.css('display') == 'none' ){
				$(this).addClass('click');
				options.other_list.fadeIn(options.slide_duration);
			} else {
				$(this).removeClass('click');
				options.other_list.fadeOut(options.slide_duration);
			}
			return false;
		});
		$(options.other_close).click(function(){
			options.other_button.removeClass('click');
			options.other_list.fadeOut(options.slide_duration);
			return false;
		});
	};

	/* 
	 * 
	 * score board
	 *
	*/
	$.sports.scoreBoard  = function(el, o) {

		var self = this;
		var options = { duration : 500 };
		var o = o || {}; $.extend(options, o);
		var current = 0;

		var _init = function(panel,current){
			$(panel.find('.sb-carousel-scroll')).ready(function(e){
				panel.find('.sb-carousel-scroll').get(0).scrollLeft =
					panel.find('.sb-carousel').width() * current;
			});
		};
		var _scroll = function(panel,to,duration){
				duration = duration ? duration : options.duration;
				panel.find('.sb-carousel-scroll').scrollTo(to,duration,{ axis:'x'});
		};
		var _grayout = function(el){
			if( !el.find('p').hasClass('grayout') ){
				el.find('p').addClass('grayout')
					.find('span').html( $(el).find('a').text());
			}
		};
		var _linked = function(el){
			if( el.find('p').hasClass('grayout') ){
				el.find('p').removeClass('grayout').find('span').html(
					$(document.createElement('a')).attr('href','#').html(el.find('span').text())
				);
			}
		};
		var _pagenate = function(panel,pages,back,next){
			if(current === 0){
				_grayout(back);
				_linked(next);
			}
			else if(current === pages.length -1){
				_linked(back);
				_grayout(next);
			}
			else {
				panel.find('.back,.next').each(function(){
					_linked($(this));
				});
			}
		};

		$(el).tabs({
			"selectedClass": "active",
			"loadingClass" : "sb-loading",
			"panelClass"   : "sb-panel",
			"hideClass"    : "sb-tabs-hide",
			"idPrefix"     : "sb-tab-"
		})
		.bind('load.ui-tabs', function(e, ui) {
			var panel = $(ui.panel);
			var key = panel.children(':first').attr('class').split(' ')[0].split('_')[1];
			var pages = panel.find('.sb_content_' + key);
			if( pages.length <= 1 ){
				panel.find('.sb_head_button').hide();
				return;
			}
			var back = panel.find('.back');
			var next = panel.find('.next');
			current = 0;

			back.click(function(){
				if( pages[current-1] ){
					current--;
					_scroll(panel,pages[current]);
					_pagenate(panel,pages,back,next);
				}
				return false;
			});
			next.click(function(){
				if( pages[current+1] ){
					current++;
					_scroll(panel,pages[current]);
					_pagenate(panel,pages,back,next);
				}
				return false;
			});
			panel.find('.sb-carousel').each(function(i){
				if( $(this).hasClass('sb-current') ){
					current = i;
				}
			});
			_pagenate(panel,pages,back,next);
			_init(panel,current);
		})
		.bind('show.ui-tabs', function(e, ui) {
			if( current > 0 ) _init( $(ui.panel),current );
		})
	};

	$.sports.slideShow = function(el, o) {
		var self = this; //bindings
		var options = {
			"slides" : $("a[rel='sson-slide']"),
			"speed"  : [3000,5000,8000],
			"modal"  : true,
			"thumbs" : 7
		};
		var o = o || {}; $.extend(options, o);
		var slides = options.slides;

		var active = 0;
		var timerId = '';
		var thumblist = {
			"ids":[],"current":0,"per_page":options.thumbs,"loaded":0,
			"center":Math.floor(options.thumbs/2)
		};
		var slidable = true;

		/*
		 * XHTML Template
		 *
		 * using css file(sson.sports.css)
		 *
		 */
		var template =
			'<div id="sson-slide-header clearfix">' +
				'<h1>写真ニュース</h1>' +
				'<a href="#" />' +
			'</div>' +
			'<div id="sson-slide-main"><p id="sson-slide-image">' +
				'<img />'+
			'</p></div>' +
			'<div id="sson-slide-nav">' +
				'<div id="sson-slide-ctrl">' +
					'<ul>' +
						'<li id="sson-slide-ctrl-auto">' +
							'<a href="#" id="sson-slide-play" />' +
							'<a href="#" id="sson-slide-stop" />' +
						'</li>' +
						'<li id="sson-slide-ctrl-manual">' +
							'<a href="#" id="sson-slide-prev" />' +
							'<a href="#" id="sson-slide-next" />' +
						'</li>' +
					'</ul>' +
					'<ul>' +
						'<li id="sson-slide-pagenate">' +
							'<strong id="sson_slide_page_current"></strong> / <strong id="sson_slide_page_all"></strong> 枚目' +
						'</li>' +
						'<li id="sson-slide-select">' +
							'再生速度：<select>' +
							'<option value="">早い</option>' +
							'<option value="" selected>普通</option>' +
							'<option value="">遅い</option></select>' +
						'</li>' +
					'</ul>' +
				'</div>' +
				'<div id="sson-slide-meta">' +
					'<h2 id="sson-slide-title" />' +
					'<p id="sson-slide-caption" />' +
					'<em id="sson-slide-date" />' +
				'</div>' +
			'</div>' +
			'<div id="sson-slide-thumb"><ul id="sson-slide-thumb-list" /></div>' +
			'<div id="sson-slide-footer">' +
				'<address>Copyright (C) 2008 Yahoo Japan Corporation All Rights Reserved.</address>' +
			'</div>';
		/*
		 * Dom Create Section
		 *
		 * create DOM and set Events
		 *
		 */

		this.container = $(document.createElement('div'))
			.attr('id','sson-slide-container').hide().html(template)
			.click(function(e){
				_clickHandle(e.target);
				return false;
			}).appendTo(document.body);

		// modal
		this.modal = $(document.createElement('div'));
		if( !options.modal ) this.modal.css('background','transparent');

		this.modal.hide().attr('id','sson-slide-modal').click(function(e){
			if( this===e.target ) self.close();
		}).appendTo(document.body);

		// options select
		$('#sson-slide-select').change(function(e){ 
			$('#sson-slide-select option:selected').each(function(){
				self.speed = $(this).val();
				self.stop();
				self.play();
			});
		});
		this.speed = options.speed[1];

		// slide setting
		slides.each(function(){
			$(this).click(function(){
				self.open();
			})
		});
		$('#sson-slide-select option').each(function(i){
			$(this).val(options.speed[i]);
		});

		this.play = function(){
			_buttonReset();
			timerId = setInterval(self.next,this.speed);
		};
		this.stop = function(){
			_buttonReset();
			clearInterval(timerId);
		};
		this.next = function(){
			if(!slidable) return;
			if(active+1>imgSrc.length-1) return;
			active++;
			_change();
		};
		this.prev = function(){
			if(active-1<0) return;
			active--;
			_change();
		};
		this.select = function(n){
			if(!slidable) return;
			active = n;
			_preload(n);
			_change();
		};
		this.open = function(){
			_ready();
			_slideSizeSet($(window));
			self.modal.fadeIn("fast",function(){
				self.container.show();
			});
			$('#sson-slide-stop').addClass('sson-slide-stop-on');
		};
		this.close = function(){
			self.stop();
			self.container.hide();
			if(options.modal) self.modal.fadeOut("fast");
		};

		/*
		 * Private Functions
		 *
		 */
		var _clickHandle = function(clicked){
			switch(clicked.id){
				case 'sson-slide-play':
						self.play();
					break;
				case 'sson-slide-stop':
						self.stop();
					break;
				case 'sson-slide-next':
						self.next();
					break;
				case 'sson-slide-prev':
						self.prev();
					break;
				default:
					if( $(clicked).hasClass('sson-slide-thumb-image') ){
						self.select(parseInt(clicked.id.split('-')[3]));
					}
					break;
			}
		};

		var _preload = function(n){
			if(!active+1>imgSrc.length-1 && !imgSrc[active+1]){
					$(new Image()).attr('src',imgSrc[n ? n : active+1]);
			}
		};
		var _ready = function(){

			// ページ番号
			$('#sson_slide_page_current').text(active+1);
			$('#sson_slide_page_all').text(imgSrc.length);

			// サムネイル準備
			$(imgSrc).each(function(i){
				var id = 'sson-slide-thumb-'+i;
				$(document.createElement('li'))
					.append(
						$(document.createElement('a'))
							.addClass('sson-slide-thumb-image')
							.addClass(i==0 ? 'sson-slide-thumb-current' : '')
							.attr({'id':id,'href': '#'})
					)
					.appendTo('#sson-slide-thumb-list');
					thumblist.ids[i] = id;
			});
			_change();
		};
		var _change = function(){
			_replace();
			_pageSet(active);
			_preload();
		};
		var _thumb = function(source){

			// first time
			var preload_pos = active + thumblist.per_page;
			if( thumblist.loaded < preload_pos){
				for(var i=thumblist.loaded;i<preload_pos;i++){
					var src = imgSrc[i]
						.replace('.jpg','-ssmall.jpg')
						.replace('view','thum');
					$('#sson-slide-thumb-'+i).css({"background-image":"url(" +src+ ")"});
				}
				thumblist.loaded = preload_pos;
			}
			$([thumblist.ids[thumblist.current],thumblist.ids[active]])
			.each(function(){
				$('#' + this).toggleClass('sson-slide-thumb-current');
			});
			thumblist.current = active;
			if( active > thumblist.center ){
				$('#sson-slide-thumb').scrollTo(
					$('#'+thumblist.ids[active-thumblist.center]),1000,{ axis:'x'});
			}
		};
		var _replace = function(o){
			slidable = false;
			$('#sson-slide-image img').fadeOut(function(){
				$(this)
					.attr({"src":imgSrc[active],"alt":imgTitle[active]})
					.css ({"width" :imgWidth[active], "height":imgHeight[active] })
					.fadeIn(function(){
						slidable = true;
					});
				$('#sson-slide-title').html(imgTitle[active]);
				$('#sson-slide-caption').html(imgCaption[active]);
				$('#sson-slide-date').html(imgDate[active]);
			});
			_thumb(imgSrc[active]);
		};
		var _buttonReset = function(){
			$('#sson-slide-play,#sson-slide-stop').each(function(){
				$(this).toggleClass(this.id + '-on')
			});
		}
		var _pageSet = function(page){
			$('#sson_slide_page_current').text(page+1);
		};
		var _slideSizeSet = function(el){
			self.container.css({
				"top":el.scrollY ? el.scrollY : el.pageYOffset,
				"left":($(document).width() - self.container.width()) / 2
			});
			self.modal.width ($(document).width()).height($(document).height());
		};

		/*
		 * resize event
		 *
		 */
		$(window).resize(function(){
				_slideSizeSet(this);
		})
	};
})(jQuery);

/* 
 * 
 * sports global functions
 * charset euc-jp
 *
*/
$.sports.globalOsType = function() {
	var u  = navigator.userAgent.toUpperCase();
	if (u.indexOf("MAC") >= 0) return 'mac';
	if (u.indexOf("WIN") >= 0) return 'windows';
	if (u.indexOf("X11") >= 0) return 'unix';
	return '';
};
$.sports.globalBrowser = function() {
	
	var b,v = ('',0);
	if (navigator.userAgent.toUpperCase().indexOf('FIREFOX') >= 0){
		b = 'firefox';
		var u = navigator.userAgent.toUpperCase();
		var s = u.indexOf('FIREFOX/',0);
		v = parseFloat(u.substring(s+8,s+8+3));
	} 
	else if($.browser.msie==true){
		b = 'msie';
		v = $.browser.version;
	}
	else if($.browser.safari==true){
		b = 'safari';
		v = parseInt($.browser.version);
		if( v < 400 ) v = 1;
		else if( v < 500 ) v = 2;
		else v = 3;
	}
	else if($.browser.mozilla==true){
		b = 'mozilla';
	}
	else if($.browser.opera==true){
		b = 'opera';
	}
	return({"browser":b, "version":v});
};
$.sports.globalEnv = function() {

	var gb = $.sports.globalBrowser();
	switch(gb.browser){

		case 'msie' :
			// 6.0 over
			if( gb.version >= 6 ) return true;
			else return false;

		case 'firefox' :
			// 2.0 over
			if( gb.version >= 2 ) return true;
			else return false;

		case 'safari' :
			// 2.0 over
			if( gb.version >= 2 ) return true;
			else return false;
		default :
			return true;
	}
};
$(function() {
	$('#global-navigation').globalNavi();
	$('#sb_tab').scoreBoard();
});

