﻿

$(document).ready(function() {


    $('.pagenation li').each(function(index) {
        $(this).attr({ index: index });

    }).click(function() {
        el = $(this);
        $(el).parent().find('li').removeClass('selected');

        el.addClass('selected');

        //$('.discount').css( {display:"none"});
        $('.pagenation-value').slideUp(100).eq(parseInt(el.attr('index'))).slideDown(200);

    }).find('a').click(function() {
        $(this).parent().trigger('click');
        return false;
    });





    $(document).click(function() {
        var box = $('.select-city ul');
        if (!box.hasClass('close')) box.addClass('close');
    });
    $('.select-city ul').click(function() {
        $(this).removeClass('close');
        return false;
    });

	$('.next, .prev').click(function (){
		var speed = 500;
		var box = $(this).parent();
		var current = box.find('.selected');
		var banners = box.find('.banner');
		var qty = banners.length;
		
		if($(this).hasClass('next')){
			var index = parseInt(banners.index(current)) + 1;
			if(index >= qty) index = 0;
			
		}else if($(this).hasClass('prev')){
			var index = parseInt(banners.index(current)) - 1;
			if(index < 0) index = qty - 1;
		}
		current.fadeOut(speed, function (){
			current.removeClass('selected');
			current.css( {display:"none"});
			
		});
		banners.eq(index).fadeIn(speed).addClass('selected');
	});

    $('.loginwobg a.ajquery').click(function() {
        LB_overlay.show().animate({ opacity: 0.7 }, 100);
        if ($.browser.msie && $.browser.version < 7) $('select').css({ 'visibility': 'hidden' });
        $('.login-box').fadeIn(100);
        return false;
    });
    $('.get-pic-box').click(function() {
        LB_overlay.show().animate({ opacity: 0.7 }, 100);
        if ($.browser.msie && $.browser.version < 7) $('select').css({ 'visibility': 'hidden' });
        LB_box.addClass('loading').fadeIn(100);

        var image = $('<img src="' + this.href + '" alt="' + this.title + '"/>');
        var txt = this.title;

        while(txt.indexOf(' ') != -1)
        {
            txt = txt.replace(' ', '&nbsp;');                  
        }
        LB_box.find('h2').html(txt);

        image.appendTo(LB_box);
        image.load(function() {
            var imageWidth = image.width();
            var imageHeight = image.height();
            
            if (imageWidth > 800)
            {
                var delta = 800 / imageWidth;
                imageWidth = 800;
                imageHeight = imageHeight * delta;
                
                image.width(imageWidth);
                image.height(imageHeight);
            }
            
            if (imageHeight > 600)
            {
                var delta = 600 / imageHeight;
                imageHeight = 600;
                imageWidth = imageWidth * delta;
                
                image.width(imageWidth);
                image.height(imageHeight);
            }
            
            var textWidth = LB_box.find('h2').width() + 40;
            imageWidth = imageWidth > textWidth ? imageWidth : textWidth;
            imageHeight = image.height() + LB_box.height() / 2;
                                    
            scrollTop = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
            LB_box.removeClass('loading').animate({
                width: imageWidth,
                marginLeft: -imageWidth / 2,
                height: imageHeight,
                marginTop: -imageHeight / 2 + scrollTop
            }, 100);
        });
        return false;
    });

    var LB_box = $('<div class="pic-box"><div class="close_LB"></div><h2>Вход для клиентов:</h2></div>').appendTo('body').hide();
    var LB_overlay = $('<div id="overlay"></div>').appendTo('body').height($(document).height()).hide().animate({ opacity: 0 }).click(close_LB);
    $('.close_LB').click(close_LB);

    $(document).keydown(function(e) {
        if (e.which == 27) close_LB();
    });

    function close_LB() {
        LB_box.css({ 'margin': '-50px 0 0 -50px', 'width': '100px', 'height': '100px' }).hide().find('img').remove();
        $('.login-box').hide();
        LB_overlay.hide();
    }


//    $('.pseudo-select a').click(function() {
//        //value = this.href.substr(this.href.indexOf('#') + 1, this.href.length);
//        but = $(this);
//        box = but.parent().parent().parent();

//        box.find('a').removeClass('selected');
//        but.addClass('selected');
//        //box.find('input').attr({ 'value': value });

//        return false;
//    });

    $('form.search').each(function() {
        thisForm = this;
        $(this).find('.reset').click(function() {
            thisForm.reset();
            $(thisForm).find('a').removeClass('selected');
            return false;
        });
    });

    $('#isReg').click(function() {
        var box = $('.get-order .simple-reg');
        this.checked ? box.slideDown() : box.slideUp();
    });
});
