// JavaScript Document
$(document).ready(function() {
	
	
	// top pull down
	
	$('li.mm_s2 > ul').css('opacity',0);
	
	$('li.mm_s2').hover(
		function() { $(this).children('ul').stop(true,false).fadeTo(150, 1); },
		function() { $(this).children('ul').stop(true,false).delay(300).fadeTo(150, 0).hide(0); }
	);
	
	
	
	
	
	// tab
	var nowHitIndx=0;
	var nowHitIndx2=0;
	
	// mouse Event
	function setMouseAction(pn){
		
		var infoNum = pn;
		var nHitIndex;
		if(infoNum==1){
			nHitIndex=nowHitIndx;
		}else{
			nHitIndex=nowHitIndx2;
		}
		var eachObj = $('div#info'+infoNum+' div.if_tub a');
		
		eachObj.each(function(i) {
			
			
			// not Hit
			if(i!=nHitIndex){
				
				// click
				$(this).click(function(){
					var parentObj=$(this).parent();
					var indx = parentObj.parent().children().index(parentObj);
					
					// change Tab HTML
					changeTab(indx, infoNum);
					return false;
				});
				
				// Reset mouse over
				var childImg=$(this).children('img');
				$(this).hover(
					function(){
						childImg.attr("src", childImg.attr("src").replace("_out.", "_over."));
					},
					function(){
						childImg.attr("src", childImg.attr("src").replace("_over.", "_out."));
					}
				);
				
			
			// new Hit Tab
			}else{
				
				$(this).click(function(){
					return false;
				});
			}
			
		});
		
	}
	
	
	// tab @tabNum, @parentNum
	function changeTab(n,pn){
		var parentNum=pn;
		var nhit;
		if(parentNum==1){
			nhit=nowHitIndx;
		}else{
			nhit=nowHitIndx2;
		}
		var tabObj = $('div#info'+parentNum+' div.if_tub');
		
		if(nhit!=n){
			
			var clickedIndex=n;
			var childImg;
			var $this;
			
			tabObj.children().children().each(function(i) {
				
				$this=$(this);
				
				if(i==clickedIndex){
					
					childImg=$this.children('img');
					if(childImg.attr('src').lastIndexOf("_out.gif")!=-1){
					  var hitSrc=childImg.attr('src').replace('_out.gif', '_on.gif');
					}else{
					  var hitSrc=childImg.attr('src').replace('_over.gif', '_on.gif');
					}
					
					childImg.attr('src',hitSrc);
					$(this).parent().replaceWith("<h2>" + $(this).parent().html() + "</h2>");
					
				}else{
					
					// now hit - remove 'on' from img src.
					if(i==nhit){
						childImg=$this.children('img');
						var defultSrc=childImg.attr('src').replace('_on.gif', '_out.gif');
						
						
						childImg.attr('src',defultSrc);
					}
					
					$(this).parent().replaceWith("<p>" + $(this).parent().html() + "</p>");
				}
			
			});
			
			if(parentNum==1){
				nowHitIndx=clickedIndex;
			}else{
				nowHitIndx2=clickedIndex;
			}
			
			//4.
			setMouseAction(parentNum);
			
			// Change Contents
			changeContents(clickedIndex, parentNum);
		}
		
	}
	
	
	
	
	/* change Contents */
	var nowShowContents=$('div#info1 div.if_main div.box1');	// first
	var nowShowContents2=$('div#info2 div.if_main div.box1');	// first
	
	function changeContents(nx, pn){
		var showContents;
		if(pn==1){
			showContents = $('div#info1 div.if_main').children('div').eq(nx);
			nowShowContents.fadeTo(100,0).hide(0);
			showContents.fadeTo(200,1, function(){ nowShowContents=showContents; });
		}else{
			showContents = $('div#info2 div.if_main').children('div').eq(nx);
			nowShowContents2.fadeTo(100,0).hide(0);
			showContents.fadeTo(200,1, function(){ nowShowContents2=showContents; });
		}
	}
	
	
	/* init */
	// mosue Event Set
	setMouseAction(1);	// info1
	setMouseAction(2);	// info2
	
	// for IE6
	try { 
    document.execCommand('BackgroundImageCache', false, true); 
	} catch(e) {} 

	
	
	/* Focus Input */
	var $inputs;
	$("input#searchfield").focus(function() {
        if($(this).val() == $(this).attr('defaultValue')){
            $(this).val('');
		}
	}).blur(function() {
        if(jQuery.trim($(this).val()) == "") {
            $(this).val($(this).attr('defaultValue'));
        }
    });
	
	$('#gotop a').click(function () {
		$(this).blur();
        $('html,body').animate({ scrollTop: 0 }, 550, 'easeOutQuart');
		return false;
    });
	
});

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {return -c * ((t=t/d-1)*t*t*t - 1) + b;}


