var ua =navigator.userAgent;
if(ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod')  > -1){
	document.addEventListener("touchstart", touchStart, false);
	document.addEventListener("touchmove", touchMove, false);
	document.addEventListener("touchend", touchEnd, false);
}

$(document).ready(function() {

slide_count = 0;
slide_max = $('.slide-li').length - 1;
//alert(slide_max);

/*
	if( !jQuery.support.opacity ){ //for IE6,7,8
		$('#slide-prev').hide();
	} else {
		$('#slide-prev').fadeTo(0,0);
	}
*/
	
	$('#archives_detail #slide-content .slide-li div:has(img)').each( function() {
		$(this).css('background-color','#eeeeee');
	});

	//slide- height --------------------------------------------
	if ( $('.contents').length ) {
		$('.relative').height($('.contents').height());
	}

	//slide - animate --------------------------------------------
	var category = $('.relative').attr("id");
	if ( category == "archives" || category == "home" ) {
		animate = 606;
	} else if ( category == "2") {
		animate = 220;
	} 
	// slide -next --------------------------------------------
	$('#slide-next').click( function() {
		slide_next(slide_count,slide_max,animate);
		//alert(slide_count);
		return false;
	});


	// slide -prev --------------------------------------------
	$('#slide-prev').click( function() {
		slide_prev(slide_count,slide_max,animate);
		//alert(slide_count);
		return false;
	});


	// clickable --------------------------------------------
	$('.clickable').hover( function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});

	$('.clickable').click( function() {
		window.location.href = $(this).find("a").attr("href");
		return false;
	});

//rollover
	var postfix = '-1';
	$('.hover img').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('-0.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(
			function() {
				img.attr('src', src_on);
			},
			function() {
				img.attr('src', src);
			}
		);
	});

	//PrettyPhoto
	$('a[href$=.jpg], a[href$=.jpeg], a[href$=.JPG], a[href$=.gif], a[href$=.png]').prettyPhoto({animationSpeed:'slow',theme:'light_square'});
});

function touchStart(e) {
	//e.preventDefault();
	var t = e.touches[0];

	//$('#dEvent').html('touchstart');
	$('#sdX').html(t.pageX);
	$('#sdY').html(t.pageY);
	start_x = t.pageX;
	//start_y = t.pageY;
}

function touchMove(e) {
	//e.preventDefault();
	var t = e.touches[0];

	//$('#dEvent').html('touchmove');
	$('#mdX').html(t.pageX);
	$('#mdY').html(t.pageY);
	move_x = t.pageX;
	//move_y = t.pageY;
}

function touchEnd(e) {
	//e.preventDefault();
	//$('#dEvent').html('touchend');
	//$('#dX').html('-');
	//$('#dY').html('-');
	distance = 0;
	if ( move_x > 0 ) { distance = start_x - move_x; }
	$('#dis').html(distance);
	//alert(animate);
	if ( distance > 70 ) {
		slide_next(slide_count,slide_max,animate);
	} else if ( distance < -70 ) {
		slide_prev(slide_count,slide_max,animate);
	}
	start_x = 0; move_x = 0;
}

function slide_next (sc,sm,an) {
//	alert("sc：" + sc + " / scm：" + sm + " / an：" + an);
		if( sc < sm ) {
			$('#slide-content > ul').animate({ left:'-=' + an}, 800);
			slide_count++;
		}
/*
		if ( sc == sm) { 
			if( !jQuery.support.opacity ){ //for IE6,7,8
				$('#slide-next').hide(); 
			} else {
				$('#slide-next').fadeTo(500,0); 
			}
		} else {
		 alert("not");
		 }
		
		if ( sc == 1 ) { 
			if( !jQuery.support.opacity ){ //for IE6,7,8
				$('#slide-prev').show();
			} else {
				$('#slide-prev').fadeTo(500,1);
			}
		}
*/
	//return(sc);
}

function slide_prev (sc, sm, an) {
//	alert("sc：" + sc + " / scm：" + sm + " / an：" + an);
		$('#slide-next').show();
		if (sc > 0) {
			$('#slide-content > ul ').animate({ left:'+=' + an}, 800);
				slide_count--;
			//alert(sc);
		}

/*
		if ( sc = 0 ) { 
			if( !jQuery.support.opacity ){ //for IE6,7,8
				$('#slide-prev').hide(); 
			} else {
				$('#slide-prev').fadeTo(500,0); 
			}
		}
		
		if ( sc == sm - 1) { 
			if( !jQuery.support.opacity ){ //for IE6,7,8
				$('#slide-next').show();
			} else {
				$('#slide-next').fadeTo(500,1);
			}
		}
*/
	//return(sc);
}
