// Problem:
// The SIMILE timeline plugin doesn't render completely 
// when used with the post tabs plugin.
// It seems that the timeline cannot be used in a way
// such that it is rendered "in the background" and then 
// unhidden. For example, if we either move the timeline 
// out  of the post tab shortcodes, OR put the timeline 
// under the first tab that is displayed, then the timeline
// will be rendered completely. Otherwise, it won't, and a 
// manual refresh needs to be done to force it to be redrawn.
// 
//
// This script is a workaround for that problem.
//
// It detects a hardcoded hash that contains the
// timeline, then forces a reload of the page.
function reloadIfWork(e) {
    var target = e ? e.target : window.event.srcElement;
        
    if ('WORK'==target.text.toUpperCase()) {
        window.location.reload(true);
    }
}

jQuery(document).ready(function(){
    // Pagination is acting weird on the WP/PHP side.
    // Putting wp_rest_query solves some pagination problems but 
    // causes new ones. We just want to hide the next/prev in the
    // home page. Resorting to javascript.
    if ('/'==document.location.pathname) {
        jQuery('div.newer').hide();
    }
    
    if ('/supporters/' == document.location.pathname) {
        var s = '';
        for (var i=0 ; i<quoteRotator.numQuotes; ++i) {
            s += quoteRotator.quotes[i];
        }
        jQuery('div#quote_in_page_container div#content').html(s);
    }
    
    if ('/support/' == document.location.pathname) {
        highlight_menu(679);
    }
    else if ('/feature/' == document.location.pathname) {
        highlight_menu(974);
    }
    else if (-1 != document.location.pathname.indexOf('/category/gallery/', 0)) {
        highlight_menu(238);
    }
    else if (-1 != document.location.pathname.indexOf('/category/', 0)) {
        highlight_menu(711);
    }
    

    jQuery("div.letter_details a").click(show_letter_details);
      
    /*var anchors = document.getElementsByTagName('A');
    var N = anchors.length;
    for (var i=0;i<N; i++) anchors[i].onclick = reloadIfWork;*/
});

function highlight_menu(menu_num) {
    jQuery('div#menu1 ul.rMenu li#menu-item-' + menu_num + ' a').css({backgroundColor:'#ff7400'});
}

function unhide_all_faq() {
    var faq_ids = [//'faq',
'faq_onepage',
'faq_why7k',
'faq_costanalysis',
'faq_costdetail',
'faq_fullcost',
'faq_howhelp',
'faq_morequestions'];
    var N = faq_ids.length;
    for (var i=0; i<N; i++) {
        show(faq_ids[i]);
    }
}

function setDisplay(e, display_style) {
    if (!e) return;
    e.style.display = display_style;
}

function setDisplayById(id, display_style) {
    var e = document.getElementById(id);
    setDisplay(e, display_style);
}

function hide(id) { setDisplayById(id, 'none'); }
function show(id) { setDisplayById(id, 'block'); }
function toggle(id) {
    var e = document.getElementById(id);
    if (!e) return;
    setDisplay(e, ('none'==e.style.display || !e.style.display)  ? 'block' : 'none');
}

function gallery(id) {
    PicLensLite.start({feedUrl:'/wp-content/plugins/nextgen-gallery/xml/media-rss.php?gid=' + id + '&mode=gallery'});
}

function nextquote() {
    quoteRotator.i = ((quoteRotator.i + 1) % quoteRotator.numQuotes);
    jQuery('div#quote_in_page_container div#content').html(quoteRotator.quotes[quoteRotator.i]);
}

function prevquote() {
    quoteRotator.i = ((quoteRotator.i - 1) % quoteRotator.numQuotes);
    if (quoteRotator.i<0) {
        quoteRotator.i = (quoteRotator.numQuotes - 1);
    }
    jQuery('div#quote_in_page_container div#content').html(quoteRotator.quotes[quoteRotator.i]);
}

function show_tell_your_friends() {
    title = 'Tell Your Friends';
    top.consoleRef=window.open('','myconsole',
    'width=700,height=500'
    +',menubar=0'
    +',toolbar=1'
    +',status=0'
    +',scrollbars=1'
    +',resizable=1');

    css = '';//"<link rel='stylesheet' href='/?bfa_ata_file=css' type='text/css' media='all' />";

    content = '<html><body><center><iframe marginheight="0" marginwidth="0" frameborder="0" width="100%" height="500" src="https://app.etapestry.com/hosted/eTapestry.com/TellAFriend.html?donationLink=http://www.buddhistliteraryheritage.org"></iframe></center></body></html>';
    top.consoleRef.document.writeln(content);
    top.consoleRef.document.close();
}


function show_letter_details() {
    title = '';
    content = jQuery(this).parent().children('#english_translation').html();

    top.consoleRef=window.open('','myconsole',
    'width=600,height=400'
    +',menubar=0'
    +',toolbar=1'
    +',status=0'
    +',scrollbars=1'
    +',resizable=1');

    css = '';//"<link rel='stylesheet' href='/?bfa_ata_file=css' type='text/css' media='all' />";

    top.consoleRef.document.writeln('<html><head><title>' + title + '</title>' + css + '</head>'
    +'<body bgcolor=white onLoad="self.focus()">'
    +content
    +'</body></html>'
    );

    top.consoleRef.document.close();
}

function click_anchor(anchor_namwe) {
    window.location.hash = anchor_namwe;
    window.location.reload(true);
}

function construct_tab_nav(this_tab_num, prev_link, prev_title, next_link, next_title) {
    //var anchors = document.getElementsByTagName('ul li A');
    var anchors = jQuery('div#tabs_0 ul li a');
    var N = anchors.length;
    var magic_nohash = 'tabs-0-';
    var magic = '#' + magic_nohash;
    
    // number of tabs
    var tabs = new Array();
    var hrefs = new Array();
    for (var i=0; i<N; i++) {
        href = anchors[i].href;
        text = anchors[i].text;
        var pos = href.indexOf(magic, 0);
        if (-1==pos) continue;
        //if (-1!=href.indexOf('javascript:', 0)) continue;
        tabs.push(text);
        hrefs.push(href);
    }

    // Get previous/next
    var is_start_tab = this_tab_num < 1;
    var is_end_tab = this_tab_num >= (tabs.length - 1);
    
    var prev = '';
    // prev is prev_tab, if there is one. otherwise, prev_link, if there is one. otherwise, none.
    if ((!is_start_tab) && (tabs.length > 0)) {
        // There are tabs, and this is not the start tab.
        // get the previous tab.
        var prev_index = this_tab_num - 1;
        prev += ("<a href=\"javascript:click_anchor('" + magic_nohash + prev_index + "');\">&laquo; " +  tabs[prev_index].toLowerCase() + "</a>");
    }
    else if (''!=prev_link) {
        prev += ("<a href='" + prev_link + "'>&laquo; " +  prev_title + "</a>");
    }
    else {
        // no previous link/tab
    }
    var prev_div = '<div class="prev_alignleft">' + prev + '</div>';
    
    var next = '';
    if ((!is_end_tab) && (tabs.length > 0)) {
        // There are tabs, and this is not the end tab.
        // get the next tab.
        var next_index = this_tab_num + 1;
        next += ("<a href=\"javascript:click_anchor('" + magic_nohash + next_index + "');\">" +  tabs[next_index].toLowerCase() + " &raquo;</a>");
    }
    else if (''!=next_link) {
        next += ("<a href='" + next_link + "'>" +  next_title + " &raquo;</a>");
    }
    else {
        // no next link/tab
    }
    var next_div = '<div class="next_alignright">' + next + '</div>';

    document.writeln(prev_div + next_div);
}
