/*
 * 
 * ImageScroller - a Image Horizental Scroll Viewer 
 * Version 0.1
 * @requires jQuery v1.2.1
 * 
 * Copyright (c) 2007 Luan
 * Email verycss-ok@yahoo.com.cn 
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Example:
 *   #viewer {height:100px; width:300px; clear:both; overflow:hidden; border:3px solid #e5e5e5;}
 *   #viewerFrame {width:505px; clear:both; padding:0;}
 *   #viewer img {width:90px; height:90px; margin:5px; display:inline; border:0;}
 *   #viewer a {display:block; float:left; width:100px; height:100px;}
 *   <script type="text/javascript">
 *   $(function(){
 *   	$("#viewer").imageScroller({
 *   	next:"btn1",
 *   	prev:"btn2",
 *   	frame:"viewerFrame",
 *   	child:"a",
 *   	auto:true
 *   	});	 
 *   });
 *   </script>
 *   <div id="viewer"><div id="viewerFrame">
 *   <a href=""><img src="pre1.jpg"></a>
 *   <a href=""><img src="pre2.jpg"></a>
 *   <a href=""><img src="pre3.jpg"></a>
 *   <a href=""><img src="pre4.jpg"></a>
 *   <a href=""><img src="pre5.jpg"></a>
 *   </div></div>
 *   <span id="btn1">prev</span><br/><span id="btn2">next</span>   
*/

jQuery.fn.imageScroller = function(params){
	var p = params || {
		next:"buttonNext",
		prev:"buttonPrev",
		frame:"viewerFrame",
		child:"a",
		auto:true
	}; 
	var _btnNext = $("#"+ p.next);
	var _btnPrev = $("#"+ p.prev);
	var _imgFrame = $("#"+ p.frame);
	var _child = p.child;
	var _auto = p.auto;
	var _itv;
	
	var turnLeft = function(){
		_btnPrev.unbind("click",turnLeft);
		if(_auto) autoStop();
		_imgFrame.animate( {marginLeft:-100}, 'fast', '', function(){
			_imgFrame.find(_child+":first").appendTo( _imgFrame );
			_imgFrame.css("marginLeft",0);
			_btnPrev.bind("click",turnLeft);
			if(_auto) autoPlay();
		});
	};
	
	var turnRight = function(){
		_btnNext.unbind("click",turnRight);
		if(_auto) autoStop();
		_imgFrame.find(_child+":last").clone().show().prependTo( _imgFrame );
		_imgFrame.css("marginLeft",-100);
		_imgFrame.animate( {marginLeft:0}, 'fast' ,'', function(){
			_imgFrame.find(_child+":last").remove();
			_btnNext.bind("click",turnRight);
			if(_auto) autoPlay(); 
		});
	};
	
	_btnNext.css("cursor","hand").click( turnRight );
	_btnPrev.css("cursor","hand").click( turnLeft );
	
	var autoPlay = function(){
	  _itv = window.setInterval(turnRight, 2500);
	};
	var autoStop = function(){
		window.clearInterval(_itv);
	};
	if(_auto)	autoPlay();
};
//图片替换
		 $(function(){
  			$("#viewer").imageScroller({
				next:"btn1",
    			prev:"btn2",
    			frame:"viewerFrame",
    			child:"a",
    			auto:true
    			});	 
			});


//===================================
// 自动轮换选项卡
$(document).ready(function(){
 $('.tab dl dt a:first').addClass('tabActive');
 $('.tab dl dd ul:first').css('display','block');
 autoroll();
 hookThumb();
});
var i=-1; //第i+1个tab开始
var offset = 2500000; //轮换时间
var timer = null;
function autoroll(){
 n = $('.tab dl dt a').length-1;
 i++;
 if(i > n){
 i = 0;
 }
 slide(i);
    timer = window.setTimeout(autoroll, offset);
 }
function slide(i){
 $('.tab dl dt a').eq(i).addClass('tabActive').siblings().removeClass('tabActive');
 $('.tab dl dd ul').eq(i).fadeIn("slow").siblings('.tab dl dd ul').hide();
 }
function hookThumb(){    
 $('.tab dl dt a').click(//hover
  function () {
    if (timer) {
                clearTimeout(timer);
    i = $(this).prevAll().length;
             slide(i); 
            }
  },
  function () {
            timer = window.setTimeout(autoroll, offset);  
            this.blur();            
            return false;
  }
); 
}

