var siteurl = location.protocol + '//' + location.host + '/';
var ua = navigator.userAgent;

$(function(){

	$('#tm').click(function(){
		location.href = siteurl;
		return false;
	});

//	$('#sash').wrapInner('<a></a>');
//	$('#sash a').attr('href', 'http://www.ousenoyado.com:9016/');



//	var elems = [];

//	if(ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod')  > -1) {

//		$('#navi li.parent').find('.first').each(function(){
//			hrefs.push(this.href);
//			$(this).attr('href', '');
//		});
//	}


	var navi_width = ['570px', '722px'];
	var navi_height = ['200px', '150px'];

	$('#navi li.parent').each(function(i){

//	elems.push(this.id);

		var children = $(this).children('.first');


		var width = $(this).css('width');
		var height = $(this).css('height');
		var li_navi_width = navi_width[i];
		var li_navi_height = navi_height[i];

		var bgPosDef = children.css('backgroundPosition');
		var bgPosX = 0;
		var bgPosY = 0;
		if (bgPosDef) {
			var bgPos = bgPosDef.split(' '),
			bgPosX = bgPos[0];
			bgPosY = bgPos[1];
		} else {
			bgPosX = children.css('backgroundPositionX');
			bgPosY = children.css('backgroundPositionY');
		}

		if (ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod')  > -1) {

			$(this).bind('touchstart', function(e){

				e.preventDefault();

				$('#navi li.parent').each(function(){

					$(this).children('.navi_org').css({
						display: 'none'
					});
					$(this).stop(true, false).animate({
							width: width
					}, 500, function(){
						$(this).css({
							height: height
						});
						children.css({
							backgroundPosition: bgPosX + ' ' + bgPosY
						});
					});
				});

				$(this).css({
					height: li_navi_height
				});
				children.css({
					backgroundPosition: '-108px ' + bgPosY
				});
				$(this).stop(true, false).animate({
					width: li_navi_width
				}, function(){
					$(this).children('.navi_org').css({
						display: 'block'
					});
				});
			});

			$(this).bind('touchmove', function(e){

				e.preventDefault();

				$('#navi li.parent').each(function(){

					$(this).children('.navi_org').css({
						display: 'none'
					});
					$(this).stop(true, false).animate({
							width: width
					}, 500, function(){
						$(this).css({
							height: height
						});
						children.css({
							backgroundPosition: bgPosX + ' ' + bgPosY
						});
					});
				});
			});

			$('.parent div a').bind('touchstart', function(){
				location.href = this.href;
				return false;
			});

		} else {

			$(this).hover(function(){

				$(this).css({
					height: li_navi_height
				});
				children.css({
					backgroundPosition: '-108px ' + bgPosY
				});
				$(this).stop(true, false).animate({
					width: li_navi_width
				}, function(){
					$(this).children('.navi_org').css({
						display: 'block'
					});
				});
			},function(){
				$(this).children('.navi_org').css({
					display: 'none'
				});
				$(this).stop(true, false).animate({
						width: width
				}, 500, function(){
					$(this).css({
						height: height
					});
					children.css({
						backgroundPosition: bgPosX + ' ' + bgPosY
					});
				});		
			});
		}


	});

//		alert (elems);

	$('a[rel^="prettyPhoto"]').prettyPhoto({
		animationSpeed: 'slow',
		theme: 'facebook',
		slideshow: 4000,
//		autoplay_slideshow: true,
		animation_speed: 'slow'
	});

	$('.subnavi li').biggerlink();
	$('.thisplan li').biggerlink();


	$('a[href^="http"]').not('[href*="'+ location.hostname + '"]').click(function(){
		window.open(this.href, '');
		return false;
	});

});


if(ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod')  > -1) {

	_include('/style/mobile.css', 'link')
}


function _include(astrfile, elem) {

	var inc_elem = document.createElement(elem);

	if(elem == 'script') {

		inc_elem.src = siteurl + astrfile;
		inc_elem.type = 'text/javascript';
//		inc_elem.defer = true;

	} else if(elem == 'link') {

		inc_elem.rel = 'stylesheet';
		inc_elem.href = siteurl + astrfile;
		inc_elem.type = 'text/css';
	}

	document.getElementsByTagName('head').item(0).appendChild(inc_elem);
}

