/**
	common
	tab
	popup
	event
	init
*/
////////////////////////////////////////////////////////////////////////////////
// common
////////////////////////////////////////////////////////////////////////////////
// ウィンドウ名
window.name = "NISSAN_CM_GALLERY_TOP";
// ブラウザチェック
// require JS/swfaddress.js
var isWindows = navigator.userAgent.toLowerCase().indexOf('win') > 0;
var isMozilla = com.asual.util.Browser.isMozilla();
var isIE = com.asual.util.Browser.isIE();
var isIE7 = navigator.userAgent.toLowerCase().indexOf('msie 7') > -1;
var isSafari = com.asual.util.Browser.isSafari();
var isSupported = com.asual.util.Browser.isSupported();

function swapimage(nm,img) {
	document.images[nm].src = img;
}
function setFrame(){
	document.getElementById("content_iframe").height = content_iframe.document.getElementsByTagName("div")[0].offsetHeight;
}
////////////////////////////////////////////////////////////////////////////////
// tab
////////////////////////////////////////////////////////////////////////////////
function tab(id){
	this.IDList = new Array();
	this.IDList.push(["carName", "ranking"]);
	this.IDList.push(["related", "detail"]);
	this.frameList = new Array();
	this.frameList.push("content_iframe");
	this.frameList.push("popup_iframe");
	this.ID = id;
	this.currentID = 0;
	this.imageDIR = "IMAGES";
}
tab.prototype = {	
	setCurrent: function(){
		var img = document.getElementById("btn_" + this.IDList[this.ID][this.currentID]);
		this.swap(this.currentID, "over", img);
	},	
	changeTab: function(id,element){
		var img = document.getElementById("btn_" + this.IDList[this.ID][this.currentID]);
		this.swap(this.currentID, "out", img);
		var img = document.getElementById("btn_" + this.IDList[this.ID][id]);
		this.swap(id, "over", img);
		this.currentID = id;
		
		if(id == 0){
			document.getElementById("TAB_POPUP_LINK0").style.display = "inline";
		}
		if(this.ID == 0){
			document.getElementById(this.frameList[this.ID]).src = element.href;
		}else if(this.ID == 1){
			if(id == 0){
				var src = "/NEWS/CM/POPUP/related_" + POPUP.ID + ".html";
			}else if(id == 1){
				var src = "/NEWS/CM/POPUP/detail_" + POPUP.ID + ".html";
			}
			if(isIE){
				var obj = this.getMovie("externalLoader");
				obj.loadFile(src);
			}else if(isMozilla){
				document.getElementById("popup_iframe").contentDocument.location.replace(src);
			}else if(isSafari){
				window.frames["popup_iframe"].location.replace(src);
			}else{
				document.getElementById("popup_iframe").src = src;
			}
		}
	},
	setHTML:function(html){
		var start = '<body';
		var startDetail = '<body id="detail">';
		var startRelated = '<body id="related">';
		var start = (html.indexOf(startDetail) > -1)? html.indexOf(startDetail) + startDetail.length: (html.indexOf(startRelated) > -1)? html.indexOf(startRelated) + startRelated.length: 0;
		var end = html.indexOf('</body>');
		var html = html.substring(start, end);
		
		// コンテンツが空の場合、詳細表示に切替
		if(html == "" || html == "\n" || html.lenggth < 1){
			this.setTabMenu_Detail();
		}else{
			window.popup_iframe.document.getElementsByTagName("body")[0].setAttribute("id",(this.currentID == 0)? "related":"detail");
			window.popup_iframe.document.getElementsByTagName("body")[0].innerHTML = html;
			window.popup_iframe.scroll(0, 0);
			window.popup_iframe.init();
		}
	},
	getMovie: function(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName]
		} else {
			return document[movieName]
		}
	},
	over: function(id){
		if(this.currentID != id){
			this.swap(id, "over");
		}
	},
	out: function(id){
		if(this.currentID != id){
			this.swap(id, "out");
		}
	},	
	swap: function(id, status){
		var btnName = "";
		if(status == "out"){
			btnName = "btn_" + this.IDList[this.ID][id] + ".gif";
		}else if(status == "over"){
			btnName = "btn_" + this.IDList[this.ID][id] + "_over.gif";
		}
		document.getElementById("btn_" + this.IDList[this.ID][id]).src = this.imageDIR + "/" + btnName;
	},	
	setTabMenu_Detail: function(){
		this.changeTab(1);
		document.getElementById("TAB_POPUP_LINK0").style.display = "none";
	}
}
var TAB_CONT = new tab(0);
var TAB_POPUP = new tab(1);

