$(document).ready(function() {
    
    //First load site
    /*$('#MainWrapper').css('opacity','0');
    
        $('#MainWrapper').animate({opacity:1}, 1000, 'linear');
    });*/

    //$('#bpsGallery').bpsGallery();

    $(window).load(function() {
        CloneHeight('#MainWrapper','#Background');
        CloneHeight('#MainWrapper','#ContentBackground');
    });
    
    
    //Default values     
    CloneHeight('#MainWrapper','#Background');
    CloneHeight('#MainWrapper','#ContentBackground');
    //CloneHeight('#RightWindow','#RightWindowBackground');
    
    $('#bpsGalleryBigPictures').children('div:first').children('img').fadeIn(3000);
    
    $('.HeadMenuItem div').css('opacity', '0.6');
    $('.HeadMenuItem a').css('opacity', '0.9');
    $('.GalleryBigButtons, .GallerySmallButtons').css('opacity', '0.5');
    $('.HeadMenuItemActive').prev().css('opacity', '1');
    $('#Portfolio .PortfolioItemPicture').css('opacity', '0.8');
    //$('#RightWindowBackground').css('opacity', '0.5');
    
    //Buttons
    $('.HeadMenuItem a').mouseenter(function() {
        $(this).stop().animate({ opacity:1 }, 250, 'linear');
        $(this).prev().stop().animate({ opacity:1 }, 250, 'linear');
    });
    $('.HeadMenuItem a').mouseleave(function() {
        $(this).stop().animate({ opacity:0.9 }, 250, 'linear');
        $(this).prev().stop().animate({ opacity:0.6 }, 250, 'linear');
    });
    $('#HeadMenuItemContact').mouseenter(function() {
        $(this).stop().animate({ opacity:1 }, 250, 'linear');
        $(this).prev().stop().animate({ opacity:1 }, 250, 'linear');
    });
    $('#HeadMenuItemContact').mouseleave(function() {
        $(this).stop().animate({ opacity:0.9 }, 250, 'linear');
        $(this).prev().stop().animate({ opacity:0.6 }, 250, 'linear');
    });
    
    $('.LeftMenuItem a').mouseenter(function() {
        $(this).parent('.ButtonUpLayer').stop().animate({ left:'0' }, 250, 'linear');
    });
    $('.LeftMenuItem a').mouseleave(function() {
        $(this).parent('.ButtonUpLayer').stop().animate({ left:'-22px' }, 250, 'linear');
    });
    
    $('#Portfolio .PortfolioItemPicture').mouseenter(function() {
        $(this).stop().animate({ opacity:1 }, 250, 'linear');
    });
    $('#Portfolio .PortfolioItemPicture').mouseleave(function() {
        $(this).stop().animate({ opacity:0.8 }, 250, 'linear');
    });
       
    //Fix for active left menu item
    $('#LeftMenu').children('div').each(function(){
        //alert($(this).find('a').attr('href'));
        //alert(document.location.href);
        var Link = $(this).find('a');
        var LinkHref = Link.attr('href');
        var LinkHTML = Link.html();
        if (LinkHref == document.location.href) {
            Link.remove();
            //$(this).find('div.ButtonDownLayer').append('<span class="LeftMenuItemActive">'+LinkHTML+'</span>');
            $(this).find('div.ButtonUpLayer').append('<span class="LeftMenuItemActive">'+LinkHTML+'</span>');
        }
    });
    $('.LeftMenuItemActive').parent().css('left','0px');
    $('.LeftMenuItemActive').parent().prev().css('left','22px');
    $('.LeftMenuItemActive').parent().parent().css('overflow','visible');
    
    
    $('.GalleryBigButtons, .GallerySmallButtons').mouseenter(function() {
        $(this).stop().animate({ opacity:1 }, 250, 'linear');
    });
    $('.GalleryBigButtons, .GallerySmallButtons').mouseleave(function() {
        $(this).stop().animate({ opacity:0.5 }, 250, 'linear');
    });
    
    
    //Scroll portfolio right window   
    function ScrollRightEntity(Object, topPadding) {
        var ObjectOffset = $(Object).offset();
        //alert($(Object).height());
        if ($(Object).height() != null) {
            $(window).scroll(function() {
                if ($(window).scrollTop() > ObjectOffset.top) {
                    $(Object).stop().animate({marginTop: $(window).scrollTop() - ObjectOffset.top + topPadding});
                }
                else {
                    $(Object).stop().animate({marginTop: 0});
                }
            });
        }
        
    }
    
    ScrollRightEntity("#PortfolioRightWindow #RightWindow", 45);
    
    //Fix onclick
    $("#HeadMenuItemContact").click(function(){
        location.replace("http://3d-3d.ru/contact");
    });
    
    $(".PortfolioItemPicture").click(function(){
        PortfolioItemLink = $(this).next().find('a').attr('href');
        location.replace(PortfolioItemLink);
    });
    
       
    //Unselect text in MainContent
    //$('#MainContent').live('selectstart dragstart', function(evt){ evt.preventDefault(); return false; });
    
});

