/**
$.fn.DELAY = function( time, name ) {

    return this.queue( ( name || "fx" ), function() {
        var self = this;
        setTimeout(function() { $.dequeue(self); } , time );
    } );

};
/**/

var week_num = 1;

var hiddenPanels = false;

var trans_delay = 250;

var about_us_cntl = 0;

var arrows = new Array();
var at_glance_img;
var at_glance = 0;
var promo_video;
var month_gallery;

function setupPanels(wnum) {
    week_num = wnum;

    if(document.images)
    {
        arrows[0] = new Image(18, 25);
        arrows[0].src="images/teasers/down_arrow.png";
        arrows[1] = new Image(18, 25);
        arrows[1].src="images/teasers/up_arrow.png";
        
        at_glance_img = new Image(400, 503);
        at_glance_img.src = "images/teasers/series/february_1.png";
        month_gallery = new Image(160, 120);
        month_gallery.src = "images/teasers/feb.png";        
        promo_video = new Image(160, 120);
        promo_video.src = "images/teasers/promo.png";        
    }
    
    //$('#to_know').css('top', -215);
    //$('#to_know').css('top', -488);
    $('#to_know').css('top', -373);
    $('#to_know #arrow').attr('src', arrows[at_glance].src);
    $('#to_know #arrow').css('width', 18);
    $('#to_know #arrow').css('height', 18);
    //$('#to_know #arrow').css('left', 215);
    $('#to_know #arrow').css('left', 115);
    //$('#videogallery').css('position', 'absolute').css('top', 420).css('left',168).css('width',100).css('height',75);
    $('#videogallery').css('position', 'absolute').css('top', 438).css('left',212).css('width',100).css('height',75);
    //$('#videogallery img').css('width',100).css('height',75);
    $('#videogallery img').css('width',80).css('height',60);
    $('#videogallery img').css('padding', 0).css('margin',0);
    $('#videogallery img').css('border','solid black 2px');
    $('#videogallery a').css('width', 100);
	
	$('#month_gallery').css('position', 'absolute').css('top', 440).css('left',112).css('width',100).css('height',75);
	$('#month_gallery img').css('width',80).css('height',60);
	$('#month_gallery img').css('padding',0).css('margin',0);
	$('#month_gallery img').css('border','solid black 2px');
    $('#month_gallery a').css('width', 100);

    $('#the_month').css('background-color', 'white');
    

    $('.pp_pic_holder').remove();
    $('.pp_overlay').remove();
    $('.ppt').remove();

    if($(".clearfix > a[rel^='prettyPhoto']").length > 0)
    {
        $(".clearfix > a[rel^='prettyPhoto']").prettyPhoto();
    }
	
    $('#to_know h3').click(function(e) {
        e.preventDefault();
        if(at_glance == 0)
        {
            //$('#to_know').stop().animate({top: '0px'}, 500);
            //$('#to_know').stop().animate({top: '20px'}, 500);
            $('#to_know').stop().animate({top: '130px'}, 500);
        }
        else
        {
            //$('#to_know').stop().animate({top: '-215px'}, 500);
            //$('#to_know').stop().animate({top: '-488px'}, 500);
            $('#to_know').stop().animate({top: '-373px'}, 500);
        }
        at_glance = (at_glance + 1) % 2;
        $('#to_know #arrow').attr('src', arrows[at_glance].src);
    });
    
    $('#to_know #arrow').click(function(e) {
        e.preventDefault();
        if(at_glance == 0)
        {
            //$('#to_know').stop().animate({top: '0px'}, 500);
            $('#to_know').stop().animate({top: '130px'}, 500);
        }
        else
        {
            //$('#to_know').stop().animate({top: '-215px'}, 500);
            $('#to_know').stop().animate({top: '-373px'}, 500);
        }
        at_glance = (at_glance + 1) % 2;
        $(this).attr('src', arrows[at_glance].src);
    });
    
    //$(".clearfix a[rel^='prettyPhoto']").prettyPhoto();    
    
    setupWorship();
}

function hideContent(callback) {
    $('#content').hide(callback);
}

function showContent() {
    $('#content').show();
}

var denis_top = 425;
var calendar_photos_top = 187;
var calendar_photos_left = 521;
var calendar_title_top = 49;
var calendar_time = 1000;

function setupCalendar() {
    $('#actual_calendar').css('top', 700).css('left', 50);
    $('#calendar_title').css('left', -990).css('top',0);
    $('#calendar_denis').css('left', -400).css('top', denis_top);
    $('#calendar_photos').css('left', calendar_photos_left).css('top', 700);
}

