// 自サイトのホスト名
var joined = false;
var hosts = new Array();
hosts['ov.yahoo.co.jp'] = true;
hosts['listing.yahoo.co.jp'] = true;
hosts['www.overture.co.jp'] = true;
hosts['blog.ov.yahoo.co.jp'] = true;
hosts['signup.im.yahoo.co.jp'] = true;
hosts['form.ov.yahoo.co.jp'] = true;
hosts['ov-yahoo-co-jp.ads.overture.co.jp'] = true;
hosts['listing-stage.ads.overture.co.jp'] = true;
hosts['overture.loftwork.info'] = true;

// Flash のメジャーバージョンが必要です
var requiredMajorVersion = 8;
// Flash のマイナーバージョンが必要です
var requiredMinorVersion = 0;
// 必要な Flash のバージョン
var requiredRevision = 24;

var chMenu=0;
var chParent=0;

function Init(){
	initRollovers();
	joinPromoCode();
}

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);
			var hsrc = src.replace(ftype, '_ov'+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('_ov'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

function subopen(tName,parent) {
	
	tMenu=(document.all)?document.all(tName).style:document.getElementById(tName).style;
	tParent=(parent)?parent:0;

	if(chMenu){
		chMenu.display="none";
		if(chParent) chParent.className = "SubClose";
	}
	if(chMenu==tMenu) {	
		chMenu=0;
		chParent=0;
	} else {
		chMenu=tMenu;
		chParent=tParent;
		tMenu.display="block";
		if(tParent) tParent.className = "SubOpen";
	}
}

function joinPromoCode() {
	if (location.search) {
		var search = location.search;
		if (search.match(/^\?(.*)$/)) search = RegExp.$1;
		
		var elements = document.getElementsByTagName('a');
		setPromoCodes(elements, search, 'href');
		var elements = document.getElementsByTagName('area');
		setPromoCodes(elements, search, 'href');
		var elements = document.getElementsByTagName('embed');
		setPromoCodes(elements, search, 'src');
	}
	joined = true;
}

function setPromoCodes(elements, search, target) {
	for (i = 0; i < elements.length; i++) {
		var element = elements[i];
		var url = element.getAttribute(target);
		if (url == null || url.length == 0) continue;
		if (url.match(/^#/)) continue;
		
		var protocol = null;
		if (url.match(/^(.*):(.*)$/)) {
			protocol = RegExp.$1;
			if (protocol != 'http' && protocol != 'https') continue;
			url = RegExp.$2;
		}
		
		var host = null;
		if (url.match(/^\/\/([^\/#\?]*)(.*)$/)) {
			host = RegExp.$1;
			if (!(host in hosts)) continue;
			url = RegExp.$2;
		}
		
		var fragment = null;
		if (url.match(/^([^#]*)#(.*)$/)) {
			url = RegExp.$1;
			fragment = RegExp.$2;
		}
		
		var path = null;
		var query = null;
		var code = null;
		if (url.match(/^([^\?]*)\?(.*)$/)) {
			path = RegExp.$1;
			query = RegExp.$2;
			code = parsePromoCode(query);
		} else {
			path = url;
		}
		
		url = '';
		if (protocol != null) url += protocol + ':';
		if (host != null) url += '//' + host;
		if (path != null && path.length > 0) url += path;
		if (code == null) {
			if (query != null && query.length > 0) {
				url += '?' + query + '&' + search;
			} else {
				url += '?' + search;
			}
		} else {
			url += '?' + query;
		}
		if (fragment != null) url += '#' + fragment;
		element.setAttribute(target, url);
	}
}

function getPromoCode() {
	return parsePromoCode(location.search);
}

function parsePromoCode(search) {
	if (('?' + search + '?').match(/\Wo=(\w*)\W/)) return RegExp.$1;
	return null;
}

function clicked() {
	if (!joined) return false;
	return true;
}

function sponsoredSearchForm(assist, mobile) {
	var code = getPromoCode();
	if (code == null) {
		code = mobile? 'JPMOBI' : 'JPHOME';
	}
	
	var url = 
'https://signup.marketingsolutions.overture.com/signupui/signup/';
	url += (assist? 'assistedSetup.do' : 'loadSignup.do');
	url += '?promoCode=' + code + (assist? 'B' : 'A');
	url += '&offer=' + (assist? 'ft' : 'ss');
	url += '&l=ja_JP';
	url += '&m=4';
	if (mobile) url += '&mobile=true';
	location.href = url;
	return false;
}

function ShowFlash(file,width,height){
	var code = getPromoCode();
	if (code == null) code = 'JPHOME';
	
	file = file.replace('.swf','');
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		alert("このページでは \"AC_RunActiveContent.js\" が必要です。");
	} else {
		var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		if(hasRightVersion) {  // 使用可能なバージョンが検出された場合
			// Flash ムービーの埋め込み
			AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0',
				'width', width,
				'height', height,
				'src', 'top',
				'quality', 'high',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'true',
				'scale', 'showall',
				'wmode', 'window',
				'devicefont', 'false',
				'id', 'top',
				'bgcolor', '#ffffff',
				'name', 'top',
				'menu', 'true',
				'allowScriptAccess','sameDomain',
				'allowFullScreen','false',
				'movie', file,
				'FlashVars', 'o=' + code,
				'salign', ''
				); //end AC code
		} else {  // Flash が古すぎるか、プラグインを検出できません
			var alternateContent = '代替 HTML コンテンツをここに配置する必要があります。 '
				+ 'このコンテンツには Adobe Flash Player が必要です。 '
	            + '<a href=http://www.macromedia.com/go/getflash/>Flash Player を入手する</a>';
			document.write("<h1><img src='images/toppage/mainimg_toppage.gif' width='800' height='231' alt='検索連動型広告シェアNO.1 オーバーチュアは、サーチのチカラでビジネスを拡げます' /></h1>");  // Flash 以外のコンテンツを挿入する
		}
	}
}

window.onload = Init;
window.document.onclick = clicked;


function RandomImg(data) {
	data = data.substr(1,data.length-2);

	var imgs = data.split(",");
	var n = Math.floor(Math.random() * imgs.length);
	filetype = imgs[n].substr(imgs[n].length-3,3);
	a = new Array();
	a = imgs[n].split("|");

	if (filetype != "swf") {
		document.write('<a href="' + a[1] + '"><img src="' + a[2] + '" width="618" height="225" alt="' + a[0] + '" /></a>');
	} else {
		ShowFlash(a[2], a[0], a[1]);
	}
}