$(function(){
	
	var mwidth;
	$('#main_nav dd').hover(
		function () {
			$(this).css({backgroundColor:'white',color:'#000'}).siblings().css({backgroundColor:'',color:''})
			$(this).parent().find('div').hide().end().end().find('div').show();
		},
		function () {	
		}
	);

	$.changeBg = function(srcs,ratio){
		$('.loading').css('zIndex','2');
		var img = new Image();
		$(img).load(function () {
			$('#bgimg').remove();
			$('body').append(this);
			$.fn.fullscreenr();
			$('.loading').css('zIndex','-2');
			// $(this).fadeIn();
		}).attr({id:'bgimg',src:srcs,rel:ratio});
	}
	
	$.changeRndBg = function(){
		var seed = bgSrc.length;
		var rndSeed = Math.floor(Math.random()*seed)
		$.changeBg(bgSrc[rndSeed],bgRatio[rndSeed]);
	}
	
		$(window).resize(function(){
		$('#scrollbar1').tinyscrollbar();
		var whg=$(window).height()-260;
		$('#scrollbar1 .viewport').height(whg);
		$('#scrollbar1').tinyscrollbar_update();
		if($('#scrollbar2').length){
			var nt_height = Math.min($('#nt_detail .content').height(),$(window).height()-330);
			$('#scrollbar2 .viewport').height(nt_height+'px');
			$('#scrollbar2').tinyscrollbar({size:nt_height});
		}
	}).trigger("resize");

	
	//==============================================ajax query
	$('.ajax a').live('click',function(){
		$.noticeHide();
		$('#notice a').css('color','');
		var whg2=$(window).height()-260;
		$('#scrollbar1 .viewport').height(whg2);
		$('#leftbar').stop();
		mwidth=$(this).attr('width');
		mwidth=mwidth?mwidth:"360px";
		scrollw=mwidth-10;
		
		var mtitle=$(this).parent().prev().attr('rel')+' / '+$(this).text();
		var mod = $(this).parent('div').attr('id');
		var sw = 'ajax.php?'+mod+'='+$(this).attr('id');
		if($('#leftbar').width()==0||parseInt($('#leftbar').css('left'))<0){
			$('#leftbar .content').html('<q></q>');
			$('#leftbar').animate({width:mwidth,left:'0'},300,function(){
				$('#scrollbar1').width($('#leftbar').width()-10);
				$('#leftbar .left_title').text(mtitle);
				$('#leftbar .content').load(sw,function(){
					$('#scrollbar1').tinyscrollbar();
					$.existctl();
					$.changeRndBg();				
				});
			});
		}else{
			$('#leftbar').stop();
			
			$('#leftbar .left_title').text(mtitle);
			if(mwidth==$('#leftbar').width()){
				$('#leftbar .content').html('<q></q>').load(sw,function(){
					$('#scrollbar1').tinyscrollbar_update();
					$.existctl();
					$.changeRndBg();
				});
			}else{
				$('#leftbar').animate({width:mwidth},300,function(){
					$('#scrollbar1').width($('#leftbar').width()-10);
					$('#leftbar .content').html('<q></q>').load(sw,function(){
						$('#scrollbar1').tinyscrollbar_update();
						$.existctl();
						$.changeRndBg();
					});
				});
			}
			
			//LoadNewContent('mcs_container',sw);
		}
	});

	//==============================================notice
	$('#notice a').click(function(){
		var whg2=$(window).height()-330;
		$('#nt_cont').stop();
		$('#notice a').css('color','');
		$(this).css('color','#ffffff');
		var noticeid = $(this).attr('rel');
		var nt = 'ajax.php?notice='+noticeid;
		if($('#nt_cont').css('right')=="-390px"){
				$('#nt_cont').animate({right:'30px'},300,function(){
					$("#nt_detail").load(nt,function(){
						$('#nt_detail').removeClass('loading');
						var nt_height = Math.min($('#nt_detail .content').height(),whg2);
						$('#scrollbar2 .viewport').height(nt_height+'px');
						$('#scrollbar2').tinyscrollbar({size:nt_height});
					});
				});
		}else{
			$("#nt_detail").load(nt,function(){
				var nt_height = Math.min($('#nt_detail .content').height(),whg2);
				$('#scrollbar2 .viewport').height(nt_height+'px');
				$('#scrollbar2').tinyscrollbar({size:nt_height});
				// $('#scrollbar2').tinyscrollbar();
			});
		}
	});



	//==============================================portfolio
	$('.pftype dd a').live('click',function(){
		var rel = $(this).attr('rel');
		$('#leftbar .content').html('<q></q>').load(rel,function(){
			$('#scrollbar1').tinyscrollbar_update();
		});
	});

	$('.pfback').live('click',function(){
		var rel = $(this).attr('rel');
		$('#leftbar .content').html('<q></q>').load(rel,function(){
			$('#scrollbar1').tinyscrollbar_update();
		});
	});

	$('.pfinfo dd a').live('click',function(){
		var link = $(this).attr('href');
		var ratio = $(this).attr('rel');
		$.changeBg(link,ratio);
		return false;
	});

	//==============================================news

	$('.newslist dd a').live('click',function(){
		var rel = $(this).attr('rel');
		$('#leftbar .content').html('<q></q>').load(rel,function(){
			$('#scrollbar1').tinyscrollbar_update();
			$('#scrollbar1').tinyscrollbar_update();
		});
	});
	//==============================================maplink
	$('#maplink').live('click',function(){
		$('#leftbar').animate({width:'740px'},200,function(){
			$('#scrollbar1').width('740px');
			$('#map').show();
		});
	});


	$('.ctlc').live('click',function(){
		$(this).parent().siblings('.ctl').children(':last-child').slideUp();
		$(this).next().slideToggle();
	})
	
	$.existctl=function(){
		var my_element = $(".ctl" ) 
		if(my_element.length>0){ 
			$('.ctl p:not(.ctlc)').hide();
		}
	}
	
	$('#backtopflist').live('click',function(){
		$('#portfolio a').trigger('click');	
	})
	
	$('.back a').live('click',function(){
		node = '#'+($(this).attr('name'));
		subnode = ($(this).attr('rel'))?' #'+($(this).attr('rel')):' a:first-child';
		$(node+subnode).trigger('click');
		return false;
	});
	
	$.noticeHide = function(){
		$('#nt_cont').animate({right:'-390px'},300);	
	}
	
	$("#nt_close").click(function(){
		$.noticeHide();
	});
	
	slideIn = function(){
		leftbarWidth = -($('#leftbar').width())+'px';
		$('#leftbar').stop().animate({left:leftbarWidth},300);
	}
	
	slideOut = function(){
		$('#leftbar').stop().animate({left:'0'},300);
	}
	
	
	var timer;
	var cl = 0;//弹出开关
	var el = 1;//贴边开关
	$('html').mousemove (function(e){
	
	
		if(e.clientX>parseInt(mwidth)+50) {
			if(cl==0){
				timer = setTimeout(function(){
					el=0;
					slideIn();
				},1000);
			}
			cl = 1;
		}else if(e.clientX<200){
			if(el==0){el=1;slideOut();}
		}else{cl=0;clearTimeout(timer);}
	});	
});



