var Fnc = {};

Fnc.RollOver = function(options){
	$(options.rollover).each(function(){
		
		$(this).find("img").each(function(){
			
			var roll_img = $(this);
			
			var preload_img = new Image;
			
			preload_img.src = roll_img.attr('src').replace(/(\.[^\.]+$)/,"_o$1");
			
			roll_img.mouseover(function(){
				if(!roll_img.is(options.unroll)){
					roll_img.attr('src', roll_img.attr('src').replace(/(\.[^\.]+$)/,"_o$1"));
				}
			});
			
			roll_img.mouseout(function(){
				if(!roll_img.is(options.unroll)){
					roll_img.attr('src', roll_img.attr('src').replace(/_o/, ""));	
				}
			});

		});
	});
};

Fnc.MainVisual = function(options){
	
	var that = this;
	
	var mainvisual = $(options.mainvisual);	
	var contents = mainvisual.find(options.contents);
	var thumbnails = $(options.thumb).children();
	var invisible = options.invisible;
	var speed = options.speed;
	var interval = options.interval;
	
	var now_visual = 0;
	var next_contents = 1;
	
	var animation_1 = false;
	var animation_2 = false;
	var thumb_click = false;
	
	function thumb_active(){
		$(thumbnails).each(function(){
			
			if(now_visual === $(thumbnails).index(this)){
				var img = $(this).find('img').get(0);	
				$(img).removeClass('unroll');
				$(img).attr('src', $(img).attr('src').replace(/_o/, ""));	
			}
			
			if(next_contents === $(thumbnails).index(this)){
				var img = $(this).find('img').get(0);	
				$(img).addClass('unroll');
				if(!$(img).attr('src').match(/_o/)){
					$(img).attr('src', $(img).attr('src').replace(/(\.[^\.]+$)/,"_o$1"));
				}
			}
		});
	}
	
	$(thumbnails).each(function(){
		
		$(this).click(function(){
			
			if ((animation_1 === false && animation_2 === false) && ($(thumbnails).index(this) !== now_visual)) {

				next_contents = $(thumbnails).index(this);
	
				$(contents[now_visual]).stop();
				$(contents[next_contents]).stop();
				
				thumb_click = true;
				
				clearTimeout(that.runTimer);
				thumb_active();
				visial_anime();
			}
		return false;
		});
	});
	
	function visial_anime(){
		
		thumb_active();
		
		animation_1 = true;
		animation_2 = true;
		
		$(contents[now_visual]).removeAttr('style');
		$(contents[next_contents]).removeAttr('style');
		
		$(contents[next_contents]).removeClass(invisible);	
		$(contents[next_contents]).css({
			opacity: 0
		});
		
		$(contents[now_visual]).animate({
			opacity: 0
		}, {
			duration: speed,
			easing: "easeInCubic",
			complete: function(){		
				$(contents[now_visual]).addClass(invisible);
				now_visual = next_contents;
				animation_1 = false;
				thumb_click = false;
				anime_repeater();
			}
		}, false);
		
		$(contents[next_contents]).animate({
			opacity: 10
		}, {
			duration: speed,
			easing: "easeInCubic",
			complete: function(){
				animation_2 = false;
			}
		}, false);
	}
	

	function anime_repeater(){
		
		if (!thumb_click) {
			next_contents = now_visual + 1;
			
			if(next_contents === contents.length){
				next_contents = 0;
			}
	
			that.runTimer = setTimeout(function(){
				visial_anime();
				clearTimeout(that.runTimer);
			}, interval);
		}
	}
	
	anime_repeater();

}

Fnc.SetHeight = function(options){

	var eq_elms = $('.' + options.classname).children();
	var childs_length = eq_elms.length;

	if($('#size-checker').length === 0){
		$('<span id="size-checker">&nbsp;</span>').appendTo("body");
		$('#size-checker').css({left : '-9999px' , top : '0' , position : 'absolute'});
	}
	var height_origin = $('#size-checker').height();

	var height_now;

	function compare(a, b) { return(b - a);}
	
	function group_equalize(){

		var counter = 0;

		var limit = options.group;

		var remainder = childs_length % options.group;

		var group_loop;

		if(remainder !== 0){
			group_loop = Math.floor(childs_length / options.group) + 1;
		}else{
			group_loop = childs_length / options.group;
		}

		for (var i=0; i<group_loop; i++) {
			
			var height_array = [];

			if(limit > childs_length || limit === childs_length){
				limit = childs_length;
			}

			for (var j=counter; j<limit; j++) {
				$(eq_elms[j]).css({height: ''});
				height_array.push($(eq_elms[j]).height());
			}
	
			height_array.sort(compare);

			for (j=counter; j<limit; j++) {
				$(eq_elms[j]).css({height: height_array[0] + 'px'});
			}

			counter = counter + options.group;
			limit = counter + options.group;
		}
	}
	
	function height_setting(){

		if (!options.group) {
			var height_array = [];
			
			eq_elms.each(function(){
				$(this).css({height: ''});
				height_array.push($(this).height());
			});
							
			height_array.sort(compare);
		
			eq_elms.each(function(){	
				var num = 0;
				if(options.type === 'type-02'){
					if($(this).css('paddingTop') !== '0px'){
							num = num + Number($(this).css('paddingTop').replace('px'));
					}else if($(this).css('paddingBottom') !== '0px'){
							num = num + Number($(this).css('paddingBottom').replace(/px/,''));
					}
				}
				$(this).css({height: height_array[0] - num + 'px'});

			});
		}else{

			group_equalize();
		}
	}

	height_setting();

	setInterval(function(){
		height_now = $('#size-checker').height();
		
		if(height_origin !== height_now){
			height_origin = height_now;
			height_setting();
		}
	},1000);
};

