$(function(){
    speed = 10; // more - slower
    
    $('div[id*=divScrollable]').each(function(){
        $(this).data('height', $(this).height());
        $(this).data('parentHeight', $(this).parent().height());
    });
    //dh = $('#divScrollable').height();
    //ph = $('#divScrollable').parent().height();

    $('.a-top').mousedown(function(){
        var divID = $(this).attr('rel') || '#divScrollable';
        var th = Math.abs(parseInt($(divID).css('margin-top').replace('px', '')));
        var x = th;
        if (x > 0) {
            $(divID).animate({marginTop: '+=' + x + 'px'}, speed * x, 'linear');
        }
    });
    $('.a-top').bind("touchstart", function(){
        var divID = $(this).attr('rel') || '#divScrollable';
        var th = Math.abs(parseInt($(divID).css('margin-top').replace('px', '')));
        var x = th;
        if (x > 0) {
            $(divID).animate({marginTop: '+=' + x + 'px'}, speed * x, 'linear');
        }
    });

    $('.a-bottom').mousedown(function(){
        var divID = $(this).attr('rel') || '#divScrollable';
        var th = Math.abs(parseInt($(divID).css('margin-top').replace('px', '')));
        var dh = $(divID).height();// .data('height');
        var ph = $(divID).parent().height(); //.data('parentHeight');
        var x = dh - th - ph;
        if (x > 0) {
            $(divID).animate({marginTop: '-=' + x + 'px'}, speed * x, 'linear');
        }
    });
    $('.a-bottom').bind("touchstart", function(){
        var divID = $(this).attr('rel') || '#divScrollable';
        var th = Math.abs(parseInt($(divID).css('margin-top').replace('px', '')));
        var dh = $(divID).height();// .data('height');
        var ph = $(divID).parent().height(); //.data('parentHeight');
        var x = dh - th - ph;
        if (x > 0) {
            $(divID).animate({marginTop: '-=' + x + 'px'}, speed * x, 'linear');
        }
    });

    $('.a-top, .a-bottom').mouseup(function(){
        var divID = $(this).attr('rel') || '#divScrollable';
        $(divID).stop();
    });
    $('.a-top, .a-bottom').bind("touchend", function(){
        var divID = $(this).attr('rel') || '#divScrollable';
        $(divID).stop();
    });
    
    $('.imgs a')
        .mouseover(function(){
            if ($(this).data('oldbg') == null)
                $(this).data('oldbg', $(this).css('background-image'));

            var url = $(this).attr('rel') || $(this).attr('rell');
            $(this).css('background-image', 'url(' + url + ')');
        })
        .mouseout(function(){
            $(this).css('background-image', $(this).data('oldbg'));
        });

    var xmas = $('a[href*=popupFormXmas]:last');
    if (xmas.length > 0)
    {
        var rel = xmas.attr('rel');
        xmas.attr('rell', rel).removeAttr('rel');
    }
    
    $("a.bookmark:not(.bk2), a[href*=popupFormXmas]").fancybox({
        'hideOnContentClick': false,
        'onClosed': function() {
	    //$("#form1 input[type=hidden], #form2 input[type=hidden]").val('add');
            //$("#form1 input[type=text], #form2 input[type=text]").val('');
	}
    });

    $('.links-tree a[href*=group]').click(function(){
        $('#divScrollable').css('marginTop', '0px');
        var href = $(this).attr('href');
        $('div[id*=divImages]').hide();
        $($(this).attr('rel')).show();
        $(href).show();
        $('a[href*=group]').addClass('disabled').filter('a[href=' + href + ']').removeClass('disabled');
        var index = $('a[href=' + href + ']').index(this);
        var div = $($(this).attr('href')).find('div.item').get(index);
        var divOffsetTop = $(div).offset().top;
        var parentOffsetTop = $('#divScrollContainer2').offset().top;
        var difference = parseInt(Math.abs(Math.round(parentOffsetTop - divOffsetTop)));
        $('#divScrollable').animate({marginTop: '-=' + difference + 'px'}, speed * 10);
        $('.body-table h2').text($(this).attr('head'));
        dh = $('#divScrollable').height();
        ph = $('#divScrollable').parent().height();
        return false;
    });
    
    $('.item a[href*=images]').click(function(){
        $('#divProductImage').empty().append('<img src="' + $(this).attr('href') + '" />');
        return false;
    });
    
    if (location.hash.length > 0)
    {
        $(location.hash).click();
    }
})
