var id, target, target_old, dl, pattern, obj, cat;
var opAnimTimer;



$(document).ready(function() {

	adjMargin();


	$("#AnythingSlider").anythingSlider({
		width:800, height:400, resizeContents:true, // Appearance options
		startPanel:1, enableKeyboard:false, buildArrows:false, buildNavigation:false, // Navigation options
		autoPlay:false, toggleControls:false, // Slideshow options
		animationTime:1000, easing:"easeInOutExpo" // Global options
	});

	$("#Home").click(function(){           $("#AnythingSlider").anythingSlider(1); });
	$("#AboutUs").click(function(){        $("#AnythingSlider").anythingSlider(2); });
	$("#BusinessDomain").click(function(){ $("#AnythingSlider").anythingSlider(3); });
	$("#Profile").click(function(){        $("#AnythingSlider").anythingSlider(4); });
	$("#Recruit").click(function(){        $("#AnythingSlider").anythingSlider(5); });
	$("#Access").click(function(){         $("#AnythingSlider").anythingSlider(6); });


	$(window).resize(adjMargin);


	$("ul#nav li a:eq(4)").css( {backgroundColor: "#ff902e"} );

	function shiftBoxes(id) {
		var columnCount = 3;
		var rowCount = 3;
		var col = ((id) % rowCount) * -1;
		var row = Math.floor(id / columnCount) * -1;
		target = $("ul#nav li a:eq(" + id + ")").attr("rel");
		if (target != target_old) {
			$("ul#room_frame").stop().animate({left: Math.round(400 * col), top: Math.round(400 * row)}, 1200, "easeOutExpo", function(){
				showQst(target, 0);
				hideObj(target_old, "Qst", 0);
				hideObj(target_old, "Ans", 0);
				target_old = target;
			});
		}
		$("ul#nav li a").css( {backgroundColor: "none", color: "#ff902e", fontWeight: "bold", cursor: "pointer"} );
		$("ul#nav li a:eq(" + id + ")").css( {backgroundColor: "#ff902e", color: "#fff", fontWeight: "bold", cursor: "default"} );
	};


	$("ul#nav li a").click(function(){
		$("ul#nav li a").css({background: "none"});
		navIndex = $("ul#nav li a").index(this);
		if (navIndex <= 8) { shiftBoxes(navIndex); } else { return false; }
	});


	$(".jScrollPane").jScrollPane();


	$("#menuItems").lavaLamp({
		fx: "easeOutBack",
		speed: 500,
		click: function(event, menuItem) { return false; }
	});


	$(".Window01").colorbox({ opacity:0.7, iframe:true, scrolling:false, innerWidth:640, innerHeight:480 });
	$(".Window02").colorbox({ opacity:0.7, iframe:true, scrolling:false, innerWidth:800, innerHeight:480 });

});



/*jQuery(function($){
	$('form').exColorboxForm({ opacity:0.7, width:"834px", height:"514px", overlayClose:true });
});*/



function showForm(target){

	if (!$.cookie("TKaccessControl")) {

		$("ul#room_frame li." + target + " div p.button a").css({display: "none"});
		$("ul#room_frame li." + target + " div p.button").animate({opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div div.text p").animate({opacity: 0}, "normal");
		$("ul#room_frame li." + target + " div div.text").animate({top: 40, opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div div.text").css({display: "none"});
		$("ul#room_frame li." + target + " div.QEmark").animate({opacity: 0}, "normal");
		if (target == "room6") {leftmgn = 148;}
		else if (target == "room7") {leftmgn = 168;}
		else if (target == "room8") {leftmgn = 188;}
		else {leftmgn = 128;};
		$("ul#room_frame li." + target + " div.staff").animate({left: leftmgn}, "normal");
		$("ul#room_frame li." + target + " div.QEshadow").animate({opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div.loginForm").css({display: "block", opacity: 1});
		}); }); });

	}

};



function showQst(target){

	if ($("ul#room_frame li." + target + " div.Qst").css("opacity") == 0) { hideObj(target, "Ans", 0); dl = 0; }
	$("ul#room_frame li." + target + " div.Qst").delay(dl).css({display: "block"}).animate({opacity: 1}, "slow", function(){
	$("ul#room_frame li." + target + " div.Qst div.staff").animate({left: 118, opacity: 1}, "slow", function(){
	$("ul#room_frame li." + target + " div.Qst div.text").css({display: "block"});
	$("ul#room_frame li." + target + " div.Qst div.text p").animate({opacity: 1});
	$("ul#room_frame li." + target + " div.Qst div.text").animate({top: 30, opacity: 1}, "slow", function(){
	$("ul#room_frame li." + target + " div.Qst p.button").animate({opacity: 1}, "fast");
	$("ul#room_frame li." + target + " div.Qst p.button a").css({display: "block"});
	}); }); });

};



