// utf-8


/* variables
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

var pos = {
	works		:0,
	media		:1,
	fortune			:2,
	konkatsu		:3,
	company		:4,
	home		:5, //Default
	inquiry		:6,
	privacy		:7
};

var first = true;
var pageHeight = 800;
var query = location.href.substring(location.href.indexOf('#')+1,location.href.lastIndexOf(''));

var tab = {
	'company':'t1',
	'works':'t1'
};

// If no query
var viewingPage;
if(query.indexOf('http') == 0){
	viewingPage = 'home';
}else{
	viewingPage = query;
}

var defaultPageNum = pos[viewingPage];


/* functions
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

function jumpto(e){
	
	// Easin'
	jQuery.extend(jQuery.easing,{
		easeInQuart: function (x, t, b, c, d){
		return c*(t/=d)*t*t*t + b;
		}
	});
	
	if(first){
		// Show Page on first time
		$('body div#wrapper div#content.index div#articles').css('top', defaultPageNum * -pageHeight + "px");

	}else{
		// Move to Page
		$('body div#wrapper div#content.index div#articles').animate({
			top: pos[e]*-pageHeight + "px"
		}, "normal", "easeInQuart");
	}
	
	// Mark to sidemenu
	$('header nav ul li.' + viewingPage).removeClass('on');
	$('header nav ul li.' + e).addClass('on');
	
	viewingPage = e;
	
	//
	first = false;
}

// Post form data to mail.php
function jesus(){
	var nameVal = $('input#name').val();
	var emailVal = $('input#email').val();
	var contVal = $('textarea#cont').val();
	$.post('/mail.php',{ name: nameVal, email: emailVal, cont: contVal },read);
    return false;
}

// Callback
function read(e){
	$('input#name').val('');
	$('input#email').val('');
	$('textarea#cont').val('');
	$('article.inquiry div#form form table tr td.submit input').attr('src','/common/img/theme-red/button_send_comp.png');
}

/* actions
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

$(function(){

	//On Load
	jumpto(viewingPage);
	
	// Load news to news page
	$('article.company div#t1 ul').load('/news.html');
	
	// Load the first 1 news to sidebar
	$('header ul#news').load('/news.html',null,function(){
		$('header ul#news li:not(li:first)').hide();
	});

	// Page Navigation
	$("a.page").click(function(){
		var href = $(this).attr("href");
		var toPage = href.substring(href.indexOf('#')+1,$(this).attr("href").lastIndexOf(''));
		jumpto(toPage);
	});
	
	// Select First Tab
	$('article ul#tab_ui li:first-child a').addClass('on');
	
	// Tab Interface
	$('article ul#tab_ui li a').click(function(){
		$(this).each(function(){
			var article = $(this).parent().parent().parent().attr('class');
			var now = $(this).attr('href').replace('#','');
			$(this).parent().parent().nextAll().filter('#'+tab[article]).hide();
			$(this).parent().parent().nextAll().filter('#'+now).show();
			$(this).parent().parent().find('.on').removeClass('on');
			$(this).addClass('on');
			tab[article] = now;
		});
		return false;
	});
	
	// Inquiry
	$('article.inquiry div.agreebutton input.yes').click(function(){
		$('article.inquiry div.scroll').hide();
		$('article.inquiry div.agreebutton').hide();
		$('article.inquiry div#form').show();
	});
	
	$('article.inquiry div.agreebutton input.no').click(function(){
		location.href="/";
	});
	
	$('article.inquiry div#form form').submit(function(){
		return jesus();
	})

});










