$(function(){
	/* --- Swicth Table Cell --- */
	$('body').on('click','.select-tb-switch',function(){
		var selectWrap = $(this).closest(".select-tb-wrap");
		if($(this).hasClass("actived")){
			$(this).removeClass("actived");
		} else {
			selectWrap.find(".select-tb-switch").removeClass("actived");
			$(this).addClass("actived");
		}
	});

	switchIntTable = function () {
		$(".select-tb-wrap").find(".select-tb-switch:nth-child(2)").attr("data-current",1);
		$(".select-tb-wrap").find(".select-tb-switch:nth-child(3)").attr("data-current",2);
		$(".tb-box-switch").each(function(){
			var cntCol = $(this).find("th").size();
			var rows = $(this).find("tr");

			rows.each(function() {
				cols = $(this).children('th, td');
				for(i=1;i<=cntCol;i++) {
					cols.filter(":eq("+i+")").attr("data-order",i);
				}
			});
		});
	}

	resetColumn = function (table) {
		var cntCol = table.find("th").size();
		var rows = table.find("tr");

		$(".select-tb-wrap").find(".select-tb-switch:nth-child(2)").attr("data-current","1");
		$(".select-tb-wrap").find(".select-tb-switch:nth-child(3)").attr("data-current","2");

		$(".select-tb-wrap").find(".select-order > li").removeAttr("class");
		$(".select-tb-wrap").find(".select-order > li[data-select='1']").addClass("hide");
		$(".select-tb-wrap").find(".select-order > li[data-select='2']").addClass("hide");

		rows.each(function() {
			cols = $(this).children('th, td');
			cols.filter("[data-order]").removeClass("actived");

			for(i=1;i<=cntCol;i++) {
				cols.filter("[data-order="+i+"]").detach().appendTo($(this));
				if(i == 1 || i == 2) {
					cols.filter("[data-order="+i+"]").addClass("actived");
				}
			}
		});
	}

	switchColumn = function(table,target,put) {

		var rows = table.find("tr");

		rows.each(function() {

			cols = $(this).children('th, td');

			cols.filter("[data-order="+put+"]").detach().insertBefore(cols.filter("[data-order="+target+"]"));

		});
	}

	setWidthSelect = function() {
		$(".tb-box-switch").each(function(){
			if($(this).prev(".select-tb-wrap").size() > 0) {
				var w1 = $(this).find("td.actived:eq(0)").width();
				var w2 = $(this).find("td.actived:eq(1)").width();
				var w3 = $(this).find("td.actived:eq(2)").width();

				$(this).prev(".select-tb-wrap").find("div:nth-child(1)").width(w1);
				$(this).prev(".select-tb-wrap").find("div:nth-child(2)").width(w2);
				$(this).prev(".select-tb-wrap").find("div:nth-child(3)").width(w3);
			}
		});
	}

	$('body').on('click','.select-tb-switch li',function(){
		var select_wrap = $(this).closest(".select-tb-wrap");
		var val_select = $(this).attr("data-select");
		var select_switch = $(this).closest(".select-tb-switch");
		var val_target = select_switch.attr("data-current");
		var val_put = val_select;
		var data_relate = select_wrap.attr("data-relate");

		select_switch.attr("data-current",val_select);
		select_wrap.find(".select-tb-switch li").removeAttr("class");

		$("."+data_relate+" tr").find("th.actived:not(:first-child)").removeClass("actived");
		$("."+data_relate+" tr").find("td.actived:not(:first-child)").removeClass("actived");

		select_wrap.find(".select-tb-switch").each(function(){
			var current_choose = $(this).attr("data-current");
			select_wrap.find(".select-tb-switch li[data-select='"+current_choose+"']").addClass("hide");

			$("."+data_relate+" tr").find("th[data-order="+current_choose+"]").addClass("actived");
			$("."+data_relate+" tr").find("td[data-order="+current_choose+"]").addClass("actived");

			switchColumn($("."+data_relate),val_target,val_put);

		});

		setWidthSelect();

	});

	timeResize = "";

	if($(".select-tb-wrap").size() > 0) {
		switchIntTable();
		setWidthSelect();
	}


	$(window).resize(function(){
			timeResize = setTimeout(function(){
				clearTimeout(timeResize);
				var w = $(window).width();

				if(w >= 1000) {
					$(".tb-box-switch").each(function(){
						resetColumn($(this));
					});
				}

				setWidthSelect();

			},100);
	});

	$(document).on('click touchend',function(e){
		/* --- CLEAR CAPTCHA FORM --- */
		if(!$(e.target).closest('.select-tb-switch').length && $('.select-tb-switch').hasClass('actived')){
			$('.select-tb-switch.actived').removeClass('actived');
		}
	});

});
