/* JAVASCRIPT LIBRARY FOR COVER4CARAVANS */

//quick hack to make sure Caravan News' breadcrumb says the right thing.
$(document).ready(function(){
    if($(".current_page_item").find(".inner_sub_li").text().trim() == 'Articles'){
        $("#breadcrumbs strong").text('Articles');
    }
});

/*  Catch and rewrite any loose links that still aren't behaving correctly */
$(document).ready(function(){
    $("a").each(function(){
        $(this).click(function(){
            if(($(this).attr('onclick') == null) && ($(this).attr('href').split("#")[1] == undefined) && ($(this).attr('href').split(":")[0] != "mailto") && ($(this).attr('href').split(".")[1] == 'alanblunden')){
                link($(this).attr('href'));
                return false;
            }
        })
    });
});

/* handle empty searches */
$("#search_button").click(function(event){ 
    if($("#fix_field").val() == ''){
        $("#fix_field").focus();
        event.preventDefault();
    }
})

/* Redirect to another page along with a variable to help preserve vertical position */
function link(url){
    scroll = window.pageYOffset;
    if(scroll == undefined){
        scroll = $(document).scrollTop();
    }
    if(scroll > 0){
        document.location.href = url + "#" + scroll;
    } else {
        document.location.href = url;
    }
    return false;
}

/* Restores a h*/
function scroll(){
    scroll = document.URL.split("#")[1];
    //to avoid javascript errors from picky browsers, make sure we only check the length if it exists at all
    if(scroll != undefined){
        if(scroll.length > 0){
            if(scroll == 'article'){
                window.scrollTo(0, 630);
            } else {
                window.scrollTo(0, scroll);
            }
        }
    }
}

function popUp(location, title, height, width){
    window.open(location, title, 'height = 650, width = 650');
}

function faqLoadPresets(){
    $(document).ready(function(){
        $(".faq_button").addClass("faq_inactive");
        $(".faq_question").addClass("hidden_question");
        $("#who_are_we_button").removeClass("faq_inactive").addClass("faq_active");
        $("#who_are_we_question").removeClass("hidden_question").addClass("active_question");    
    })
}
function showFaq(q){
    var button = q + "_button";
    var question = q + "_question";
    
    $(".faq_button").removeClass("faq_active").addClass("faq_inactive");
    $('#' + button).removeClass("faq_inactive");
    $('#' + button).addClass("faq_active");
  
    $(".faq_question").css('display', 'none').css('visibility', 'hidden');
    $('#' + question).css('display', 'block').css('visibility', 'visible');
}
