var sliderw=0;
var o = 0;
var o1 = 1;

var moveLeft = function() {
    sliderw = sliderw + 127;
    $("#motion").animate({marginLeft:sliderw}, 300);
    if (sliderw > 0)
        $('.move_l').css({opacity: 0.1}).unbind('click', moveLeft);
    if (-sliderw < ($('#motion').children('table').width() - $('#slider').width()))
        $('.move_r').css({opacity: 1}).click(moveRight);
}

var moveRight = function() {
    sliderw = sliderw - 127;
    $("#motion").animate({marginLeft:sliderw}, 300);
    if (sliderw <= 0)
        $('.move_l').css({opacity: 1}).click(moveLeft);
    if (-sliderw > ($('#motion').children('table').width() + 54 - $('#slider').width()))
        $('.move_r').css({opacity: 0.1}).unbind('click', moveRight);
}

var showHideArrows = function() {
    $(".move_l").css({opacity: 1});
    $(".move_r").css({opacity: 1});
    if ($('#motion').children('table').width() > ($('#slider').width() - 54)) {
        $(".move_l").click(moveLeft);
        $(".move_r").click(moveRight);
    } else {
        $(".move_l").css({opacity: 0.1});
        $(".move_r").css({opacity: 0.1});
        $(".move_l").unbind('click', moveLeft);
        $(".move_r").unbind('click', moveRight);
    }
}

$(document).ready(function() {
    $('#motion').find('img').load(showHideArrows);
    showHideArrows();
    $(".fade0").animate({opacity: "0.2"},1);

    $(".fade,.fade0").click(function() {
        $('#bigGalleryList').find('img').css('opacity', 0);
        oldImg = $(".lim1");
        oldImg.css({opacity: 1}).animate({opacity: 0},300).removeClass('lim1').css({'z-index': 15});
        setTimeout( "oldImg.addClass('lim2').css({'z-index': null});", 300);

        var index = $(this).parent().parent().find('div').index(this);
        bigImg = $($('#bigGalleryList').find('img').get(index));
        bigImg.removeClass('lim2').addClass('lim1').attr('width', bigImg.width()).attr('height', bigImg.height()).css({opacity: 0.2});
        bigImg.animate({opacity: 1},300);
        setTimeout( "$('#bigGalleryList').find('img').css('opacity', 'inherit');", 300);

        $('.fade0').removeClass('fade0').addClass('fade').animate({opacity: '1'},300);
        $(this).removeClass('fade').addClass('fade0').animate({opacity: "0.2"},300);

        o = o + 1;
        o1=0;
    });

    $(".fade,.fade0").hover(function() {
        $(this).animate({opacity: "0.2"},100);
    }, function() {
        if ($(this).hasClass('fade'))
            $(this).animate({opacity: ''},100);
    });
    $('.cd').change(function() {
        window.location = $(this).val();
    });
    var maxw = 0;
    for(var i=0; i<$('.mn20').length; i++)
        if ($($('.mn20')[i]).width() > maxw)
            maxw = $($('.mn20')[i]).width();
    if ($('.sd').length)
        $($('.sd')[0]).width(maxw);
});