function slidebox()
{
	this.slides = false;
	this.elms = { l: 0, r: 2 };
	this.selector = false;
	this.slider = false;
	this.image = $( 'DIV#imagebox' );
	this.timer = null;
	this.changer = null;
	this.chgidx = 1;
	this.restart = false;
	
	this.init = function( selector, slider )
	{
		this.slider = slider;
		this.selector = selector;
		this.slides = $( selector );
		this.loadimages( 0 );
		this.automate();
	}

	this.loadimages = function( i, j ) {

		var t = this;

		if( typeof( j ) == 'undefined' )
			var j = 0;

		if( i < t.slides.length && typeof( t.slides.eq( i ) ) != 'undefined' ) {

			var img = new Image();
			var img2 = new Image();

			$( img ).load( function() {

				++i;
				++j

				if( img.attachEvent && j == 9 ) {

					j = 0;
					setTimeout( function() { t.loadimages( i, j ); }, 100 );

				}
				else
					t.loadimages( i, j );

			});

			var src = $( 'DIV#imagebox P' ).eq( i ).find( 'IMG' ).attr( 'src' );
			var src2 = t.slides.eq( i ).find( 'IMG' ).attr( 'src' );

			if( src2 != null )
				$( img2 ).attr( 'src', src2 );

			if( src != null ) {
				
				$( 'DIV#imagebox P' )
					.eq( i )
					.wrapInner( '<a href="' + t.slides.eq( i ).find( 'A' ).attr( 'href' ) + '" />' );

				$( img )
					.attr( 'src', src );

			}

		}
		else if( t.slides.length > 1 ) {

			t.enablenavi();

		}

	}

	this.enablenavi = function() {
		
		var t = this;
		$( 'DIV#imagebox P:first' ).show();

		$( 'DIV#imagebox, A.slider' ).mouseenter( function() {

			clearTimeout( t.changer );
			t.changer = null;
		
		});
		
		$( 'DIV#imagebox, A.slider' ).mouseleave( function() {

			t.automate();

		});

		t.slides.mouseenter( function() {

			clearTimeout( t.changer );
			t.changer = null;

			var sl = $( this );

			t.timer = window.setTimeout( function() {

				var idx = sl.index();
				
				if( $( 'DIV#imagebox P' ).eq( idx ).length ) {

					$( 'DIV#imagebox P' ).hide();
					$( 'DIV#imagebox P' ).eq( idx ).show();

				}

			}, 150 );

		});
		
		t.slides.mouseleave( function() {

			clearTimeout( t.timer );
			t.timer = null;

			t.automate();

		});

		$( 'A#arr-left' ).click( function() {
	
			if( $( t.slider + ':animated' ).length )
				return false;
	
			if( t.elms.l > 0 ) {

				var wd = t.slides.eq( t.elms.l-1 ).outerWidth();
				$( t.slider ).animate( { left: '+=' + wd }, 600, 'easeInOutQuart', function() {

					t.elms.l--;
					t.elms.r--;

					if( t.elms.r + 1 < t.slides.length )
						$( 'A#arr-right' ).show();

					if( t.elms.l == 0 )
						$( 'A#arr-left' ).hide();

				});

			}

			$( this ).blur();
			return false;

		});
	
		$( 'A#arr-right' ).click( function() {
	
			if( $( t.slider + ':animated' ).length )
				return false;
	
			if( t.elms.r + 1 < t.slides.length ) {

				var wd = t.slides.eq( t.elms.r+1 ).outerWidth();
				$( t.slider ).animate( { left: '-=' + wd }, 600, 'easeInOutQuart', function() {

					t.elms.l++;
					t.elms.r++;
					
					if( t.elms.r + 1 == t.slides.length )
						$( 'A#arr-right' ).hide();

					if( t.elms.l > 0 )
						$( 'A#arr-left' ).show();

				});

			}
	
			$( this ).blur();
			return false;

		});

		if( t.slides.length > 3 )
			setTimeout( function() { $( 'A#arr-right' ).show(); }, 200 );

	}
	
	this.automate = function() {

		var t = this;

		if( t.elms.r + 1 == t.slides.length && t.slides.length > 3 )
			t.restart = true;

		t.changer = window.setTimeout( function() { t.autochange(); }, 8000 );

	}
	
	this.autochange = function() {

		var t = this;

		if( t.restart ) {

			t.chgidx = 0;

			$( t.slider ).animate( { left: 0 }, 1200, 'easeInOutQuart', function() {

				t.elms.l = 0;
				t.elms.r = 2;

				if( t.slides.length > 3 )
					$( 'A#arr-right' ).show();
				$( 'A#arr-left' ).hide();

			});

		}

		if( t.chgidx > t.elms.r ) {

			if( ( t.chgidx - t.elms.r ) > 1 ) {

				var off = ( t.chgidx - t.elms.r ) * 311;
				
				t.elms = { l: ( t.chgidx - 2 ), r: t.chgidx };
				$( t.slider ).animate( { left: '-=' + off }, 600 );

				if( t.elms.r + 1 == t.slides.length )
					$( 'A#arr-right' ).hide();

				if( t.elms.l > 0 )
					$( 'A#arr-left' ).show();
			}
			else
				$( 'A#arr-right' ).click();
		}

		if( t.chgidx < t.elms.l ) {

			if( t.chgidx < 3 ) {

				$( t.slider ).animate( { left: 0 }, 600 );
				t.elms = { l: 0, r: 2 };

			}
			else {

				$( t.slider ).animate( { left: ( ( t.chgidx - 2 ) * -311 ) }, 600 );
				t.elms = { l: ( t.chgidx - 2 ), r: t.chgidx };

			}

			if( t.elms.r + 1 < t.slides.length )
				$( 'A#arr-right' ).show();

			if( t.elms.l == 0 )
				$( 'A#arr-left' ).hide();
		}

		if( $( 'DIV#imagebox P' ).eq( t.chgidx ).length ) {

			$( 'DIV#imagebox P' ).hide();
			$( 'DIV#imagebox P' ).eq( t.chgidx ).show();

		}
		
		t.chgidx++;
		t.restart = false;

		if( t.chgidx == t.slides.length ) {

			clearTimeout( t.changer );
			t.changer = null;
			t.chgidx = 0;
			t.restart = true;

		}

		clearTimeout( t.changer );
		t.changer = null;
		t.automate();
		
	}

}


