
function TranslateCategoryToPath(categoryName)
{
    categoryName = categoryName.toLowerCase();
    categoryName = categoryName.replace("[news] ", "").replace("[business] ", "");
    return GetCategoryMapping(categoryName + "/");
}
function GetCategorySpecificity(category)
{
    specificity = category.split("/").length - 1;
    return specificity;
}
function GetCategoryMapping(cat)
{
    if(cat == "/community/neighbors/")
    {
        return "/news/life/neighbors/";
    }
    else if(cat == "/news/nationworld/")
    {
        return "/news/nation-world/";
    }
    else if(cat == "/sports/community sports/")
    {
        return "/news/sports/community/";
    }
    else if(cat == "/sports/prep sports/")
    {
        return "/news/sports/preps/";
    }
    else if(cat == "/opinion/letters to editor/")
    {
        return "/news/opinion/letters/";
    }
    else if(cat.substring(0, 5) != "/news")
    {
        return "/news" + cat;
    }
    else
    {
        return cat;
    }
}
function GoogleizeURL(ungooglized)
{
    var valid_characters = /\//;
    var parts = Array();
    var unclean_parts = ungooglized.split(valid_characters);
    var k = 0;
    var ktop = unclean_parts.length;
    while(k < ktop)
    {
        var this_part = unclean_parts[k];
        if(this_part.length > 0)
        {
            parts.push(this_part.toLowerCase());
        }
        k = k + 1;
    }
    return parts.join("_");
}
function GoogleizeCategory(ungooglized)
{
    var split_category = /] /;
    var parts = Array();
    var unclean_major_parts = ungooglized.split(split_category);
    parts.push(unclean_major_parts[0].substr(1).toLowerCase());

    var split_tiers = /\//;
    var unclean_minor_parts = unclean_major_parts[1].split(split_tiers);
    var k = 0;
    var ktop = unclean_minor_parts.length;
    while(k < ktop)
    {
        var this_part = unclean_minor_parts[k];
        if(this_part.length > 0)
        {
            var split_tier_components = /[^a-zA-Z0-9]/;
            var this_part_parts = this_part.split(split_tier_components);
            var m = 0;
            var mtop = this_part_parts.length;
            var this_part_parts_arr = Array();
            while(m < mtop)
            {
                var this_part_parts_part = this_part_parts[m];
                if(this_part_parts_part.length > 0)
                {
                    this_part_parts_arr.push(this_part_parts_part);
                }
                m = m + 1;
            }
            this_part_string = this_part_parts_arr.join("-");
            parts.push(this_part_string.toLowerCase())
        }
        k = k + 1;
    }
    return parts.join("_");
}
function GetCategoryTranslation(categories)
{
    bestMatch = undefined;
    matchSpecificity = 0;

    catMax = categories.length;
    catCurr = 0;

    while(catCurr < catMax)
    {
        currCategory = categories[catCurr];
        path = TranslateCategoryToPath(currCategory);
        elementMatch = $("#sf-main-navigation a[href=" + path + "]");
        if(elementMatch.length > 0)
        {
            specificity = GetCategorySpecificity(path);
            if(specificity > matchSpecificity)
            {
                bestMatch = path;
                matchSpecificity = specificity;
            }
        }
        catCurr = catCurr + 1;
    }
    return bestMatch;
}
function CheckForAssignedPath()
{
    var variables = GetUrlVars();
    if(variables["navPath"])
    {
        return variables["navPath"];
    }
    else
    {
        return false;
    }
}
function GetUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split("=");
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function SetNavigationStatus(path)
{    
    if(path === undefined)
    {
        if(CheckForAssignedPath())
        {
            path = CheckForAssignedPath();
        }
        else if(typeof(ArticleCategories) === "undefined")
        {
            path = window.location.pathname;
        }
        else
        {
            path = GetCategoryTranslation(ArticleCategories);
        }
    }
    currentElement = $("#sf-main-navigation a[href=" + path + "]");
    if(currentElement.length > 1)
    {
        /* if we match more than one element, let's select the second. */
        currentElement = jQuery(currentElement[1])
    }

    if(currentElement.parent().parent().parent().parent().attr("tagName") == "UL")
    {
        /* Second Level Navigation is selected */
        // Mark current parent LI as hovered
        currentElement.parent().parent().parent().attr("class", "sfHover");
        // Mark current parent UL as visible
        currentElement.parent().parent().css("display", "block").css("visibility", "visible")
        // Mark current LI as current
        currentElement.parent().attr("class", "current");
    }
    else
    {
        /* First Level Navigation is selected */
        // Mark adjacent UL as visible
        currentElement.parent().children("ul").css("display", "block").css("visibility", "visible");
        // Mark self as selected
        currentElement.parent().attr("class", "sfHover");
    }
}
function ClearNavigationStatus(path)
{
    if(path === undefined)
    {
        if(CheckForAssignedPath())
        {
            path = CheckForAssignedPath();
        }
        else if(typeof(ArticleCategories) === "undefined")
        {
            path = window.location.pathname;
        }
        else
        {
            path = GetCategoryTranslation(ArticleCategories);
        }
    }
    var currentElement = $("#sf-main-navigation a[href=" + path + "]");
    var firstParent = currentElement.parent();
    firstParent.attr("class", "");
    ulChildren = firstParent.children("ul").css("visibility", "hidden").css("display", "none");

    var secondParent = firstParent.parent().parent()
    if(secondParent.attr("tagName") == "LI")
    {
        secondParent.attr("class", "");
        firstParent.parent().css("display", "none").css("visibility", "hidden");
    }
}
function DeleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
    Alert("Your cookies have been cleared.  Please refresh your page and continue.")
}

