var homeSliderActive = 0;
var homeNewsTimer;
var pageSliderActive = 0;
var searchShown = false;

$(document).ready(function() {
     
    $('.fancybox').fancybox();
     
    $('.content-side').each(function() {
        heightSide = $(this).height();
        if ($(this).next().hasClass('content-main'))
        {
            heightMain = $(this).next().height();
        
            height = heightMain - heightSide + 1;
            $(this).next().find('.content-side-bottom').css('height', height);
        }
    });
    
    $('.content-finish').each(function() {

        heightMain = $(this).find('.content-main').height();

        $(this).find('.content-side-bottom').css('height', heightMain);
    });
    
    /*$('.header-lang').hover(function() {
        $(this).stop().animate({
            width: 120
        }, 500);
        $(this).find('.lang-2').stop().animate({
            right: 40
        }, 500);
        $(this).find('.lang-3').stop().animate({
            right: 80
        }, 500);
    }, function() {
        $(this).stop().animate({
            width: 40
        }, 500);
        $(this).find('.lang-2').stop().animate({
            right: 0
        }, 500);
        $(this).find('.lang-3').stop().animate({
            right: 0
        }, 500);
    });*/
    
    $('.header-search span').click(function() {
        if (searchShown == true)
        {
            $(this).parent().parent().stop().animate({
                width: 40
            }, 500);
            searchShown = false;
        }
        else
        {
            $(this).parent().parent().stop().animate({
                width: 266
            }, 500);
            searchShown = true;
        }
    });
    
    $('.tabs').each(function() {
        var $tabs = $(this);
        var $menu = $tabs.prev();
        
        tabActivate($tabs, 0);
        
        $menu.find('a').click(function(e) {
            e.preventDefault();

            tabActivate($tabs, $(this).parent().index());
        });
    });
});

function decorateStatesSelect()
{
    $('.select-states').each(function() {
        $select = $(this);
        
        if ($select.is(':visible') == false)
        {
            return;
        }
        
        $select.hide();
        
        $container = $('<div class="select-replacement-container" />').insertAfter($select);
        $container.css('width', $select.css('width'));
        
        $wrap = $('<div class="select-replacement" />').appendTo($container);
        $wrap.css('width', $select.css('width'));
        
        $title = $('<a href="#" class="title" />').appendTo($wrap);
        $title.text($select.find('option:selected').text());
        $title.css('width', parseInt($select.css('width')));
        
        $list = $('<ul class="options" />').appendTo($wrap);
        $list.css('width', parseInt($select.css('width')) - 2);
        
        $select.find('option').each(function() {
            $list.append('<li class="' + $(this).attr('value') + '"><a href="#">' + $(this).text() + '</a></li>'); 
        });

        $title.click(function(e) {
            e.preventDefault();
        
            $('.select-replacement ul.options').hide();
        
            $currentLIst = $(this).parent().find('ul');
            if ($currentLIst.is(":visible"))
            {
                $currentLIst.hide();
            }
            else
            {
                $currentLIst.show();
            }
        });
        
        $('body').live('click', function(e) {
            if ($(e.target).closest('.select-replacement').length == 0)
            {
                $('.select-replacement ul.options').hide();
            }
        });
        
        $list.find('li a').click(function(e) {
            e.preventDefault();
            
            $currentList = $(this).parent().parent();            
            $currentList.hide();
            
            $currentWrap = $currentList.parent();
            $currentWrap.find('.title').text($(this).text());
            
            $currentSelect = $currentWrap.parent().prev();
            $currentSelect.val($(this).parent().attr('class'));
            $currentSelect.trigger('change');
        });
        
        $(this).change(function() {
        
            $currentWrap = $(this).next();
            $currentWrap.find('.title').text($(this).find('option:selected').text());
        
            val = parseInt($(this).val());
            if (val > 0)
            {
                $('.distribution-list .element').hide();
                $('.distribution-list .element-state-' + val).show();
                
                $('.distribution-map-box').css('background-position', '-' + (parseInt(val) * 411) + 'px 0px ');
                mapPosition = '-' + (parseInt(val) * 411) + 'px 0px ';
            }
            else
            {
                $('.distribution-list .element').show();
                
                $('.distribution-map-box').css('background-position', '0px 0px');
            }
            
            reStyleDistributionList();
        });
    });
}

