var origRatio;

function init() {
	var img = document.getElementById("coverPhoto");
	var origWidth = img.width;
	var origHeight = img.height;
	origRatio = origHeight/origWidth;

	var w = window.innerWidth;

	/*
	xxx safariのみ15px大きく初期化 
	if (w < origWidth && isSafari()) {
		fitInner(w + 15);
	} else {
		fit();
	}
	*/
	fit();
}

function fit() {
	var img = document.getElementById("coverPhoto");
	var w = getWidth();
	fitInner(w);
	document.body.style.overflowX="hidden";
}

function fitInner(width) {
	var img = document.getElementById("coverPhoto");
	img.width = width;
	img.height = width * origRatio;
}

function isSafari() {
	var userAgent = navigator.userAgent;
	return (userAgent.indexOf("Safari")!=-1);
}

function getWidth ( ) {  
    if ( window.innerWidth ) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
	}  else if ( document.body ) {
		return document.body.clientWidth;
	}  
    return 0;  
}

function getHeight ( ) {  
    if ( window.innerHeight ) {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight != 0) {
		return document.documentElement.clientHeight;
	}  else if ( document.body ) {
		return document.body.clientHeight;
	}  
    return 0;  
}



// 左から何ピクセルの位置で一時停止するか
var stopLineLeftMargin = 210;

// 一時停止する時間（ミリ秒）
var stopTimespan = 4000;

// 速度（pix/秒）
var velocity = 600;

// アニメート対象のボックス
var targetElements = getNewsBanners();

var tid = null;


var Floater = function(id) {
	this.id = id;
	this.tid = null;
};
Floater.prototype = {

	invoke : function() {
		// 欄外へ
		this._init();
		
		// 移動距離を計算
		var amount = this._getAmount();

		// 移動実行
		this._moveBy(amount);
	},

	_init : function() {
		var e = document.getElementById(this.id);
		this.minWidth = e.offsetWidth;
		this.curWidth = Math.min(e.offsetWidth, e.parentNode.offsetWidth);
		e.style.left = this._startPosition() + "px";
		e.style.visibility = "visible";
		e.style.zIndex += 100;
	},

    _startPosition : function() {
		return this.curWidth;
    },

    _getAmount : function() {
		var e = document.getElementById(this.id);
		var left = e.style.left;
		if (left.indexOf("px") == left.length - 2) {
			left = left.substring(0, left.length - 2);
		}
		return stopLineLeftMargin - left;
    },

	_moveBy : function(amount) {
		var attrs = {
			points: { by: [amount, 0] }
		};
		var motion = new YAHOO.util.Motion(this.id, attrs);
		motion.duration = Math.abs(amount / velocity);
		motion.onComplete.subscribe(this._callback);
		motion.animate();
	},

	_callback : function() {
		var el = this.getEl();
		var amount = -1 * (stopLineLeftMargin + 260);
		var attrs = {
			points: { by: [amount, 0] }
		};
		var motion = new YAHOO.util.Motion(el.id, attrs);
		motion.duration = Math.abs(amount / velocity);
		tid = window.setTimeout(
			function() {
				motion.animate();
				el.style.zIndex -= 100;
				var nextFloater = selectFloater(el.id);
				nextFloater.invoke();
				window.clearTimeout(tid);
				tid = null;
			},
			stopTimespan
		);
	}

};

function selectFloater(currentId) {
	var length = targetElements.length;
	var rnd = Math.floor(Math.random() * length);
	var nextId = targetElements[rnd];
	if (nextId != currentId) {
		return new Floater(nextId);
	} else {
		return selectFloater(currentId);
	}
}

function getNewsBanners() {
  var targetElements = new Array();
  for (var i=1; i<=5; i++) {
    var name = "newsBanner" + i;
    if (document.getElementById(name) != null) {
      targetElements.push(name);
    }
  }
  return targetElements;
}

// キック
var firstFloater = selectFloater(null);
var firstTid = window.setTimeout(
	function() {
		firstFloater.invoke();
		window.clearTimeout(firstTid);
		firstTid = null;
	},
	1000
);


var userAgent = navigator.userAgent;

function isFirefox() {
	return (userAgent.indexOf("Firefox/2")!=-1);
}

function isMac() {
	return (userAgent.indexOf("Mac")!=-1);
}

var fontSize;
if(isFirefox() && isMac()){
	fontSize="1.05em";
}else{
	fontSize=".85em";
}

document.write ("<style type=\"text\/css\">");
document.write ("body{ font-size:" +  fontSize + "}");
document.write ("#container table th, #container table td{ font-size:" +  fontSize + "}");
document.write ("<\/style>");