jQuery(function(){
    jQuery('ul.sf-menu').superfish({
        pathClass: 'current',
        autoArrows: false,
        onHide: SetNavigationStatus(),
        onBeforeShow: ClearNavigationStatus()
        });
});
$(document).ready(function(){        
    SetNavigationStatus()    
});
function votemod(content_type, object_id, mod, original)
{
    var url = "/ratings/vote/";
    var data = {value: String(mod), object_id: String(object_id), content_type: String(content_type)};
    voting_object = $("#item_score_" + String(content_type) + "_" + String(object_id));
    var current_score = eval('item_vote_original_' + String(content_type) + '_' + String(object_id));
    if(mod == original)
    {
        var new_score = current_score;
    }
    else
    {
        var new_score = current_score + mod;
    }
    if(new_score >= 0)
    {
        var new_score_string = String(new_score);
    }
    else
    {
        var new_score_string = String(new_score);
    }
    if(mod >= 0)
    {
        voting_object.addClass("upvote")
        voting_object.removeClass("downvote");
    }
    else
    {
        voting_object.addClass("downvote")
        voting_object.removeClass("upvote");
    }
    voting_object.html(new_score_string);
    $.post(url, data, function(){}, "json");
}
function flash_object_container_check(div)
{
    var this_container = div
    if(this_container.css("position") == "absolute")
    {
        return this_container;
    }
    else
    {
        return false;
    }
}
function flash_object_expansion(div, direction, new_size)
{
    var this_div = $("#" + div)
    var parent_div = flash_object_container_check(this_div.parent());
    if(parent_div != false)
    {
        var flash_object = this_div.children().children();
        //this_div.after("<div>Expanding #" + div + ";" + direction + ";" + new_size + "</div>");
        if(direction == 'L')
        {
            var current_width = this_div.width();
            parent_div.css("left", (parent_div.offset().left - (new_size - current_width)) + "px");
            this_div.css("width", new_size + "px");
        }
        else if(direction == "U")
        {
            var current_height = this_div.height();
            parent_div.css("top", (parent_div.offset().top - (new_size - current_height)) + "px");
            this_div.css("height", new_size + "px")
        }
    }
}
function flash_object_contraction(div, direction, new_size)
{
    this_div = $("#" + div)
    var parent_div = flash_object_container_check(this_div.parent());
    if(parent_div != false)
    {
        var flash_object = this_div.children().children();
        //this_div.after("<div>Shrinking #" + div + ";" + direction + ";" + new_size + "</div>");
        if(direction == 'L')
        {
            var current_width = this_div.width();
            parent_div.css("left", (parent_div.offset().left + (current_width - new_size)) + "px");
            this_div.css("width", new_size + "px");
        }
        else if(direction == "U")
        {
            var current_height = this_div.height();
            parent_div.css("top", (parent_div.offset().top + (current_height - new_size)) + "px");
            this_div.css("height", new_size + "px")
        }
    }
}
function flash_object_announce(name, actiontype, details)
{
    var post_url = "http://django.columbian.com/ads/post/?callback=?";
    var post_data = {
        "ad_name": name,
        "action_type": actiontype,
        "details": details
    };
    $.getJSON(post_url, post_data, function(){});
}
function reveal_comments()
{
    $("div.comment_list").show();
    $("div.comment_gate").hide();
}
                