function hideCalendar(callback) {
    $('#calendar_title').stop().animate({left:'-990px'},{queue:true, duration:calendar_time});
    $('#calendar_denis').stop().animate({left:'-400px'},{queue:true, duration:calendar_time});
    $('#calendar_photos').stop().animate({top:'700px'},{queue:true, duration:calendar_time});
    $('#calendar_photos').css('left', calendar_photos_left);
    $('#actual_calendar').stop().animate({top:'700px'}, calendar_time, 'linear', function() {
        $('#calendar').stop(true, true).hide('slow', callback);});
}

function showCalendar() {
    setCookie('current_control', 'events', 1);
    $('#calendar').css('left', 0);
    $('#calendar_photos').css('left', calendar_photos_left);
    $('#content').show();
    $('#calendar').show('fast', function() {
        $('#calendar_title').stop().animate({left:'0px'},{queue:true, duration:calendar_time});
        $('#calendar_denis').stop().animate({left:'0px'},{queue:true, duration:calendar_time});
        $('#calendar_photos').stop().animate({top:calendar_photos_top+'px'},{queue:true, duration:calendar_time});
        var winW = $(window).width();
    //    var minW = parseInt($('body').css('minWidth'),10);
    //    if(winW < minW)
    //    {
    //        $('#calendar').css('left', 0);
    //    }
    //    else
    //    {
    //        $('#calendar').css('left', (winW - 800) / 2);
    //    }
        
        $('#actual_calendar').stop().animate({top:'125px'}, {queue:false, duration:calendar_time});
    });
}

function hideHistory(callback) {
    $('#history').hide(callback);
}

function hideAboutUs(callback) {
    var s_time = 500;
    $('#about_us').each(function() {
        $(this).stop().animate({top:'-650px'}, {queue:true, duration:s_time}).hide('slow', callback);  
    }); 
//    $('#about_us #controls').hide(callback);
}

function hideCoreValues(callback) {
    $('#core_values').hide(callback);
}

function hideBeliefs(callback) {
    $('#believe').hide(callback);
}

function hideFunnel(callback) {
    $('#funnel').hide(callback);
}

function showHistoryHelper(divs) {
    $(divs[0]).css('left', 5);
    $(divs[0]).css('top', 70);
    $(divs[1]).css('left', 400);
    $(divs[1]).css('top', 15);
}

function showHistory() {
    $('#history').show();
    //showHistoryHelper($('#history div').get());
}

function showCoreValuesHelper(core_divs) {
    values_hdr = $('#core_values h2').get(0);
    $(values_hdr).show();

    $(core_divs[0]).css('left', 5);
    $(core_divs[0]).css('top', 70);
    $(core_divs[1]).css('left', 400);
    $(core_divs[1]).css('top', 15);   
    
    $(core_divs).show();
}

function showCoreValues() {
    $('#core_values').show();
    //showCoreValuesHelper($('#core_values div').get());
}

function showBeliefsHelper(belief_divs) {
    beliefs_hdr = $('#believe h2').get(0);
    $(beliefs_hdr).show();

    $(belief_divs[0]).css('left', 5);
    $(belief_divs[0]).css('top', 70);
    $(belief_divs[1]).css('left', 400);
    $(belief_divs[1]).css('top', 15);   
    
    $(belief_divs).show();
}

function showBeliefs() {
    $('#believe').show();
    //showBeliefsHelper($('#believe div').get());
}

function showFunnelHelper(imgs, divs) {
    $(imgs).css('position', 'absolute');
    $(imgs[0]).css('left', 5).css('top', 100);
    $(imgs[1]).css('left', 450).css('top',   2);
    $(divs[0]).css('left', 460).css('top',  10);
    $(divs[0]).css('background-image', 'url(images/bg/social_vision_top.png)');
    $('p',divs[0]).css('background-image', 'url(images/bg/social_vision.png)');
    $(divs[1]).css('left', 460).css('top', 155);
    $(divs[1]).css('background-image', 'url(images/bg/mission_top.png)');
    $('p',divs[1]).css('background-image', 'url(images/bg/mission.png)');
    $(divs[2]).css('left', 460).css('top', 260);
    $(divs[2]).css('background-image', 'url(images/bg/culture_shaping_top.png)');
    $('p',divs[2]).css('background-image', 'url(images/bg/culture_shaping.png)');
    $(divs[3]).css('left', 460).css('top', 355);       
    $(divs[3]).css('background-image', 'url(images/bg/spiritual_antagonism_top.png)');
    $('p',divs[3]).css('background-image', 'url(images/bg/spiritual_antagonism.png)');
    $(divs[4]).css('left', 25);       
}

function showFunnel() {
    $('#funnel').show();
    //showFunnelHelper($('#funnel img').get(), $('#funnel div').get());
}

function calculatePosition() {
    position = 0;
    if(about_us_cntl == 0)
        position = 90;
    else if(about_us_cntl == 1)
        position = 150;
    else if(about_us_cntl == 2)
        position = 210;
    else if(about_us_cntl == 3)
        position = 270;
        
    return position;
}

