﻿var IE = document.all?true:false;
var debugIndex = 1;
var timeoutId = 0;
	var ua = navigator.userAgent;
	var isMobile = false;

	if(ua.search('Mobile')!=-1){
		isMobile = true;
	}else{
		isMobile = false;
	}

$(function() {
	if(isMobile == true) {/*
		$('td #about_v1').children('a').attr('href', 'EN/upload/images/non_flash/video/GEH_EN.mp4');
		$('td #about_v2').children('a').attr('href', 'EN/upload/images/non_flash/video/Grand_Opening.mp4');
		$('td #about_v3').children('a').attr('href', 'EN/upload/images/non_flash/video/grand_virtual.mp4');*/
		$('td[id="about_v1"], td[id="about_v2"], td[id="about_v3"]').children('a')
			.attr('href', '#')
			.attr('target', '_self').bind('click', function(){
				popMp4Win($(this).parent().attr('id'));
			});
	}
	
	$('img.btn_over').mouseover(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('.g','_over.g').replace('.j','_over.j'));
    }).mouseout(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('_over.g','.g').replace('_over.j','.j'));
    });
	$('.keyCheck').each(function(){
		$(this).bind('keyup', function(e){keyCheck(this, e, 'up')}).bind('keypress', function(e){keyCheck(this, e, 'press')});
	})
	if(id_sequence[0] == '2') {
		loadMenu();
	} else {
		subMenuStart();	
		subMenu();
		//subMenuResize();
	}
	//alert(id_sequence[0]);
});

function debug(str){
	if($('#debugDIV').length==0){
		$("body").append('<div id="debugDIV" style="position:absolute; top: 100px; left: 100px; padding: 5px; background: #090; color: #900; z-index: 10000;"></div>');
	}
	//$('#debugDIV').html('debug ID: ' + (debugIndex++)+ '\r\n' + str);
	$('#debugDIV').html(str);
}

function loadMenu() {
	var menu='';
	currentMenuSection = 'menu_'+id_sequence[0];
	//debug(currentMenuSection);
	$('#nav_menu > li').mouseenter(function() {
		$(this).children('a').addClass('selected');
		menu = $(this).children('div');
		menuClass = $(this).attr('class');
		if($(menu).length > 0) {
			$(this).children('div').fadeIn();
		}
	}).mouseleave(function() {
		menu = $(this).children('div');
		$(this).children('a').removeClass('selected');
		$(menu).fadeOut('fast');
	});
}

var currentSelectedMenu = false;

function subMenuReset() {
	currentMenuSection = 'menu_'+id_sequence[0];
	//currentSubMenuSection = 'submenu_'+id_sequence[1];

	$('#nav_menu li > div').css('z-index',-1);
	$('#nav_menu li > div').fadeOut();
	$('#nav_menu li > a').removeClass('selected');
}

function subMenuResize() {
	$('#nav_menu li.'+currentMenuSection+' > div').fadeIn();
}

function subMenuStart() {
	currentMenuSection = 'menu_'+id_sequence[0];
	//currentSubMenuSection = 'submenu_'+id_sequence[1];
	$('#nav_menu > li.'+currentMenuSection+' > a').addClass('selected');
	
	if( $('#nav_menu li.'+currentMenuSection+' > div').length > 0) {
		//subMenuResize();
		$('#nav_menu li.'+currentMenuSection+' > div').stop(true, true).fadeIn();
	}
}

function subMenu() {
	currentMenuSection = 'menu_'+id_sequence[0];
	currentSubMenuSection = 'menu_'+id_sequence[1];
	
	$('#nav_menu > li').mouseenter(function() {
		currentMenu = $(this).attr('class');
		currentSelectedMenu = true;
		$(this).children('a').addClass('selected');
		//debug(currentMenu+', '+currentMenuSection);
		//debug(currentMenuSection);
		if(currentMenu != currentMenuSection) {
			subMenuReset();
			if( $(this).children('div').length > 0) {
				//debug(timeoutId);
				clearTimeout(timeoutId);
				$(this).children('div').stop(true, true).fadeIn();
			}
			//alert(($this).class);
		}else{
			subMenuStart();
		}
		
		
		/*if( $(this).children('div').length > 0) {
			subMenuResize();
			$(this).children('div').fadeIn();
		}*/
	}).mouseleave(function() {
		currentMenu = $(this).attr('class');
		currentSubMenuSection = 'menu_'+id_sequence[1];
		currentSelectedMenu = false;
		if(currentMenu != currentMenuSection) {
			subMenuReset();
			timeoutId = setTimeout(function ()
			{
				if(currentSelectedMenu == false) {
					subMenuStart();
				}
			}, 800);
		}
			
		
	});
}

$(window).load(function (){
	var menu_w = 0;
	var temp;
	var i=0;
	var j=0;
	for(i=1;i<=$('#nav_menu > li').length;i++){
		menu_w = 0;
		for(j=1;j<=$('#nav_menu > li:nth-child('+i+') > div > ul > li').length;j++){
			menu_w += $('#nav_menu > li:nth-child('+i+') > div > ul > li:nth-child('+j+')').outerWidth();
			//alert($('#nav_menu > li:nth-child('+i+') > div > ul > li:nth-child('+j+')').outerWidth());
		}
		if(id_sequence[0]==2){
			$('#nav_menu > li:nth-child('+i+') > div').css("width", menu_w+19+"px");
		}else{
			$('#nav_menu > li:nth-child('+i+') > div').css("width", menu_w+14+"px");
		}
		$('#nav_menu > li:nth-child('+i+') > div').css("display", "none");
		$('#nav_menu > li:nth-child('+i+') > div').css("visibility", "visible");
	}
	$('.menu_'+id_sequence[0]+' > div').css("display", "block");
});

var lang = ["", "EN", "TC", "SC", "JP"];
function getLang(){
	var langRegex = location.href.match(/lang=(\d+)/);
	if(langRegex){
		return langRegex[1];
	}else{
		return 2;
	}
}
var mp4FileNames = {
	"about_v1": {"name": "geh_"+lang[getLang()].toLowerCase(), "w": 320, "h": 240},	// + lang[getLang()].toLowerCase()
	"about_v2": {"name": "grand_opening", "w": 320, "h": 240},
	"about_v3": {"name": "grand_virtual", "w": 400, "h": 298}
};
function popMp4Win($id){
	var theVideo = mp4FileNames[$id];
	var doc_h = $(window).height();
	var video_h = theVideo.h;
	var mp4player_h = (doc_h - video_h)/2;
	var owin = window.open("","Movie", "");
	owin.document.write("<"+"!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
	owin.document.write("<"+"html>");
	owin.document.write("<"+"head>");
	owin.document.write("<"+"script src='js/qtobject.js' language='javascript'><"+"/script>");
	owin.document.write("<"+"/head>");
	owin.document.write("<"+"body style='padding: 0; position:relative; margin: 0; width: 100%; height: 100%; background: #000000;'>");
	owin.document.write("<"+"div id='QTCONTENT' style='width: "+theVideo.w+"px; height: "+theVideo.h+"px; margin:"+mp4player_h+"px auto;'><"+"/div>");
	owin.document.write("<"+"script>");
	owin.document.write("var myQTObject = new QTObject('/"+lang[getLang()]+"/upload/av/"+theVideo.name+".mp4', 'QT', '400', '300');");
	owin.document.write("myQTObject.write('QTCONTENT');");
	owin.document.write("<"+"/script>");
	owin.document.write("<"+"/body>");
	owin.document.write("<"+"/html>");
	owin.document.close();
}
