
/*メインのjs
---------------------------------------------------------------------------------------------------- */
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
if (isiPad || isiPhone) {
	window.onload = function(){setTimeout(ipLoad,100);};
	
	var sc;
	if (isiPad) sc = 0.7;
	else sc = 0.3;
	document.write('<meta name="viewport" content="width=device-width; initial-scale='+sc+'; maximum-scale=1.0; user-scalable=1;"/>');

}

function ipLoad(){
	loadConts('news');
	loadConts('concept');
	loadConts('coordinate');
	loadConts('shoplist');
	loadConts('links');
	loadConts('regular_sales_agent');
	loadConts('privacy_policy');
	loadConts('about_this_site');
	loadConts('caution');
}
function ipLoadConts(conts){
	var sprite = $('#'+conts+'-inner');
	if(String(sprite.html()).length > 10) return;
	var url = './inc/'+conts+'.html';
	sprite.append('<img src="./share/images/ajax-loader.gif">');
	$.ajax({
		url: url,
		type: "GET",
		success: function(html) {
			if(sprite.html().length > 10) {
				sprite.empty();
				sprite.append(html);
				isReady = true;
				initLoad();
			}
		}
	});
}

//
var nowConts = '';
var isReady = false;
var newsId = 23;

//
$(function() {
	
	//固定要素
	initLoad();
	
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE,
								function(e){
									showConts(e.path);
								});
	
	if (window.addEventListener) {
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
	}
	/** IE/Opera. */
	window.onmousewheel = document.onmousewheel = wheel;
	
	//初期に読んでおくコンテンツのロード
	loadConts('home');
	
});

function wheel(e){
	$('html, body').stop();
	checkscroll($( document ).scrollTop());
}
function checkscroll(posy){
	
	//$('#debug').empty();
	//getURL("javascript:void(_gaq.push(['_trackPageview','/concept/']));");
	
	if(posy < $('#home').offset().top+$('#home').height() + 800) {
		//$('#debug').append('home');
		SWFAddress.setValue('home');
	} else if(posy < $('#news').offset().top+$('#news').height() + 500) {
		//$('#debug').append('news');
		SWFAddress.setValue('news');

				
		
	} else if(posy < $('#concept').offset().top+$('#concept').height() + 500) {
		//$('#debug').append('concept');
		SWFAddress.setValue('concept');

		
	} else if(posy < $('#coordinate').offset().top+$('#coordinate').height() + 500) {
		//$('#debug').append('coordinate');
		SWFAddress.setValue('coordinate');

		
	} else if(posy < $('#shoplist').offset().top+$('#shoplist').height() + 500) {
		//$('#debug').append('shoplist');
		SWFAddress.setValue('shoplist');

		
	} else if(posy < $('#links').offset().top+$('#links').height() + 500) {
		//$('#debug').append('links');
		SWFAddress.setValue('links');

		
	} else if(posy < $('#regular_sales_agent').offset().top+$('#regular_sales_agent').height() + 500) {
		//$('#debug').append('regular_sales_agent');
		SWFAddress.setValue('regular_sales_agent');

		
	} else if(posy < $('#privacy_policy').offset().top+$('#privacy_policy').height() + 500) {
		//$('#debug').append('privacy_policy');
		SWFAddress.setValue('privacy_policy');

		
	} else if(posy < $('#about_this_site').offset().top+$('#about_this_site').height() + 500) {
		//$('#debug').append('about_this_site');
		SWFAddress.setValue('about_this_site');

			
	} else if(posy < $('#caution').offset().top+$('#caution').height() + 500) {
		//$('#debug').append('caution');
		SWFAddress.setValue('caution');

		
	}
}

function loadConts(conts){
	//if(isReady) SWFAddress.setValue(conts+'/');//2,3回実行してしまう・・・
	
	var sprite = $('#'+conts+'-inner');
	if(String(sprite.html()).length > 10) return;
	
	var url = './inc/'+conts+'.html';
	
	sprite.append('<img src="./share/images/ajax-loader.gif">');
	$.ajax({
		url: url,
		type: "GET",
		success: function(html) {
			if(sprite.html().length > 10) {
				sprite.empty();
				sprite.append(html);
				isReady = true;
				initLoad();
				actBtn();
				deepLink();
			}
		}
	});
}

