/*////////////////////////////////////////////////////////////////////////////////*/
/* vars */
/*////////////////////////////////////////////////////////////////////////////////*/

jQuery.noConflict();
var $ = jQuery;	
window.lng = "e";
window.tgt = "all";
window.isStart = true;
window.tgtURL = null;
window.ajxH = 0;

/*////////////////////////////////////////////////////////////////////////////////*/
/* ready */
/*////////////////////////////////////////////////////////////////////////////////*/

$(function($){
	$("#page").css('opacity','0');

	//reshapre
	smartColumns();

	//set menu
	$("#menu_"+tgt).addClass("toggle_active");

	//set language
	toggleLng();
	$("#"+lng).addClass("toggle_active");		

	//change language
	$('.lng').click(function(){
		if(!isAnimated()) {
			tgt_lng = $(this).attr('id');
			if(tgt_lng != lng){
				$("#"+lng).removeClass("toggle_active");
				lng = tgt_lng;
				$("#"+lng).addClass("toggle_active");
				toggleLng();
			}
		}
		return false;
	});

	//spcify category
	$('.cat_toggle').click(function(){
		if(!isAnimated()){
				$('.block a').not($("."+tgt+" a")).not('.menu a').data('disabled','false');
			nextC =  $(this).html().toLowerCase();
			if(nextC == 'all'){
				$('.block a').not('.menu a').data('disabled','false');
				$("#menu_"+tgt).removeClass("toggle_active");
				tgt = "all";
				$("#menu_"+tgt).addClass("toggle_active");
				$('.block').animate({'opacity':'1'},{queue:false},8000);									
				$('body').animate({backgroundColor:'#ddd'},{queue:false}, 8000);			
			}else {
				var tgtC = '#c6c6c6';				
				$("#menu_"+tgt).removeClass("toggle_active");
				tgt = $(this).html().toLowerCase();
				$("#menu_"+tgt).addClass("toggle_active");
				if (tgt != "all"){
					$('.block').not('.menu').animate({'opacity':'1'},{queue:false},8000);
				}								
				$('.block a').not($("."+tgt+" a")).not('.menu a').data('disabled','true');
				$('.block').not($("."+tgt)).not('.menu').animate({'opacity':'0.2'},{queue:false},8000);
				$('body').animate({backgroundColor:tgtC},{queue:false},8000);
			}
		}			
		return false;
	});

	//hover post
	$('.mask').hover(function(){
		isON = $(this).children("a").data('disabled');
		if(isON == 'false' || isON ==null){
			$(this).data('orgOpac',$(this).css('opacity'));
			var op = $(this).css('opacity');
			$(this).animate({'opacity':'0.5'},{queue:false},200);
		}
	},function(){
		isON = $(this).children("a").data('disabled');
		if(isON == 'false' || isON ==null){
			var op = $(this).css('opacity');
			$(this).animate({'opacity': '1'},{queue:false},200);
		}		
	});

	/*
	//hover post
	$('.block').not('.menu').not('.copyright').hover(function(){
			$(this).data('orgOpac',$(this).css('opacity'));
			$(this).css('opacity','0.7');
	},function(){
			$(this).css('opacity', $(this).data('orgOpac'));
	});
	*/


	//load Ajax
 	$("a").not(".cat_toggle").not(".lng").click(function(){
		if(!isAnimated()){
			if(tgtURL != $(this).attr('href')){
				if($(this).data('disabled') != 'true'){
					//when closed
					if($("#ajx").css('display') != "block"){
						tgtURL = $(this).attr('href');
						if($(window).scrollTop() > 0){
							$('body').scrollTo( $('#top'), 1000, function(){
								$('#ajx').animate({ height : 0 }, 1000, function(){loadAjx();});
							});
						}else{
							loadAjx();					
						}
					}else{
						tgtURL = $(this).attr('href');
						if($(window).scrollTop() > 0){
							$('body').scrollTo( $('#top'), 1000, function(){
								$('#ajx').animate({ height : 0 }, 1000, function(){loadAjx();});
							});
						}else{
							$('#ajx').animate({ height : 0 }, 1000, function(){loadAjx();});					
						}
					}
				}
			}
		}
		return false;
	});
	
	$(window).scroll(function(){
		if($(window).scrollTop() > 0){
			$(".top").fadeIn();
		}else{
			$(".top").fadeOut();		
		}		
	});
	
	
});

function loadAjx(obj){
	$.ajax({
		async:true,
		type: "GET",
		url: tgtURL,
		dataType: "html",
		cache: false,
		success: function(html){
			var $div = $('#ajx');
			$div.html(html);
		},
		error: function(xhr, status, errorThrown){
			alert("error");
		}
					
	});
	
}
/*////////////////////////////////////////////////////////////////////////////////*/
/* isAnimated */
/*////////////////////////////////////////////////////////////////////////////////*/

function isAnimated(){
	if($(":animated").size()) return true;
}

/*////////////////////////////////////////////////////////////////////////////////*/
/* toggleLng */
/*////////////////////////////////////////////////////////////////////////////////*/

function ajaxLng() {	
	if(lng=="j"){
		$(".desc_e").hide();
		$(".desc_j").show();	
	} else {
		$(".desc_j").hide();	
		$(".desc_e").show();	
	}
}

/*////////////////////////////////////////////////////////////////////////////////*/
/* toggleLng */
/*////////////////////////////////////////////////////////////////////////////////*/

function toggleLng(){
	if(isStart ==false){
		$("#page").animate({opacity:0},function(){
			var tid=setTimeout("reshape()");
			if(lng=="j"){
				$(".desc_e").hide();
				$(".desc_j").show();	
			} else {
				$(".desc_j").hide();	
				$(".desc_e").show();	
				}
			$("#page").animate({opacity:1});
			
		});
	}else {
		isStart = false;
		if(lng=="j"){
			$(".desc_e").hide();
			$(".desc_j").show();	
		} else {
			$(".desc_j").hide();	
			$(".desc_e").show();	
			}
	}
}

/*////////////////////////////////////////////////////////////////////////////////*/
/* smartColumns */
/*////////////////////////////////////////////////////////////////////////////////*/

function smartColumns() {
	$("#content").css({ 'width' : "100%"});	
	var colWrap = $("#content").width();
	var colNum = Math.floor(colWrap / 190);
	var colFixed = Math.floor(colWrap / colNum);
	$("#content").css({ 'width' : colWrap});
	$(".blockwrap").css({ 'width' : colFixed});

}	

/*////////////////////////////////////////////////////////////////////////////////*/
/* reshape */
/*////////////////////////////////////////////////////////////////////////////////*/

function reshape(){	
	smartColumns();
	$('#content').masonry();
}

/*////////////////////////////////////////////////////////////////////////////////*/
/* reshape when resize */
/*////////////////////////////////////////////////////////////////////////////////*/

$(window).resize(function () {
	smartColumns();
	$('#content').masonry();

});

function initWin(){
	smartColumns();
	$('#content').masonry();
	$("#page").css('opacity','1');

}

