/*-- common.js --*/

// ### google analytics ###

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26976966-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

// ### jQuery functions ###

function linkActions() {
	$("a[rel='external']").live("click",function(){
		window.open($(this).attr("href"));
		return false;
	});

	$("a[href^='#'], area[href^='#']").click(function(){
		var tgt = $(this).attr("href").substr(1);
		var pos = new Object();
		pos = $("#"+tgt+"").position();
		if(!$(pos).size()) {
			pos = {"top":0};
		}
		
		$($.browser.safari ? "body" : "html").animate({
			"scrollTop": pos.top
		},1000,"easeOutExpo");

		return false;
	});

	$("a[hreflang]").click(function(){
		if($(this).attr("hreflang") == "ja-jp") {
			location.href = location.pathname.substr(6);
		}else{
			if($(this).hasClass("foreign")) {
				location.href = "/"+$(this).attr("hreflang")+location.pathname.substr(6);
			}else{
				location.href = "/"+$(this).attr("hreflang")+location.pathname;
			}
		}
		return false;
	});
}

function menuDisplay() {
	var p = $("body").attr("id");
	
	$("#mainNavi li").each(function(){
		if($(this).hasClass(p)) {
			$(this).addClass("selected");
		}else{
			$(this).hover(
				function(){
					$(this).stop().animate({"width":"157"},750,"easeOutBack");
				},
				function(){
					$(this).stop().animate({"width":"120"},400,"easeOutBack");
				}
			);
		}
	});

	$("footer nav li").each(function(){
		if($(this).hasClass(p)) {
			$(this).addClass("selected");
		}
	});
}

function imgCanvas(){
	var obj = new Array();
	var path = 'http://'+location.hostname+'/common/canvas/list.xml';
	var inner = $("#background");
	var delay = '8000';

	imgTurn = function(){
		var i=0;
		foword = function(){
			if(i<obj.length){
				$(inner).animate({"opacity":0},2000,function(){
					$(inner).css({"background-image":"url('"+obj[i].path+"')"}).delay(1000).animate({"opacity":1},2000,function(){
						i++;
						setTimeout(foword,delay);
					});
				});
			}else{
				i=0;
				setTimeout(foword,100);
			}
		}
		foword();
	},

	dataLoad = function(XML){
		var i = new Number(0);
		$(XML).find("Img").each(function(){
			obj[i]  = {"path":$(this).find("Path").text(),"ID":$(this).find("ID").text()};
			i++;
		});

		if(i>0){
			imgTurn();
		}
	}
	
	$.ajax({
		'url': path,
		'type': 'get',
		'dataType': 'xml',
		'data': {},
		'cache': false,
		'success': dataLoad
	});
}

function lightedSign() {
	$(".opening li").each(function(){
		var startNode = $(this).children("time").get(0);
		var start = Date.parse($(startNode).attr("datetime")); //is not Object
		var endNode = $(this).children("time").get(1);
		var end = Date.parse($(endNode).attr("datetime")); //is not Object
		var now = new Date(); //is Object

		if(now > start && end > now) {
			if(now.getMonth() ==11 && now.getDate().toString().match(/(23|24|25)/)) {
				if(Date.parse(now) > now.setHours(16,30,00) && now.setHours(24,00,00) > Date.parse(now)) {
					$(this).addClass("nowOn");
				}
			}else{
				if(Date.parse(now) > now.setHours(16,30,00) && now.setHours(22,00,00) > Date.parse(now)) {
					$(this).addClass("nowOn");
				}
			}
		}
	});
}

function lazyLoad() {
	$("img").lazyload({ 
		effect : "fadeIn"
	});
}

$(function(){
	$.getScript("/common/js/jquery.easing.1.3.js");
	//$.getScript("/common/js/jquery.lazyload.js");
	$.getScript("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
	$.getScript("http://platform.twitter.com/widgets.js");
	$.getScript("/common/js/W3CDTF.js");

	linkActions(),menuDisplay(),imgCanvas(),lightedSign();
});