////////////////////////////////////////////////////////////////////////////////
// popup
////////////////////////////////////////////////////////////////////////////////
function popup(){
	this.ID = "TOP";
	this.movSize = "";
	this.isOpen = false;
	this.isWindows = isWindows;
	this.isMozilla = isMozilla;
	this.isIE = isIE;
	this.isIE7 = isIE7;
	this.BODY = null;
	this.CONTENT_IFRAME = null;
	this.POPUP_OVERLAY = null;
	this.POPUP_CONTAINER = null;
	this.POPUP_MOVIE = null;
	this.POPUP_DETAIL = null;
	this.POPUP_BOX  = null;
	this.mainHeaderInner = null;
	this.recommendInner = null;
	this.footerInner = null;
	
	this.changeTimePeriod = 35;
	this.changeTimerId = 0;
	this.opacityInit = 0;
	this.opacityFin = 85;
	this.opacityGlow = 5;
	this.opacityValue = 0;
}
popup.prototype = {
	init: function(){
		this.BODY = document.body;
		this.POPUP_OVERLAY = document.getElementById("popup_overlay");
		this.POPUP_CONTAINER = document.getElementById("popup_container");
		this.POPUP_BOX = document.getElementById("popup_box");
		this.POPUP_MOVIE = document.getElementById("popup_movie");
		this.POPUP_DETAIL = document.getElementById("popup_detail");
		this.POPUP_IFRAME = document.getElementById("popup_iframe");
		
		this.POPUP_CONTAINER.style.position = "fixed";
		
		// IE6 以下
		if(this.isIE && !this.isIE7) this.POPUP_CONTAINER.style.position = "absolute";
		// win ff
		if(this.isMozilla && this.isWindows) this.POPUP_MOVIE.style.position = "fixed";
		
		// オーバーレイクリック設定
		if (window.addEventListener) {
			this.POPUP_OVERLAY.addEventListener("click", POPUP.close, false);
		} else if (window.attachEvent && document.getElementById) {
			this.POPUP_OVERLAY.attachEvent("onclick", POPUP.close);

			// IE6 以下
			if(this.isIE && !this.isIE7){
				this.POPUP_BOX.attachEvent("onclick", function(e){if(window.event) e=window.event; e.cancelBubble = true;});
				this.POPUP_CONTAINER.attachEvent("onclick", function(){if(POPUP.isOpen) POPUP.close();});
			}
		}
	},
	open: function(){
		if(!this.isOpen){
			this._open();
		}else{
			this.update();
		}
	},
	_open: function(){
		this.isOpen = true;
		
		// flash contents を非表示位置へ移動
		document.getElementById("index").style.marginLeft = "-1000px";
		document.getElementById("indexsearchBox").style.marginLeft = "-1000px";
		document.getElementById("recommend_swf").style.marginLeft = "-1000px";
		document.getElementById("footer").style.marginLeft = "-1000px";

		// FireFox 以外
		if(!this.isMozilla){
			this.POPUP_OVERLAY.style.backgroundColor = "#000000";
			this.POPUP_OVERLAY.style.backgroundImage = "none";
			this.setOpacity(this.POPUP_OVERLAY, 0.85);
		}
		this.POPUP_OVERLAY.style.height = 0 + "px";
		this.POPUP_OVERLAY.style.display = "block";
		this.POPUP_BOX.style.display = "block";
		this.setPosition();
	},
	
	
	change: function(){
		this.POPUP_OVERLAY.style.display = "none";
		this.opacityValue = this.opacityInit;
		this.opacityFin = 85;
		this.setOpacity(this.POPUP_OVERLAY, 0);
		this.POPUP_OVERLAY.style.display = "block";
		this._change();
	},
	_change: function(){
		var self = this;
		if (this.opacityValue < this.opacityFin){
			this.opacityValue += this.opacityGlow;
			if (this.opacityValue > this.opacityFin) this.opacityValue = this.opacityFin;
			if(this.changeTimerId) clearTimeout(this.changeTimerId);
			this.changeTimerId = setTimeout(function(){self._change()}, this.changeTimePeriod);
			this.setOpacity(this.POPUP_OVERLAY, eval(this.opacityValue/100));
		}else{
			clearTimeout(this.changeTimerId);
			this.setPosition();
		}
	},
	close: function(){
		var target = (this == POPUP)? this: POPUP;
		target.isOpen = false;
		target.POPUP_OVERLAY.style.display = "none";
		target.POPUP_BOX.style.top = (-1000 - document.body.scrollHeight) + "px";
		// flash contents の表示位置を戻す
		document.getElementById("index").style.marginLeft = "0px";
		document.getElementById("indexsearchBox").style.marginLeft = "0px";
		document.getElementById("recommend_swf").style.marginLeft = "0px";
		document.getElementById("footer").style.marginLeft = "0px";
		SWFAddress.setValue('TOP');
	},	
	update: function(){
	},	
	setOpacity: function(elment, opacity) {
		if(typeof(elment.style.opacity) != "undefined") {
			elment.style.opacity = opacity;
			POPUP.setOpacity = function(elment, opacity) {
				elment.style.opacity = opacity;
			};
		} else if(typeof(elment.style.MozOpacity) != "undefined") {
			elment.style.MozOpacity = opacity;
			POPUP.setOpacity = function(elment, opacity) {
				elment.style.MozOpacity = opacity;
			};
		} else if(elment.filters) {
			elment.style.filter = "Alpha(opacity=" + (opacity * 100) + ")";
			POPUP.setOpacity = function(elment, opacity) {
				elment.style.filter = "Alpha(opacity=" + (opacity * 100) + ")";
			};
		}
	},	
	setPosition: function(){
		if(this.isOpen){
			var target = (this == POPUP)? this: POPUP;
			var winWidth  =  window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ? document.body.clientWidth : 0;
			var winHeight =  window.innerHeight? window.innerHeight: document.documentElement.clientHeight? document.documentElement.clientHeight: document.body.clientHeight? document.body.clientHeight : 0;
			target.POPUP_BOX.style.top = "0px";
			target.POPUP_BOX.style.marginTop  = Math.round((winHeight - target.POPUP_BOX.offsetHeight) / 2) + "px";
			target.POPUP_OVERLAY.style.width  = (winWidth < 990)? "990px": "100%";
			target.POPUP_OVERLAY.style.height = Math.max(document.documentElement.scrollHeight, winHeight) + "px";
			if(this.isIE && !this.isIE7){
				target.POPUP_BOX.style.marginTop = 0 + "px";
				target.POPUP_OVERLAY.style.height = Math.max(document.body.scrollHeight, winHeight) + "px";
				var scroll = document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop? document.body.scrollTop:0;
				target.POPUP_BOX.style.top = scroll + Math.round((winHeight - target.POPUP_BOX.offsetHeight) / 2) + "px";
			}
		}
	},
	onLoad: function() {
		var target = (this == POPUP)? this: POPUP;
		if(target.isOpen) target.setPosition();
	},	
	onScroll: function(){
		if(this.isOpen){
			var target = (this == POPUP)? this: POPUP;
			var winHeight =  window.innerHeight? window.innerHeight: document.documentElement.clientHeight? document.documentElement.clientHeight: document.body.clientHeight ? document.body.clientHeight : 0;
			var scroll = document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop? document.body.scrollTop:0;
			target.POPUP_BOX.style.top = scroll + Math.round((winHeight - target.POPUP_BOX.offsetHeight) / 2) + "px";
		}
	},
	onResize: function() {
		var target = (this == POPUP)? this: POPUP;
		if(target.isOpen) target.setPosition();
	}
}
var POPUP = new popup();


