//by fjm 2011.02.17

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-li').hide();
	$('#slide-prev').hide();
	slide_count = 0;
	slide_max = $('.slide-li').length - 1;
	
	$('.post-edit-link').attr("target","_blank");

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

	//slide - animate --------------------------------------------
	var category = $('.relative').attr("id");
	if ( category == "works" || category == "home" ) {
		animate = 160;
	} else if ( category == "works_detail") {
		animate = 480;
	} else if ( category == "about") {
		animate = 480;
	} else if (category == "award") {
		animate = 319;
	}

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


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


	// clickable --------------------------------------------
	$('.clickable').hover( function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	}).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'});

	$(".slide-li").each(function(i){
		var delay = 200 * i;
		//$(this).delay(delay).fadeTo(1000,1);
		var timer = setTimeout(function(){
			$($(".slide-li")[i]).addClass("animation").fadeTo(800,1,function() {
				if ( !jQuery.support.opacity ) { this.style.removeAttribute('filter'); }
			});
			clearTimeout(timer);
		}, delay);
	});

});

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) {
	if( sc < sm ) {
		$('#slide-content > ul').animate({ left:'-=' + an}, 800,function() {
			if ( sc + 1 == sm) {
				$('#slide-next').each(function() {
					if ( !jQuery.support.opacity ){ $(this).hide(); } else { $(this).fadeTo(200,0); }
					//if ( !jQuery.support.opacity ) { this.style.removeAttribute('filter'); }
				}).addClass('hide');
			}
		});
		$('#slide-prev').each(function() {
			if ( !jQuery.support.opacity ){ $(this).show(); } else { $(this).fadeTo(500,1); }
		}).removeClass('hide');
		slide_count++;
	}
}

function slide_prev (sc, sm, an) {
	if (sc > 0) {
		$('#slide-content > ul ').animate({ left:'+=' + an}, 800, function() {
			if ( sc - 1 == 0 ) {
				$('#slide-prev').each(function() {
					if ( !jQuery.support.opacity ){ $(this).hide(); } else { $(this).fadeTo(200,0); }
				}).addClass('hide');
			}
		});
		$('#slide-next').each(function() {
			if ( !jQuery.support.opacity ){ $(this).show(); } else { $(this).fadeTo(500,1); }
		}).removeClass('hide');
		slide_count--;
	}
}

