$(function(){
	var container = $('#slide-container');
	var obj = $('#slider');
	obj.css({visibility:'hidden'});
	var obj_length = $("li", obj).length;
	var w = obj.width();
	$('.exp', obj).css({visibility:'visible',display:'none'});
	container.css({overflow:'visible'});
	var n = 0;
	var offset = 0;
	var sec = 6000;
	var pause_counter = 0;
	sb = new Boolean(true);
	if($('li',obj).length == 1) sb = false;
	$(window).load(function() {
		w = 0;
		$('li', obj).each(function(){
			w += $(this).width();
		});
		$('ul', obj).width(w);
		if($('#wrapper').width() > w) sb = false;
		initW = w;
		container.append('<a href="#" id="btn-prev">prev</a><a href="#" id="btn-next">next</a>');
		controlBtn();
		sizer();
		obj.css({display:'none',visibility:'visible'}).hide().fadeIn(300);
		timerID = setInterval(move, sec);
	});
	$('a', obj).hover(function(){
		clearInterval(timerID);
		$('li', obj).clearQueue().animate({opacity:0.5},150);
		$(this).closest('li').stop().css({opacity:1});
		$(this).children('div.exp').hide().slideDown(100);
	},function(){
		$('li', obj).animate({opacity:1},150);
		$(this).children('div.exp').slideUp(100);
		pause_counter = 0;
		play();
	});
	function controlBtn(){
		$('a#btn-prev').click(function(){
			if(timerID!=0) clearInterval(timerID);
			move();
			pause_counter = 0;
			return false;
		});
		$('a#btn-next').click(function(){
			if(timerID!=0) clearInterval(timerID);
			move("next");
			pause_counter = 0;
			return false;
		});
	}
	function play(s){
		if(timerID!=0) clearInterval(timerID);
		if(s == null) s = sec; 
		timerID = setInterval(move, s);
	}
	var minus = 0;
	function move(arg){
		$(":animated").clearQueue();
		if(sb) {
			if(arg=="next"){
				if(n+minus<=0) {
					var pos = $("ul", obj).css("marginLeft").replace("px", "");
					pos = Number(pos);
					$("li", obj).eq(obj_length-1).clone(true).prependTo($("ul", obj));
					w +=$("li", obj).eq(1).width();
					$("ul", obj).width(w);
					$("ul", obj).css({marginLeft:0+pos-$('li', obj).eq(1).width()});
					offset = 0;
					minus--;
					n++;
				} else {
					offset += $('li', obj).eq(n-1).width();
					n--;
				}
			} else {
				if(minus>=0) {
					$("li", obj).eq($("li", obj).length%obj_length).clone(true).appendTo($("ul", obj));
					w +=$("li", obj).eq(n).width();
					$("ul", obj).width(w);
					n++;
				} else {
					minus++;
				}
				offset -= $('li', obj).eq(n).width();
			}
			$("ul", obj).animate({marginLeft:offset}, 1000, "swing", function(){
				play();
				pause_counter++;
				if(pause_counter >= obj_length ) pause();
			});
		}
	}
	function pause() {
		clearInterval(timerID);
	}
	$(window).resize(function(){
		sizer();
	});
	function sizer() {
		var l = 0;
		bw = $('html').width();
		if($('#wrapper').width() < bw) {
			if(bw > initW) {
				l = (bw-initW)/2-(bw-$('#wrapper').width())/2;
				obj.css({left:l,width:initW});
			} else {
				obj.width(bw);
				l = (bw-$('#wrapper').width())/2*-1;
				obj.css({left:l});
			}
			$('#btn-prev').css({left:l+obj.width()-65, top:container.height()/2-13})
			$('#btn-next').css({left:l+obj.width()-35, top:container.height()/2-13});
		}
	}
});