function popupInitHandler(e){
	POPUP.init();
}
function popupChangeHandler(e){
	// タイトル
	//SWFAddress.setTitle("日産：CMギャラリー");
	document.title = "日産：CMギャラリー";

	var hashDir = e.path.substr(1, e.path.length - 2).split("/");
	var tempID = (hashDir[0] == "")? "TOP": hashDir[0];
	
	if(POPUP.ID != tempID){
		POPUP.ID = tempID;
		if(POPUP.ID != "TOP"){
			TAB_POPUP.changeTab(0);
		}
	}
	POPUP.ID = tempID;
	POPUP.movieSize = hashDir[1];

	// ムービーサイズ
	if(POPUP.movieSize == "MEGA"){
		POPUP.POPUP_DETAIL.style.display = "none";
		document.getElementById("popup_movie").style.paddingLeft = 138 + "px";
		document.getElementById("popup_movie").style.width = 524 + "px";
		document.getElementById("popup_external").style.width = 524 + "px";
	}else{
		document.getElementById("popup_external").style.width = 320 + "px";
		document.getElementById("popup_movie").style.width = 320 + "px";
		document.getElementById("popup_movie").style.paddingLeft = 40 + "px";
		POPUP.POPUP_DETAIL.style.display = "block";
	}
	
	if(e.value == "/" || e.value == "/TOP/"){
		if(POPUP.isOpen) POPUP.close();
	}else{
		POPUP.open();
	}
}