//等待dom元素加载完毕.
$(document).ready(function(){
	$(".has_children").click(function(){
		$(this).addClass("highlight")			//为当前元素增加highlight类
		.children("a").show().end()			//将子节点的a元素显示出来并重新定位到上次操作的元素
		.siblings().removeClass("highlight")		//获取元素的兄弟元素，并去掉他们的highlight类
		.children("a").hide();				//将兄弟元素下的a元素隐藏
	});
});
/*首页开始*/
$(document).ready(function(){
	var imgDivs = $("#photoShow>div");
	var imgNums = imgDivs.length; //图片数量
	var divWidth = parseInt($("#photoShow").css("width")); //显示宽度
	var imgWidth = parseInt($(".photo>img").css("width")); //图片宽度
	var minWidth = (divWidth - imgWidth)/(imgNums-1); //显示其中一张图片时其他图片的显示宽度
	var spanHeight = parseInt($("#photoShow>.photo:first>span").css("height")); //图片介绍信息的高度
	$(imgDivs[0]).find("span").stop().animate({bottom: 0}, "slow");
	for(var k = 1; k < imgNums; k++ ) {
		$(imgDivs[k]).stop().animate({left: (k-1)*minWidth+imgWidth}, "slow");
	}
	imgDivs.each(function(i){
		$(imgDivs[i]).css({"z-index": i, "left": i*(divWidth/imgNums)});
		$(imgDivs[i]).hover(function(){
			$(this).find("span").stop().animate({bottom: 0}, "slow");
			imgDivs.each(function(j){
				if(j<=i){
					$(imgDivs[j]).stop().animate({left: j*minWidth}, "slow");
				}else{
					$(imgDivs[j]).stop().animate({left: (j-1)*minWidth+imgWidth}, "slow");
				}
			});
		},function(){
			imgDivs.each(function(k){
				$(this).find("span").stop().animate({bottom: -spanHeight}, "slow");
				$(imgDivs[k]).stop().animate({left: k*(divWidth/imgNums)}, "slow");
			});
			$(imgDivs[0]).find("span").stop().animate({bottom: 0}, "slow");
			for(var h = 1; h < imgNums; h++ ) {
				$(imgDivs[h]).stop().animate({left: (h-1)*minWidth+imgWidth}, "slow");
			}
		});
	});
});


	/*二级开始*/
$(document).ready(function(){
	var imgDivs = $("#photoShowa>div");
	var imgNums = imgDivs.length; //图片数量
	var divWidth = parseInt($("#photoShowa").css("width")); //显示宽度
	var imgWidth = parseInt($(".photoa>img").css("width")); //图片宽度
	var minWidth = (divWidth - imgWidth)/(imgNums-1); //显示其中一张图片时其他图片的显示宽度
	var spanHeight = parseInt($("#photoShowa>.photoa:first>span").css("height")); //图片介绍信息的高度
		$(imgDivs[0]).find("span").stop().animate({bottom: 0}, "slow");
	for(var k = 1; k < imgNums; k++ ) {
		$(imgDivs[k]).stop().animate({left: (k-1)*minWidth+imgWidth}, "slow");
	}
	imgDivs.each(function(i){
		$(imgDivs[i]).css({"z-index": i, "left": i*(divWidth/imgNums)});
		$(imgDivs[i]).hover(function(){
			$(this).find("span").stop().animate({bottom: 0}, "slow");
			imgDivs.each(function(j){
				if(j<=i){
					$(imgDivs[j]).stop().animate({left: j*minWidth}, "slow");
				}else{
					$(imgDivs[j]).stop().animate({left: (j-1)*minWidth+imgWidth}, "slow");
				}
			});
		},function(){
			imgDivs.each(function(k){
				$(this).find("span").stop().animate({bottom: -spanHeight}, "slow");
				$(imgDivs[k]).stop().animate({left: k*(divWidth/imgNums)}, "slow");
			});
			$(imgDivs[0]).find("span").stop().animate({bottom: 0}, "slow");
			for(var h = 1; h < imgNums; h++ ) {
				$(imgDivs[h]).stop().animate({left: (h-1)*minWidth+imgWidth}, "slow");
			}
		});
	});
});	

//圆角边
	$(function(){	// shorthand for $(document).ready() BTW
        $('div.demo').each(function() {
			// The text of the paragraphs in the rounded divs is also the
			// jQuery code needed to create that effect. Cosmic.
             eval($(this).corner("15px"));
        });
        $('#main p').wrap("<code></code>");
	});
