$(function() {
    $('a[rel$="external"]').addClass("external").click(function(){this.target = "_blank";});

    if($("#features").length){ //on home page
        //feature tabs
        $("#features").tabs({fx:{opacity: "toggle"}, selected:0}).tabs("rotate", 8000, true);
        $("#features").hover(function() {
                $("#features").tabs("rotate",0,true);
            },function() {
                $("#features").tabs("rotate",8000,true);
            }
        );
        //feed tabs
        $("#feeds").tabs({selected:0});
        $('#feeds .feedtext .item h2 a').click(function(e){
            var alink = $(this).attr('href')
            if($(this).hasClass('fbpop')){
                $.fn.colorbox({ iframe:true, width:'550px', title:'facebook', height:'630px',opacity:0.7, href: alink});
            }else{
                window.location = alink;
            }
            e.preventDefault();
            return false;
        });
        $('#feeds .feedtext .item').click(function(){
            $("h2 a", this).trigger('click');
        });

        $('#feeds .feedtext .item').hover(function(){
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });


        $(".slide-info").css('top','131px');

        $('.home-box-bottom a.link').click(function(e){
            window.location = $(this).attr('href');
            e.preventDefault();
            return false;
        });
        $('.home-box-content').click(function(){
            $(this).siblings('.home-box-bottom').children('a').trigger('click');
        });

        $('.home-box').hover(function(){
            $(this).addClass('hover');
            $(".slide-info", this).stop().animate({top:'78px'},{queue:false,duration:160});
        }, function() {
            $(this).removeClass('hover');
            $(".slide-info", this).stop().animate({top:'131px'},{queue:false,duration:160});
        });

        // slider
        var conveyor = $(".content-conveyor", $("#sliderContent")),
        item = $(".item", $("#sliderContent"));
        conveyor.css("width", item.length * parseInt(item.css("width")));
        var sliderOpts = {
            max: (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $("#sliderContent")).css("width")),
            slide: function(e, ui) {
                conveyor.css("left", "-" + ui.value + "px");
            }
        };
        $("#slider").slider(sliderOpts);

        //reflections
        $('#sliderContent .item img').reflect({height: 0.4, opacity: 0.3});
    }

    //home box interactions
    $("ul#nav li.pnav").mouseover(function(){
        $(this).addClass("hover");
        $("div", this).css('left','0px');
    });
    $("ul#nav li.pnav").mouseout(function(){
        $(this).removeClass("hover");
        $("div", this).css('left','-9999px');
    });



    var gcalpopped = false;
    $('#gcalpop').click(function(){
        if (gcalpopped==false){
            $('#gcal').html('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showDate=0&amp;showPrint=0&amp;showTabs=0&amp;showCalendars=0&amp;mode=AGENDA&amp;height=240&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=icchurchadmin%40googlemail.com&amp;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="464" height="240" frameborder="0" scrolling="no"></iframe>');
            gcalpopped=true;
        }
    });

    $('a.pop').colorbox({opacity:0.7}).click(function(){
        googleTracking($(this).attr("href"));
    });
    $('a.fbpop').colorbox({iframe:true, width:'550px', height:'630px',opacity:0.7});
    $('a.popvid').colorbox({iframe:true, width:'509px', height:'470px',opacity:0.7});
    $('a.mappop').colorbox({iframe:true, width:'729px', height:'476px',opacity:0.7});

    if ($('#content ul.list').length){
        //lists
        $('#content ul.list li h2 a').click(function(e){
            window.location = $(this).attr('href');
            return false;
            e.preventDefault();
        });
        $('#content ul.list li').click(function(){
            $("h2 a", this).trigger('click');
        });
        $('#content ul.list li').hover(function(){
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
    }
});
function googleTracking(page){
	pageTracker._trackPageview(page);
}