if(isSupported){
	SWFAddress.addEventListener(SWFAddressEvent.INIT, popupInitHandler);
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, popupChangeHandler);
}
////////////////////////////////////////////////////////////////////////////////
// event
////////////////////////////////////////////////////////////////////////////////

function onResizeHandler(){
	POPUP.onResize();
}
function onLoadHandler(){
	if(isSupported) {
		POPUP.onLoad();
		//TAB_POPUP.setCurrent();
	}else{
		// ハッシュ取得
		var index = location.href.indexOf('#');
		if (index != -1) {
			var hash = location.href.substring(index).replace(/^#/g, '');
		}
		var hashDir = hash.substr(1, hash.length - 2).split("/");
		
		if(hashDir[0] != "" || hashDir[0] != "TOP"){
			CmMvPopWin(hashDir[0], hashDir[1]);
		}
		location.href = "./#" + hashDir[0] + "/" + hashDir[1] + "/";
	}
	
	TAB_CONT.setCurrent();
	MM_preloadImages('IMAGES/btn_carName.gif', 'IMAGES/btn_ranking_over.gif', 'IMAGES/btn_related.gif', 'IMAGES/btn_detail_over.gif', 'IMAGES/btn_play_over.gif', 'IMAGES/btn_close_over.gif');
}
function onScrollHandler(){
	POPUP.onScroll();
}

if (window.addEventListener) {
	window.addEventListener("load",onLoadHandler,false);
	window.addEventListener("resize",onResizeHandler,false);
}
else if (window.attachEvent && document.getElementById) {
	window.attachEvent("onload",onLoadHandler);
	
	if(isSupported) window.attachEvent("onresize",onResizeHandler);
	
	// SWFAddressサポート外 && IE6 以下
	if(isSupported && !isIE7){
		window.attachEvent("onscroll",onScrollHandler);
	}
}