// !required prototype.js

/* Trailer Layer
--------------------------------------------------*/
function viewTrailer(p,m,w,a) {
	var v = new Trailer(p,m,w,a);
}

function Trailer(player,movie,w,autoplay) {
	var blogurl =  $("blogurl").getAttribute("HREF");
	this.player = blogurl+"swf/"+player;
	this.movie = movie;
	this.myWidth = w;
	if (player != "flvplayer_mid.swf") this.myHeight = Math.round(((this.myWidth/16)*9) + 18);
	else this.myHeight = Math.round(((this.myWidth/4)*3) + 18);
	this.autoplay = autoplay;
	this.alpha = 0;
	new Insertion.After('body-container', '<div id="trailer"></div>');
	new Insertion.Bottom($('trailer'), '<div id="trailer-bg"></div>');
	new Insertion.Bottom($('trailer'), '<div id="trailer-main-' + this.myWidth +'"></div>');
	$('trailer').style.height = $('body-container').offsetHeight +"px";
	Event.observe($("trailer-bg"), 'mousedown', this.hide.bind(this), false);
	var wHeight = (document.all && !window.opera) ? document.documentElement.clientHeight : window.innerHeight;
	var sTop = (navigator.appVersion.indexOf("Safari")>-1) ? document.body.scrollTop : document.documentElement.scrollTop;
	$("trailer-main-"+this.myWidth).style.top = (sTop + (wHeight/2) - ($("trailer-main-"+this.myWidth).offsetHeight/2)) + "px";
	this.fadeIn();
	this.timer = setInterval(this.fadeIn.bind(this),50);
}

Trailer.prototype.fadeIn = function() {
	if (this.alpha < 100) {
		this.alpha = Math.min(100, this.alpha+25);
		this.setOpacity($('trailer'),this.alpha);
		
	} else {
		clearInterval(this.timer);
		flvplayer = ''
		flvplayer += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + this.myWidth +'" height="' + this.myHeight +'" id="flvplayer" align="middle">';
		flvplayer += '<param name="allowScriptAccess" value="sameDomain" />';
		flvplayer += '<param name="allowFullScreen" value="false" />';
		flvplayer += '<param name="movie" value="' + this.player + '" />';
		flvplayer += '<param name="quality" value="high" />';
		flvplayer += '<param name="wmode" value="' + ((navigator.userAgent.indexOf("Gecko/") > -1)?'transparent':'window') + '" />';
		flvplayer += '<param name="menu" value="false" />';
		flvplayer += '<param name="bgcolor" value="#000000" />';
		flvplayer += '<param name="flashvars" value="' + 'movie=' + this.movie + '&autoplay=' + this.autoplay + '" />';
		flvplayer += '<embed src="' + this.player + '" quality="high" wmode="' + ((navigator.userAgent.indexOf("Gecko/") > -1)?'transparent':'window') + '" menu="false" bgcolor="#000000" flashvars="' + 'movie=' + this.movie + '&autoplay=' + this.autoplay + '" width="' + this.myWidth +'" height="' + this.myHeight +'" id="flvplayer" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		flvplayer += '</object>';
		flvplayer += '<div id="trailer-close"><a href="#" id="trailer-close-link">Close</a></div>';
		$("trailer-main-"+this.myWidth).innerHTML = flvplayer;
		Event.observe($("trailer-close-link"), 'mousedown', this.hide.bind(this), false);
	}
}


Trailer.prototype.hide = function(event) {
	Element.remove($('flvplayer'));
	Element.remove($('trailer-close'));
	this.fadeOut();
	this.timer = setInterval(this.fadeOut.bind(this),50);
	Event.stop(event);
	return false;
}

Trailer.prototype.fadeOut = function() {
	if (this.alpha > 0) {
		this.alpha = Math.max(0, this.alpha-25);
		this.setOpacity($('trailer'),this.alpha);
		
	} else {
		clearInterval(this.timer);
		Element.remove($('trailer'));
	}
}

Trailer.prototype.setOpacity = function(obj,op) {
	obj.style.filter='alpha(opacity='+op+')';
	obj.style.mozOpacity= (op/100);
	obj.style.opacity = (op/100);
}

/* Sub Layer
--------------------------------------------------*/
mySubLayer = null;
function viewSubLayer(f,w,h,b,a,c,l,s) {
	// filepath,width,height,bgcolor,args,closebutton,lite,shrink
	mySubLayer = new SubLayer(f,w,h,b,a,c,l,s);
}