var about_positions = [30, 680, 1330, 1980];

function showAboutUsHelper() {
    about_cntl = $('#about_us #controls').get(0);
    cntls = $('#about_us #controls h2').get();

    $(cntls).css('padding-top', 15);    
    $(cntls[0]).css('top', 100);
    $(cntls[1]).css('top', 160);
    $(cntls[2]).css('top', 220);
    $(cntls[3]).css('top', 280);
    
    $('h2.bg').css('top', 90);
    
    var s_time = 500;
    
    $(cntls).each( function()
        {
            $(this).hover(
                function() {
                    if($(this).attr('class')!='active')
                    {
                        $(this).css('color', 'red');
                    }
                },
                function() {
                    if($(this).attr('class')!='active')
                    {
                        $(this).css('color', 'rgb(20, 20, 20)');
                    }
                });
                
            $(this).click( function(e)
                {
                    e.preventDefault();
                    $('#about_us #controls h2').css('color', 'rgb(20, 20, 20)');
                    $('#about_us #controls h2').removeClass('active');
                    $(this).addClass('active');
                    about_us_cntl = $('#about_us #controls h2').index(this);
                    position = calculatePosition();
                    var tmp = this;
                    
                    $('h2.bg').stop().animate({top:position+'px'}, {queue:true, duration:s_time});

                    var positions = [about_positions[0],about_positions[1],about_positions[2],about_positions[3]];
                    position = -about_us_cntl * 650;
                    positions[0] += position;
                    positions[1] += position;
                    positions[2] += position;
                    positions[3] += position;
                    
                    $('#funnel').stop().animate({top:positions[0]+'px'}, {queue:true, duration:s_time});
                    $('#history').stop().animate({top:positions[1]+'px'}, {queue:true, duration:s_time});
                    $('#core_values').stop().animate({top:positions[2]+'px'}, {queue:true, duration:s_time});
                    $('#believe').stop().animate({top:positions[3]+'px'}, {queue:true, duration:s_time});
                    $(this).css('color', 'rgb(255, 255, 255)');
                    return false;
                });
        });

    $(about_cntl).css('left', 750);
    $('#about_us').css('top', -650);
}

function setupAboutUs() {
    showHistoryHelper($('#history div').get());
    showCoreValuesHelper($('#core_values div').get());
    showBeliefsHelper($('#believe div').get());
    showFunnelHelper($('#funnel img').get(), $('#funnel div').get());
    showAboutUsHelper();
    padding = 30;
    if(false)//document.images)
    {
        var about_us_nav_img = new Image(277, 609);
        about_us_nav_img.src = "images/bg/about_nav.png";
        var funnel_img = new Image(448, 350);
        funnel_img.src = "images/about_us/funnel.png";
        var about_txt_bg = new Image(330, 450);
        about_txt_bg.src = "images/bg/text_panel.png";
    }
    
    $('#funnel').css('top', about_positions[0] + padding);
    $('#history').css('top', about_positions[1] + padding);
    $('#core_values').css('top', about_positions[2] + padding);
    $('#believe').css('top', about_positions[3] + padding);
}

function showAboutUs() {
    setCookie('current_control', 'about_us', 1);
    position = calculatePosition();
        
    $('h2.bg').css('top', position);
    about_cntl = $('#about_us #controls').get(0);
    $('#content').show();
    $('#about_us').show();
    $(about_cntl).show();
    $('#about_us').each(function() {
        $(this).stop().animate({top:'0px'}, {queue:true, easing: 'easeInOutCirc', duration:1000});
    }); 
}

function hideGiving(callback) {
    $('#give').stop(true, true).hide('slow',callback);
}

function showGiving() {
    setCookie('current_control', 'give', 1);
    $('#give').show('slow');
}

function hideContactUs(callback) {
    var num_contacts = $('.contact').length;
    
    $('.contact').each(function()
    {
        var index = $('.contact').index(this);
        $(this).css('top', Math.floor(index / 3) * 200 + 50);
        $(this).stop().animate({left:(-1500 + 100+Math.floor(index % 3)*350)+'px'}, 1000, "linear");
    });
    
    $('#contact_us').hide('slow',callback);
}

function setupContactUs() {
    $('.contact').each(function()
    {
        var index = $('.contact').index(this);
        $(this).css('position', 'absolute');
        $(this).css('top', Math.floor(index / 3) * 200 + 50).css('left', -400);
    });
}

function showContactUs() {
    setCookie('current_control', 'contact_us', 1);
    var num_contacts = $('.contact').length;
    $('#content').show();
//    hideFunnel();
//    hideBeliefs();
//    hideCoreValues();
//    hideHistory();
    $('#contact_us').show();
    $('.contact').each(function()
    {
        var index = $('.contact').index(this);
        $(this).css('position', 'absolute');
        $(this).css('top', Math.floor(index / 3) * 200 + 50);
        $(this).stop().animate({left:(100+Math.floor(index % 3)*350)+'px'}, {queue:true, duration:1000});
    });
}

