jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};

jQuery('#peach_slider').rdy(function($) {
	var $=jQuery;
	
	$.getScript('js/jquery.lightbox-0.5.min.js', function(){
		$('#peach_slider a').lightBox();
	});
	
	$('#peach_slider li').each(function(){
		var t=$(this),
				image = t.find('img').attr('src'),
				eWidth = t.width()/10;
		t.find('img').remove();
		for(var i=0;i<10;i++){
			$('<span/>').css({
				left:(i*eWidth)+'px',
				background:'url('+image+') no-repeat -'+(i*eWidth)+'px 0'
			}).hide().appendTo(t);
		}
	});
	
	function showPeach(e, callback) {
		if($('#peach_slider li span:animated').length>0) {return ;}
		e.show().find('span').each(function(i){
			var t=$(this);
			window.setTimeout(function(){
				if(typeof(callback)==='function'){
					callback.apply();
				}
				
				t.show(500, function(){
					if(t.index()===9){
						e.addClass('current').siblings().removeClass('current').hide().find('span').hide();
					}
				});
			}, i*200);
		});
	};//showPeach

	var firstLi = $('#sideMenu li:first').clone(true);
	$('#sideMenu li:first').remove();
	
	
	$('#sideMenu.suitePicker').wrapInner('<div id="sideMenuScrollWrapper" />');
	$('#sideMenu').prepend('<ul/>').find('ul:first').append(firstLi);
	
	Cufon.replace('#sideMenu a');
	
	var firstPeach = $('#peach_slider li:first');

	$('#sideMenu.suitePicker ul:eq(1) a').click(function(){
		var t=$(this).parent();
		showPeach($('#peach_slider li').eq(t.index()), function(){
			t.addClass('s').siblings().removeClass('s');
		});
		return false;
	});
	$('#sideMenu.suitePicker ul:eq(1) li:first a').click();
	
	var div = $('div#sideMenuScrollWrapper'),
		ul = div.find('ul');	

	up = holdon = false;
	scrollBy = 5000;
	
	function autscroll() {
		if(stillAnimated) {
			stillAnimated = false;
			div.stop().animate({
				scrollTop: up ? "-="+scrollBy : "+="+scrollBy
			}, 30000, 'linear');
		}
	};//autscroll
	
	var divHeight = div.height();
	var lastLi = ul.find('li:last-child');
	var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight();
	
	div.bind('mouseenter mouseleave mousemove', function(e){
		
		var top = e.pageY - div.offset().top;
		
		if(e.type==='mousemove'){
			stillAnimated = true;
			if(top > 350){
				up = false;
			}else if(top < 190){
				up = true;
			}else {
				up = false;
				stillAnimated = false;
				div.stop();
			}
			if((top > 350  || top < 190) && !div.is(':animated')){
				div.stop();
				autscroll();
			}
		}
		if(e.type==='mouseleave'){
			div.stop(true);
		}
	});	
});

