// JavaScript Document
$(document).ready(function(){
// CALENDER
$(function() { 
	$("#datepicker").datepicker(function (date,obj,targetId){
		$.getJSON('/ajax.php?do=calendar&date='+date,function (json){
			var ulObj=$('<ul></ul>');
			var num=0;
			if(json.item!=null&&json.item.length>0){
				for(var i=0;i<json.item.length;i++){
					num+=1;
					var liObj=$('<li></li>');
					var linkObj=$('<a href="'+json.item[i].url+'" target="_blank"></a>');
					linkObj.append('<p class="time">'+json.item[i].ctime+'</p>');
					linkObj.append('<p class="duration">'+json.item[i].duration+'</p>');
					linkObj.append('<p class="event">'+json.item[i].title+'</p>');
					liObj.append(linkObj);
					ulObj.append(liObj);
				}
			}
			if(num>0){
				$('#datepicker').fadeOut(300,function (){
					$('#calendarItem').html('');
					$('#calendarItem').append(ulObj);
					$('#calendarDiv').fadeIn(300);
				})
			}
		});
	},function (date){
		$.getJSON('/ajax.php?do=calendarNum&m='+date,function (json){
			if(json.item!=null&&json.item.length>0){
				for(var i=0;i<json.item.length;i++){
					if(json.item[i].num>0){
						var d=json.item[i].date;
						$('#datepicker #'+d).addClass('hasCalendar');
					}
				}
			}
		});
	});	
});
$('#calendarClose').click(function (){
	$('#calendarDiv').fadeOut(300,function (){
		$('#datepicker').fadeIn(300);
	});
});

//SMOOTH SCROLL from CSS Tricks
$(function(){
    $('a.gotarget[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});

// LOADING
function loadingShow(obj){
	var loading = $("div.loading");
	var holder = obj.parents("div.loadingHolder").height();
	loading.css("opacity","0.9");
	loading.height(holder);
	loading.fadeIn(500);
	return true;
}
function loading() {
	$("div.loadingHolder").append("<div class='loading'></div>");
	$(".goNxt").click( function() {
		if(!$(this).attr('dontGoNxt'))
			return loadingShow($(this));
	});
}
$(function() { loading(); });


// LOG PANEL
var fullHeight = $(document).height()
var transbg = $("<div class='trans'></div>");

//var n = $("#banner img").length;
transbg.css("height", fullHeight).css("opacity", "0.75");
$("body").append(transbg);


//$("#topNav a:has(span)").click(function(){
//	transbg.fadeIn(400);
//	$("#popWrapper").fadeIn(200);
//	return false;
//	});
transbg.click(function(){
	$("#popWrapper").hide();
	$("#contactDetail").hide();
	$("div.panelholder").hide();
	$("div.loading").hide();
	$("#contactDetail .panelDetailWrapper div").hide();
	$('#contactDetail #tabCtrl li').removeClass("tabNow");
	$(this).fadeOut(400);
	});
$("#loginDetail a.panelOpen").click(function(){
	$("#popWrapper").hide();
	transbg.fadeOut(400);
	return false;
	});

$("#loginBtn").click(function(){
	transbg.fadeIn(400);
	$("#popWrapper").fadeIn(200);
	return false;
	});

//SIGNUP
$("#pInfo a.button").click(function(){
	transbg.fadeIn(400);									  
	$("#signupPop").fadeIn(200);
	return false;
	});

$("li.signupAction input.cancel").click(function(){
	$("div.fnpSignup").hide();
	$("div.loading").hide();
	transbg.fadeOut(400);
	});



// MENU
$("ul#menu a").mouseover(function(){
				$(this).parent().find("ul").fadeIn(300);
				$(this).parent().hover(function(){
					}, function(){
						$(this).parent().find("ul").hide();
					});
			});

$("ul#menu li ul").hover(function(){
	$(this).parent().addClass("hoverNow");
	}, function(){
		$(this).parent().removeClass("hoverNow");
	});

// FUNCTION TAB

$('ul#functionTab li a').click(function(){
	$('ul#functionTab li').removeClass("ftNow");
	$(this).parent().addClass("ftNow");
	var currentTab = $(this).attr("href");
	$("div.functionPanel").hide();
	$(currentTab).fadeIn(500);
	return false;
	});
// Recent Activity
$("#infoSec ul li:last").css("borderBottom","none");

$("li.tableMain p:odd").addClass("tableOdd");

//Course
$(".secList").each(function (){
	$(this).find("li.classContent:odd").addClass("classTdOdd");
})

$("a.classOrder").click(function(){
	transbg.fadeIn(400);									  
	if($('#courseJoinForm').find(':hidden[name=mid]').val()>0){
		loadingShow($('#courseJoinForm input[name=joinMember]'));
		$('#courseJoinForm').ajaxSubmit(myeval);
		$("#courseSignupDone").fadeIn(200);
	}else{
		$("#courseSignupNon").fadeIn(200);
	}
	return false;
	});

$("a.close").click(function(){
	$(this).parents("div.panelholder").hide();
	$("div.loading").hide();
	transbg.fadeOut(400);
	return true;
	});

$("a.contactMe").click(function(){
	transbg.fadeIn(400);									  
	$("#contactMe").fadeIn(200);
	return false;
	});

$("a.askQ").click(function(){
	transbg.fadeIn(400);									  
	$("#askQ").fadeIn(200);
	return false;
	});

//Forget Password
$("a.forgetPW").click(function(){
	$(".panelholder").hide();
	$("#popWrapper").hide();
	$("#forgetPw").fadeIn(400);
	return false;
	});

//Recruit List

//PRODUCT 3-1
$('a[id^=notifyMeCourse]').click(function (){
	transbg.fadeIn(400);									  
	$("#notifyMeCoursePop").fadeIn(200);
	return false;
})

$("div.steps a").toggle(function(){
	$(this).addClass("closepanel");
	var openPanel = $(this).attr("href");
	$(openPanel).slideDown();
	return false;
	}, function(){
		$(this).removeClass("closepanel");
		var openPanel = $(this).attr("href");
		$(openPanel).slideUp();
		return false;
	});

// Equalheight function by Rob Glazebrook @ www.cssnewbie.com
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

equalHeight($("#secLeft.howtheysay ul.qlist li p")); // set equal height

// How They Say
$("#secLeft.howtheysay ul.q1 li:nth-child(4n)").addClass("graybg");
$("#secLeft.howtheysay ul.q1 li:nth-child(4n+3)").addClass("graybg");
$("#secLeft.howtheysay ul.q2 li:odd").addClass("graybg");

// Mindgame
$('div.adp a').click(function(){
	$(this).parent().hide();
	var adNow = $(this).attr("href");
	$(adNow).fadeIn();
	return false;
	});
$('.mindgame a.close').click(function(){
	$(this).parent().hide();
	$(this).parent().siblings().fadeIn();
	return false;
	});

//Control Font Size
$('a#sp').click(function(){
	SetCookie('fontSizeId','sp');
	$('div#fontSize a').removeClass();
	$('#secLeft').removeClass("fontl");
	$('#secLeft').removeClass("fontm");
	$(this).addClass("sizeNow");
	return false;
	});
$('a#mp').click(function(){
	SetCookie('fontSizeId','mp');
	$('div#fontSize a').removeClass();
	$('#secLeft').removeClass("fontl");
	$(this).addClass("sizeNow");
	$('div#secLeft').addClass("fontm");
	return false;
	});
$('a#lp').click(function(){
	SetCookie('fontSizeId','lp');
	$('div#fontSize a').removeClass();
	$('#secLeft').removeClass("fontm");
	$(this).addClass("sizeNow");
	$('div#secLeft').addClass("fontl");
	return false;
	});

//Forum
$('.fList table tbody tr:odd').addClass("odd");

//Forum Topic
var replyPost = $('#secLeft.fTopic table tbody a.reply:not(:last)');
var replyField = $('#replyForm');
$(replyPost).click(function(){
	$(replyField).detach();
	var insertTarget = $(this).attr("href");
	$(this).parents().find(insertTarget).after(replyField);
	return true;
	});
$('#secLeft.fTopic table tbody a.reply:last').click(function(){
	$(replyField).detach();
	$(replyField).appendTo('#secLeft.fTopic table tfoot');
	return false;
	});

//bookDetail
$('a.changepay').click(function(){
	$(this).parent().hide();
	var changepay = $(this).attr("href");
	$(changepay).fadeIn(400);
	return false;
	});

//JOB
$("#tp5").fadeIn(400);
$("#jobpolicy a").click(function(){
	$("#jobpolicy li").removeClass("panelNow")
	$(this).parent().addClass("panelNow");
	var openPanel = $(this).attr("href");
	$(".jbtp").hide();
	$(openPanel).fadeIn(400);
	return false;
	});

//Contact Panel
$("a#pContact").click(function(){
	transbg.fadeIn(400);
	$("#contactDetail").fadeIn(200);
	$('#contactDetail #tabCtrl li:first').addClass("tabNow");
	$("#ct1").slideDown(300);
	return false;
	});
$("#contactDetail a.panelOpen").click(function(){
	$("#contactDetail").hide();
	transbg.fadeOut(400);
	return false;
	});
$("#contactDetail a.close").click(function(){
	$("#contactDetail").hide();
	$("#ct2").hide();
	$('#contactDetail #tabCtrl li').removeClass("tabNow");
	transbg.fadeOut(400);
	return false;
	});
$('#contactDetail #tabCtrl li a').click(function(){
	$('#contactDetail #tabCtrl li').removeClass("tabNow");
	$(this).parent().addClass("tabNow");
	var contactNow = $(this).attr("href");
	$("#contactDetail .panelDetailWrapper div").hide();
	$(contactNow).slideDown(400);
	return false;
	});
//$('#ct2 label').hover(function(){
//	$(this).addClass("focus");
//	}, function(){
//		$(this).removeClass("focus");
//	});
$('#ct2 label').click(function(){
	$(this).addClass("focus");
	$(this).parent().find("input").focus();
	});
$('#ct2 input').focus(function(){
		$(this).parent().find("label").addClass("focus");
		})
		.blur(function(){
		if ($(this).val() == "") {
			$(this).parent().find("label").removeClass("focus");
		}
		});

// Search Recommend
$('#searchRecommend ul li').hover(function(){
	$(this).css("backgroundColor", "#FFC")
		   .css("cursor", "pointer");
	}, function(){
		$(this).css("backgroundColor", "#FFF")
			   .css("cursor", "normal");
	});

//IE 6
$('#upclose').click(function(){
	$('#alert').slideUp(400);
	return false;
	});

// swfobject
	var flashvars = {};
	var params = {};
	params.menu = "false";
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF("img/index.swf", "frontpage", "960", "320", "9.0.0", false, flashvars, params, attributes);

// No copy
var message="";

function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")

// Button Disabled
$('#profile li.rowFunc span.notify').css("opacity","0.6");
$('span.classOrder').css("opacity","0.6");
	
//BANNER
$('#banner').cycle({ 
    fx:    'fade', 
    speed:  3000,
	timeout: 6000
 });

//GROUPBUY
$("a[id^=groupbuy]").click( function() {
	transbg.fadeIn(400);
	$('#groupBuy #groupBuyProdid').val($(this).attr('prodid'));
	$("#groupBuy").fadeIn(200);
	return false;
});



})
