/*
 * BabyYak Community Slider JS
 * 
 * @require jquery
 */

//slider js

var yak_community = {
	innerbox: $('#com-inner'),
	iboxid: 'com-inner',
	movesize: 130,
	movespeed: 500,
	limit: 0,
	sflag: false,
	slide: function(dire){
		yak_community.sflag = true;
		
		var pos = $('#com-inner').css('left');
		pos = pos.replace(/(\d+).*$/, '$1');
		pos = Number(pos);
		
		if(dire == 'left'){
			pos = pos + this.movesize;
			
			if(pos <= 0){
				$('#'+this.iboxid).animate({
					left: pos+'px'
				}, this.movespeed, function(){
					yak_community.sflag = false;
				});
			}else{
				yak_community.sflag = false;
			}
			
		}else if(dire == 'right'){
			pos = pos - this.movesize;
			
			if(pos >= (-1)*this.limit){
				$('#'+this.iboxid).animate({
					left: pos+'px'
				}, this.movespeed, function(){
					yak_community.sflag = false;
				});
			}else{
				yak_community.sflag = false;
			}
		}
		
		if(pos < 0){
			$('#arw_back').fadeIn();
		}else{
			$('#arw_back').fadeOut();
		}
		
		if(pos <= (-1)*this.limit){
			$('#arw_next').fadeOut();
		}else{
			$('#arw_next').fadeIn();
		}
		return;
	},
	init: function(){
		var n = $('#com-inner > ul').size();
		var w = $('#community #community_box').width();
		var s = w/this.movesize;
		s = Math.floor(s);
		if((n - s) > 0){
			this.limit = (n-s)*this.movesize;
		}
		$('#arw_back').hide();
		
		$('#arw_back a').click(function(){
			if(!yak_community.sflag){
				yak_community.slide('left');
				return false;
			}
		});
		
		$('#arw_next a').click(function(){
			if(!yak_community.sflag){
				yak_community.slide('right');
				return false;
			}
		});
	}
};

$(function(){
	yak_community.init();
});

