var naviTabs = (function() {
	function NaviTabs(){
		this.initTabs = function(pos){
			var Container = $('.cl_NaviTabs_'+pos);
			var Tabs = $('.cl_NaviTabs_'+pos+' div.tab');
			var hidePx = (pos=='left') ? 18 : -18; 
			Container.css({
				'height': (Tabs.length*60)+6
			},300)
			Tabs.each(function(index){
				$(this).pos = index;
				$(this).css({
					'left' : ($(this).hasClass('inactive')||$(this).hasClass('disabled')) ? hidePx : 0,
					'top': index*60+6 
				});
			})
			
			Tabs.mouseover(function(){tabSlide($(this),0);});
			Tabs.mouseout(function(){tabSlide($(this),hidePx);});
			Tabs.click(function(){	makeActive($(this));});
			
			function makeActive(obj){
				if($('#NaviLoading_'+pos).css('display')=='none'){
				if(obj.hasClass('inactive')){
					var opDisabledTab = $('.cl_NaviTabs_'+ ((pos=='left') ? 'right':'left') +' div.disabled');
					var activeColor = (pos==='left') ? "#F7F7F7" : "#FFFFFF";
					var activeTab = $('.cl_NaviTabs_'+pos+' div.active');
					var opTabs = $('.cl_NaviTabs_'+ ((pos=='left') ? 'right':'left') +' div#'+obj.attr('id'));
					obj.unbind('mouseover');
					obj.unbind('mouseout');
					/*
					obj.animate({
						'left': '0',
						'background-color': activeColor,
						'color': '#be0000'
					},300);
					activeTab.animate({
						'left': hidePx,
						'background-color': '#DEDEDE',
						'color': '#7d7d7d'
					}
					,300, function(){
						obj.attr('class', 'tab active');
						$(this).attr('class', 'tab inactive');
						$(this).mouseover(function(){tabSlide($(this),0);})
						$(this).mouseout(function(){tabSlide($(this),hidePx);})
						
					});
					opDisabledTab.animate({
						'background-color': '#DEDEDE',
						'color': '#7d7d7d'
					},300, function(){
						opDisabledTab.attr('class', 'tab inactive');
					});
					$('.cl_NaviTabs_'+ ((pos=='left') ? 'right':'left') +' div#'+obj.attr('id')).animate({
						'background-color': '#282828',
						'color': '#333333'
					},300, function(){
						$(this).attr('class', 'tab disabled');
					});
					*/
					activeTab.attr('class', 'tab inactive');
					opTabs.attr('class', 'tab disabled');
					opDisabledTab.attr('class', 'tab inactive');
					obj.attr('class', 'tab active');
					
					
					obj.css({
						'left': '0',
						'background-color': activeColor,
						'color': '#be0000'
					});
					
					activeTab.mouseover(function(){tabSlide(activeTab,0);})
					activeTab.mouseout(function(){tabSlide(activeTab,hidePx);})
					
					activeTab.animate({
						'left': hidePx,
						'background-color': '#DEDEDE',
						'color': '#7d7d7d'
					},300);
					opDisabledTab.animate({
						'background-color': '#DEDEDE',
						'color': '#7d7d7d'
					},300);
					opTabs.animate({
						'background-color': '#282828',
						'color': '#333333'
					},300);
					
					naviTabs.fillContent(pos,obj.attr('id'));
					return false;
				}
				}
				
			}
			function tabSlide(obj,px){
				if(obj.hasClass('inactive')){
					/*obj.stop();
					obj.animate({
						'left': px
					}
					,500);*/
					obj.css({'left': px});
					return false;
				}
			}
		}
		this.fillContent = function(pos,page){
			
			
			
			var preload = $('#'+page+'Preload');
			var loading = $('#NaviLoading_'+pos);
			var showContent = $('#naviContent_'+pos);
			var showContentWrap = $('.cl_NaviContent');
			var spd = 500;
			var oppsitContent = $('#naviContent_'+((pos=='left')?'right':'left'));
			//var oppositContentHeight = $('#naviContent_'+((pos=='left')?'right':'left')).height();
			var tabL = $('.cl_NaviTabs_left #'+page).children('.indicator');		
			var tabR = $('.cl_NaviTabs_right #'+page).children('.indicator');		
			tabL.css('display', 'none');
			tabR.css('display', 'none');
			loading.fadeIn(spd);
			showContent.fadeOut(spd,function(){
				var minHeight = 315;
				
				
				if(preload.html()==''){
					$.post('asyncphp/'+page+'.php'+naviTabs.postSid+'pos='+pos, function(data) {
						
						preload.html(data);
						showContent.html(preload.html());
						
						oppsitContent.css({'display':'block'});
						var showContentHeight = (showContent.css('max-height')!='none')&&(showContent.css('max-height')>0) ? showContent.css('max-height') : preload.height();
						var oppositContentHeight = (oppsitContent.css('max-height')!='none')&&(oppsitContent.css('max-height')>0) ? oppsitContent.css('max-height') : oppsitContent.height();
						showContent.css({'display':'none'});
						
						
						if(showContentHeight>oppositContentHeight){
							$('.cl_NaviContent_'+pos).css({'max-height': (showContentHeight>minHeight)?showContentHeight:minHeight});
							$('.cl_NaviContent_'+((pos=='left')?'right':'left')).css({'max-height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight});
							
							showContentWrap.animate({'height': (showContentHeight+5>minHeight)?(showContentHeight+5):minHeight},spd,function(){
								
								
								
								$('.cl_NaviContent_'+pos).css({'height': (showContentHeight>minHeight)?showContentHeight:minHeight})
								$('.cl_NaviContent_'+((pos=='left')?'right':'left')).css({'height': (showContentHeight>minHeight)?showContentHeight:minHeight})
								showContent.fadeIn(spd);
								loading.fadeOut(spd);
							});
						}else{
							$('.cl_NaviContent_'+pos).css({'max-height': (showContentHeight>minHeight)?showContentHeight : minHeight})
							$('.cl_NaviContent_'+((pos=='left')?'right':'left')).css({'max-height': (oppositContentHeight>minHeight)?oppositContentHeight : minHeight})
								
							showContentWrap.animate({'height': (oppositContentHeight+5>minHeight)?(oppositContentHeight+5):minHeight},spd,function(){
								$('.cl_NaviContent_'+pos).css({'height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight})
								$('.cl_NaviContent_'+((pos=='left')?'right':'left')).css({'height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight})
								showContent.fadeIn(spd);
								loading.fadeOut(spd);
							});
						}
					});
				}else{
					showContent.html(preload.html());
					
					oppsitContent.css({'display':'block'});
					var showContentHeight = (showContent.css('max-height')!='none')&&(showContent.css('max-height')>0) ? showContent.css('max-height') : preload.height();
					var oppositContentHeight = (oppsitContent.css('max-height')!='none')&&(oppsitContent.css('max-height')>0) ? oppsitContent.css('max-height') : oppsitContent.height();
					showContent.css({'display':'none'});
						
					if(showContentHeight>oppositContentHeight){
						showContentWrap.animate({'height': (showContentHeight+5>minHeight)?(showContentHeight+5):minHeight},spd,function(){
							$('.cl_NaviContent_'+pos).css({
								'height': (showContentHeight>minHeight)?showContentHeight:minHeight,
								'max-height': (showContentHeight>minHeight)?showContentHeight:minHeight
							})
							$('.cl_NaviContent_'+((pos=='left')?'right':'left')).css({
								'height': (showContentHeight>minHeight)?showContentHeight:minHeight,
								'max-height': (showContentHeight>minHeight)?showContentHeight:minHeight
							})
							showContent.fadeIn(spd);
							loading.fadeOut(spd);
						});
					}else{
						showContentWrap.animate({'height': (oppositContentHeight+5>minHeight)?(oppositContentHeight+5):minHeight},spd,function(){
							$('.cl_NaviContent_'+pos).css({
								'height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight,
								'max-height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight
							});
							$('.cl_NaviContent_'+((pos=='left')?'right':'left')).css({
								'height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight,
								'max-height': (oppositContentHeight>minHeight)?oppositContentHeight:minHeight
							});
							showContent.fadeIn(spd);
							loading.fadeOut(spd);
						});
					}
				}
			});
		}
	}
	return new NaviTabs();
}());