function hideObj(target, obj, pattern){

	if (pattern) {
		$("ul#room_frame li." + target + " div." + obj + " p.button a").css({display: "none"});
		$("ul#room_frame li." + target + " div." + obj + " p.button").animate({opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div." + obj + " div.text p").animate({opacity: 0}, "normal");
		$("ul#room_frame li." + target + " div." + obj + " div.text").animate({top: 40, opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div." + obj + " div.staff").animate({left: 128, opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div." + obj).animate({opacity: 0}, "normal", function(){
		$("ul#room_frame li." + target + " div." + obj).css({display: "none"});
		}); }); }); });
	} else {
		$("ul#room_frame li." + target + " div." + obj + " p.button a").css({display: "none"});
		$("ul#room_frame li." + target + " div." + obj + " p.button").css({opacity: 0});
		$("ul#room_frame li." + target + " div." + obj + " div.text p").css({opacity: 0});
		$("ul#room_frame li." + target + " div." + obj + " div.text").css({top: 40, opacity: 0});
		$("ul#room_frame li." + target + " div." + obj + " div.staff").css({left: 128, opacity: 0});
		$("ul#room_frame li." + target + " div." + obj).css({opacity: 0, display: "none"});
		$("ul#room_frame li." + target + " div.loginForm").css({display:"none"});
	}

};



function showAns(target){

	if ($("ul#room_frame li." + target + " div.Ans").css("opacity") == 0) { hideObj(target, "Qst", 1); dl = 2200; } else { dl = 0; }

	$("ul#room_frame li." + target + " div.Ans").delay(dl).css({display: "block"}).animate({opacity: 1}, "slow", function(){
	$("ul#room_frame li." + target + " div.Ans div.staff").animate({left: 118, opacity: 1}, "slow", function(){
	$("ul#room_frame li." + target + " div.Ans div.text").css({display: "block"});
	$("ul#room_frame li." + target + " div.Ans div.text").animate({top: 30, opacity: 1}, "slow");
	$("ul#room_frame li." + target + " div.Ans div.text p").animate({opacity: 1}, "slow");
	$('.jScrollPane').jScrollPane().data('jsp').reinitialise();
	$("ul#room_frame li." + target + " div.Ans p.button").animate({opacity: 1}, "fast");
	$("ul#room_frame li." + target + " div.Ans p.button a").css({display: "block"});
	}); });

};



function resizeText(size) {

	var targetElem = document.getElementById("body");

	if (size == "L") {
		$.cookie("tkadTextSize","L",{ expires: 30 });
		targetElem.style.fontSize = "1.0em";
		targetElem.style.lineHeight = "1.6em";
		$('#textS').removeClass('active');
		$('#textM').removeClass('active');
		$('#textL').addClass('active');
	} else if (size == "M") {
		$.cookie("tkadTextSize","M",{ expires: 30 });
		targetElem.style.fontSize = "0.9em";
		targetElem.style.lineHeight = "1.5em";
		$('#textS').removeClass('active');
		$('#textL').removeClass('active');
		$('#textM').addClass('active');
	} else {
		$.cookie("tkadTextSize","S",{ expires: 30 });
		targetElem.style.fontSize = "0.8em";
		targetElem.style.lineHeight = "1.4em";
		$('#textM').removeClass('active');
		$('#textL').removeClass('active');
		$('#textS').addClass('active');
	}

	$('.jScrollPane').jScrollPane().data('jsp').reinitialise();

};



function adjMargin() {

	var topMgn = Math.max(parseInt(($('html').attr('clientHeight') - 620) / 2), 0);
	var leftMgn = Math.max(parseInt(($('html').attr('clientWidth') - 940) / 2), 0);

	$('#contentBodyWrapper').css({paddingTop: topMgn});
	$('#contentFootWrapper').css({top: topMgn + 540});
	$('#contentBodyInner').css({left: leftMgn});
	$('#contentFootInner').css({left: leftMgn});
}



