$(document).ready( function() {
	
	$(".homeListRight li").hover(
		//mousein
		function() {
			$("section#home div.right").addClass("homeListHover-"+$(this).attr('name'));
		},
		//mouseout
		function() {
			$("section#home div.right").removeClass("homeListHover-"+$(this).attr('name'));
	});
	
	$("nav#main a").click(function() {
		$newTab = $(this).attr('name');
		if( $("#home").is(":visible") ) {
		  $("section#"+$newTab).css({'margin-left' : $(window).width()+20+'px'});
		  if($newTab != "contact") {
		    $("section#"+$newTab).load($newTab+'.htm', function() {
			    $("nav.division ul li").click(function () { 
				  if ( $(this).hasClass('active') ) {
					  //do nothing
				    }
				    else {
					  $(this).parent().children('li').removeClass('active');
					  $(this).addClass('active');
					  targetName = $(this).attr('name');
					  $("div.right:visible > section").hide();
					  $('section#' + targetName).fadeIn(500);
					  
				    }
				  
			    });
			    $("div.right nav li").click(function () {
				    if ( $(this).hasClass('active') ) {
					  //do nothing
				    }
				    else {
					  $(this).parent().children('li').removeClass('active');
					  $(this).addClass('active');
					  
					  $('section#' + $(this).attr('name')).siblings('section').hide();
					  $('section#' + $(this).attr('name')).fadeIn(350);
				    }
			    });
			  
		    });
		  }
		  $("#home").fadeOut(500, function(){
			$("#container").animate({'width':'100%'},500, function(){
			  $("#frame").show();
			  $("section#"+$newTab).fadeIn(1500);
			}).animate({'margin-top':'64px'},500).delay(1500);
		  });
		  $(this).addClass('active');
		}
		else {
		 
		  if($(this).hasClass("active")) {
			//donothing
		  }
		  else
		  {
			  if ( $("section#"+$newTab).html().length == 0 ) {
				$("section#"+$newTab).load($newTab+'.htm', function() {
					$("nav.division ul li").click(function () { 
					      if ( $(this).hasClass('active') ) {
						      //do nothing
						}
						else {
						      $(this).parent().children('li').removeClass('active');
						      $(this).addClass('active');
						      targetName = $(this).attr('name');
						      $("div.right:visible > section").hide();
						      $('section#' + targetName).fadeIn(500);
						      
						}
					      
					});
					$("div.right nav li").click(function () {
						if ( $(this).hasClass('active') ) {
						      //do nothing
						}
						else {
						      $(this).parent().children('li').removeClass('active');
						      $(this).addClass('active');
						      
						      $('section#' + $(this).attr('name')).siblings('section').hide();
						      $('section#' + $(this).attr('name')).fadeIn(350);
						}
					});
				      
				});
			  }
			  $activeTab =  $("nav#main ul li a.active").attr('name');
			  $("nav#main a").removeClass("active");
			  $(this).addClass("active");
			  
			  ///*console.log($("#frame section#"+$newTab).attr('name'))*/;
			  if( parseInt($("#frame section#"+$activeTab).attr('data-order')) > parseInt($("section#"+$newTab).attr('data-order') )) {
				  $("section#"+$activeTab).css({"margin-left":"0"});
				  $("section#"+$newTab).show();
				  $("section#"+$newTab).animate({"margin-left":$(window).width()+20+'px'}, 1000, function() {
					  $("section#"+$activeTab).hide();
					  $activeTab = $newTab;
				  });
			  }
			  else {
				  $("section#"+$newTab).show();
				  
				  $("section#"+$activeTab).animate({"margin-left":"40px"}, 1000, function() {
					  $("section#"+$activeTab).hide();
					  $("section#"+$newTab).css({"margin-left":$(window).width()+20+'px'});
					  $activeTab = $newTab;
				  });
			  }
		  }
		}
	});
});

