/**************
all inhouse javascript goes here
**************/


if(!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] === obj) {
                return i;
            }
        }
        return -1;
    }
}
function get_ng_ns() {
    if(!window.ng) {
        window.ng = {};
    }
    return window.ng;
}
function jumpToAnchor(anchor) {
    var url = window.location.toString();
    window.location = url.replace(/#.*$/, '') + '#' + anchor;
}
function ng_autolink(txt){
    if(!txt) return;
    var txtstr = txt + ''; //make sure it's a string
    var url_re = /(http(s)?:\/\/[^\s)]+)/ig;
    return txtstr.replace(url_re, '<a href="$1" target="_blank" rel="nofollow">$1</a>') || txt;
}

function ngpopup(url, winname, v_width, v_height) {
    var popWidth= v_width || "800";
    var popHeight= v_height || "600";
    windowOptions = "width=" + popWidth + ", height=" + popHeight + ", menubar=no,scrollbars=yes,status=no,toolbar=no,location=no,resizable=yes";
    return window.open(url,winname,windowOptions);
}

/* ****** page tools: collapsible section with + - images ********* */
function toggleMenu(objID,objParentID) {
if (!document.getElementById) return;
var ob = document.getElementById(objID).style;
var ctrlOb = document.getElementById(objParentID).style;
 if(ob.display == '' || ob.display == 'none'){
  ctrlOb.backgroundImage = 'url(/css/icon_tools_share_minus.gif)';
  ob.display = 'block';
 }else{
  ctrlOb.backgroundImage = 'url(/css/icon_tools_share_plus.gif)';
  ob.display = 'none';
 }
}
function hideMenuOnLoad(objID){  // same objID that's passed to toggleMenu()
 /* ***
 * can't have display:none in CSS - JS might be disabled in browser, can do
 *	1. call this function from <body onload=()> to initially collapse the menu
 *	2. or set "display:block" for collapsible list inside <noscript> tag
 *** */
 if (!document.getElementById) return;
 var ob = document.getElementById(objID).style;
 ob.display = 'none';
}

/* ****** page tools:share on facebook specific encode-url javascript ******* */
function fbs_click() {
// inside <a> javascript:void(window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent('http://www.washingtonpost.com/wp-dyn/content/article/2008/10/10/AR2008101000003.html?referrer=facebook')+'&t='+encodeURIComponent('Dispelling Illusions'),'sharer','toolbar=no,width=642,height=436'));
 u=location.href;
 t=document.title;
 window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
 return false;
}


/* ****** clickable tabs with links in the right column  ********* */
function init_ti_forum_tabs(){
    $('#titab').click(function(ev){
                        if(!$('#techInsider-forum-menu').hasClass('tableft')){
                          $('#techInsider-forum-menu').addClass('tableft');
                          $('#techInsider-forum-menu').removeClass('tabright');
                          $('#tab-links-forum').hide();
                          $('#tab-links-techins').fadeIn(1000);
                        }
                        ev.preventDefault();
                      });
    $('#forumtab').click(function(ev){
                           if(!$('#techInsider-forum-menu').hasClass('tabright')){
                             $('#techInsider-forum-menu').addClass('tabright');
                             $('#techInsider-forum-menu').removeClass('tableft');
                             $('#tab-links-techins').hide();
                             $('#tab-links-forum').fadeIn(1000);
                           }
                           ev.preventDefault();
                        });
}
function init_ti_resources_tabs(){
    $('#titab').click(function(ev){
                           if(!$('#techInsider-forum-menu').hasClass('tableft')){
                             $('#techInsider-forum-menu').addClass('tableft');
                             $('#techInsider-forum-menu').removeClass('tabright');
                             $('#tab-links-resources').hide();
                             $('#tab-links-techins').fadeIn(1000);
                           }
                           ev.preventDefault();
                         });
    $('#resourcestab').click(function(ev){
                               if(!$('#techInsider-forum-menu').hasClass('tabright')){
                                 $('#techInsider-forum-menu').addClass('tabright');
                                 $('#techInsider-forum-menu').removeClass('tableft');
                                 $('#tab-links-techins').hide();
                                 $('#tab-links-resources').fadeIn(1000);
                               }
                               ev.preventDefault();
                      });
}
function init_resources_forum_tabs(){
    $('#resourcestab').click(function(ev){
                               if(!$('#techInsider-forum-menu').hasClass('tableft')){
                                 $('#techInsider-forum-menu').addClass('tableft');
                                 $('#techInsider-forum-menu').removeClass('tabright');
                                 $('#tab-links-forum').hide();
                                 $('#tab-links-resources').fadeIn(1000);
                               }
                               ev.preventDefault();
                      });
    $('#forumtab').click(function(ev){
                           if(!$('#techInsider-forum-menu').hasClass('tabright')){
                             $('#techInsider-forum-menu').addClass('tabright');
                             $('#techInsider-forum-menu').removeClass('tableft');
                             $('#tab-links-resources').hide();
                             $('#tab-links-forum').fadeIn(1000);
                           }
                           ev.preventDefault();
                         });
}
function init_wb_resources_tabs(){
    $('#wbtab').click(function(ev){
                           if(!$('#techInsider-forum-menu').hasClass('tableft')){
                             $('#techInsider-forum-menu').addClass('tableft');
                             $('#techInsider-forum-menu').removeClass('tabright');
                             $('#tab-links-resources').hide();
                             $('#tab-links-forum').fadeIn(1000);
                           }
                           ev.preventDefault();
                         });
    $('#resourcestab').click(function(ev){
                               if(!$('#techInsider-forum-menu').hasClass('tabright')){
                                 $('#techInsider-forum-menu').addClass('tabright');
                                 $('#techInsider-forum-menu').removeClass('tableft');
                                 $('#tab-links-forum').hide();
                                 $('#tab-links-resources').fadeIn(1000);
                               }
                               ev.preventDefault();
                      });
}
function init_most_read_emailed_tabs(){
    $('#metab').click(function(ev){
                        if(!$(this).hasClass('selected')){
                          $(this).attr('class','selected');
                          $('#mrtab').attr('class','');
                          $('#tab-most-read').hide();
                          $('#tab-most-emailed').fadeIn(1000);
                        }
                        ev.preventDefault();
                      });

    $('#mrtab').click(function(ev){
                        if(!$(this).hasClass('selected')){
                          $(this).attr('class','selected');
                          $('#metab').attr('class','');
                          $('#tab-most-emailed').hide();
                          $('#tab-most-read').fadeIn(1000);
                        }
                        ev.preventDefault();
                      });

}

$(document).ready(function(){
    $('#column_right .textad .status_text').each(function() {
                                                     var s = $(this);
                                                     var n = ng_autolink(s.html());
                                                     s.html(n);
                                                 });

    if($('#titab').length > 0 && $('#resourcestab').length > 0) {
        init_ti_resources_tabs();
    }
    if($('#titab').length > 0 && $('#forumtab').length > 0) {
      init_ti_forum_tabs();
    }
    if($('#resourcestab').length > 0 && $('#forumstab').length > 0) {
      init_resources_forum_tabs();
    }
    if($('#wbtab').length > 0 && $('#resourcestab').length > 0) {
      init_wb_resources_tabs();
    }

    init_most_read_emailed_tabs();
});
