function menuInit ()
{
    var $submenus = $('.submenu'),
        $menuLinks = $('.menu a');
    
    $menuLinks
        .mouseover(function () 
        {
            if ($(this).is('.selected')) return;
            
            $menuLinks.removeClass('selected')
            $(this).filter('.with-submenu').addClass('selected')
            
            $submenus
                .fadeOut(100)
                .filter('[rel = "'+ $(this).attr('rel') +'"]')
                .fadeIn(100)
        })
}

function lj_share (url, text)
{
    var event = '<a href="http://' + window.location.hostname + url + '">' + text + '</a>',
        subject = text;
    
    window.open(
        'http://www.livejournal.com/update.bml?event='+ encodeURIComponent(event) +'&subject='+ encodeURIComponent(subject),
        'sharer',
        'toolbar=0,resizable=1,scrollbars=1,status=1,width=730,height=500'
    )
}

function fb_share(url, text) 
{
    var u = 'http://' + window.location.hostname + url,
        t = text;
    
    window.open(
        'http://www.facebook.com/sharer.php?u='+ encodeURIComponent(u) +'&amp;t='+ encodeURIComponent(t),
        'sharer',
        'toolbar=0,status=0,width=626,height=436'
    )
}

function twitter_share (url, text)
{
    var url = 'http://' + window.location.hostname + url,
        text = text;
    
    window.open(
        'http://twitter.com/share?url='+ encodeURIComponent(url) +'&amp;text='+ encodeURIComponent(text),
        'sharer',
        'toolbar=0,status=0,width=626,height=436'
    )
}

function vk_share (url, text)
{
    var url = 'http://' + window.location.hostname + url,
        text = text;
    
    window.open(
        'http://vkontakte.ru/share.php?url='+ encodeURIComponent(url) +'&amp;text='+ encodeURIComponent(text),
        'sharer',
        'toolbar=0,status=0,width=626,height=436'
    )
}


$(function () 
{
    /*
     * Photos
     */     
    var $allPhotos = $('.photos');
    
    $allPhotos.filter(':has(.selector)').each(function () 
    {
        var $photos  = $(this).find('.photo'),
            $squares = $(this).find('.selector .square');
            
        if (!$photos.length || !$squares.length) return;
        
        function nextPhoto ()
        {                                                                        
            $squares
                .filter('.selected')
                .next()
                .add($squares[0])
                .filter(':last')
                .trigger('click')                        
        }                

        var tid = setTimeout(nextPhoto, 5000)

        $squares.click(function () 
        {
            clearTimeout(tid)
            tid = setTimeout(nextPhoto, 5000)
            
            if ($(this).is('.selected')) return false;
            
            var index = $squares.removeClass('selected').index(this)
            $(this).addClass('selected')
            
            changePhoto(this, $photos)
                        
            return false
        })        
    })   
    
    function changePhoto (target, $photos)
    {
        var index = $(target.parentNode).find('div').removeClass('selected').index(target)
        $(target).addClass('selected')
        
        $photos
            .stop(true, true)                
            .fadeOut(1000)                
            .filter(':eq('+index+')')
            .fadeIn(1000)
    }
    
    $allPhotos.filter(':has(.slider)').each(function () 
    {
        var $slider = $(this).find('.slider'),
            $thumbs = $(this).find('.thumbs');
        
        $(this).find('.window').click(function() {
            switchPhoto(-1)
        })

        $thumbs
            .find('.thumb')
            .click(function () { 
                changePhoto(this, $allPhotos.find('.photo')) 
            })
        
        $slider
            .append($('<div class="arr left"/>'))
            .append($('<div class="arr right"/>'))
            .find('.left, .right')
                .click(function () 
                {
                    $(this).is('.left') 
                        ? switchPhoto( 1)
                        : switchPhoto(-1)
                })
        
        $(document.body).bind('keydown', function (e) 
        {
            if (e.keyCode == 37) switchPhoto( 1);
            if (e.keyCode == 39) switchPhoto(-1);            
        })
        
        function switchPhoto (direction) 
        {
            if ($thumbs.is(':animated')) return;
            
            var $thumb = direction < 0
                ? $thumbs.find('.selected').next().trigger('click')
                : $thumbs.find('.selected').prev().trigger('click')
            
            if (!$thumb.length) return;
            
            var dist = $thumb[0].offsetWidth + parseInt($thumb.css('margin-right'));
            
            $thumbs.animate({left: '+=' + dist * direction}, 100)
        }
    })
    
    /*
     * Videos
     */    
    $('.videos').each(function () 
    {
        var $_ = $(this);
        
        var $selectors = $_.find('.selector div'),
            $videos = $_.find('.video');
        
        $selectors.click(function ()
        {
            if ($(this).is('selected')) return;

            var index = $selectors.removeClass('selected').index(this)
            $(this).addClass('selected')
            
            $videos
                .stop(true, true)
                .fadeOut(1000)
                .filter(':eq('+index+')')
                .fadeIn(1000)
        })  
    })
    
    /*
     * Popup
     */
    var $popup = $('.popup')
    
    $('.popup-trigger').click(function (e) 
    {
        if ($popup.is(":visible")) {
            $popup.find('.title').html('')
            $popup.find('.text').html('')
            $popup.hide()
            if ($(this).hasClass('hover-disabled')){ 
                $('.hover-disabled').addClass('hover-enabled');
                return false;
            }
            $('.hover-disabled').addClass('hover-enabled');
        }
        
        $(this).addClass('hover-disabled');
        $(this).removeClass('hover-enabled');

        if ($(this).data('title') == $popup.find('.title').html()) 
        {
            $popup.find('.title').html('')
            $popup.find('.text').html('')
            return;
        }
        
        $popup.find('.title').html($(this).data('title'))
        $popup.find('.text').html($(this).data('text'))
        
        $popup.show()
        
        return false
    })
    
    //$popup.click(function() {return false})
    $(document).click(function () {
        $('.hover-disabled').addClass('hover-enabled');
        $('.hover-enabled').removeClass('hover-disabled');
        $popup.find('.title').html('')
        $popup.find('.text').html('')
        $popup.hide();
    })
})

