var ota = new Object();

if (!("console" in window)) {
	window.console = {};
	window.console.log = function(str){
		return str;
	};
}


$(document).ready(function(){

	//--------------------------------------------
	// common 
	//--------------------------------------------
	
	//browser check -------------------------------------------- */
	if (navigator.platform.indexOf("Win") != -1) {
		$('body').addClass("win");
		if ( !jQuery.support.leadingWhitespace ) { $('body').addClass("win-ie"); }
		
		if(!jQuery.support.opacity){
			if(!jQuery.support.style){
				if (typeof document.documentElement.style.maxHeight != "undefined") {
					//IE7
					remarks.browser = "IE7";
				} else {
					//IE6
					remarks.browser = "IE6";
				}
			} else {
				//IE8
				remarks.browser = "IE8";
			}
		}
	}


	//scroll -------------------------------------------- */
	$('.a-pagetop').click( function() { 
		$('html,body').animate({ scrollTop: 0 }, 800, 'quart');
		return false;
	});

	//scroll - anchor  -------------------------------------------- */
	$('.a-anchor').click( function() { 
		var target = $(this).attr('href');
		var scroll = $(target).offset();
		$('html,body').animate({ scrollTop: scroll.top }, 800, 'quart');
		return false;
	});

	//rollover -------------------------------------------- */
	$('a:has(img), input[type="image"]').not('.current').hover(function(){
		$(this).stop(true, false).animate({ opacity: .5 }, { duration: 200});
		if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
	},function() {
		$(this).stop(true, false).animate({ opacity: 1 }, { duration: 200});
		if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
	});

	//mail -------------------------------------------- */
	$('.a-mail').each(function() {
		var e = $(this).html().split(' ').join('@');
		$(this).attr('href','mailto:'+ e).html(e);
	});
	
	//a.mail -------------------------------------------- */
	$('.main a[href^="mailto"]').addClass('mail');
	$('.main a[target="_blank"]').addClass('blank');

	//--------------------------------------------
	// ota 
	//--------------------------------------------
	//objects -------------------------------------------- */
	ota = {
		body : $('body'),
		footer :$('#footer')
	};
	
	//init -------------------------------------------- */
	bodyHeight = ota.body.height();
	window_size();
	window_footer(ota.h);
	
	//nav -------------------------------------------- */
	$('#nav .current').fadeTo(0,0.4);
	
	//.c -------------------------------------------- */
	if(jQuery.support.opacity){
		$('.c').hide().fadeTo(1000,1);
	}
	
	//index -------------------------------------------- */
	$('#index .img').fadeOut(0,0.1);
	
	//thumbnail -------------------------------------------- */
	$('.thumbnail li').each(function(e) {
		$(this).find('a').each(function() {
			$(this).hide().delay(50*e).fadeTo(300,1,function() {
			});
		});
	});
	
	//portfolio -------------------------------------------- */
	$('a.slide-nav').hover(function() {
		$(this).stop(true, false).animate({ opacity: .5 }, { duration: 0});
	}, function() {
		$(this).stop(true, false).animate({ opacity: 1 }, { duration: 0});
	});
	$('.portfolio').each(function() {
		ota.portfolio = new Object();
		ota.portfolio = {
			count : new Number(0),
			prev : $(this).find('.slide-prev'),
			next : $(this).find('.slide-next'),
			content : $(this).find('ul'),
			content_li: $(this).find('ul').children(),
			content_length : $(this).find('ul').children().length,
			content_w : new Number(0), // all width
			content_w_array : new Array(), //each width
			content_w_array_p : new Array(), //each pos ( center ),
			content_w_array_l : new Array() //each pos ( left )
		};
		
		var width = new Number(0);
		ota.portfolio.content.children().each(function(e) {
			$(this).find('img').each(function() {
				$(this).hide().delay(200*e).fadeTo(300,1,function() {
					var src = $(this).attr('src');
					$(this).parent().css('background','url(' + src + ') left top no-repeat');
					$(this).attr('src','/assets/images/spacer.gif');
				});
			});
			
			$(this).find('iframe').each(function() {
				var src = $(this).attr('src');
				$(this).attr('src',src + '&amp;wmode=transparent');
			});
			
			width = $(this).width();
			ota.portfolio.content_w_array[e] = parseInt(width); //set each width
			$(this).width(width);
			ota.portfolio.content_w += parseInt(width); // set all width
			ota.portfolio.content_w_array_l[e+1] = ota.portfolio.content_w;
			
			$(this).click(function() {
				ota.portfolio.count = e;
				check_status(e);
			});
		});
		ota.portfolio.content_li.eq(0).addClass('unactive');

		ota.portfolio.content.width(ota.portfolio.content_w);
		portfolio_size();
		
		if ( ota.portfolio.content_length == 1 ) {
			$(this).find('.slide-nav').hide();
		} else {
			//check_status(ota.portfolio.count);
		}
		
		var hash = window.location.hash.replace('#id','');
		if (hash) {
			ota.portfolio.count = new Number(hash);
			check_status(ota.portfolio.count);
		}
		
		ota.portfolio.prev.click(function() {
			if (ota.portfolio.count > 0) {
				ota.portfolio.count--;
				check_status(ota.portfolio.count);
			}
			return false;
		});
		
		ota.portfolio.next.click(function() {
			if ( ota.portfolio.content_length > ota.portfolio.count + 1 ) {
				ota.portfolio.count++;
				check_status(ota.portfolio.count);
			}
			return false;
		});
		
		$('html').keyup(function(e){
			switch(e.which) {
				//← キー
				case 37:
					if (ota.portfolio.count > 0) {
						ota.portfolio.count--;
						check_status(ota.portfolio.count);
					}
					break;
				//→ キー
				case 39:
					if ( ota.portfolio.content_length > ota.portfolio.count + 1 ) {
						ota.portfolio.count++;
						check_status(ota.portfolio.count);
					}
					break;
			}
		});
		
		function check_status (count) {
			console.log('count：' + count);
			console.log('this Center：' + ota.portfolio.content_w_array_p[count]);
			console.log('thisMargin：' + ota.portfolio.content.css('margin-left'));
			
			if (count == 0) {
				ota.portfolio.prev.hide();
				ota.portfolio.content.animate(
					{marginLeft : ota.portfolio.content_w_array_p[0] + 'px'},
					800, 'easeOutQuint', function() {
						ota.portfolio.content_li.removeClass('unactive');
						ota.portfolio.content_li.eq(0).addClass('unactive');
					}
				);
			} else {
				if (count == ota.portfolio.content_length - 1 ) {
					ota.portfolio.next.hide();
				} else {
					ota.portfolio.prev.show();
					ota.portfolio.next.show();
				}
				ota.portfolio.content.animate(
					{marginLeft : ota.portfolio.content_w_array_p[ota.portfolio.count] - ota.portfolio.content_w_array_l[ota.portfolio.count]+ 'px'},
					800, 'easeOutQuint', function() {
						ota.portfolio.content_li.removeClass('unactive');
						ota.portfolio.content_li.eq(ota.portfolio.count).addClass('unactive');
					}
				);
			}
			location.hash = "id" + ota.portfolio.count;
		}
		
		$(window).bind('resize', function() {
			portfolio_size();
			ota.portfolio.count = 1;
		});
		
	});
	
});


