var initAnimationComplete = false;
var worksDetailId = '';
var data;

var selectValue1 = [
	 "Select a topic"
	,"About our applications"
	,"About ordering an application development for iPhone/iPad/Android"
	,"About ordering a website development"
	,"Other topics"
];

var selectValue2 = [[
	"Select a topic"
],[
	 "Select a topic"
	,"App for iPhone"
	,"App for iPad"
	,"App for Android"
],[
	 "Select a topic"
	,"App for iPhone"
	,"App for iPad"
	,"App for Android"
	,"App for iPhone &amp; iPad"
	,"App for iPhone &amp; Android"
],[
	 "Select a topic"
	,"Website for PC"
	,"Website for Smartphone"
	,"Website for Mobile"
],[
	"---------"
]];


function initRollovers() {
	
	if (!document.getElementById) return false;
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');
	
	for (var i = 0; i < aImages.length; i++) { 
	
		if (aImages[i].className && aImages[i].className.indexOf('ovr') >= 0) {
	
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_ovr'+ftype);
	
	 
			aImages[i].setAttribute('hsrc', hsrc);
	
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
	
			if (aImages[i].className && aImages[i].className.indexOf('select') >= 0) {
				aImages[i].setAttribute('src', hsrc);
			}else{
				aImages[i].onmouseover = function() {
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
	
				aImages[i].onmouseout = function() {
					if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_ovr'+ftype, ftype);
					this.setAttribute('src', sTempSrc);
				}
			}
		}
	}
}

function initGoogleMap() {

	if (!GBrowserIsCompatible()) return;

	var ballon_msg = '<a href="http://www.atech.ne.jp"><font size="3"><strong>エーテック株式会社</strong></font></a><br>〒105-0004<br>東京都港区新橋2-14-4 Rビル6F<br>Tel.03-5511-5521　Fax.03-5511-5571';

	var map = new GMap2(document.getElementById("map_canvas"));
	map.setMapType(G_NORMAL_MAP);
	map.setCenter(new GLatLng(35.666645, 139.75551), 16);

	//マップコントローラ
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMapTypeControl());
	//マーカー
	var marker = new GMarker(new GLatLng(35.666645, 139.75551));
	map.addOverlay(marker);

	GEvent.addListener(marker, 'click', function(){
		marker.openInfoWindowHtml(ballon_msg);
	});
}

function pageScrollTo(val) {
	val -= 30;
	jQuery('html, body').animate({scrollTop:val}, 800, 'expoout');
}

var prevTargetOffset = -15;

function scrollNavigation() {

	if(!initAnimationComplete) {
		setTimeout( scrollNavigation, 200);
		return;
	}

	var targetOffset = jQuery('body').scrollTop();
	targetOffset = Math.max(targetOffset, document.documentElement.scrollTop);

	targetOffset -= 15;
	jQuery('#sideBoxL').animate({ top: targetOffset }, {queue: false, duration: 500, easing:'expoout'} );

	if(Math.abs( targetOffset - prevTargetOffset ) >= 5 ) {
		jQuery("#icoMenu img").attr('src','common/img/ico_yz_updown.gif');
	}else{
		jQuery("#icoMenu img").attr('src','common/img/ico_yz.gif');
	}
	prevTargetOffset = targetOffset;
	setTimeout( scrollNavigation, 200);

}

var prevTargetOffset = -15;



function worksDetailClose() {
	worksDetailId = '';
	if(jQuery.browser.msie){
		jQuery('div#worksDetail').hide();
	}else{
		jQuery('div#worksDetail').fadeOut();
	}

}

function worksDetailNext() {
	var id = worksDetailId.match(/(pc|mb|sp|app)([0-9]{2})/);
	var type = id[1];
	var targetIndex;
	for( var i=0; i < data[type].length; i++) {
		if( data[type][i].id == worksDetailId) {
			targetIndex = i;
		}
	}
	dataChange(type, Math.min(targetIndex+1, data[type].length - 1 ));
}