function showConts(conts){
	
	var dir = conts.split('/');
	if(!dir[1]) return;
	
	if(conts != nowConts) {
		nowConts = conts;
		
		//
		SWFAddress.setValue(conts);
		
		//
		actBtn();
		//
		if(String($('#'+dir[1]+'-inner').html()).length < 10) loadConts(dir[1]);
		if(dir.length > 2) scrollConts(dir[1], true);
		_gaq.push(['_trackPageview', '/'+dir[1]+'/']);
		
		
		/*console.log(dir[1]);*/
		
	} else {
		if(dir.length > 2) scrollConts(dir[1], false);
	}
}
function scrollConts(conts, bo){
	
	//
	var targetOffset = 0;
	if($('#'+conts).offset()) targetOffset = parseInt($('#'+conts).offset().top);
	
	//trace(targetOffset);
	if(bo) $('html,body').stop().animate({scrollTop:targetOffset},'slow', "easeInOutExpo", deepLink);
	else $('html,body').stop().animate({scrollTop:targetOffset},'slow', "easeInOutExpo");
	
	
	//↓何故かmacのchromeでリモートのみ動かない・・・
	//if(bo) $('html,body').stop().animate({scrollTop:targetOffset,scrollLeft:0},'fast',deepLink);
	//else $('html,body').stop().animate({scrollTop:targetOffset,scrollLeft:0},'fast');
}

function deepLink(){
	var deeplinkId = -1;
	
	var dir = nowConts.split('/');
	if(dir[2]) deeplinkId = dir[2];
	else deeplinkId = -1;
	
	var conts = dir[1];
	if(deeplinkId == -1 && conts == 'news') deeplinkId = newsId;
	if(deeplinkId == -1) return;
	
	var sprite;
	if(conts == 'news') {
		sprite = $('#news-inner');
		if(String(sprite.html()).length > 10) {
			opNews(deeplinkId);
		}
	}
}
var newsPalel;
var nowPabel;
function opNews(i){
	if(nowPabel) nowPabel.hide();
	nowPabel = newsPalel = $("#newsPalel"+i);
	newsPalel.fadeIn();
};

function initLoad(){
	
	//ページリンク
	$('.pagelink').each(function(i) {
		$(this).unbind("click");
		$(this).bind("click", function(e){
			var conts = $(this).attr('href');
			conts.substring(conts.indexOf('#')+1);
			var target = '/'+conts.substring(conts.indexOf('#')+1)+'/';
			showConts(target);
			return false;
		});
	});
	
	//縞々
	$('.stripe tr').mouseover(function(){
		$(this).addClass("over");
	});
	$('.stripe tr').mouseout(function(){
		$(this).removeClass("over");
	});
	$('.stripe tr:even').addClass('alt');
	$('.stripe li').mouseover(function(){
		$(this).addClass("over");
	});
	$('.stripe li').mouseout(function(){
		$(this).removeClass("over");
	});
	$('.stripe li:even').addClass('alt');
	
	//ロールオーバー
	var image_cache = new Object();
	$('.swap').each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
		$(this).unbind("mouseover");
		$(this).bind("mouseover", function(e){this.src = imgsrc_on});
		$(this).unbind("mouseout");
		$(this).bind("mouseout", function(e){this.src = imgsrc});
	});
	
	$('.swapdef').each(function(i) {
		$(this).attr('imgsrc', this.src.replace("_on.", "_def."));
		$(this).attr('imgsrc_def', $(this).attr('imgsrc'));
		$(this).attr('imgsrc_on', this.src.replace("_def.", "_on."));
		$(this).unbind("mouseover");
		$(this).bind("mouseover", function(e){
			this.src = $(this).attr('imgsrc_on');
		});
		$(this).unbind("mouseout");
		$(this).bind("mouseout", function(e){
			this.src = $(this).attr('imgsrc');
		});
		//
		image_cache[this.src] = new Image();
		image_cache[this.src].src = $(this).attr('imgsrc_on');
	});
		
	//
	initIE();
}
function initIE(){
	//ieのみ実行
	//if(navigator.userAgent.indexOf("MSIE") != -1){
	if(navigator.userAgent.indexOf("MSIE 6.0")>0) {
		
		//固定
		$('#bg').exFixed();
		$('#bg-wrap').exFixed();
		$('#header-wrap').exFixed();
		
		//透過
		DD_belatedPNG.fix('.ddpng');
		
		var oldFixPng = DD_belatedPNG.fixPng;
	   	DD_belatedPNG.fixPng = function (el) {
	    	oldFixPng(el);
	    	if (el.vml && el.vml.image.fill.getAttribute("src").match(/_def?./)) {
	    	    el.vml.image.shape.attachEvent('onmouseenter', function() {
	    	        var image = el.vml.image.fill;
	    	        image.setAttribute("src", image.getAttribute("src").replace("_def.", "_on."));
	    	    });
	    	    el.vml.image.shape.attachEvent('onmouseleave', function() {
	    	        var image = el.vml.image.fill;
	    	        image.setAttribute("src", image.getAttribute("src").replace("_on.", "_def."));
	    	    });
			}
		};
		DD_belatedPNG.fix('.ddpng');
	}		
}

