$(function() {
	// 連携キット、local時はexit
	if ($.LiVE.bdkit && location.protocol=="file:") return;
	
	//メニューリンク設定
	$.LiVE.Elements.SIDES.siteheader.find("a").each(function() {
		var $this = $(this),
			$body = $.LiVE.Elements.BODY,
			_url = $this.attr("href");
		
		if (_url.match(/^#/)) {
			if (!$.LiVE.mobile.check) {
				$this.click(function() {
					$.LiVE.navi.jump(_url.match(/id[0-9]+/));
				});
			}
			else {
				$this.click(function() {
					$body.stop().animate({
						scrollTop: $(_url).offset().top
					},{
						duration: 500,
					});
					return false;
				});
			}
		}
	});
	
	var $menu = $.LiVE.Elements.SIDES.sitemenu,
		$header = $.LiVE.Elements.SIDES.siteheader,
		menuNowColor = $('#sitemenu').find('li').find('a').css('color');
	
	$header.find('div.vertical').css('textAlign','left');
	
	// 表示コールバック
	$.LiVE.navicontrol.callbacks.push(function(sid){
		location.href.match(/[a-zA-Z0-9_]+\.html#id[0-9]+/g);
		var fileAnchor = RegExp.lastMatch;
		location.href.match(/[a-zA-Z0-9_]+\.html/g);
		var fileAnchorIndex = RegExp.lastMatch;
		
		$menu.find("li").each( function() {
			var li = $(this);
			var $menuAnchor = li.find('a').attr('href');
			if ( $menuAnchor == fileAnchor ) {
				li.addClass('on');
			} else {
				li.removeClass('on');
			}
			if ( $menuAnchor == fileAnchorIndex ) li.addClass('on');
		});
		currentcss();
	});
	
	function currentcss() {
		var val = $.LiVE.side.siteheader,
			$sitemenu = $('#sitemenu'),
			$sitemenuList = $sitemenu.find('li'),
			menuNow = [];
			menuNow[0] = $sitemenu.css('backgroundImage');
			menuNow[1] = $sitemenu.css('backgroundColor');
			menuNow[2] = $sitemenu.css('backgroundRepeat');
			menuNow[3] = $sitemenu.css('backgroundPosition');
			menuNow[4] = $sitemenu.css('backgroundAttachment');
			menuNow[5] = $sitemenu.css('backgroundSize');
		if (val.currentbg) {
			$sitemenuList.each( function() {
				var $this = $(this),
					this_anchor = $(this).find('a');
				if ( $(this).hasClass('on') ) {
					for (keys in val.currentcss) {
						$(this).css(keys, val.currentcss[keys]);
						if ( keys == "color" ) this_anchor.css(keys, val.currentcss[keys]);
					}
					bgset($this,val.currentbg);
					$this.css('backgroundRepeat','no-repeat');
					if ( $this.hasClass('menuBgIconRight') ) $this.css('backgroundPosition','right top');
				} else {
					$this.css({
						'backgroundImage': menuNow[0],
						'backgroundColor': menuNow[1],
						'backgroundRepeat': menuNow[2],
						'backgroundPosition': menuNow[3],
						'backgroundAttachment': menuNow[4],
						'backgroundSize': menuNow[5],
						'border':'none'
					});
					this_anchor.css('color',menuNowColor);
					if (val.menubg) {
						bgset($this,val.menubg);
						$this.css('backgroundRepeat','no-repeat');
						if ( $this.hasClass('menuBgIconRight') ) $this.css('backgroundPosition','right top');
					}
					for (keys in val.menucss) {
						$this.css(keys, val.menucss[keys]);
						if ( keys == "color" ) this_anchor.css(keys, val.menucss[keys]);
					}
				}
			});
		}
	}
	
	function bgset(e,css) {
		var bgpath = (css[1] == "none" || css[1] == "") ? "" : "url("+css[1]+")",
			bgsize = "auto";
		if (bgpath.length > 0) e.css("background-image", bgpath);
		
		e.css({
			"background-color": css[0],
			"background-repeat": css[2],
			"background-position": css[3] + " " + css[4],
			"background-attachment": css[5],
			"background-size": bgsize
		});
	}
});