function reStyleDistributionList()
{
    $('.distribution-list .element').removeClass('odd');
    $('.distribution-list .element').removeClass('even');
    
    currentClass = 'odd';
    $('.distribution-list .element').each(function() {
        if ($(this).is(":visible"))
        {
            $(this).addClass(currentClass);
            currentClass = (currentClass == 'odd') ? 'even' : 'odd';
        }
    });
}

function galleryClose($gallery)
{
    $gallery.find('.gallery-wrap').animate({
        height: 50
    }, 500, function() {
        $gallery.closest('.project-single').addClass('project-closed');
    });
    
    $gallery.prev().animate({
        height: 50
    }, 500);

}

function galleryShowImage($gallery, index)
{
    $gallery.find('.gallery-slider').animate({
        left: -600 * index
    }, 500);
    
    newHeight = $gallery.find('img').eq(index).height(); 
    
    $gallery.find('.gallery-wrap').animate({
        height: newHeight
    }, 500);
    
    $gallery.prev().animate({
        height: newHeight
    }, 500);
    
    $gallery.prev().find('.gallery-pagination li').removeClass('active');
    $gallery.prev().find('.gallery-pagination li').eq(index).addClass('active');
}

function tabActivate($tabs, index)
{
    $menu = $tabs.prev();

    $tabs.find('.tab').hide();
    $tabs.find('.tab').eq(index).show();
    
    $menu.find('li').removeClass('active');
    $menu.find('li').eq(index).addClass('active');
}

function pageSlideShowSlide(index)
{
    markerRight = $('.page-slider #icon-' + (index+1)).css('right');
    markerWidth = $('.page-slider #icon-' + (index+1)).css('width');

    $('.page-slider .marker').stop().animate({
        right: markerRight,
        width: markerWidth
    }, 1000);
    
    $('.page-slider .slide').hide();
    $('.page-slider .slide').eq(index).fadeIn(500);
   
    pageSliderActive = index;
}

function homeNewsShowNext()
{
    index = $('.hb-news-pages li.active').index();
    
    index++;
    if (index >= $('.hb-news-pages li a').length)
    {
        index = 0;
    }
    
    homeNewsShowSlide(index);
    homeNewsTimer = setTimeout(homeNewsShowNext, 2000);
}   

function homeNewsShowSlide(index)
{
    $('.hb-news-pages li').removeClass('active');
    $('.hb-news-pages li').eq(index).addClass('active');
    
    $('.hb-news-list .hb-news').hide();
    $('.hb-news-list .hb-news').eq(index).fadeIn(300);
}

function homeLightShowSlide(index)
{
    $('.home-light .menu li').removeClass('active');
    $('.home-light .menu li').eq(index).addClass('active');
    
    $('.home-light .menu-bottom span').removeClass('active');
    $('.home-light .menu-bottom span').eq(index).addClass('active');
    
    $('.home-light .slides .slide').hide();
    $('.home-light .slides .slide').eq(index).show();
}   

function homeSlideShowSlide(index)
{
    markerRight = $('.home-slider #icon-' + (index+1)).css('right');
    markerWidth = $('.home-slider #icon-' + (index+1)).css('width');

    $('.home-slider .marker').stop().animate({
        right: markerRight,
        width: markerWidth
    }, 1000);
    
    $('.home-slider .slides').stop().animate({
        left: -960 * index
    }, 1000, function() {
        $(this).css('left', -960 * index);
    });
    
    homeSliderActive = index;
    
}
