
var swfobject;
var exte;

//------------ TEST START --------------
var g_count = 0;
var g_time = 0; 
//var g_timer = new Timer(500);
//g_timer.On(function(){ g_time += 1; });
//------------ TEST END --------------

jQuery(function(){
/*
	//---------- TEST START -----------------------
	var a = document.createElement("div");
	a.id = "output";
	$("body").append(a);
	$("#output").css({position:"fixed",left:"5px",top:"5px",border:"solid 1px black",backgroundColor:"white",padding:"3px"});
	g_timer.On(function(){
		$("#output").html(
			"width:"+($(window).width())+"<br />"+
			"height:"+($(window).height())
		);
	});
	//---------- TEST END -----------------------
*/
	if( !!swfobject )
	{
		swfobject.embedSWF("reqimages/game_splash_new.swf?0715","splash",658,686,"9.0");
	}
	$("#splash a").css("opacity","0").hover(
		function(){ $(this).stop().fadeTo(100,1); },
		function(){ $(this).stop().fadeTo("nomal",0); } );
	//------- TEMP -------------
	$("#language").click(function(){
		var lang = "ja";
		if( !!$.cookie("language") && $.cookie("language") == "ja" ){
			lang = "en";
		}
		$.cookie("language", lang, { path: "/", expires: 7 });
		location.reload();
		return false;
	});
	//--------------------------

	(function(){
		var news = $("#contents_left .content .windows .news_window .window_body");
		var news_h = news.height();
		var update = $("#contents_left .content .windows .update_window .window_body");
		var update_h = update.height();
		if( !!news_h && !!update_h ){
			var judge = news_h > update_h ? news_h : update_h;
			update.height(judge);
			news.height(judge);
		}
	})();
	
	var shadowDecideHeight = function(){
		var _h = $("#contents_left").innerHeight();
		$("#contents_right,#contents_left_shadow").height(_h);
	};
	shadowDecideHeight();
	setTimeout(shadowDecideHeight,500);

	$("a.lightbox").lightBox();
	
	$(".copyright,p.pagetop").click(function(){
		ScrollTo($("body"));
	});

	(function(){
		var exec = false;
		exec = exec && document.title != "Q-Games Official Site";
		if( !exec ){ return true; }
		
		var pattern = {
			"walk0": [[128,128],[160,128],[192,128],[224,128],[192,128],[160,128]],
			"walk1": [[128,160],[160,160],[192,160],[224,160],[192,160],[160,160]],
			"walk2": [[128,192],[160,192],[192,192],[224,192],[192,192],[160,192]],
			"walk3": [[128,224],[160,224],[192,224],[224,224],[192,224],[160,224]],
			"walk4": [[  0,224],[ 32,224],[ 64,224],[ 96,224],[ 64,224],[ 32,224]],
			"walk5": [[  0,192],[ 32,192],[ 64,192],[ 96,192],[ 64,192],[ 32,192]],
			"walk6": [[  0,160],[ 32,160],[ 64,160],[ 96,160],[ 64,160],[ 32,160]],
			"walk7": [[  0,128],[ 32,128],[ 64,128],[ 96,128],[ 64,128],[ 32,128]],

			"alert0": [[128,  0],[128,  0],[160,  0],[192,  0],[224,  0],[224,  0],[224,  0],[224,  0],[192,  0],[160,  0],[128,  0],[128,  0]],
			"alert1": [[128, 32],[128, 32],[160, 32],[192, 32],[224, 32],[224, 32],[224, 32],[224, 32],[192, 32],[160, 32],[128, 32],[128, 32]],
			"alert2": [[128, 64],[128, 64],[160, 64],[192, 64],[224, 64],[224, 64],[224, 64],[224, 64],[192, 64],[160, 64],[128, 64],[128, 64]],
			"alert3": [[128, 96],[128, 96],[160, 96],[192, 96],[224, 96],[224, 96],[224, 96],[224, 96],[192, 96],[160, 96],[128, 96],[128, 96]],
			"alert4": [[  0, 96],[  0, 96],[ 32, 96],[ 64, 96],[ 96, 96],[ 96, 96],[ 96, 96],[ 96, 96],[ 64, 96],[ 32, 96],[  0, 96],[  0, 96]],
			"alert5": [[  0, 64],[  0, 64],[ 32, 64],[ 64, 64],[ 96, 64],[ 96, 64],[ 96, 64],[ 96, 64],[ 64, 64],[ 32, 64],[  0, 64],[  0, 64]],
			"alert6": [[  0, 32],[  0, 32],[ 32, 32],[ 64, 32],[ 96, 32],[ 96, 32],[ 96, 32],[ 96, 32],[ 64, 32],[ 32, 32],[  0, 32],[  0, 32]],
			"alert7": [[  0,  0],[  0,  0],[ 32,  0],[ 64,  0],[ 96,  0],[ 96,  0],[ 96,  0],[ 96,  0],[ 64,  0],[ 32,  0],[  0,  0],[  0,  0]],
			
			"crash0": [[384,  0],[416,  0],[448,  0],[480,  0],[448,  0],[480,  0],[448,  0],[480,  0],[448,  0],[480,  0],[448,  0],[480,  0],[448,  0],[480,  0],[448,  0],[480,  0]],
			"crash1": [[384, 32],[416, 32],[448, 32],[480, 32],[448, 32],[480, 32],[448, 32],[480, 32],[448, 32],[480, 32],[448, 32],[480, 32],[448, 32],[480, 32],[448, 32],[480, 32]],
			"crash2": [[384, 64],[416, 64],[448, 64],[480, 64],[448, 64],[480, 64],[448, 64],[480, 64],[448, 64],[480, 64],[448, 64],[480, 64],[448, 64],[480, 64],[448, 64],[480, 64]],
			"crash3": [[384, 96],[416, 96],[448, 96],[480, 96],[448, 96],[480, 96],[448, 96],[480, 96],[448, 96],[480, 96],[448, 96],[480, 96],[448, 96],[480, 96],[448, 96],[480, 96]],
			
			"crash4": [[352, 96],[320, 96],[288, 96],[256, 96],[288, 96],[256, 96],[288, 96],[256, 96],[288, 96],[256, 96],[288, 96],[256, 96],[288, 96],[256, 96],[288, 96],[256, 96]],
			"crash5": [[352, 64],[320, 64],[288, 64],[256, 64],[288, 64],[256, 64],[288, 64],[256, 64],[288, 64],[256, 64],[288, 64],[256, 64],[288, 64],[256, 64],[288, 64],[256, 64]],
			"crash6": [[352, 32],[320, 32],[288, 32],[256, 32],[288, 32],[256, 32],[288, 32],[256, 32],[288, 32],[256, 32],[288, 32],[256, 32],[288, 32],[256, 32],[288, 32],[256, 32]],
			"crash7": [[352,  0],[320,  0],[288,  0],[256,  0],[288,  0],[256,  0],[288,  0],[256,  0],[288,  0],[256,  0],[288,  0],[256,  0],[288,  0],[256,  0],[288,  0],[256,  0]],

			"dance0": [[384,128],[416,128],[448,128],[480,128],[480,128],[448,128],[416,128],[384,128]],
			"dance1": [[384,160],[416,160],[448,160],[480,160],[480,160],[448,160],[416,160],[384,160]],
			"dance2": [[384,192],[416,192],[448,192],[480,192],[480,192],[448,192],[416,192],[384,192]],
			"dance3": [[384,224],[416,224],[448,224],[480,224],[480,224],[448,224],[416,224],[384,224]],
			"dance4": [[256,224],[288,224],[320,224],[352,224],[352,224],[320,224],[288,224],[256,224]],
			"dance5": [[256,192],[288,192],[320,192],[352,192],[352,192],[320,192],[288,192],[256,192]],
			"dance6": [[256,160],[288,160],[320,160],[352,160],[352,160],[320,160],[288,160],[256,160]],
			"dance7": [[256,128],[288,128],[320,128],[352,128],[352,128],[320,128],[288,128],[256,128]]
		};
		
		var attack_count = 0;

		for(var i=0;i<3;i+=1)
		{
			(function(){
				var div = document.createElement("div");
				div.id = "tikiman"+i;
				div.className = "tikiman";
				$("body").append(div);
	
				var tiki = $("#tikiman"+i);
				var animation = new AnimationXY(tiki,pattern);

				setTimeout(function _Local(){
					var time_limit = 20000 << attack_count; // 20sec
					setTimeout(_Local,time_limit);
					if( $(window).height() < 480 )return;
					if( $(window).width() < 640 )return;
					if( ~~(Math.random()*10) != 1 ){ return; }

					var screen_width = $(window).width() - 32;
					var screen_height= $(window).height() - 32;
					var offset_x = document.body.scrollLeft || document.documentElement.scrollLeft;
					var offset_y = document.body.scrollTop  || document.documentElement.scrollTop;
					
					var start_x = ~~(Math.random()*screen_width) + offset_x;
					var start_y = ~~(Math.random()*screen_height) + offset_y;
					var end_x	= ~~(Math.random()*screen_width) + offset_x;
					var end_y	= ~~(Math.random()*screen_height) + offset_y;
					
					var ret_x = end_x - start_x;
					var ret_y = end_y - start_y;
					var ret_direction = AnimeteDirection(ret_x,ret_y);
					var ret_crash = false;

					animation.On("dance4",F2ms(9));
					tiki.css({left:start_x+"px",top:start_y+"px"})
					.mouseover(function(){
						if( ret_crash )return;
						animation.On("crash"+ret_direction,F2ms(5),1);
						$(this).stop(true).css("opacity",1).fadeOut(1000,function(){animation.Off();});
						ret_crash = true;
						attack_count += 1;
					})
					.fadeIn(5000,function(){
						animation.On("walk"+ret_direction,F2ms(15));
					})
					.animate({
						left: end_x+"px",
						top: end_y+"px"
					},{
						duration: Math.sqrt(ret_x*ret_x+ret_y*ret_y) * 14.2857, // 14.2857 = 1/70 70px = 1sec
						easing: "linear",
						complete: function(){
							animation.On("dance4",F2ms(9));
						}
					})
					.fadeOut(2000,function(){
						animation.Off();
					});
				},1);
/*				var timer = new Timer();

				var frame = time_limit = 30*20;//30 * 60 * 0.5;
				timer.On(function(){
					if( (frame+=1) < time_limit )return;
					//~~(Math.random()*5) == 1
					frame = 0;
	
					var screen_width = $(window).width() - 32;
					var screen_height= $(window).height()- 32;
					(function(){
						var start_x = ~~(Math.random()*screen_width);
						var start_y = ~~(Math.random()*screen_height);
						var end_x	= ~~(Math.random()*screen_width);
						var end_y	= ~~(Math.random()*screen_height);
	
						animation.On("dance4",F2ms(9));
						tiki.css({left:start_x+"px",top:start_y+"px"})
						.fadeIn(5000,function(){
							animation.On("walk"+AnimeteDirection(end_x-start_x,end_y-start_y),F2ms(15));
						})
						.animate({
							left: end_x+"px",
							top: end_y+"px"
						},{
							duration: Math.sqrt((end_x-start_x)*(end_x-start_x)+(end_y-start_y)*(end_y-start_y)) * 14.2857, // 14.2857 = 1/70 70px = 1sec
							easing: "linear",
							complete: function(){
								animation.On("dance4",F2ms(9));
							}
						})
						.fadeOut(2000,function(){
							animation.Off();
						});
						
					})();
				});

				//var frame = 100;
				//timer.On(function(){
				//	if( (frame+=1) < 60 )return;
				//	frame = 0;
				//	//animation.On("dance"+~~(Math.random()*7+1),F2ms(9));
				//	//animation.On("alert"+~~(Math.random()*7+1),F2ms(5));
				//	animation.On("walk"+~~(Math.random()*7+1),F2ms(15));
				//});
*/
			})();
	
		}//for
		
		function F2ms(a_frame){ return 1/a_frame*1000; }
		function AnimeteDirection(a_x,a_y){ return ~~(Math.abs(Math.atan2(a_x,a_y)-Math.PI)*1.2732395447); } // 1.2732395447 = 1/(Math.PI*2/8)
	
	})();
	
	if( !!exte )exte();
});

function ScrollTo(a_obj,a_duration){
	a_duration = a_duration || 500;
	var _ofs = a_obj.offset();
	var _attr = {scrollLeft:_ofs["left"],scrollTop:_ofs["top"]};
	$("html,body").animate(_attr,a_duration);
};

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};