Fnc.SetStay = function(options){
	
	var navi = $(options.navi);
	var hierarchy = options.hierarchy;

	var now_directory = location.pathname.replace(/[^\/]+.[^\.]+$/,"").split('/')[hierarchy];
	
	if(now_directory === ''){
		return false;
	}
	
	navi.find('.parent').each(function(){
		var link = $(this).find('a').get(0);
		var directory = $(link).attr('href').replace(/[^\/]+.[^\.]+$/,"").split('/')[hierarchy];
				
		if(now_directory === directory){
			var btn = $(link).find('img').get(0);
			$(btn).addClass('unroll');
			$(btn).attr('src', $(btn).attr('src').replace(/(\.[^\.]+$)/,"_c$1"));
		}
	});
};

Fnc.pageTop = function(options){
	var btns = $(options.btn);
	
	btns.click(function(){
		
		var scroll_amount_top = $(window).scrollTop();
		
		if(scroll_amount_top !== 0){
			$('html,body').animate({
				scrollTop: 0
			}, {
				duration: 'slow',
				easing: "easeInOutExpo",
				complete: function(){
				}
			}, false);
		}

		return false;
		
	});
}

Fnc.ClickableCnts = function(options){
	var cnts = $(options.cnts);
	
	cnts.each(function(){
		
		var obj = $(this);
		
		var link = obj.find('a').get(0);
		
		obj.mouseover(function(){
			obj.addClass('on');
			obj.css({cursor : 'pointer'});
		});
		
		obj.mouseout(function(){
			obj.removeClass('on');
		});
		
		obj.click(function(){
			window.location = link;
		});
		
		var childs = obj.find('*');
		
		if(childs.is('.roll')){
			var img = cnts.find('.roll img');
			
			obj.mouseover(function(){
				
				if (!img.is('.unroll')) {
					img.attr('src', img.attr('src').replace(/(\.[^\.]+$)/, "_o$1"));
					img.addClass('unroll');
				}
			});
			obj.mouseout(function(e){
				
				var out_check = false;
				
				for (var i=0; i<childs.length; i++) {
					
					if(childs[i] === e.relatedTarget){
						out_check = true;
					}
					
				}
				
				if(!out_check){
					img.removeClass('unroll');
					img.attr('src', img.attr('src').replace(/_o/, ""));	
				}
			});			
		}		
	});	
}

Fnc.AnswerDisplay = function(){
	$(document).ready(function(){
		$('#answer .disp-contents').each(function(){

			var display = false;
			
			var container = $(this);
			var btn_open = $(container.find(".btn-open").get(0));
			var btn_close = $(container.find(".btn-close").get(0));
			var contents = $(container.find(".answer-inner").get(0));
			
			var contents_height = contents.height();
			
			if(contents_height < 100){
				contents_height = 100;
			}
			
			contents.css({
				height : 0,
				visibility : 'hidden'
			});
			
			btn_open.click(function(){
				
				if(display){
					return false;
				}
					
				contents.css({
					visibility : 'visible'
				});
									
				contents.animate({
					height : contents_height
				}, {
					duration: 300,
					easing: 'linear',
					complete: function(){
						display = true;
					}
				}, false);
				
				return false;
			});
			
			btn_close.click(function(){
				
				if(!display){
					return false;
				}
				
				contents.animate({
					height : 0
				}, {
					duration: 300,
					easing: 'linear',
					complete: function(){alert
			contents.css({
				visibility : 'hidden'
			});				
						display = false;
					}
				}, false);
				
				return false;
			});

		});

		var hash = window.location.href.split('#')[1];
	
		if(hash === 'application'){
			var application_top = $('#application').position().top;
			$('html,body').scrollTop(2250);
		}
	});
}

Fnc.SmoothScroll = function(options){
	
	var btn = $(options.btn);
	
	btn.click(function(){
		
		var target = $($(this).attr('href'));

		var target_top = Math.floor(target.position().top + Number(target.css('marginTop').replace(/px/,'')));
			
		var screen_height = document.documentElement.clientHeight;
		var contents_height = document.body.scrollHeight;

		if(contents_height - target_top < screen_height){
			target_top = contents_height - screen_height;
		}

		$('html,body').animate({
			scrollTop: target_top
		}, {
			duration: 2000,
			easing: "easeInOutExpo",
			step : function(){
			},
			complete: function(){	
			}
		}, false);
		
		return false;
		
	});	
}

$(document).ready(function(){

	Fnc.RollOver({rollover : '.roll' , unroll : '.unroll'});
	Fnc.SetHeight({classname : 'equalize'});
	Fnc.SetHeight({classname : 'equalize-01'});
	Fnc.SetHeight({classname : 'equalize-02'});
	Fnc.SetHeight({classname : 'equalize-03'});
	Fnc.SetHeight({classname : 'equalize-type-02-01', type : 'type-02'});
	Fnc.SetStay({navi : '#global-navi' , hierarchy : 2});
	Fnc.pageTop({btn : '.pagetop'});
	Fnc.pageTop({btn : '.pagetop2'});
	Fnc.ClickableCnts({cnts : '.clickable'});
	Fnc.SmoothScroll({btn : '.anchor'});

	Fnc.MainVisual({
		mainvisual : '#main-visual',
		contents : '.contents',
		thumb : '#mv-thumb-list',
		invisible : 'invisible',
		speed : 700,
		interval : 6000
	});

});


