var interval = 3000;
var carouselAttached = false;
var list_carousel_timeout = new Array;
    
$(document).ready(function() {
    
    setHomepageLinks();
    $("#building_container").jCarouselLite({
        btnNext: "#who_right",
        btnPrev: "#who_left"
    });
    $("#talking_container").jCarouselLite({
        btnNext: "#talking_right",
        btnPrev: "#talking_left"
    });
    $("#favorite_container").jCarouselLite({
        btnNext: "#fav_right",
        btnPrev: "#fav_left"
    });
    $('a.close_hp_content').click(function(){
       closeHpLayers();
       return false;
    });
    
    var random_project = Math.floor(Math.random() * (6 - 2 + 1)) + 2;
    $('#home_two_tab_content_projects .video_still').attr('src','/img/video_still_' + random_project + '.jpg');
    
    var homeTwoTab = $('#home_two_tabs a');
    homeTwoTab.click(function(){
        
        $('#home_two_tabs li').removeClass('active');
        var index = homeTwoTab.index(this);
        var tab = $(this).text().replace(' ','-').toLowerCase();
        
        $(this).parent().addClass('active');
        window.location.hash = '+' + tab;
        
        $('.two_tab_content').hide();
        $('.two_tab_content:eq('+ index +')').fadeIn();
        
        if(index == 1) {
            listCarousel('lc_recent_projects',3);
            carouselAttached = true;
        }
        
        return false;
    });
    
    $('#play_tab_about').click(function(){
        $('#modal_container').fadeIn('fast',function(){
            $('#modal_content').html('<img src="/img/ajax-loader.gif" alt="Loading" id="loader" /><p class="title">Meet The IdeaLists</p><iframe src="http://player.vimeo.com/video/25672281?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=true" width="849" height="472" frameborder="0"></iframe>')
            .addClass('video_modal_content');
        }).css({
            'width':'919px',
            'left' : '80px',
            "top": $(window).scrollTop() - 40 + "px"
        });
        
        return false;
    });
    $('#play_tab_projects').click(function(){
        $('#modal_container').fadeIn('fast',function(){
            $('#modal_content').html('<img src="/img/ajax-loader.gif" alt="Loading" id="loader" /><p class="title">The IdeaLists Reel: Our First Year Favorites</p><iframe src="http://player.vimeo.com/video/22135281?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=true" width="849" height="472" frameborder="0"></iframe>')
            .addClass('video_modal_content');
        }).css({
            'width':'919px',
            'left' : '80px',
            "top": $(window).scrollTop() - 40 + "px"
        });
        
        return false;
    });
});

function listCarousel(id, show){
    
    clearInterval(list_carousel_timeout[id]);
    
    var num_showing = show-1;
    var list_showing = 1;
    var total_lists = Math.ceil($('#lc_recent_projects li').length / show);
    var $this = $('#'+id);
    
    //show initial list items
    $this.find('li:gt('+ num_showing +')').hide();
    $this.find('li:lt('+ show +')').show();
    
    list_carousel_timeout[id] = setInterval(function(){
        
        $this.find('li:visible').hide();
        
        if(list_showing < total_lists){
            list_showing = list_showing + 1;
            
            $this.find('li').slice(show*(list_showing-1),show*list_showing).fadeIn();
        } else {
            $this.find('li:lt('+ show +')').fadeIn(function(){
                list_showing = 1;
            });
        }

    }, interval);
}

function setHomepageLinks(){
    $('div#car_top a').click(function(){

       tab_id = $(this).attr('id') + '_tab';
       section_id = $(this).attr('id') + '_section';
       target_id = $(this).attr('id') + '_hp_c';

       $('div#car_top a span').removeClass('selected');
       $('div#car_top a span').html('learn more');

       $('.car_content').hide();

       if ( $('#' + tab_id).is(":visible") ){
           $('div.homepage_tab').hide();
           //$('#' + target_id).fadeOut(1000);
           

       } else {
           $('div.homepage_tab').hide();
           $('#' + tab_id).show();
           $(this).children('span').addClass('selected');
           $(this).children('span').html('close');

           $('#' + target_id).show();
       }
       return false;
    });
}

function closeHpLayers(){
    $('.car_content').hide();
    $('div#car_top a span').removeClass('selected');
    $('div#car_top a span').html('learn more');
    $('.car_content').hide();
    $('div.homepage_tab').hide();
}