function closeSubLayer() {
	if (mySubLayer != null) {
		mySubLayer.hide();
	}
}

function SubLayer(f,w,h,b,a,c,l,s) {
	var blogurl =  $("blogurl").getAttribute("HREF");
	this.file = blogurl+f;
	this.myWidth = w;
	this.myHeight = h;
	this.bgcolor = b;
	this.args = a;
	this.closeButton = c;
	this.lite = l;
	this.shrink = s;
	this.alpha = 0;
	new Insertion.After('body-container', '<div id="sublayer"></div>');
	new Insertion.Bottom($('sublayer'), '<div id="sublayer-bg"></div>');
	new Insertion.Bottom($('sublayer'), '<div id="sublayer-main"></div>');
	if (this.lite) Element.addClassName($("sublayer-bg"),"lite");
	$('sublayer').style.height = $('body-container').offsetHeight +"px";
	Event.observe($("sublayer-bg"), 'mousedown', this.hide.bind(this), false);
	var wHeight = (document.all && !window.opera) ? document.documentElement.clientHeight : window.innerHeight;
	if (wHeight < (this.myHeight+60) && this.shrink) {
		var zoom = Math.max(0.75, wHeight/(this.myHeight+60));
		this.myHeight = parseInt(this.myHeight*zoom);
		this.myWidth = parseInt(this.myWidth*zoom);
	}
	var sTop = (navigator.appVersion.indexOf("Safari")>-1) ? document.body.scrollTop : document.documentElement.scrollTop;
	$("sublayer-main").style.width = this.myWidth + "px";
	$("sublayer-main").style.height = this.myHeight + "px";
	//$("sublayer-main").style.backgroundColor = this.bgcolor;
	$("sublayer-main").style.top = Math.max(sTop+10,(sTop + (wHeight/2) - ($("sublayer-main").offsetHeight/2))) + "px";
	this.fadeIn();
	this.timer = setInterval(this.fadeIn.bind(this),50);
}

SubLayer.prototype.fadeIn = function() {
	if (this.alpha < 100) {
		this.alpha = Math.min(100, this.alpha+25);
		this.setOpacity($('sublayer'),this.alpha);	
	} else {
		clearInterval(this.timer);
		
		sublayerContent = '';
		if (this.file.indexOf(".swf")>=0) {
			sublayerContent += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + this.myWidth + '" height="' + this.myHeight +'" id="sublayer-content" align="middle">';
			sublayerContent += '<param name="allowScriptAccess" value="sameDomain" />';
			sublayerContent += '<param name="allowFullScreen" value="false" />';
			sublayerContent += '<param name="movie" value="' + this.file + '" />';
			sublayerContent += '<param name="quality" value="high" />';
			sublayerContent += '<param name="wmode" value="' + ((navigator.userAgent.indexOf("Gecko/") > -1)?'transparent':'window') + '" />';
			sublayerContent += '<param name="menu" value="false" />';
			sublayerContent += '<param name="bgcolor" value="' + this.bgcolor + '" />';
			sublayerContent += '<param name="flashvars" value="' + this.args + '" />';
			sublayerContent += '<embed src="' + this.file + '" quality="high" wmode="' + ((navigator.userAgent.indexOf("Gecko/") > -1)?'transparent':'window') + '" menu="false" bgcolor="' + this.bgcolor + '" flashvars="' + this.args + '" width="' + this.myWidth +'" height="' + this.myHeight +'" id="sublayer-content" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			sublayerContent += '</object>';
		} else {
				
			sublayerContent += '<div id="sublayer-content"><img src="' + this.file + '" alt="" width="' + this.myWidth + '" height="' + this.myHeight +'" /></div>';
						
			
		}
		if (this.closeButton) sublayerContent += '<div id="sublayer-close"><a href="#" id="sublayer-close-link">Close</a></div>';
		$("sublayer-main").innerHTML = sublayerContent;
		
		if (this.closeButton) {
			if (this.lite) Element.addClassName($("sublayer-close"),"lite");
			Event.observe($("sublayer-close-link"), 'mousedown', this.hide.bind(this), false);
		}
	}
}

SubLayer.prototype.hide = function(event) {
	Element.remove($('sublayer-content'));
	if ($('sublayer-close')) Element.remove($('sublayer-close'));
	this.fadeOut();
	this.timer = setInterval(this.fadeOut.bind(this),50);
	if (event) Event.stop(event);
	return false;
}

