function gSetting() {
	$('#gnav ul li dl, #gnav ul li ol').parent().addClass("child");

	// location
	var path = location.pathname;
	var domain = document.domain;
	var url = location.href;
	var rel = getRelativeRoot();

	$('#gnav li a').each( function() {
		var link = $(this).attr('href');
		var root = ('http://' || 'https://') + domain + '/';
		var rootf = ('http://' || 'https://') + domain + '/index.html';

		var isIE6 = link.substr(0, 7) == 'http://';

		if (!isIE6 && path.match('/creation/destination/')) {
			$("#gnav li a[href*='/creation/destination/']").parent('dd').addClass('current');
			$("#gnav li a[href*='/creation/destination/']").parents('dl, li').addClass('active');
		}

		if (isIE6) {
			link = ie6WorkAround(link);
		}

		if (path.match('creation/destination/') && link.match('creation/destination/')) {
			$(this).parent('dd, li').not('.child').addClass('current');
			$(this).parents('dl, ol, li').addClass('active');
		}

		if (!((url == root) || (url == rootf))) {
			if (path.indexOf(link) != -1) {
				$(this).parent('dd, li').not('.child').addClass('current');
				$(this).parents('dl, ol, li').addClass('active');
			}
		}

		$(this).attr('href', rel + link);
	});

	$('#gnav .current img').each(function() { this.src = this.src.replace('.gif', '_ov.gif'); });

	$('#gnav .active p img').not('#gnav .active.child p img').each(function() { this.src = this.src.replace('.gif', '_ov.gif'); });

	$("#gnav img").each( function() {
		var index = this.src.indexOf('common/');
		this.src = rel + this.src.substring(index);
	});

	// rollover
	var aPreLoad = new Object();
	$("#gnav a img, #footer #footerInner a img").not("[src*='_ov.']").each(function(i) {
		var tsrc = this.src;
		var ftype = this.src.lastIndexOf('.');
		var hsrc = this.src.substr(0, ftype) + '_ov' + this.src.substr(ftype, 4);
		aPreLoad[this.src] = new Image();
		aPreLoad[this.src].src = hsrc;
		$(this).hover( function() { this.src = hsrc; }, function() { this.src = tsrc; });
	});

}

$(function(){
	if (isCompactMode()) {
		setUpForCompactMode();
		return;
	}
	// load
	var rel = getRelativeRoot();
  
	$("#gnav").load(rel += "common/inc/webponav.txt", function() { gSetting(); });

	// rollover
	var aPreLoad = new Object();
	$("#footer ul a img, img.imgover, .btn input, .btn a img, .totop a img, #localNav a img")
	.not("[src*='_crt.'], [src*='_ov.'], .current img").each(function(i) {
		var tsrc = this.src;
		var ftype = this.src.lastIndexOf('.');
		var hsrc = this.src.substr(0, ftype) + '_ov' + this.src.substr(ftype, 4);
		aPreLoad[this.src] = new Image();
		aPreLoad[this.src].src = hsrc;
		$(this).hover( function() { this.src = hsrc; }, function() { this.src = tsrc; });
	});

});

function getScriptPath() {
  var elems=document.getElementsByTagName("script");
  var src=elems[elems.length-1].src;
  return src.slice(0, src.lastIndexOf("/") + 1);
}

function getRelativeRoot() {  
  /** For local debug **/
  if(location.pathname.indexOf("/C:/",0)==0) {
    var root = getScriptPath();
    return root.slice(0,root.lastIndexOf("common/js/"));
  }

  var depth = location.pathname.split("/").length;
  var rel = "";
  for (var i = 2; i < depth - 1; i++) {
    rel += "../";
  }
  return rel;
}

function getQueryString() {
	var index = location.href.indexOf('?');
	return location.href.substring(index + 1);
}

function isCompactMode() {
	var index = getQueryString().indexOf('compact');
	return (index != -1);
}

function setUpForCompactMode() {
	var hash = location.hash;
	var sc = "";
	if (hash) {
		sc = " onload=\"location.hash='';location.hash='" + hash + "';\" ";
	}
	$('head').append('<link rel="stylesheet" type="text/css" ' + sc + ' href="' + getRelativeRoot() + 'common/css/compact.css" media="screen,print">');
	$('body').attr('id', 'pagetop');
	$('#wrap').after('<div id="close"><a href="javascript:window.close();">閉じる</a></div>');
	$('a').each(function() {
		var href = $(this).attr('href');
		if (href && href.indexOf('?') == -1 && href.charAt(0) != '#' && href.substring(0, 11) != 'javascript:') {
			$(this).attr('href', href + '?compact');
		}
	});
}

function ie6WorkAround(href) {
	var index = href.indexOf("/html/") + 6;
	var path = href.substring(index);
	var dirs = path.split("/");
	var realPath = new Array();
	for (var i = 0; i < dirs.length; i++) {
		if (i < location.pathname.split("/").length - 3) {
			continue;
		}
		realPath.push(dirs[i]);
	}
	return realPath.join("/");
}