// GOOGLE ADS
var google_ad_position_queue = new Array();
function post_registrations_to_google()
{
    var curr = 0;
    var queue_length = window.google_ad_position_queue.length;
    while(curr < queue_length)
    {
        this_ad_spot = window.google_ad_position_queue[curr]
        GA_googleAddSlot("ca-pub-9467325037722181", this_ad_spot)
        curr = curr + 1;
    }
}
function load_ads_and_replace()
{
    GA_googleFetchAds();
    var curr = 0;
    var queue_length = window.google_ad_position_queue.length;
    while(curr < queue_length)
    {
        this_ad_spot = window.google_ad_position_queue[curr]
        GA_googleFillSlot(this_ad_spot)
        curr = curr + 1;
    }
}
function debug_announce(message)
{
    //var page = $("#page");
    //page.after("<div>" + message + "</div>");
}
function position_ads()
{
    var curr = 0;
    var queue_length = window.google_ad_position_queue.length;
    while(curr < queue_length)
    {
        var this_ad_spot = window.google_ad_position_queue[curr];
        var ga_ad_placeholder = $("#ga_ad_placeholder_" + this_ad_spot);
        var this_ad_spot_div_id = "#google_ads_div_" + this_ad_spot;
        var this_ad_spot_div = $(this_ad_spot_div_id);
        if(this_ad_spot_div.length > 0)
        {
            ga_ad_placeholder.addClass("ad");
            ga_ad_placeholder.css("display", "block");
            this_ad_spot_div.css("position", "absolute");
            this_ad_spot_div.css("left", ga_ad_placeholder.offset().left + "px");
            this_ad_spot_div.css("top", ga_ad_placeholder.offset().top + "px");
            if($.browser.msie == true)
            {
                // For some undetermined reason, IE likes to misplace ads loaded from
                // doubleclick.  I first check if the advertisement exists where I 
                // expect it to be, and if it doesn't, see if one might be where 
                // IE predictably (and, unless I'm mistaken, wrongly) puts it.
                var this_ad_children = this_ad_spot_div.children();
                if(this_ad_children.length == 1)
                {
                    if(this_ad_children[0].nodeName.toUpperCase() == "SCRIPT")
                    {
                        // Yep-- only one node in this ad position, and it's a script tag.
                        // this is the telltale sign of this bug.
                        var first_object_child = $("#temporary_placement_holder > object")[0];
                        this_ad_spot_div.append(first_object_child);
                    }
                }
            }
        }
        curr = curr + 1;
    }
}
function register_ad_position(position_name)
{
    window.google_ad_position_queue.push(position_name);
}
