jQuery.fn.slider = function( settings ) {
	settings = jQuery.extend({
        speed : 300,
		scroll : 2000
    }, settings);
    return this.each( function() {
		jQuery.fn.slider.run( jQuery( this ), settings );
    });
};

jQuery.fn.slider.run = function( $this, settings ) {

	var slides = jQuery( ".list", $this );
	var slide = jQuery( ".item", $this );
	var sliderWidth = slides.parent().width();
	var $btn_next = jQuery( ".btn_next", $this );
	var $btn_prev = jQuery( ".btn_prev", $this );
	//var slideWidth = parseInt( jQuery( slide[0] ).width()) + parseInt( jQuery( slide[0] ).css( "marginRight" ) )*2;
	var slideWidth = 96;
	slides.css('width', slideWidth*slide.length);
	var animating = false;

	nextSlide = function() {
        if ( !animating ) {
			animating = true;
			offsetLeft = parseInt( slides.css( "left" ) ) - slideWidth;
			if ( offsetLeft + slides.width() > sliderWidth ) {
				slides.animate( {
					left: offsetLeft
				}, settings.speed, function() {
					animating = false;
				} );
			} else {
				animating = false;
			}
		}
		return false;
    };

	prevSlide = function() {
		if ( !animating ) {
			animating = true;
			offsetLeft = parseInt( slides.css( "left" ) ) + slideWidth;
			if ( offsetLeft  <= 0 ) {
				slides.animate( {
					left: offsetLeft
				}, settings.speed, function() {
					animating = false;
				} );
			} else {
				animating = false;
			}
		}
		return false;
	};

	scrollLeft = function(){
        if ( !animating ) {
			animating = true;
			offsetLeft = parseInt( slides.css( "left" ) );
			if (offsetLeft + slides.width() > sliderWidth) {
				slides.animate({
					left: ( -slides.width() + sliderWidth )
				}, settings.scroll, function() {
					animating = false;
				});
			}
			else {
				animating = false;
			}
		}
		return false;
    };

	scrollRight = function(){
        if ( !animating ) {
			animating = true;
			offsetLeft = parseInt( slides.css( "left" ) );
			if (offsetLeft < 0) {
				slides.animate( {
					left: 0
				}, settings.scroll, function() {
					animating = false;
				} );
			}
			else {
				animating = false;
			}
		}
		return false;
    };

	scrollStop = function() {
		slides.stop();
		animating = false;
		return false;
	}

	$btn_next.click( nextSlide ).mousedown( scrollLeft ).mouseup( scrollStop );
	
	$btn_prev.click( prevSlide ).mousedown( scrollRight ).mouseup( scrollStop );

};



$(function() {
	$('a.darkbox').click(function() {
		var link = $(this);
		if(!$('div.darkbox-frame').length) {
			darkbox = $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><div class="darkbox-button"></div></div></div>').appendTo('body');
		}
		var frame = darkbox.clone().appendTo('body').addClass('darkbox-frame-on');
		var shadow = frame.find('div.darkbox-shadow').animate({opacity:0.6},300);
		var canvas = frame.find('div.darkbox-canvas');
		var button = frame.find('div.darkbox-button');
		var image = $('<img src="'+ link.attr('href') +'" alt="'+ link.attr('title') +'"/>');
		image.appendTo(canvas);
		image.load(function(){
			var imageWidth = image.width();
			var imageHeight = image.height();
			var frameWidth = frame.width()-40;
			var frameHeight = frame.height()-40;
			if(imageWidth > frameWidth) {
				imageWidth = frameWidth;
				image.width(imageWidth);					
				while(image.height() > frameHeight) {
					image.width(imageWidth);
					imageWidth--;
				}
				imageHeight = image.height();
			}
			if(imageHeight > frameHeight) {
				imageHeight = frameHeight;
				image.height(imageHeight);						
				while(image.width() > frameWidth) {
					image.height(imageHeight);
					imageHeight--;
				}
				imageWidth = image.width();
			}
			canvas.addClass('darkbox-canvas-load').animate({
				width:imageWidth,
				marginLeft:-imageWidth/2,
				height:imageHeight,
				marginTop:-imageHeight/2
			},500,function() {
				canvas.addClass('darkbox-canvas-done');
				button.addClass('darkbox-button-on');
				button.addClass(navigator.platform.toLowerCase().indexOf('mac')+1?'darkbox-button-left':'darkbox-button-right');
				image.animate({opacity:1},500,function() {
					shadow.click(closer);
					button.click(closer);
				});
			});
		});
		var closer = function() {
			canvas.remove();
			shadow.animate({opacity:0},300,function() {
				frame.remove();
			});
		}
		$(document).keydown(function(e) {
			if(e.which==27) closer();
		});
		return false;
	});
	
	
	$( ".b-pic-slider" ).slider();

	
});



function add($id)
{
	$.post("/ajax/add2cart.php", {"id": $id},
        function(data)
        {                                   
            $('#html1').html(data.html1);
            alert('Товар добавлен в корзину.');
        }, 'json'
    );
}

function logout()
{
        $.getJSON("/ajax/logout.php",null,
        function(data){
           window.location = '/';
        });
}

function red()
{
       window.location = '/registration/';
}

