
var headerUri = "index.html";
var footerUri = "footer.html";

/*
var headRnd = Math.floor(Math.random()*2);
*/

$(document).ready(function(){
	$("#menuContent").load(headerUri + " #header-container ul[id=header-content]",null,headerLoaded);
	$("#header-flash").load(headerUri + " #header-body div[id=header-bodyContent]",null);
	$("#footerInner").load(footerUri + " #footer-container div[id=footer-content]");
	
	var imgTagCredit = $(".imgThumb p");
	$(".imgThumb p").html(function(i, html){
							   if(html.match("\n")) {
								   html = html.split("\n").join("<br>");
								   if(html.match("\r")) html = html.split("\r").join(" ");
								   return html;
							   }
							   else if(html.match("\r")) {
								   html = html.split("\r").join("<br>");
								   return html;
							   }
							   else if(html.match("&#10;")) {
								   html = html.split("&#10;").join("<br>");
								   if(html.match("&#13;")) html = html.split("&#13;").join(" ");
								   return html;
							   }
							   else if(html.match("&#13;")) {
								   html = html.split("&#13;").join("<br>");
								   return html;
							   }
							   else return html = html;
	});
	
	
	textAreaBoxHeight = $(".credit textarea");
	ua = navigator.userAgent;
	if(ua.indexOf('MSIE')!=-1) {
		/**
		* Actual function that does all the work. Returns an array with all the info.
		* My Assumption is that most of the browsers will have arial set as their default sans-serif font.
		*/
		var Detector = function(){
			var h = document.getElementsByTagName("BODY")[0];
			var d = document.createElement("DIV");
			var s = document.createElement("SPAN");
			d.appendChild(s);
			d.style.fontFamily = "serif";		//font for the parent element DIV.
			s.style.fontFamily = "serif";		//have to use serif coz in FF3.0, it doesn't fall back to font of parent element.
			s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
			s.innerHTML        = "mmmmmmmmmml";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
			h.appendChild(d);
			var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
			var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
			h.removeChild(d);
			/* test
			 * params:
			 * font - name of the font you wish to detect
			 * return:
			 * f[0] - Input font name.
			 * f[1] - Computed width.
			 * f[2] - Computed height.
			 * f[3] - Detected? (true/false).
			 */
			 function debug(font) {
				 h.appendChild(d);
				 var f = [];
				 f[0] = s.style.fontFamily = font;       // Name of the font
				 f[1] = s.offsetWidth;                           // Width
				 f[2] = s.offsetHeight;                          // Height
				 h.removeChild(d);
				 font = font.toLowerCase();
				 if (font == "serif") {
					 f[3] = true;    // to set arial and sans-serif true
				 } else {
					 f[3] = (f[1] != defaultWidth || f[2] != defaultHeight); // Detected?
				}
				return f;
			}
			function test(font){
				f = debug(font);
				return f[3];
			}
		this.detailedTest = debug;
		this.test = test;
		}
		var fonts = [];
		/**
		* other stuff
		*/
		
		var detective = new Detector();
	
		if(detective.test('Meiryo') == true){
		}
		else{
			textAreaBoxHeight.css("font-size", "12px");
			textAreaBoxHeight.css("line-height", "16px");
		}
	}
	
});

/*
function headerChange(){
	var h$ = jQuery;
	if(headRnd < 1){
		h$("#header-bannerSet1").css("display", "none");
		h$("#header-bannerSet0").css("display", "block");
	}
	else{
		h$("#header-bannerSet0").css("display", "none");
		h$("#header-bannerSet1").css("display", "block");
	}
}
*/

function headerLoaded(){
	var j$ = jQuery;
	var jArray = new Array();
	var closeArray = new Array();
	var btnArray = new Array();

	j$(function(){

		function setBackground() {
			var $last = j$("#header-content > li:last > a");
			if($last.hasClass("close"))
				$last.css("background-position", "left top");
			else
				$last.css("background-position", "left top");
		}

		j$("#header-content").each(function(){
			j$(".menuSection > ul", this).wrap("<div></div>");
			j$(".menuSection > div:not(:last)", this).prepend("<div class='topP'>&nbsp;</div>");
			j$(".menuSection > div:not(:last)", this).append("<div class='bottomP'>&nbsp;</div>");
			j$(".menuSection > div:last"      , this).prepend("<div class='topP'>&nbsp;</div>");
			j$(".menuSection > div:last"      , this).append("<div class='bottomP'>&nbsp;</div>");
		
			j$("li > a", this).each(function(index){
				var $this = j$(this);
			
				$this.addClass("close").next().hide();
				setBackground();

				var prms = {height:"toggle", opacity:"toggle"};
			
				var thisUri = this.toString();
				var thisURIFlag = thisUri.indexOf("htm", 0);
				if(thisURIFlag < 0){
					var closeStr = $this.attr("name");
					if(closeStr.length > 0){
						closeArray.push($this);
						var thisNumStr = $this.attr("href");
						var thisNum = thisNumStr.charAt(thisNumStr.length-1);
						closeArray[thisNum].click(function(){
							jArray[thisNum].click();
							return false;
						});
					}
					else{
						jArray.push($this);
					}
					
					$this.click(function(){
						j$(this).toggleClass("close").next().animate(prms, {duration:"fast"})
							.parent().siblings().children("div:visible").animate(prms, {duration:"fast"}).prev().addClass("close");
						setBackground();
						return false;
					});
				}
				else {
					if($("a[class^=event-fancy]")){
						
					}
					else {	
						$("a[href^=../]").click(function(){
							window.open(this.href, "_top");
						});
					}
				}
				
			});
		});
			
		j$("#header-btnBox").each(function(){
			j$("li > a", this).each(function(index){			 
				var $btns = j$(this);
				btnArray.push($btns);
				$btns.click(function(){
					var thisNum = $btns.attr("name");
					jArray[thisNum].click();
					return false;
				});
			});
		});
	});
	
	$(window).load(function() {
		$("#rFrame0").contents().find("#news").bind("click", function() {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
			   	'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

	return false;


		});			
	});
	
	
};

