(function() {
	
	
	
	$.fn.picbox = function( s ) {
		var t = this;
		var settings = {
			speed : 400,
			offset : 0,
			slide_speed : 3000,
			range : 110,
			size : 0,
			cur : 0,
			margin : false,
			show_overlay : true,
			position : 'h',
			preview : true
		};
		t.settings = $.extend( true, settings, s );
		
		function startup() {
			t.settings.size = $('.i-push', t).length || $('.dummy', t).length;
			t.startup = 1;
			
			if (t.settings.position == 'h') {
				if (t.settings.size) {
					$('.boxer:first', t).css({
						width: t.settings.range * t.settings.size
					});
				}
			}
			if( t.settings.offset == 0 ) {
				$('#prev', t).addClass('disabled');
				$('#home', t).addClass('disabled');
			} else if( t.settings.offset == t.settings.size - 1) {
				$('#next', t).addClass('disabled');
				$('#end', t).addClass('disabled');
			}
			if ( !t.settings.onSlide ) {
				loadWith(t.settings.offset);
			}
			regAnim();
			set_nav();
		}
		
		function loadWith( i ) {
			var prd = new Image();
			
			var cursor = $('.i-push').eq(i);
			$('#preload', t).show();
			$('#b-icon', t).fadeOut(200, function() {
				$(this).unbind().attr('src', '/images/blank.gif');
			});
			
			if (t.settings.margin) {
				var l_count, count = parseInt($('.i-car :first').css('margin-left'));
				l_count = count < 0 ? -1 * count : count;
				l_count = Math.round(l_count / t.settings.range);
				var r_count = t.settings.size - i;
			
				if ((i - l_count + 1) % 6 == 0) {
					if (r_count > 1) {
						$('.i-car :first', t).animate({
							marginLeft: Math.round(count - t.settings.range)
						}, t.settings.speed, function() {
							set_view_image(cursor, i);
							
						});
					}
				} else if( (i - l_count + 1) % 6 == 1 ) {
					if (t.settings.cur > 0 && count < 0) {
						$('.i-car :first', t).animate({
							marginLeft: Math.round(count+t.settings.range)
						}, t.settings.speed, function() {
							set_view_image(cursor, i);
							
						});
					}
				}
			}
			else {
				if (t.settings.position == 'v' ) {
					$('.i-car', t).animate({
						scrollTop: Math.round(i * t.settings.range)
					}, t.settings.speed, function() {
						set_view_image(cursor, i);
						__pause( 290 );
					});
				}
				if (t.settings.position == 'v' && t.settings.proportion ) {
					var prevSr = parseInt($('.i-car', t).scrollTop());
					var pos = i < t.settings.cur ? prevSr - t.settings.range : prevSr + t.settings.range;
					$('.i-car', t).animate({
						scrollTop: Math.round(pos)
					}, t.settings.speed, function() {
						set_view_image(cursor, i);
						__pause( 390 );
					});
				}
				if( t.settings.position == 'h' ) {
					$('.i-car', t).animate({
						scrollLeft: Math.round(i * t.settings.range)
					}, t.settings.speed, function() {
						set_view_image(cursor, i);
						__pause( 390 );
					});
				}
			}
			$('.i-push', t).removeClass('current');
			cursor.addClass('current');
			
			t.settings.cur = i;
		}
		
		function set_view_image( cursor, i ) {
			
			if (t.settings.preview) {
				if( t.settings.onContent ) {
					t.settings.onContent.call(t, cursor);
				}
				
				$('#b-icon', t).unbind().bind('load', function() {
					$('#preload', t).hide();
					$(this).unbind();
					$('.lightbox', t).attr('href', cursor.attr('rel'));
					if( t.settings.onResize ) {
						t.settings.onResize.call( t, this );
					}
				}).fadeIn().attr('src', cursor.attr('href'));
				
				
			} else if( t.settings.onSlide ) {
				t.settings.onSlide( i, t, cursor );
			}
		}
		var timer_dd = null;
		function set_nav() {
			$('#prev:not(.disabled)', t).live('click', function() {
				$('#next.disabled', t).removeClass('disabled');
				$('#end.disabled', t).removeClass('disabled');
				cleanInterval();
				
				if( t.settings.cur > 0 ) {
					loadWith( t.settings.cur-1 );
				}
				if( t.settings.cur == 0 ) {
					$(this).addClass('disabled');
					$('#home', t).addClass('disabled');
				}
				clearTimeout(timer_dd);
				timer_dd = setTimeout(function() { slideShow( t.settings.cur ); }, 100);
				return false;
			});
			
			$('#next:not(.disabled)', t).live('click', function() {
				$('#prev.disabled', t).removeClass('disabled');
				$('#home.disabled', t).removeClass('disabled');
				cleanInterval();
				if( t.settings.cur < t.settings.size - 1 ) {
					loadWith( t.settings.cur+1 );
				}
				if (t.settings.position == 'h') {
					if (t.settings.cur >= t.settings.size - 1) {
						$(this).addClass('disabled');
						$('#end', t).addClass('disabled');
					}
				} else if( t.settings.position == 'v') {
					if (t.settings.offset_calc) {
						var pos = parseInt($('.i-car', t).scrollTop());
						var h = parseInt($('.boxer', t).height());
						if (pos >= h - t.settings.range * 3) {
							$(this).addClass('disabled');
							$('#end', t).addClass('disabled');
						}
						return false;
					} else {
						if (t.settings.cur >= t.settings.size - 1) {
						$(this).addClass('disabled');
						$('#end', t).addClass('disabled');
						return false;
					}
					}
					return false;
				}
				clearTimeout(timer_dd);
				timer_dd = setTimeout(function() { slideShow( t.settings.cur ); }, 100);
				return false;
			});
			
			$('#home:not(.disabled)', t).live('click', function() {
				loadWith( 0 );
				cleanInterval();
				$('#end').removeClass('disabled');
				$('#next').removeClass('disabled');
				$('#prev').addClass('disabled');
				$(this).addClass('disabled');
				return false;
			});
			
			$('#end:not(.disabled)', t).live('click', function() {
				loadWith( t.settings.size - 1 );
				cleanInterval();
				$('#home').removeClass('disabled');
				$('#prev').removeClass('disabled');
				$('#next').addClass('disabled');
				$(this).addClass('disabled');
				return false;
			});
		}
		
		var gallery_on = false;
		var time_interval = null;
		
		function cleanInterval() {
			clearInterval(time_interval);
			time_interval = null;
		}
		
		function slideShow(i) {
		
				time_interval = setInterval(function(){
					
					
					if( t.settings.cur + 1 >= t.settings.size - 1 ) {
						$('#next', t).addClass('disabled');
						$('#end', t).addClass('disabled');
					} else {
						$('#next', t).removeClass('disabled');
						$('#end', t).removeClass('disabled');
					}
					if( t.settings.cur == 0 ) {
						$('#prev', t).addClass('disabled');
						$('#home', t).addClass('disabled');
					} else {
						$('#prev', t).removeClass('disabled');
						$('#home', t).removeClass('disabled');
					}
					if (t.settings.cur >= t.settings.size - 1) {
						loadWith(0);
						//clearInterval( time_interval );
						$('#next', t).removeClass('disabled');
						$('#end', t).removeClass('disabled');
					}
					else {
						loadWith(t.settings.cur + 1);
					}
				}, t.settings.slide_speed);
			
		}

	
		function regAnim() {
			$('#play', t).click(function() {
				gallery_on = !gallery_on;
				t.settings.cur = 0;
				slideShow( 0 );
				return false;
			});
			
			if( t.settings.auto ) {
				gallery_on = !gallery_on;
				t.settings.cur = 0;
				slideShow( 0 );
			}
			
			$('.i-push', t).click(function() {
				var rel = $('.i-push', t).index(this);
				if( rel == t.settings.size - 1 ) {
					$('#next', t).addClass('disabled');
					$('#end', t).addClass('disabled');
				} else {
					$('#next', t).removeClass('disabled');
					$('#end', t).removeClass('disabled');
				}
				if( rel == 0 ) {
					$('#prev', t).addClass('disabled');
					$('#home', t).addClass('disabled');
				} else {
					$('#prev', t).removeClass('disabled');
					$('#home', t).removeClass('disabled');
				}
				
				loadWith( rel );
				return false;
			})
		}
		
		function __pause( ms ) {
			var date = new Date();
			var curDate = null;
			do { curDate = new Date(); }
			while ( curDate - date < ms);
		}
		
		startup();
	}
})();


