/**********
 トップページ
**********/

$(function(){
	
	/*タブクラス*/
	var ClassTab = function(tabs_, pages_, select_) {
		this.tabs = tabs_;
		this.pages = pages_;
		this.select = select_;
	}
	ClassTab.prototype = {
		rollOver : function(index_) {
			var tabs = this.tabs;
			var imgs = tabs.find("img");
			
			imgs.each(function(idx_){
				var index = imgs.index(this);
				var img = $(this);
				img.unbind("mouseenter").unbind("mouseleave");//img.unbind('hover');ではできないらしい
				
				if(index_ == idx_) {
					img.attr("src", img.attr("src").replace("_off.", "_on."));
				}
				else {
					img.attr("src", img.attr("src").replace("_on.", "_off."));

					img.hover(function () {
						$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
					},function () {
						$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
					});
				}
			});
		},
		showpage : function(index_) {
			var pages = this.pages;
			
			pages.each(function(idx_){
				if(idx_ == index_) {
					$(this).removeClass("hide");
				}
				else{
					$(this).addClass("hide");
				}
			});
		},
		init : function() {
			var tabs = this.tabs;
			var imgs = tabs.find("img");
			var pages = this.pages;
			var select = this.select;
			var cls = this;
			
			pages.each(function(idx_){
				if(idx_ == select) {
					$(this).removeClass("hide");
				}
				else{
					$(this).addClass("hide");
				}
			});
			
			tabs.click(function(){
				var index =  tabs.index(this);
				cls.showpage(index);
				
				//選択されたタブの番号をクッキーに保存
				$.cookie("topTab", index, { path:'/',expires: 30 });
				
				return false;
			});
			
			imgs.click(function(){
				var index = imgs.index(this);
				cls.rollOver(index);
				
			});
			
			cls.rollOver(select);
			
		}
	};
	
	//トピックスのタブ実装
	var tabSelect = 0;
	if($.cookie("topTab")) {
		tabSelect = $.cookie("topTab");
	}
	var tab = new ClassTab($("#tab .list li"), $("#tab .box2"), tabSelect);
	tab.init();
	











	//ティッカー$.fn.tickerとすると$.ticker({～})と呼べない。$.fn.tickerは$("～").tickerで呼ぶこと。
	$.ticker = function(cfg_) {
		
		// 引数のデフォルト値を渡す {}内は、カンマ（,）で区切って複数可能
		cfg_ = $.extend({
				parent: null,
				text: null,
				btn: null,
				stop: '停止',
				btn: '再開'
			},cfg_);
		
		if(cfg_.parent == null || cfg_.text == null || cfg_.btn == null) {
			return;
		}
		
		//padding、borderを含めたティッカー横幅取得
		var tickerW = cfg_.parent.outerWidth(false);
		
		//ティッカーの高さを文字の高さより余裕を持たせる
		cfg_.parent.css("height", "1.4em");
		
		cfg_.parent.css("position", "relative");
		cfg_.text.css("position", "absolute");

		//ティッカー動作初期
		var animeFlg = true;
		
		//ボタンのタイトル設定
		if(animeFlg) {
			tickerStart();
		}
		else {
			tickerStop();
		}
		
		cfg_.btn.click(function(){
			if(animeFlg) {
				tickerStop();
			}
			else {
				tickerStart();
			}
		});


		function tickerStart() {
			animeFlg = true;
			cfg_.btn.val(cfg_.stop);
			
			//テキストの横幅取得
			var s = $("<span />")
				.text(cfg_.text.text())
				.hide()
				.appendTo("body");
			var w = s.width()+100;//100余裕をみて（chromeだと途切れるから）
			s.remove();
			
			//googleでabsoluteのanimateで下に文字が落ちるためテキストに横幅設定
			cfg_.text.css("width", w);
			
			//移動距離
			var dis = cfg_.text.position().left - (-1 * w);
			
			//1ピクセルの移動にかかる時間（ミリ秒）
			var pixTime = 20;

			//繰り返すためのこの関数の参照
			var func = arguments.callee;
			
			cfg_.text.animate({
				left: (-1 * w)
			},{queue: true, duration: dis * pixTime, easing: "linear", complete: function(){
				cfg_.text.css("left", tickerW);
				func();
			}});
			

			
		}
		
		function tickerStop() {
			animeFlg = false;
			cfg_.btn.val(cfg_.play);
			cfg_.text.stop();
		}

	};

	$.ticker({  parent:$(".tickerMod .box2"), text:$(".tickerMod .item"), btn:$(".tickerMod .input"), stop:'停止', play:'再開'  });












	/*広告バナー*/
	$(".banner2Mod .list").eqGroupHeight(5);
	$(".banner2Mod .list li").each(function(idx_) {
		//一番左だったら
		if(idx_%5==0) {
			$(this).addClass("firstLeftCom");
		}
	});


});







$(function(){
	
	if (swfobject.hasFlashPlayerVersion("8")) {
		//FLASH表示
		var flashvars = {};
		var params = {};
		params.menu = "false";
		params.wmode = "transparent";//メニューの上に乗っかる対策
		var attributes = {};
		swfobject.embedSWF("material/img_top/top_winter.swf", "headerFlash2", "704 ", "106", "9.0.0", "material/js/swfobject/expressInstall.swf", flashvars, params, attributes);
	}
	else {
		$("#headerFlash2").html('');
	}
});