function postForm(id) {
	var rawPwStr = $("#form" + id).val();
	var hashPwStr = $.sha1(rawPwStr);
	$.cookie("tkadRoomKey", hashPwStr, {path:"/"});
	for (i = 0; i < 4; i ++) {
		$("#form" + i).val(rawPwStr);
	}
}



function gMapAPI() {
	if (GBrowserIsCompatible()) {

		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(35.625565, 139.72531), 17);

		var icon = new GIcon();
		icon.image = "./images/tkpin.png";
		icon.shadow = "./images/tkpinshadow.png";
		icon.iconSize = new GSize(60, 60);
		icon.shadowSize = new GSize(60, 60);
		icon.iconAnchor = new GPoint(13, 55);
		icon.infoWindowAnchor = new GPoint(28, 5);

		var marker = new GMarker(new GLatLng(35.625565, 139.72531), icon);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml('<div align="center"><img src="./images/QRcode.gif" width="183" height="183" alt="QR-Code" /></div>');
		});

		map.addOverlay(marker);

	}
}



function sideA() {

	var timer = setInterval(loop, 1000 / 60);

	var delmin = 2500;

	var elems = [];

	var mtrx = [1,1,1,1,1,0,1,0,1,
							0,0,0,0,0,0,1,0,0,
							1,1,1,1,1,1,1,1,1,
							0,0,1,0,0,0,1,0,0,
							1,0,1,0,1,0,1,0,1,
							1,0,1,0,0,0,1,0,1,
							1,1,1,1,1,0,1,1,1,
							0,0,0,0,0,0,0,0,0,
							0,0,0,0,1,0,0,0,0,
							1,1,1,1,1,1,1,1,1,
							1,0,0,0,0,0,0,0,0,
							1,0,0,0,1,0,0,0,1,
							1,0,0,0,1,1,1,1,1];

	for (var j = 0; j < 13; j++) {
		for (var i = 0; i < 9; i++) {

			if (mtrx[ j * 9 + i ]) {

				var elem = {},
					r = 255,	// #ff
					g = 144,	// #90
					b = 46,	// #2e
					dia = 1000,
					rad = Math.random() * 360,
					sX = dia * Math.cos(rad / 180 * Math.PI) + 400,
					sY = dia * Math.sin(rad / 180 * Math.PI) + 200,
					dX = i * 16 + 328,
					dY = j * 16 + 107;

				var target = document.getElementById('target');

				var domElement = document.createElement('div');
					domElement.style.left = sX + 'px';
					domElement.style.top = sY + 'px';

				elem.domElement = domElement;
				elem.param = 0;
				elem.x = sX;
				elem.y = sY;
				elem.delay = Math.round(Math.random() * 2500);

				if (delmin > elem.delay) { delmin = elem.delay; }

				var first = new TWEEN.Tween(elem)
					.to({ param: 10, x: dX, y: dY }, 2500)
					.delay(elem.delay)
					.onUpdate(function() {
						if (this.param > 9) { g = 255; b = Math.round((this.param - 9) * 255); }
						else if (this.param <= 7) { g = 0; b = 0; }
						else { g = Math.round((this.param - 7) * 128); b = 0; }
						this.domElement.style.left = Math.round(this.x) + 'px';
						this.domElement.style.top = Math.round(this.y) + 'px';
						this.domElement.style.backgroundColor = 'rgb(255,' + g + ',' + b + ')';
//						this.domElement.innerText = this.delay;
					})
					.easing(TWEEN.Easing.Sinusoidal.EaseOut)
					.start();

				target.appendChild(elem.domElement);

				elems.push(elem);

			}

		}

	}


	var second = new TWEEN.Tween()
		.delay(2500 - delmin)
		.onUpdate(function(){
			clearInterval(timer);
			sideAsecond();
		});


	first.chain(second);

}



function sideAsecond() {

	var i = 1;
	$("#TKLogo").animate({opacity: 1}, "slow", function(){
		$("#TKLogoTxt").animate({opacity: 1}, "slow", function(){
			$("#sideB a").css({display: "block"});
			$("#sideB").animate({top: 0, opacity: 1}, 1000, "easeOutQuart", function move(){
				i = i > 0 ? -1 : 1;
				$("#sideB").animate({top: i * 4}, { duration: 1000, complete: move });
			});
		});
	});

	$.cookie("tkadSession", true);

}



function loop() {

	TWEEN.update();

}

