function showPhotosPopup(thumb){
    var src = thumb.css('background-image').replace('"', '').replace('"','');
        
    var emptyStyle = '';
    
    if(thumb.hasClass('empty')){
        src = '/bitrix/templates/layout/blank.gif';
    }
    
    var height = parseInt(thumb.css('height'));
    var width = parseInt(thumb.css('width'));
    
    var wBonus = 0;
    var hBonus = 0;
    if(width == 119){
       // wBonus = -1;
    } 
    
    if(height == 79){
        //hBonus = -1;
    }
    
    $('#photos-popup').css('left', parseInt(thumb.css('left')) - 20 - wBonus );
    $('#photos-popup').css('top', parseInt(thumb.css('top')) - 20 - hBonus );
    
    var addImg = '';
    if(thumb.hasClass('video')){
        addImg = '<img src="/bitrix/templates/layout/images/playicon.png" style="margin:'+(-8-hBonus)+'px 0 0 '+(-8-wBonus)+'px;" />';
    }
    
    $('#photos-popup').html('<div class="close"></div><div class="left"><a href="'+thumb.attr('href')+'" style="height:'+height+'px;width:'+width+'px;background-image:'+src+';">'+addImg+'</a></div><div class="right"><h3><a href="'+thumb.attr('href')+'">'+thumb.attr('desctitle')+'</a></h3><div class="desc">'+thumb.attr('desc')+'</div></div>');
    
    $('#photos-popup').show();

    $('#photos-popup .close').unbind().bind('click', function(){
        $('#photos-popup').hide();
        return false;
    });

    return false;
}

function ie(){
    $('.b-photoreports-list .photoreport:nth-child(3n-1), .b-news-list-columns .news-item:nth-child(3n-1)').addClass('nth-child3n-1');
}


function hideMenuPopup(){
    $('.b-menu-popup').animate({'opacity':0,'bottom':80},100, function(){
        $('.b-menu-popup').css('display','none');
    });
    $('.b-menu-popup, .b-links-container').unbind('mouseleave');
}

