window.onload = init;

function init() {
	smartPhoneRedirect();
	initRollovers();
	border_none();
	popupWindow(800, 570);
	crossfade("pickuplist",1000,3000);
}

//-----------------------------------------------
//	西暦表示
//-----------------------------------------------
function setYear() {
	var date = new Date();
	document.write(date.getFullYear());
}

//-----------------------------------------------
//	ロールオーバー
//-----------------------------------------------
function initRollovers() {
	if (!document.getElementById) return;

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {	
		if (aImages[i].className == 'rollover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			hsrc = src.replace(ftype, '_over'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);

			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;

			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('_over'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

//-----------------------------------------------
//	クリック時のフォーカスを非表示に
//-----------------------------------------------
function border_none(){
	var all_link = document.links;
	for(i=0;i<all_link.length;i++){
		all_link[i].onfocus = function(){
			this.blur();
		}
	}
}

//-----------------------------------------------
//	ポップアップ
//-----------------------------------------------
function popupWindow(_width, _height) {
	var options = "width=" + _width + ",height=" + _height + ",scrollbars=no";
	var open = function(url) {
		var newWindow = window.open(url, "popup", options);
		//newWindow = window.open(url,"popup","width=800,height=570,scrollbars=no");
		newWindow.focus();
	}
	
	var links = document.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++) {
		if(links[i].className == "popup") {
			links[i].onclick = function() {
				open(this.getAttribute("href"));
				return false;
			}
		}
	}
}

//-----------------------------------------------
//	クロスフェード
//-----------------------------------------------

function crossfade(IDname , fadetime , speed){
	if(!document.getElementById(IDname)){
		return;
	}
	var news = document.getElementById(IDname);
	var newslinks = news.getElementsByTagName("li");
	for(var i=0; i<newslinks.length; i++){
		newslinks[i].id = [i];
		newlength = new Array([i]);
	}
	for(var j=0; j<newslinks.length; j++){
		newlength[j]=[j];
	}
	new Crossfader(newlength, fadetime, speed );
}

var useBSNns;

if (useBSNns){
	if (typeof(bsn) == "undefined")
		bsn = {}
	var _bsn = bsn;
}else{
	var _bsn = this;
}

_bsn.Crossfader = function (divs, fadetime, delay ){
	this.nAct = -1;
	this.aDivs = divs;
	
	for (var i=0;i<divs.length;i++){
		document.getElementById(divs[i]).style.opacity = 0;
		document.getElementById(divs[i]).style.position = "absolute";
		document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
		document.getElementById(divs[i]).style.visibility = "hidden";
	}
	
	this.nDur = fadetime;
	this.nDelay = delay;
	this._newfade();
}

_bsn.Crossfader.prototype._newfade = function(){
	if (this.nID1)
		clearInterval(this.nID1);
	
	this.nOldAct = this.nAct;
	this.nAct++;
	if (!this.aDivs[this.nAct])	this.nAct = 0;
	
	if (this.nAct == this.nOldAct)
		return false;
	
	document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";
	
	this.nInt = 50;
	this.nTime = 0;
	
	var p=this;
	this.nID2 = setInterval(function() { p._fade() }, this.nInt);
}

_bsn.Crossfader.prototype._fade = function(){
	this.nTime += this.nInt;
	
	var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
	var op = ieop / 100;
	document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
	document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";
	
	if (this.nOldAct > -1){
		document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
		document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}
	
	if (this.nTime == this.nDur){
		clearInterval( this.nID2 );
		
		if (this.nOldAct > -1)
			document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";
		
		var p=this;
		this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
	}
}

_bsn.Crossfader.prototype._easeInOut = function(t,b,c,d){
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}


function winClose(){
	if(!document.getElementById("close")){
		return false;
	}
	var btnClose = document.getElementById("close");
	btnClose.onclick = function(){
		window.close();
	}
}

function prepareLinks() {
    if(!document.getElementsByTagName){
    	return false;
    }
    var links = document.getElementsByTagName("a");
    for (var i=0; i<links.length; i++) {
        if(links[i].getAttribute("className") == "popup" || links[i].getAttribute("class") == "popup") {
            links[i].onclick = function() {
                popUp(this.getAttribute("href"));
                return false;
            }
        }
    }
}

function smartPhoneRedirect(){

	var career = new Array("iPhone",  			// Apple iPhone
							"iPod", 			// Apple iPod touch
							"incognito", 		// Other iPhone browser
							"webmate", 			// Other iPhone browser
							"Android", 			// 1.5+ Android
							"dream", 			// Pre 1.5 Android
							"CUPCAKE", 			// 1.5+ Android
							"blackberry9500",	// Storm
							"blackberry9530",	// Storm
							"blackberry9520",	// Storm v2
							"blackberry9550",	// Storm v2
							"blackberry 9800",	// Torch
							"webOS",			// Palm Pre Experimental
							"s8000", 			// Samsung Dolphin browser
							"bada",				// Samsung Dolphin browser
							"Googlebot-Mobile"	// the Google mobile crawler
						);

	var user = navigator.userAgent;
	
	for(i=0;i<career.length;i++){
		
		if(user.indexOf(career[i]) != -1){
			
			var btn = document.createElement("div");
			btn.style.fontFamily = 'Meiryo,"ヒラギノ角ゴ Pro W3","Hiragino Kaku Gothic Pro W3","ＭＳ Ｐゴシック",sans-serif';
			btn.style.fontSize = "36px";
			btn.style.cursor = "pointer";
			btn.style.textAlign = "center";
			btn.style.margin = "0";
			btn.style.padding = "0";
			btn.style.fontWeight = "bold";
			btn.style.width = "100%";
			btn.style.height = "100px";
			btn.style.lineHeight = "100px";
			btn.style.color = "#ffffff";
			btn.style.whiteSpace = "nowrap";
			btn.style.textShadow = "0px -1px 1px #000000";
			btn.style.border = "solid 1px #000000";
			btn.style.background = "-webkit-gradient(linear, left top, left bottom, from(#ffbcdf), color-stop(50%,#fc88c5), color-stop(50%,#fd64b5), to(#f6389a))";
			
			btn.innerHTML = 'スマートフォン版はコチラ';
			
			btn.onclick = function(){
				
				document.cookie = "mode=sp;path=/;";
				
				host_name = location.host;
				now_url = location.href;
				urlArray = now_url.split(host_name);
				spURL = urlArray.join(host_name+"/sp");
				spURL = spURL.replace("link/","");

				location.href = spURL;
			}
			document.body.style.backgroundPosition = "0 102px";
			document.body.insertBefore(btn,document.body.firstChild);
			
			break;
		}
	}
	return;

}
