var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("android") > -1 || ua.indexOf("iphone") > -1 || ua.indexOf("ipod") > -1) {
	path = location.toString().replace('http://', '');
	path = path.substring(path.indexOf('/'));
	
	if (path.match(/^\/(index.html)?$/)) {
		location.href='/sp/';
	}

	if (path.match(/^\/unkou\/(index.html)?$/)) {
		location.href='/unkou/sp.html';
	}

	if (path.match(/^\/station\/(index.html)?$/)) {
		location.href='/sp/station/index.html';
	}

	if (path.match(/^\/station\/index02.html$/)) {
		location.href='/sp/station/index02.html';
	}

	if (path.match(/^\/station\/index03.html$/)) {
		location.href='/sp/station/index03.html';
	}

	if (path.match(/^\/shop\/category\/(index.html)?$/)) {
		location.href='/sp/shop/category/index.html';
	}

	if (path.match(/^\/shop\/routemap\/(index.html)?$/)) {
		location.href='/sp/shop/routemap/index.html';
	}

	if (path.match(/^\/shop\/brand\/(index.html)?$/)) {
		location.href='/sp/shop/brand/index.html';
	}

	if (path.match(/^\/ticket\/search\/index.php$/)) {
		location.href='/sp/ticket/search/index.php';
	}

	if (path.match(/^\/ticket\/search\/$/)) {
		location.href='/sp/ticket/search/index.php';
	}


}

/* --------------------------------------------------
	IE BackgroundImageCache
-------------------------------------------------- */
try{
	document.execCommand("BackgroundImageCache", false, true);
}
catch(err){}


/* --------------------------------------------------
	LOAD FUNCTION
-------------------------------------------------- */
(function($){
$(function(){
	/* Basic */
	$('.over').imgHover({ suffix : '_o' });
	$('.boxover').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);

	$('#pagetopComp a,ul.anchorLink a,div.anchorLink a').scrollFor();

	$('a.historyBack').historyBack();

	/* Layer */
	$('a.colorbox').colorbox({
		close : '<img src="/common/images/stSearchMod_btn_02.gif" alt="閉じる" width="69" height="23" />'
	});
});

})(jQuery);


/* --------------------------------------------------
	HISTORY BACK
-------------------------------------------------- */
(function($){
$.fn.extend({
	historyBack : function(){
		this.click(function(e){
			if(document.referrer != ''){
				history.back();
				e.preventDefault();
			}
		});
	}
});

})(jQuery);


/* --------------------------------------------------
	WINDOW CLOSE
-------------------------------------------------- */
(function($){
$(function(){
	$('a.windowClose').click(function(e){
		window.close();
		e.preventDefault();
	});
});
})(jQuery);


/* --------------------------------------------------
	PAGE PRINT
-------------------------------------------------- */
(function($){
$(function(){
	$('a.pagePrint').click(function(e){
		window.print();
		e.preventDefault();
	});
});
})(jQuery);


/* --------------------------------------------------
	STAY LOCAL
-------------------------------------------------- */
(function($){
addCssRule(document.styleSheets[0], '#localNav .lNavList_1st ul', 'display : none;');
addCssRule(document.styleSheets[0], 'body #fontsizeWrap', 'display : block;');

$(function(){
	var _prefixBodyClass = 'lStay';
	var _prefixId = 'lNav';

	var classArray = $('body').attr('class').split(' ');
	$.each(classArray, function(index, value){
		if(value.search('^' + _prefixBodyClass) != -1){
			var _targetId = _prefixId + value.substr(_prefixBodyClass.length);
			var _targetNode = $('#' + _targetId).addClass('open');
			_targetNode.parentsUntil('.lNavList_1st').not('li').addClass('open');
			_targetNode.children('a').addClass('stay');
			_targetNode.children('ul').addClass('open');
		}
	});

	var _localNav = $('#localNav');
	_localNav.find('li').has('ul:visible').addClass('down');
	_localNav.find('li:visible').last().addClass('last');

	_localNav.find('ul.lNavList_2nd:not(.open)').css({ 'display' : 'none' });
	_localNav.find('ul.lNavList_3rd:not(.open)').css({ 'display' : 'none' });

});

function addCssRule(stylesheet, selector, property) {
	if(stylesheet.insertRule) {
		stylesheet.insertRule(selector + '{' + property + '}', stylesheet.cssRules.length);
	} else if(stylesheet.addRule){
		stylesheet.addRule(selector, '{' + property + '}');
	}
}

})(jQuery);

/* --------------------------------------------------
	STYLE SWITCH
-------------------------------------------------- */
(function($){
	var cookiekey = 'metroFontSize';
	var btns;

	$(function() {
		btns = $('#fontsizeList li').click(function(e) {
			var c = this.id;
			switchStylestyle(c);
			switchImgActiviity(c);
			this.blur();
			e.preventDefault();
		});
		var c = $.readCookie(cookiekey);
		if(!c){ c = 'fontsize_s'; }
		switchStylestyle(c);
		switchImgActiviity(c);
	});
	function switchStylestyle(styleName) {
		var linkElems = $('link[rel*=style][title]').each(function(i, item) {
			this.disabled = true;
		});
		linkElems.each(function(){
			if (this.getAttribute('title') == styleName) {
				this.disabled = false; 
			}
		});
		$.createCookie(cookiekey, styleName, 365);
	}
	function switchImgActiviity(styleName) {
		btns.each(function(i, item){
			var _this = $(this);
			var _img = _this.find('img');


			if(this.id == styleName){
				_img.attr('src', _img.data('src_o')).addClass('active').unbind();
			} else if(_img.hasClass('active')) {
				_img.attr('src', _img.data('src')).removeClass('active').imgHover();
			}
		});
	}
})(jQuery);



/* --------------------------------------------------
	Tooltip
-------------------------------------------------- */
(function($){
$(function(){
	$(".tooltip").each(function(index, item){
		var $this = $(item);
		var _opts = {
			delay: 0,
			extraClass: "",
			id: "tooltip",
			balloonSel: ".balloon",
			balloonImgSel: ".balloonImg"
		};
		if($this.find(_opts.balloonImgSel).length > 0){
			_opts.extraClass = 'balloonIcon';
		}
		$this.tooltip(_opts);
	});
});

})(jQuery);


/* --------------------------------------------------
	Popup
-------------------------------------------------- */
(function($){
$(function(){
$('a.popup').click(function(e){
	var url = $(this).attr('href');
	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = parseQuery(queryString);
	var opt = {
		width : 500,
		height : 500,
		toolbar : 'no',
		location : 'no',
		directoryies : 'no',
		status : 'no',
		menubar : 'no',
		scrollbars : 'yes',
		resizable : 'no',
		copyhistory : 'no'
	};
	for(var key in params){
		opt[key] = params[key];
	}

	window.open(url, 'NewWin', 'toolbar='+opt.toolbar+',location='+opt.location+',directoryies='+opt.directoryies+',status='+opt.status+',menubar='+opt.menubar+',scrollbars='+opt.scrollbars+',resizable='+opt.resizable+',copyhistory='+opt.copyhistory+',width='+params.width+',height='+params.height);
	e.preventDefault();

});
function parseQuery (query) {
	var Params = {};
	if ( ! query ) {return Params;}// return empty object
	var Pairs = query.split(/[;&]/);
	for ( var i = 0; i < Pairs.length; i++ ) {
		var KeyVal = Pairs[i].split('=');
		if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
		var key = unescape( KeyVal[0] );
		var val = unescape( KeyVal[1] );
		val = val.replace(/\+/g, ' ');
		Params[key] = val;
	}
	return Params;
}
});

})(jQuery);