var benefitAnimation = false;
$(document).ready(function(){

    var instanceOne = new ImageFlow();
	instanceOne.init({ 
	   ImageFlowID: 'header-flow',
	   startID:  1,
	   buttons: false,
	   reflections: false,
	   reflectionP: 0.2,
	   circular: true,
	   imageFocusM: 0.8,
	   imageFocusMax: 5,
	   aspectRatio: 3 
    });

    if($.browser.msie){
        ie();
    }
    
    // Memorials
    $('.b-about-region-section .memorials a').hover(function(){
        $(this).find('img').animate({'opacity' : 1}, 300);
    }, function(){
        $(this).find('img').animate({'opacity' : 0.7}, 100);
    });

    // Map
    $('.b-map-section .b-links-container a').bind('mouseover', function(){
        $('.'+$(this).attr('rel')).mouseover();
        $('#map-popup').hide();
    });
    
    $('.b-map-section .b-links-container a').bind('mouseleave', function(){
        $('.'+$(this).attr('rel')).mouseout();
    });
    
    $('area').bind('mouseover', function(){
        $('#map-popup').stop(true, true);
        $('#map-popup').fadeIn(200);
        
        var title = $(this).attr('desctitle');
        var desc = $(this).attr('desc');
        var isrc = $(this).attr('isrc');
        
        $('#map-popup').html( '<div class="left"><img src="'+isrc+'" /></div><div class="right"><h3>'+title+'</h3>'+'<div class="b-popup-desc">'+desc+'</div></div>');
        return false;
    });
    
    $('area').bind('mouseout', function(){
        $('#map-popup').fadeOut(100);
    });
    
    $('.b-map').bind('mousemove', function(e){
        $('#map-popup').css( 'left', parseInt(e.pageX) - $(this).attr('offsetLeft') + 20 );
        $('#map-popup').css( 'top',  parseInt(e.pageY) - $(this).attr('offsetTop') - 40 - $('#map-popup').height() );    
    });

    // Photos
    $('.b-thumbnails-container .thumb-link').bind('mouseover', function(){
        
        $('.b-thumbnails-container .thumb-link').stop(true, true);
        
        
        $('.b-thumbnails-container .thumb-link.generated').remove();
        
        var iwidth = parseInt($(this).attr('iwidth'));
        var iheight = parseInt($(this).attr('iheight'));
        
        var top  = $(this).attr('offsetTop');
        var left = $(this).attr('offsetLeft');
        var desc = $(this).attr('desc');
        var desctitle = $(this).attr('desctitle');
        var href = $(this).attr('href');
        var src = $(this).css('background-image').replace('"', '').replace('"', '').replace('url(','').replace(')','');


        var empty = '';
        if($(this).hasClass('empty')){
            empty = 'empty';
        }
        
        var video = '';
        if($(this).hasClass('video')){
            video = 'video';
        }
        
        var backgroundImg = '';
        if(src){
            backgroundImg = 'background-image:url('+src+');';
        }
        
        var wBonus = 0;
        var hBonus = 0;
        if(iwidth == 119){
            wBonus = -1;
        } 
        
        if(iheight == 79){
            hBonus = -1;
        }
        
        var addImg = '';
        if($(this).hasClass('video')){
            addImg = '<img src="/bitrix/templates/layout/images/playicon.png" style="margin:'+(-8-hBonus)+'px 0 0 '+(-8-wBonus)+'px;" />';
        }
        
        //$('.b-thumbnails-container .thumb-link.video img').css('z-index','10');
        
        $('.b-thumbnails-container').append('<a class="thumb-link generated '+empty+' '+video+'" href="'+href+'" style="'+backgroundImg+'position:absolute;top:'+(Math.ceil(top-(iheight - 74) /2))+'px;left:'+(Math.ceil(left-(iwidth - 74) /2))+'px;display:none;width:'+iwidth+'px;height:'+iheight+'px;" desc="'+desc+'" desctitle="'+desctitle+'">'+addImg+'</a>');
        
        $('.b-thumbnails-container .thumb-link.generated').fadeIn(200);
        
        var caller = $(this);
        
        $('.b-thumbnails-container .thumb-link.generated').bind('click',function(){
            //$('.b-thumbnails-container .thumb-link.video img').css('z-index','10');
            //caller.find('img').css('z-index', 10000);
            
            showPhotosPopup($(this));
            return false;
        });
        
        $('.b-thumbnails-container .thumb-link img').bind('click',function(){
            if($(this).css('z-index') == 10000 && $('#photos-popup').css('display') == 'none'){
                showPhotosPopup($(this).parent().siblings('.generated'));
                return false;
            }
        });
                                             
        $('.b-thumbnails-container .thumb-link.generated').bind('mouseleave',function(){
            $(this).remove();
        });
            
    });
    
    
    
    $('.b-thumbnails-container').bind('mouseleave', function(){
        $('.b-thumbnails-container .generated').remove();
    });
    
    $('.b-search-tiny input').bind('focus',function(){
        if($(this).attr('value') == $(this).attr('default')){
            $(this).attr('value', '');
        }
    });
    
    $('.b-search-tiny input').bind('blur',function(){
        if($(this).attr('value') == ''){
            $(this).attr('value', $(this).attr('default'));
        }
    });
    
    if($("a[rel^='prettyPhoto']").length){
        $("a[rel^='prettyPhoto']").prettyPhoto({theme: 'dark_square'});
    }
    
    if($("a[rel^='prettyVid']").length){
        $("a[rel^='prettyVid']").prettyPhoto({theme: 'dark_square', default_width:640, default_height:360});
    }
    // Menu popup
    $('.b-header, .l-right, .b-content, .b-about-region-section, .b-links-container.three-columns a').bind('mouseover', function(){
        hideMenuPopup();

    });
    
    $('.b-menu-popup a').unbind('mouseover');
    
    $('.b-links-container.three-columns .three li:first-child a').unbind('mouseover').bind('mouseover', function(){
    
        if($('.b-menu-popup').css('display') == 'none'){
            var h = parseInt($('.b-menu-popup').height());
        
            $('.b-menu-popup').css('display','block').css('opacity','0').animate({'opacity':1,'bottom': parseInt($('.b-links-container.three-columns').height())+30 },200, function(){
            });            
        }
    });

    /*$('.b-banners-gov a, .b-banners-bottom a').hover(function(){
        $(this).animate({'opacity': '1'}, 200);
    }, function(){
        $(this).animate({'opacity': '.5'}, 100);
    });*/

    $('.b-banners-blog a').hover(function(){
        $(this).animate({'opacity': '1'}, 200);
    }, function(){
        $(this).animate({'opacity': '.85'}, 100);
    });
    
    /* Logo animate
    $('.main-page .b-logo').css('margin-left', '800px');
    $('.main-page .b-logo').animate({'margin-left':0},800);
    */
        
});
