﻿/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2-vsdoc.js" />

$.fx.speeds._default = 1000;

$(document).ready(function () { // Дождаться загрузки DOM

    var smallImg = $('#small a');
    var showImg = $('.show');
    smallImg.click(function () {
        if ($('#big img').attr('src') != $(this).attr('href')) {
            var bigImg = $('#big img').hide().attr('src', $(this).attr('href'));
            showImg.fadeTo(100, 1);
            $(this).fadeTo(1000, 0.5);
            bigImg.load(function () {
                $(this).fadeIn(2000);
            });
        }
        return false;
    });

    $('#switchGal').toggle(function () {
        $('#gallery').slideDown(1000);
    },
    function () {
        $('#gallery').slideUp(1000);
    });

    $('#go_left').hover(function () {
        $(this).attr('src', 'images/go_left_hover.png');
    },
    function () {
        $(this).attr('src', 'images/go_left.png');
    });

    $('#go_right').hover(function () {
        $(this).attr('src', 'images/go_right_hover.png');
    },
    function () {
        $(this).attr('src', 'images/go_right.png');
    });


    $('#go_left').click(function () {
        showImg.fadeTo(100, 0.01);
        var last_href = $('#last').attr('href');
        var last_img = $('#last').html();
        var prev_href;
        var prev_img;
        smallImg.each(function () {
            if ($(this).attr('id') == 'first') {
                prev_href = $(this).next().attr('href');
                prev_img = $(this).next().html();
                $(this).next().attr('href', $(this).attr('href'));
                $(this).next().html($(this).html());
            } else if ($(this).attr('id') != 'last') {
                var tmp_href = $(this).next().attr('href');
                var tmp_img = $(this).next().html();
                $(this).next().attr('href', prev_href);
                $(this).next().html(prev_img);
                prev_href = tmp_href;
                prev_img = tmp_img;
            }
        });
        $('#first').attr('href', last_href);
        $('#first').html(last_img);
        showImg.fadeTo(1000, 1);
    });

    $('#go_right').click(function () {
        showImg.fadeTo(100, 0.01);
        var first_href = $('#first').attr('href');
        var first_img = $('#first').html();
        smallImg.each(function () {
            if ($(this).attr('id') != 'last') {
                $(this).attr('href', $(this).next('a').attr('href'));
                $(this).html($(this).next().html());
            }
        });
        $('#last').attr('href', first_href);
        $('#last').html(first_img);
        showImg.fadeTo(500, 1);
    });
});  