/*
*/
function actBtn(){
	if (isiPad || isiPhone) return;
	
	var dir = nowConts.split('/');
	var conts = dir[1];
	
	var href;
	var img;
	$("#header_prop li").each(function(i) {
		href = $(this).find('a').attr('href');
		if(href && href.indexOf('#') != -1) {
			href = href.substring(1);
			img = $(this).find('a img');
			
			if(conts == href) {
				img.attr('imgsrc', img.attr('imgsrc_on'));
				img.attr('src', img.attr('imgsrc_on'));
			} else {
				img.attr('imgsrc', img.attr('imgsrc_def'));
				img.attr('src', img.attr('imgsrc'));
			}
		}
	});
}




/* 以下共通
---------------------------------------------------------------------------------------------------- */

/* opwin
---------------------------------------------------------------------------------------------------- */
function opwin(url, width, height, trg){
	if (!!window && url) {
		if (!trg) trg = "_blank";
		options = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,favorites=no";
		var whop = "width="+width+",height="+height+","+options;
		win = window.open(url, trg, whop);
		win.focus();
	}
}

function opwin2(url, width, height, trg){
	if (!!window && url) {
		if (!trg) trg = "_blank";
		options = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,favorites=no";
		var whop = "width="+width+",height="+height+","+options;
		win = window.open(url, trg, whop);
		win.focus();
	}
}

function opwin_c(url, trg, w, h, scroll) {
	var win_width = (screen.width - w) / 2;
	var win_height = (screen.height - h) / 2;
	win_detail = 'height='+h+',width='+w+',top='+win_height+',left='+win_width+',scrollbars='+scroll;
	win = window.open(url, trg, win_detail)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//<a onClick="return refreshLink(this)" href="http://www.google.com" target="_blank">
function refreshLink(url) {
	var url = el.href;
	w = window.open();
	w.document.write('<meta http-equiv="refresh" content="0;url='+url+'">');
	w.document.close();
	return false;
}

/* rollover - fade
---------------------------------------------------------------------------------------------------- */
$(function(){
	$('.fade').hover(
		function() { $(this).fadeTo(100, 0.5); $(this).fadeTo(300, 1.0); },
		function() { $(this).fadeTo(100, 0.5); $(this).fadeTo(300, 1.0); }
	);
});

/* rollover - swapfade
---------------------------------------------------------------------------------------------------- */
$(function() {
  var image_cache = new Object();
  $('.swapfade').each(function(i) {
    var imgsrc = this.src;
    var dot = this.src.lastIndexOf('.');
    var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
    image_cache[this.src] = new Image();
    image_cache[this.src].src = imgsrc_on;
    
    var name = "swapfade"+i;
	$(this).parent().append('<img src="'+imgsrc_on+'" class="'+name+'">');
    $(this).css("position", "absolute");
    $("."+name).css("position", "absolute");
    $("."+name).fadeTo(0, 0.0);
    $("."+name).hover(
      function() { $("."+name).fadeTo(320, 1.0);$("."+name).fadeTo(20, 0.2);$("."+name).fadeTo(100, 1.0); },
      function() { $("."+name).fadeTo(320, 0.0); });
    
  });
});

/* flash - liquid
---------------------------------------------------------------------------------------------------- */

function chageHeight(h)
{
	$('#index').height(h);
}