function showHome() {
    setCookie('current_control', 'home', 1);
    $('#content').stop(true, true).hide();
    $('#main').css('visibility','visible');//$('.teaser').show('fast');
    //, function() {
    //    $('#content').fadeOut();
    //});
    //$('#content').fadeOut();
    //$('#feature').fadeIn();
}

function showWeek()
{
    setCookie('current_control', 'week', 1);
    if(week_num == 1)
        showWorship();
    else if(week_num == 2 )
        showService();
    else if(week_num == 3)
        showLearn();
    else
        showCircles();
//    //showWorship();
//    //showService();
//    showLearn();
//    //showCircles();
}

function bringinNewbies() {
    $('.current').each(
        function() {
            var current_name = $('.current').text();
            //alert('current name: ' + current_name);
            //alert('Show '+current_name);
            if(current_name == "HOME")
            {
                showHome();
            }
            else if(current_name == "WORSHIP")
            {
                showWorship();
            }
            else if(current_name == "LEARN")
            {
                showLearn();
            }
            else if(current_name == "THIS WEEK")
            {
                showWeek();
            }
            else if(current_name == "EVENTS")
            {
                showCalendar();
            }
            else if(current_name == "ABOUT US")
            {
                showAboutUs();
            }
            else if(current_name == "CONTACT US")
            {
                showContactUs();
            }
            else if(current_name == "SERVE")
            {
                showService();
            }
            else if(current_name == "CIRCLES")
            {
                showCircles();
            }
            else if(current_name == "GIVE")
            {
                showGiving();
            }
        }
    );
}

function hideHome(callback) {
    //$('#feature').fadeOut("normal", bringinNewbies);
    $('#main').css('visibility', 'hidden');//stop(true, true).hide();//$('.teaser').hide('fast');
    $('#content').show('slow', callback);
}

function hideWeek(callback) {
    if(week_num == 1)
        hideWorship(callback);
    else if(week_num == 2 )
        hideService(callback);
    else if(week_num == 3)
        hideLearn(callback);
    else
        hideCircles(callback);
}

function removeOldies() {
    $('.toRemove').each(
        function() {
            var remove_name = $('.toRemove').text();
            var current_name = $('.current').text();
            //alert('remove name: ' + remove_name + 'current name: ' + current_name);
            //alert('Hiding '+remove_name);
            if(remove_name == "HOME")
            {
                hideHome(bringinNewbies);
            }
            else if(remove_name == "WORSHIP")
            {
                hideWorship(bringinNewbies);
            }
            else if(remove_name == "LEARN")
            {
                hideLearn(bringinNewbies);
            }
            else if(remove_name == "THIS WEEK")
            {
                hideWeek(bringinNewbies);
            }
            if(remove_name == "EVENTS")
            {
                hideCalendar(bringinNewbies);
            }
            else if(remove_name == "ABOUT US")
            {
                hideAboutUs(bringinNewbies);
            }
            else if(remove_name == "CONTACT US")
            {
                hideContactUs(bringinNewbies);
            }
            else if(remove_name == "SERVE")
            {
                hideService(bringinNewbies);
            }
            else if(remove_name == "CIRCLES")
            {
                hideCircles(bringinNewbies);
            }
            else if(remove_name == "GIVE")
            {
                //alert('remove name: ' + remove_name + 'current name: ' + current_name);
                hideGiving(bringinNewbies);
            }

            $('.toRemove').removeClass('toRemove');
        }
    );
}

function hideAllPanels(callback) {
    //hideAboutUs();
//    hideContactUs(function()
//    {  
//        hideCalendar(function()
//        {
//            hideAboutUs(function()
//            {
//                hideHistory(function()
//                {
//                    hideBeliefs(function()
//                    {
//                        hideContent(callback);
//                    });
//                });
//            });
//        });
//    });
    $('#feature').hide(); // comment out this line when you want to use the panel
    $('#content').hide();
    $('#calendar').hide();
    $('#contact_us').hide();
    $('#about_us').hide();
    $('#serve').hide();
    $('#circles').hide();
    $('#learn').hide();
    $('#worship').hide();
    $('#give').hide();
    
    setupCalendar();
    setupAboutUs();
    setupService();
    setupCircles();
    setupLearn();
//    $('#history').hide();
//    $('#core_values').hide();
//    $('#believe').hide();
    callback();
//    hideContent();
//    hideContactUs(function(){});
//    hideCalendar(function() {});
//    hideAboutUs(function() {});
//    hideHistory(function() {});
//    hideBeliefs(callback);
//    callback();
}

function updateSite() {
    removeOldies();
}
/**/