SubLayer.prototype.fadeOut = function() {
	if (this.alpha > 0) {
		this.alpha = Math.max(0, this.alpha-25);
		this.setOpacity($('sublayer'),this.alpha);
		
	} else {
		clearInterval(this.timer);
		Element.remove($('sublayer'));
		mySubLayer = null;
	}
}

SubLayer.prototype.setOpacity = function(obj,op) {
	obj.style.filter='alpha(opacity='+op+')';
	obj.style.mozOpacity= (op/100);
	obj.style.opacity = (op/100);
}

/* News Headline
--------------------------------------------------*/
function initHeadlines() {
	
	if (!document.getElementById("newsheadlines") || document.getElementById("newsheadlines").tagName != "UL") return;
	current = 0;	
	amountX = 3;
	marginX = 40;
	listItems = document.getElementById("newsheadlines").getElementsByTagName("li");
	listLength = listItems.length;
	
	listWidth = new Array();
	for (i=0; i<listLength; i++) {
		listWidth[i] = listItems[i].offsetWidth;	
		
	}
	listLeft = new Array();
	for (i=0; i<listLength; i++) {		
		listItems[i].style.visibility = "visible";
		if (i>0) {
			listLeft[i] = parseInt(listLeft[i-1]) + listWidth[i-1] + marginX + "px";
			listItems[i].style.left = Math.min(listItems[i].parentNode.offsetWidth,parseInt(listLeft[i]))+"px";
		}
		else listItems[i].style.left = listLeft[i] = listItems[i].parentNode.offsetWidth+"px";	
	}
	setInterval("updateHeadlines()",100);
	
}

function updateHeadlines() {
	for (i=0; i<listLength; i++) {
		n = (current+i)%listLength;
		with (listItems[n]) {
			prev = (n>0)? n-1:listLength-1;

			if ( parseInt(style.left) == -listWidth[n]) {

				listLeft[n] = Math.max(parseInt(listLeft[prev])+listWidth[prev] + marginX,listItems[n].parentNode.offsetWidth + marginX) + "px";
				style.left = Math.min(listItems[n].parentNode.offsetWidth,parseInt(listLeft[n]))+"px";
				current = (current+1)%listLength;
				break;
			}
			else {
				listLeft[n] = Math.max(-listWidth[n],parseInt(listLeft[n])-amountX)+"px";
				style.left = Math.min(listItems[n].parentNode.offsetWidth,parseInt(listLeft[n]))+"px";			
			}		
		}
	}
}

Event.observe(window, 'load', initHeadlines, false);

/* Categories
--------------------------------------------------*/
function initCategory() {
	if (document.getElementsByClassName('gnav').length>0) {
		var gnav = document.getElementsByClassName('gnav')[0].getElementsByTagName("LI");
		if (gnav.length) {
			var gnavOrder = new Array(
				'news',
				'theaters',
				'media',
				'blog',
				'manga',
				'report',
				'rostock',
				'download',
				'goroku',
				'links',
				'trailer'
			);
			var gnavNewOrder = new Array();
			for (var i=0; i<gnav.length; i++) {
				var n = gnavOrder.indexOf(gnav[i].id.split("-")[1]);
				if (n>=0) {
					gnavNewOrder[n] = '<li id="' + gnav[i].id + '">'+gnav[i].innerHTML+'</li>';
				}
			}
			
			gnavNewOrder.compact();
			var gnavHTML = "";
			if (gnavNewOrder.length) {
				for (var i=0; i<gnavNewOrder.length; i++) {
					gnavHTML+=gnavNewOrder[i];
				}
			}
			
			document.getElementsByClassName('gnav')[0].innerHTML = gnavHTML;
		}
	}
}


/* About The Movie
--------------------------------------------------*/
function openWindow(p,n,w,h,options,center){
	if (p) {
		if (center == "yes" || center == 1) {
			scWidthCenter = screen.availWidth / 2;
			scHeightCenter = screen.availHeight / 2;
			options2 = ",left=" + (scWidthCenter - (w / 2)) + ",top=" + (scHeightCenter - (h / 2));
		} else {
			options2 = "";
		}

		url = p.getAttribute("HREF");
		w = window.open(url,n,"width=" + w + ",height=" + h + "," + options + options2);
		w.focus();
		return false;
	} else {
		return true;
	}
}

function closeWindow(){
	var w = (parent)?parent:this;
 	w.window.close();
 	return false;
}