$(window).load(function() {

	$('#index #header').fadeTo(1000,1, function() {
		$(this).next().delay(300).fadeTo(500,1);
		$(this).next().next().delay(300).fadeTo(500,1);
		$(this).next().next().next().delay(300).fadeTo(500,1);
	});
	
	$(window).resize(function(){
		window_size();
		window_footer(ota.h);
	});


});


$(window).scroll(function () {
});

jQuery.easing.quart = function (x, t, b, c, d) { return -c * ((t=t/d-1)*t*t*t - 1) + b; };

//window size -------------------------------------------- */
function window_size () {
	ota.w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || $(window).width();
	ota.h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || $(window).height();
	$('#debug #w').text('w:' + ota.w);
}

//window_footer -------------------------------------------- */
function window_footer(y) {
	console.log('body-height：' + bodyHeight);
	if ( y > bodyHeight - 60 + 63 ) {
		ota.body.height(y);
		ota.footer.addClass('absolute');
	} else {
		ota.body.removeAttr('style');
		ota.footer.removeClass('absolute');
	}
}

//portfolio_size -------------------------------------------- */
function portfolio_size() {
		for( i=0; i<ota.portfolio.content_w_array.length; i++) { 
			ota.portfolio.content_w_array_p[i] = ota.w / 2 - ota.portfolio.content_w_array[i]/2; // set each pos ( center )
		}
		
		$('#debug #content_w_array_p').text("content_w_array_p：" + ota.portfolio.content_w_array_p);
		$('#debug #content_w_array_l').text("content_w_array_l：" + ota.portfolio.content_w_array_l);

		ota.portfolio.content.css('margin-left', ota.portfolio.content_w_array_p[0] + 'px');

		$('#debug #init_m').text("init_m：" + ota.portfolio.content_w_array_p[0]);
		$('#debug #length').text("length：" + ota.portfolio.content.children().length);
		$('#debug #width').text("width：" + ota.portfolio.content_w);

}


