﻿$(
 	function() {
 	    var speed = 2000;
		
 	    var timerId;
 	    var timerStop = false;
 	    var imgCount = $('.index_banner>.viewer>img').length;
		 var imgCountX=(imgCount-1)*1;
 	    $('.index_banner>.bottom>.controlButton>li').mouseover(
			function() {
			    var self = this;
			    var $li = $('.index_banner>.bottom>.controlButton>li');

			    $li.each(function() {
			        var bgp = $(this).css('backgroundPosition');
			        var pos = bgp.split(' ');
			        var pos_x = pos[0], pos_y = self == this ? '16px' : '0px';
			        $(this).css('backgroundPosition', pos_x + ' ' + pos_y);
			    });

			    var $img = $('.index_banner>.viewer>img');
			    $img.stop(true)
			    .fadeTo(1, 1)
			    .css('display', 'none');
			    $($img[imgCountX - $li.index(self)])
				    .fadeIn('slow');
			    $('.index_banner>.bottom>.title').html($(self).attr('description'));
			}
		);

 	    $('.index_banner>.title,.bottom>.controlButton').hover(
			function() {
			    timerStop = true;
			    if (timerId) window.clearTimeout(timerId);
			},
			function() {
			    timerStop = false;
			    cb();
			}
		);

 	    var slideHandler = function() {

 	        var $curImg = $('.index_banner>.viewer>img:visible');
			
 	        // 	        $curImg.fadeOut('slow', function() {
 	        // 	            var $imgs = $('.index_banner>.viewer>img');
 	        // 	            nextIdx = $imgs.index(this) + 1;
 	        // 	            if (nextIdx >= imgCount) nextIdx = 0;
 	        // 	            $('.index_banner>.bottom>.controlButton>li').each(function(i) {
 	        // 	                var bgp = $(this).css('backgroundPosition');
 	        // 	                var pos = bgp.split(' ');
 	        // 	                var pos_x = pos[0], pos_y = (nextIdx == 3 - i) ? '16px' : '0px';
 	        // 	                $(this).css('backgroundPosition', pos_x + ' ' + pos_y);

 	        // 	                if (nextIdx == 3 - i)
 	        // 	                    $('.index_banner>.bottom>.title').html($(this).attr('description'));
 	        // 	            });
 	        // 	            $($imgs[nextIdx]).fadeIn('slow', cb);
 	        // 	        });
 	        var $imgs = $('.index_banner>.viewer>img');
 	        nextIdx = $imgs.index($curImg[0]) + 1;
 	        if (nextIdx >= imgCount) nextIdx = 0;
 	        $('.index_banner>.bottom>.controlButton>li').each(function(i) {
 	            var bgp = $(this).css('backgroundPosition');
 	            var pos = bgp.split(' ');
 	            var pos_x = pos[0], pos_y = (nextIdx ==imgCountX - i) ? '16px' : '0px';
 	            $(this).css('backgroundPosition', pos_x + ' ' + pos_y);

 	            if (nextIdx == imgCountX - i)
 	                $('.index_banner>.bottom>.title').html($(this).attr('description'));
 	        });
// 	        $curImg.animate({ opacity: 0 }, { duration: 'slow', step: function() {
// 	            var op = $(this).attr('opacity');
// 	            $($imgs[nextIdx]).fadeTo('slow',1- op);
// 	        }, complete: cb
 	        // 	        });
 	        $curImg.fadeOut(500);
 	        $($imgs[nextIdx]).fadeIn(2000, cb);

 	    };
 	    var cb = function() {
 	        if (timerId) window.clearTimeout(timerId);
 	        if (!timerStop)
 	            timerId = window.setTimeout(slideHandler, speed);
 	    };
 	    cb.call(this);
 	}
 );
