// uses the first segment of the page to set the nav section
// the body onload sets the correct sub-nav
// in the footer, code sets the orange tab for the current section

//function to check for a value in the query string. used for topics in the sub-nav
function getTopic(){
    var parts = window.location.pathname.split('/');
    
    if ((0 == window.location.pathname.indexOf('/CLK_CVverge') && parts.length > 4) || (window.location.pathname.indexOf('/CLK_CVverge') < 0 && parts.length > 3)) {
        return parts[3];
    }
    else {
        var file_parts = parts[parts.length - 1].split('.');
        
        return file_parts[0];
    }
}

if (!topic) {
    var topic = getTopic();
}
var title;


switch (topic) {
    case "Arts":{
        title = "Arts &amp; Humanities";
        break;
    }
    case "Edtech":{
        title = "Ed Tech";
        break;
    }
    case "Workforce":{
        title = "Workforce";
        break;
    }
    default:
        {
            if (topic) {
                title = topic;
                page = topic.toLowerCase();
            }
            else {
                title = 'Undefined';
                topic = 'Undefined';
            }
            break;
        }
}



var section;

switch (true) {
    case 0 == window.location.pathname.indexOf("/mag-subscribe") ||
    0 == window.location.pathname.indexOf("/newsletter") ||
    0 == window.location.pathname.indexOf("/rss") ||
    0 == window.location.pathname.indexOf("/CLK_CVverge/mag-subscribe") ||
    0 == window.location.pathname.indexOf("/CLK_CVverge/rss") ||
    0 == window.location.pathname.indexOf("/CLK_CVverge/newsletter"):{
        section = "subscribe";
        break;
    }
    case 0 == window.location.pathname.indexOf("/advertise") || 0 == window.location.pathname.indexOf("/CLK_CVverge/advertise"):{
        section = "advertise";
        break;
    }
    case 0 == window.location.pathname.indexOf("/paper") || 0 == window.location.pathname.indexOf("/CLK_CVverge/paper"):{
        section = "papers";
        break;
    }
    case 0 == window.location.pathname.indexOf("/blog") || 0 == window.location.pathname.indexOf("/CLK_CVverge/blog"):{
        section = "blogs";
        break;
    }
    case 0 == window.location.pathname.indexOf("/jobs") || 0 == window.location.pathname.indexOf("/CLK_CVverge/jobs"):{
        section = "jobs";
        break;
    }
    case 0 == window.location.pathname.indexOf("/award") || 0 == window.location.pathname.indexOf("/CLK_CVverge/award"):{
        section = "awards";
        break;
    }
    case 0 == window.location.pathname.indexOf("/event") || 0 == window.location.pathname.indexOf("/CLK_CVverge/event"):{
        section = "events";
        break;
    }
    case 0 == window.location.pathname.indexOf("/story") || 0 == window.location.pathname.indexOf("/CLK_CVverge/story"):
    case 0 == window.location.pathname.indexOf("/") || 0 == window.location.pathname.indexOf("/CLK_CVverge/"):{
        section = "stories";
        break;
    }
    default:
        {
            section = "";
            break;
        }
}



//jquery onload actions
$(document).ready(function(){

    var img_path = 0 == window.location.pathname.indexOf("/CLK_CVverge/") ? '/CLK_CVverge' : '';
    
    // frame image inserts
    $(".frame:not(.sidebar)").prepend('<img src="' + img_path + 'http://media.convergemag.com/designimages/article.box.top_CV.gif" alt="" />');
    $(".frame:not(.sidebar)").append('<img src="' + img_path + 'http://media.convergemag.com/designimages/article.box.bottom_CV.gif" alt="" />');
    $(".frame_thirds").prepend('<img src="' + img_path + 'http://media.convergemag.com/designimages/article.box.tops.third_CV.gif" alt="" />');
    $(".frame_thirds").append('<img src="' + img_path + 'http://media.convergemag.com/designimages/article.box.bottoms.third_CV.gif" alt="" />');
    
    // displays the correct sub-nav
    $("#sub_nav_" + section).show();
    
    // highlights the correct orange tab
    $("#nav_" + section).addClass("on");
    
    // highlights the correct sub-nav section
    $("#" + topic + "_link").css("color", "black");
    
    // nav button separators
    $("#bar").prepend('<img src="' + img_path + 'http://media.convergemag.com/designimages/mast.nav.left.end_CV.gif" />');
    // $(".nav_btn:not(.on)").after('<img src="http://media.convergemag.com/designimages/mast.nav.separator_CV.gif' />');
    
    
    // nav roll-over handler
    var hovered = "nav_"+section;
    var lastHovered;
    
    $(".nav_btn").hover(function(){
        // ON MOUSE OVER
        
        // de-orange all buttons
        $(".nav_btn").removeClass("on");       
        // orange-ify the top button for the section
        $(this).addClass("on");
        
        // hide all the sub-navs
        $(".subnav").hide();
        // show the sub-nav for this section
        $("#sub_" + $(this).attr("id")).show();
        
        // set a variable for the highlighted top button
        hovered = lastHovered = $(this).attr("id");
    }, function(){
        // ON MOUSE OUT
       
        $(this).removeClass("on");    
        // hide all the sub-navs
        $(".subnav").hide();
        
        // show the sub-nav for the page
        $("#sub_nav_" + section).show();
        // orange-ify the button for the top section
        $("#nav_" + section).addClass("on");

        hovered = null;
    }).end();
    
    //sub-nav roll-over handler
    $(".subnav").hover(function(){
        // ON MOUSE OVER

        // Orange-ify the current top button
        if(lastHovered)
        {
            $(".nav_btn").removeClass("on");     
            $("#" + lastHovered).addClass("on");
        }
        hovered = lastHovered;
        
       // Hide all the sub-navs
       $(".subnav").hide();
       // Show the current sub-nav
       $(this).show();
    }, function(){
        // ON MOUSE OUT
        // Hide the current sub-nav
        $(this).hide();
        // De-orange-ify the current top button
        if(hovered)
            $("#" + hovered).removeClass("on");
            
        // Display the page sub-nav again
        $("#sub_nav_" + section).show();
        // Display the pae button again
        $("#nav_" + section).addClass("on");
        hovered = 0;
        lastHovered = "nav_" + section;
    }).end();


    
    // expanding article comment form box
    $(".commentlink").click(function(){
        $(".commentlink").toggle();
        $(".frame.form.comment").toggle();
        return false;
    }).end();
    
    // expanding article comment hilite box
    $(".showmore").click(function(){
        $(this).hide();
        $(".showless").show();
        $(".summaryshort").hide();
        $(".summarylong").show();
        return false;
    }).end();
    
    $(".showless").click(function(){
        $(this).hide();
        $(".showmore").show();
        $(".summaryshort").show();
        $(".summarylong").hide();
        return false;
    }).end();
    
    // advanced search page
    $("#adv_search_btn").click(function(){
        $("#adv_search_pane").toggle();
        return false;
    });
});


