﻿/*!
 *
 * Copyright 2011, Anton Stupak
 *
 * e-mail: puppies@bk.ru	
 *
 * Date: Sut Feb 26 22:55:33 2011 
 */
﻿
$.fn.rudSlider = function (options) {
    var defaults = {
        speed: 200,
        start_position: 1
    };

	var mclk = false;
	var auto_refresh = setInterval(
	function()
	{
		if (mclk == false) { next(); }
	}, 7000);

    var options = $.extend(defaults, options);
    var step_width = 60 + parseInt($('.menu_a').css('margin-right'));
    $('#slide-img-small').width(($(this).length) * (50 + step_width));
    $('#slide-img-small').css('left', -((defaults.start_position - 1) * step_width));
    $('#slide-img-small li:eq(' + defaults.start_position + ') .img-small').css({
        'width': 120 + 'px',
        'height': 120 + 'px',
        'top': -40 + 'px'
    });
    var position = defaults.start_position;
    var idimg;
    var documentHeight = $(document).height();
    var documentWidth = $(document).width();
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var imgHolder_src = $('.blackDIV img.img').attr('src');
    hide_all();
    img_small_animate(position)
    show_product(position);
    arrows_hide();
		
		
    $('.blackDIV img.img').parent().css({
        'top': (documentHeight / 2) - 350,
        'left': (windowWidth / 2) - 350,
        'cursor': 'pointer'
    });

    function hide_all() {
        var img = $('#slider-inner .img-big');
        var circle = $('.circle');
        circle.css('top', -470 + 'px');
        img.css('top', 310 + 'px');
        $('#slider-inner').children().not('.mask, .menu-line').hide();
    };

    function show_product(position) {
        img = $('#slider-inner .img-big.' + idimg);
        text = $('#slider-inner .text.' + idimg);
        circle = $('.circle');
        img.show().animate({
            'top': -40 + 'px'
        }, defaults.speed, function () {
            text.fadeIn(defaults.speed)
        });
        circle.show().animate({
            'top': 0 + 'px'
        }, defaults.speed);
    };

    function img_small_animate(index) {
        var menu = $('#slide-img-small li:eq(' + index + ') .img-small');
        var menu_all = $('#slide-img-small li .img-small');
        idimg = menu.attr('id');
        menu_all.animate({
            'width': 60 + 'px',
            'height': 60 + 'px',
            'top': 0 + 'px'
        }, {
            duration: defaults.speed,
            queue: false
        });
        menu.animate({
            'width': 120 + 'px',
            'height': 120 + 'px',
            'top': -40 + 'px'
        }, defaults.speed).addClass('active_img').parents('li').siblings('li').find('.img-small').removeClass('active_img');
    };

    function next() {
        if (position >= ($('.menu_a').length - 1)) {
            return false
        } else {
            ++position;
            $('#prev,#next,#strelka_left_b,#strelka_right_b').removeClass('hidden');
            hide_all();
            $('#slide-img-small').animate({
                'left': '-=' + step_width
            }, defaults.speed);
            img_small_animate(position);
            show_product(position);
            arrows_hide();
        };
    };

    function prev() {
        if (position < 1) {
            return false
        } else {
            --position;
            $('#prev,#next,#strelka_left_b,#strelka_right_b').removeClass('hidden');
            hide_all();
            $('#slide-img-small').animate({
                'left': '+=' + step_width
            }, defaults.speed);
            img_small_animate(position);
            show_product(position);
            arrows_hide();
        };
    };

    function arrows_hide() {
        if (position >= ($('.menu_a').length - 1)) {
            $('#next').addClass('hidden');
            $('#strelka_right_b').addClass('hidden');
        } else if (position <= 0) {
            $('#prev').addClass('hidden');
            $('#strelka_left_b').addClass('hidden');
        };
    };

    function modalW() {
        if ($('.blackDIV').is(':animated')) {
            return false
        } else {
		
            var src_imgBig = String($('.img-big.' + idimg).attr('src'));
            var src = src_imgBig.replace("images/products/", "images/products/pop-up/");
		  $.ajax({
			url : src,
			success : function () {
				$('.blackDIV').show().children('.black').css({
					'height': documentHeight + 'px',
					'width': documentWidth + 'px'
				}).stop(true, true).fadeTo(500, 0.5);
				$('#progressBar').css({
					'top': windowHeight / 2 - $('#progressBar').height() / 2 + 'px',
					'left': windowWidth / 2 - $('#progressBar').width() / 2 + 'px',
					'position': 'absolute',
					'z-index': 9999
				}).show();
				$('.blackDIV img.img').attr('src', src).parent().css({
					'top': (windowHeight / 2) - 350,
					'left': (windowWidth / 2) - 350,
					'cursor': 'pointer'
				}).hide();
				if (($('.blackDIV img.img').attr('id') == idimg) || ($('.blackDIV img.img').attr('src') == imgHolder_src)) {
					$('#progressBar').hide();
					$('.blackDIV img.img').parent().show();
				} else {
					$('.blackDIV img.img').bind('load', function () {
						$('#progressBar').hide();
						$('.blackDIV img.img').parent().show().end().attr('id', $('.img-big.' + idimg).attr('class').replace('img-big ', ''));
						imgHolder_src = $('.blackDIV img.img').attr('src');
					});
				};
			}
		  });

        };
    };
    $('.mask, .img-big').click(function () {
		mclk = true;
        modalW();
    });
    $('.blackDIV img').click(function () {
        $('.blackDIV .black').fadeOut().hide();
        $('.blackDIV').hide();
    });
    $(window).resize(function () {
        documentHeight = $(document).height();
        documentWidth = $(document).width();
        windowHeight = $(window).height();
        windowWidth = $(window).width();
        $('.blackDIV .black').css({
            'height': documentHeight + 'px',
            'width': windowWidth + 'px'
        }).fadeTo(500, 0.5);
        $('.blackDIV .wrapper').css({
            'top': windowHeight / 2 - $('.blackDIV img.img').height() / 2 + 'px',
            'left': windowWidth / 2 - $('.blackDIV img.img').width() / 2 + 'px'
        });
    });
    $('.menu_a').click(function (event) {
        if ($('#slide-img-small').is(':animated')) {
            return false
        } else {
            if ($(this).find('img').hasClass('active_img')) {
				mclk = true;
                modalW();
            } else {
                var index = $(this).parent().index();
                if ((position < index)) {
				   mclk = true;
                   next();
                } else if ((position) && (position > index)) {
				   mclk = true;
                   prev();
                };
            };
            event.preventDefault();
            return false;
        };
    });
    $('#next').click(function (event) {
        if ($('#slide-img-small').is(':animated')) {
            return false
        } else {
			mclk = true;
            next();
            event.preventDefault();
            return false;
        };
    });
    $('#prev').click(function (event) {
		mclk = true;
        if ($('#slide-img-small').is(':animated')) {
            return false
        } else {
            prev();
            event.preventDefault();
            return false;
        };
    });
};