function worksDetailPrev() {
	var id = worksDetailId.match(/(pc|mb|sp|app)([0-9]{2})/);
	var type = id[1];
	var targetIndex;
	for( var i=0; i < data[type].length; i++) {
		if( data[type][i].id == worksDetailId) {
			targetIndex = i;
		}
	}
	dataChange(type, Math.max(targetIndex-1, 0));
}


function dataChange(type, index) {
	if(type != 'pc' && type != 'mb' && type != 'app' && type != 'sp') return;


	jQuery('div#worksDetail h5').html(data[type][index].title);
	if(type == 'pc' ) {
		jQuery('div#worksDetail div.pht').html('<img src="img/works_' + data[type][index].id + '.jpg" width="220" height="170" alt="" />');
	}else if( type == 'mb') {
		jQuery('div#worksDetail div.pht').html('<img src="img/works_' + data[type][index].id + '.jpg" width="220" height="220" alt="" />');
	}else if(type == 'sp' ) {
		jQuery('div#worksDetail div.pht').html('<img src="img/works_' + data[type][index].id + '.jpg" width="220" height="170" alt="" />');
	}else if(type == 'app'){
		jQuery('div#worksDetail div.pht').html('<img src="img/works_' + data[type][index].id + '.jpg" width="220" height="170" alt="" />');
	}
	if(type == 'pc' || type == 'mb' ) {
		jQuery('div#worksDetail p.link').show();
		jQuery('div#worksDetail p.link').html('<a href="' + data[type][index].link + '" target="_blank">' + data[type][index].link + '</a>');
	}else if(type == 'so' || type == 'app') {
		jQuery('div#worksDetail p.link').hide();
	}

	jQuery('div#worksDetail p.info').html(data[type][index].info);

	if(type == 'pc' || type == 'mb' ) {
		jQuery('div#worksDetail table.listTable2').hide();
		jQuery('div#worksDetail table.listTable').show();
		jQuery('div#worksDetail td.flag1').html( (data[type][index].flag.substring(0,1) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag2').html( (data[type][index].flag.substring(1,2) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag3').html( (data[type][index].flag.substring(2,3) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag4').html( (data[type][index].flag.substring(3,4) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag5').html( (data[type][index].flag.substring(4,5) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag6').html( (data[type][index].flag.substring(5,6) == '1' ? '●' : 'ー') );
	}else if(type == 'sp' || type == 'app') {
		jQuery('div#worksDetail table.listTable').hide();
		jQuery('div#worksDetail table.listTable2').show();
		jQuery('div#worksDetail td.flag7').html( (data[type][index].flag.substring(0,1) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag8').html( (data[type][index].flag.substring(1,2) == '1' ? '●' : 'ー') );
		jQuery('div#worksDetail td.flag9').html( (data[type][index].flag.substring(2,3) == '1' ? '●' : 'ー') );
	}
	if(type == 'pc' || type == 'mb' || type == 'sp' ) {
		jQuery('div#worksDetail p.btn').hide();
	}else if( type == 'app') {
		jQuery('div#worksDetail p.btn').hide();
		//jQuery('div#worksDetail p.btn').show();
		//jQuery('div#worksDetail p.btn').html('<a href="' + data[type][index].link + '" target="_blank"><img src="common/img/bt_app.gif" width="116" height="40" /></a>');
	}
	worksDetailId = data[type][index].id;

}

jQuery(function(){

	initRollovers();

	jQuery('a').focus( function() { this.blur(); } );

	jQuery('div.pageTop a').click(function(){
		if(!initAnimationComplete) return;
		pageScrollTo(0);
		return false;
	});

	jQuery('h1 a').click(function(){
		if(!initAnimationComplete) return;
		pageScrollTo(0);
		return false;
	});

	jQuery('a[href^=#]').click(function(){
		if(!initAnimationComplete) return;
		var targetOffset = jQuery(jQuery(this).attr("href")).offset().top;
		pageScrollTo(targetOffset);
		var target = jQuery(this).attr("href").substring(1);
		if(target == 'news' || target == 'service' || target == 'works' || target == 'company' || target == 'contact' || target == 'privacy' || target == 'about' ) {
/*
			pageTracker._trackEvent('display', target);//for Log;
*/
			if(location.href.indexOf('index_en') > 0 ){
				pageTracker._trackEvent('en', 'display', target);
			}else{
				pageTracker._trackEvent('ja', 'display', target);
			}




			jQuery.getJSON( 'log.html?pageid=' + target ) //for Log;
		}
		return false;
	});

	jQuery('div#newsBoxes a').click(function(){
		var _thisContent = jQuery(this).parent().next();
		_thisContent.slideToggle(250);
	});

	jQuery('ul.worksBox li a').click(function(){
		var offset = jQuery(this).offset().top;
		offset += 115;
		//jQuery('#out').html(offset);
		var id = jQuery('img', this).attr('src').match(/(pc|mb|sp|app)([0-9]{2})/);
		if(worksDetailId == '') {
			jQuery('div#worksDetail').css({ top: offset } );
			if(jQuery.browser.msie){
				jQuery('div#worksDetail').show();
			}else{
				jQuery('div#worksDetail').fadeIn();
			}
			//jQuery('#out').html(id[0]);
		}else{
			jQuery('div#worksDetail').animate({ top: offset }, {queue: false, duration: 300, easing:'swing'} );
		}
		var type = id[1];
		var targetIndex;
		for( var i=0; i < data[type].length; i++) {
			if( data[type][i].id == id[1] + id[2]) {
				targetIndex = i;
			}
		}
		dataChange(type, targetIndex);

	});

	//データ取得

	var dataPath;
	if(location.href.indexOf('index_en') > 0 ){
		dataPath = 'common/data/data_en.js';
	}else{
		dataPath = 'common/data/data.js';
	}
	jQuery.getJSON( dataPath , function( msg ) {
		data = msg;
	});

	if (location.href.indexOf('#') >= 0){
			jQuery("p.loading").hide(); 
			jQuery('#sideBoxL').show();
			var targetOffset = jQuery(location.href.substring(location.href.indexOf('#'))).offset().top;
			targetOffset -= 30;
			jQuery('html, body').scrollTop(targetOffset);
			jQuery('#sideBoxR').show();
			scrollNavigation();
			initGoogleMap();
			initAnimationComplete = true;
	}else{
		//初期アニメーション
		jQuery('#sideBoxL').css({ top: -2000 } );
		jQuery('#sideBoxR').css({ top: 2000 } );
		jQuery('#sideBoxL').show();
		jQuery('#sideBoxR').show();

		setTimeout( function() {
			jQuery("p.loading").hide(); 
			initAnimationComplete = true;
			scrollNavigation()
			jQuery('#sideBoxR').animate({ top: 0 }, {queue: false, duration: 1000, easing:'swing', complete: function() { initGoogleMap();  }} );
		}, 100 );
	}

	// 2011.10.06　追加　一色
	var value1 = "";
	var val2index = 0;
	for(var i=0; i < selectValue1.length; i++) {
		if(lastQueryType1 == selectValue1[i] ) {
			value1 += "<option value='" + selectValue1[i] + "' selected='selected'>" + selectValue1[i] + "</option>";
			val2index = i;
		}else{
			value1 += "<option value='" + selectValue1[i] + "'>" + selectValue1[i] + "</option>";
		}
		//console.log(value1);
	}
	jQuery('#querytype1').html(value1);

	var value2 = "";
	for(var i=0; i < selectValue2[val2index].length; i++) {
		if(lastQueryType2 == selectValue2[val2index][i] ) {
			value2 += "<option value='" + selectValue2[val2index][i] + "' selected='selected'>" + selectValue2[val2index][i] + "</option>";
		}else{
			value2 += "<option value='" + selectValue2[val2index][i] + "'>" + selectValue2[val2index][i] + "</option>";
		}
	}
	jQuery('#querytype2').html(value2);

	jQuery("#querytype1").change(function(){
		for(var i=0; i < selectValue1.length; i++) {
			if(selectValue1[i] == jQuery("#querytype1").val() ) {
				var value2 = "";
				for(var j=0; j < selectValue2[i].length; j++) {
					value2 += "<option value='" + selectValue2[i][j] + "'>" + selectValue2[i][j] + "</option>";
				}
				//console.log(value2);
				jQuery('#querytype2').html(value2);
			}
		}
	});

});

