/// <reference path="jquery-1.4.1.js" />
// The above line provides Intellisense capabilities for jQuery in Visual Studio.

// Copyright (c) Brazen Careerist 2010

// Configurable location of image data.
if (!bcImageLocation) {
    var bcImageLocation = "http://static.brazencareerist.com/v6/ui_widget/images/";
}

// Configurable location of JSON data.
if (!bcJSONLocation) {
    var bcJSONLocation = "http://www.brazencareerist.com/widget/json/" + uid + '?callback=?&width=' + width;
}

// Configurable location of the JQuery library.
if (!bcJQueryLocation) {
    var bcJQueryLocation = "http://static.brazencareerist.com/v6/ui_widget/jquery-1.4.1.min.js";
}

// Write the jQuery script tag if it doesn't exist.
var bcJQueryScriptOutputted = false;
var bcGoogleScriptOutputted = false;
function bcInitJQuery() {

    if (typeof (jQuery) == 'undefined') {
        if (!bcJQueryScriptOutputted) {
            bcJQueryScriptOutputted = true;
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + bcJQueryLocation + "\"></scr" + "ipt>");
            
            bcGoogleScriptOutputted = true;
            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        }
        setTimeout("bcInitJQuery()", 50);
    } else if (typeof (_gat) == 'undefined') {
        if (!bcGoogleScriptOutputted) {
          bcGoogleScriptOutputted = true;
          var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
          document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        }
        setTimeout("bcInitJQuery()", 50);
    } else {
        jQuery(function() {
            bcLoadContent();
        });
    }
    
}
bcInitJQuery();

// The currently active div ID 
var bcActiveContent = "bcContent1";

// Activate the given tab
function bcActivateTab(tabID) {

    bcActiveContent = tabID;
    if (tabID == "bcContent1") {
        jQuery("#bcwidget" + width + "-" + uid + " .bcContent1").css("display", "block");
        jQuery("#bcwidget" + width + "-" + uid + " .bcContent2").css("display", "none");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab1").css("background-image", "url('" + bcImageLocation + "tab_selected.png')");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab2").css("background-image", "url('" + bcImageLocation + "tab_unselected.png')");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab1 div").css("background-image", "url('" + bcImageLocation + "tab_selected.png')");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab2 div").css("background-image", "url('" + bcImageLocation + "tab_unselected.png')");
    }
    else {
        jQuery("#bcwidget" + width + "-" + uid + " .bcContent2").css("display", "block");
        jQuery("#bcwidget" + width + "-" + uid + " .bcContent1").css("display", "none");

        jQuery("#bcwidget" + width + "-" + uid + " .bcTab1").css("background-image", "url('" + bcImageLocation + "tab_unselected.png')");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab2").css("background-image", "url('" + bcImageLocation + "tab_selected.png')");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab1 div").css("background-image", "url('" + bcImageLocation + "tab_unselected.png')");
        jQuery("#bcwidget" + width + "-" + uid + " .bcTab2 div").css("background-image", "url('" + bcImageLocation + "tab_selected.png')");
    }
    
    if (-1 == bcMaxIndex[bcActiveContent]) {
        jQuery("#bcwidget" + width + "-" + uid + " .nav").hide();
    } else if (bcIndices[bcActiveContent] == bcMaxIndex[bcActiveContent]) {
        jQuery("#bcwidget" + width + "-" + uid + " .nav").show();
        jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").hide();
        jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").show();
    } else if (bcIndices[bcActiveContent] == 0) {
        jQuery("#bcwidget" + width + "-" + uid + " .nav").show();
        jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").hide();
        jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").show();
    } else {
        jQuery("#bcwidget" + width + "-" + uid + " .nav").show();
        jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").show();
        jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").show();
    }
}

// Associative array of respective indices for each tab's active content
var bcIndices = new Array();
bcIndices["bcContent1"] = 0;
bcIndices["bcContent2"] = 0;

var bcMaxIndex = new Array();
bcMaxIndex["bcContent1"] = 0;
bcMaxIndex["bcContent2"] = 0;

// Animate the currently active tab.
function bcAnimateTab(right) {

    if (right) {
        if (bcIndices[bcActiveContent] < bcMaxIndex[bcActiveContent]) {
            jQuery("#bcwidget" + width + "-" + uid + " ." + bcActiveContent).animate({ "left": "-=" + (width - 2) + "px" }, "slow");
            bcIndices[bcActiveContent] += 1;
        }
        if (bcIndices[bcActiveContent] == bcMaxIndex[bcActiveContent]) {
            jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").hide();
            jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").show();
        } else {
            jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").show();
            jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").show();
        }
    } else {
        if (bcIndices[bcActiveContent] > 0) {
          jQuery("#bcwidget" + width + "-" + uid + " ." + bcActiveContent).animate({ "left": "+=" + (width - 2) + "px" }, "slow");
          bcIndices[bcActiveContent] -= 1;
        }
        if (bcIndices[bcActiveContent] == 0) {
            jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").show();
            jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").hide();
        } else {
            jQuery("#bcwidget" + width + "-" + uid + " .nextbutton").show();
            jQuery("#bcwidget" + width + "-" + uid + " .prevbutton").show();
        }
    }

    return false;
}

// Write actual content into the tab locations.
function bcWriteContent(element, content, href, topidea, index, replies, recommendations) {

    // Make sure the valeus are integers so they can be used in comparisons later.
    replies = parseInt(replies);
    recommendations = parseInt(recommendations);

    var contentLength = 165;

    // When writing a top idea, we prepend an index into the data, but need to keep
    // the data aligned, so we wrap it in a table.
    var container = "";
    var endcontainer = "";
    if (topidea) {
        contentLength = 150;

        container = "<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; word-wrap:break-word; font-family: Arial; font-size: 10px; width: " + (width-22) + "px; margin: 10px;'><tr><td valign='top' width='15' style='vertical-align:top;'><font color='#fa7818'><b>" + index + ".</b></font></td><td valign='top' style='vertical-align:top;' width='" + (width-37) + "'>";
        endcontainer = "</td></tr></table>";

    } else {
        container = "<div style='width: " + (width-22) + "px; margin: 10px; padding: 0; vertical-align:top;'>";
        endcontainer = "</div>";
    }

    // Truncate the content if it is too long.
    var formattedContent = content;
    formattedContent += "<br><b>";

    if (replies > 0) {
        formattedContent += "<a href='" + href + "?utm_source=" + uid + "&utm_medium=reply&utm_campaign=widget' style='color:#597daf; text-decoration:none;'>" + replies;

        if (replies == 1) {
            formattedContent += " reply";
        } else {
            formattedContent += " replies"
        }
        formattedContent += "</a>";

        if (recommendations > 0) {
            formattedContent += " | ";
        }
    }

    if (recommendations > 0) {
        formattedContent += "<a href='" + href + "?utm_source=" + uid + "&utm_medium=recommend&utm_campaign=widget' style='color:#597daf; text-decoration:none;'>" + recommendations;

        if (recommendations == 1) {
            formattedContent += " recommend";
        } else {
            formattedContent += " recommends";
        }

        formattedContent += "</a></b>";
    }

    // Set up the CSS for the td cell.
    var ref = "#bcwidget" + width + "-" + uid + " ." + element;
    jQuery(ref).css("font-size", "10px");
    jQuery(ref).css("font-family", "Arial");
    jQuery(ref).attr("valign", "top");

    jQuery(ref).html(container + formattedContent + endcontainer);
}

// Truncate the data by looping backwards over it, removing
// entire words.  If that doesn't get us a decent result, lop
// off the end since there isn't much we can do.
function truncateData(data) {

    if (data.length > 35) {

        var lastSpace = data.length;

        while (lastSpace > 0 && data.length > 35) {
            lastSpace = data.lastIndexOf(" ", lastSpace);
            data = data.substring(0, lastSpace);
        }

        if (data.length > 35) {
            data = data.substring(0, 35);
        }
    }

    if (data.lastIndexOf(",") == (data.length - 1)) {
        data = data.substring(0, (data.length - 1));
    }

    return data;
}

// Load the content from the json location.
function bcLoadContent() {
    if (typeof (_gat) != 'undefined') {    
      var bcPageTracker = _gat._getTracker("UA-3762378-1");
      bcPageTracker._initData();
      bcPageTracker._setDomainName('none');
      bcPageTracker._setAllowLinker(true);
      bcPageTracker._setAllowHash(false);
      bcPageTracker._trackEvent('Widget', 'View', uid + '');
    }

    jQuery.getJSON(bcJSONLocation, function(data, textStatus) {        
        var spacer = "<div style='height: 10px;'></div>";

        var name = truncateData(data.name);
        var title     = data.title;
        var company   = data.company;
        var industry  = data.industry;
        var separator = (title && company) ? ", " : "";

        var position = title + separator + company;
        position = truncateData(position);
  
        var personal_rows = (name.length ? 1 : 0) + (title.length ? 1 : 0) + (company.length ? 1 : 0) + (industry.length ? 1 : 0);
        
        var cssstyle = "<style type=\"text/css\">"
                  + "#bcwidget" + width + "-" + uid + " div, #bcwidget" + width + "-" + uid + " table, #bcwidget" + width + "-" + uid + " img, #bcwidget" + width + "-" + uid + " td {"
                  + "background-color:#fff;color:#000;border:none;margin:0;padding:0;line-height:13px;text-align:left;vertical-align:middle;"
                  + "}"
                  + "#bcwidget" + width + "-" + uid + " a, #bcwidget" + width + "-" + uid + " a:hover {text-transform: none; font-weight: normal; font-size: 10px; background: none; font-family: Arial; }"
                  + "#bcwidget" + width + "-" + uid + " img { position: relative; top: 0; right: 0; bottom: 0; left: 0; }"
                  + "</style>";
        
        var personal = "<div><table border='0' cellpadding='0' cellspacing='0' style='left: 10px; position: relative; font-family: Arial; font-size: 11px; table-layout:fixed; word-wrap:break-word;'><tr><td rowspan='4' style='width:55px;'><a href='" + data.href + "?utm_source=" + uid + "&utm_medium=photo&utm_campaign=widget'><img src='" + data.img + "' border='0' width='55' height='55' /></a></td><td rowspan='4' width='10'>&nbsp;</td><td style='overflow: hidden;'><nobr><a href='" + data.href + "?utm_source=" + uid + "&utm_medium=fullname&utm_campaign=widget' style='text-decoration:none; color: #000;'><strong>" + name + "</strong></a></nobr></td></tr>";
        if (title.length) {
          personal += "<tr><td style='color: #777777; overflow: hidden;'><nobr>" + title + "</nobr></td></tr>";
        }
        if (company.length) {
          personal += "<tr><td style='color: #777777; overflow: hidden;'><nobr>" + company + "</nobr></td></tr>";
        }
        if (industry.length) {
          personal += "<tr><td style='color: #777777; overflow: hidden;'><nobr>" + industry + "</nobr></td></tr>";
        }
        for (var i = personal_rows; i < 4; i++) { 
          personal += "<tr><td>&nbsp;</td></tr>";
        }
        personal += "</table></div>";

        if (0 == data.topIdeas.length) {
        
          var tabs = "<table class='bcTabs' cellpadding='0' cellspacing='0' style='width: " + width + "px; height: 18px;'><tr>"
                   + "<td style='background-image: url(\"" + bcImageLocation + "tab_selected.png\"); background-position: top left; font-size: 10px; font-family: Arial; text-align: center; margin: 0; padding: 0; width:" + (width/2) + "px; height: 18px;' class='bcTab1'><div style='background-image: url(\"" + bcImageLocation + "tab_selected.png\"); background-position: top right; height: 18px; margin: 0 0 0 5px; padding: 0; line-height: 18px;'><a href='#bcTab1' onClick='bcActivateTab(\"bcContent1\"); return false;' style='color:#597daf; text-decoration:none;'><b>ACTIVITY</b></a></div></td>"
                   + "<td style='background-image: url(\"" + bcImageLocation + "tab_unselected.png\"); background-position: top left; font-size: 10px; font-family: Arial; text-align: center; width:" + (width/2) + "px; height: 18px;' class='bcTab2'><div style='background-image: url(\"" + bcImageLocation + "tab_unselected.png\"); background-position: top right; height: 18px; margin: 0 0 0 5px; padding: 0; line-height: 18px;'><a href='#bcTab2' onClick='bcActivateTab(\"bcContent2\"); return false;' style='color:#597daf; text-decoration:none;'><b>TOP IDEAS</b></a></div></td>"
                   + "</tr></table>";
                   
        } else {

          var tabs = "<table class='bcTabs' cellpadding='0' cellspacing='0' style='width: " + width + "px; height: 18px;'><tr>"
                   + "<td style='background-image: url(\"" + bcImageLocation + "tab_selected.png\"); background-position: top left; font-size: 10px; font-family: Arial; text-align: center; margin: 0; padding: 0; width:" + (width/2) + "px; height: 18px;' class='bcTab1'><div style='background-image: url(\"" + bcImageLocation + "tab_selected.png\"); background-position: top right; height: 18px; margin: 0 0 0 5px; padding: 0; line-height: 18px;'><a href='#bcTab1' onClick='bcActivateTab(\"bcContent1\"); return false;' style='color:#597daf; text-decoration:none;'><b>TOP IDEAS</b></a></div></td>"
                   + "<td style='background-image: url(\"" + bcImageLocation + "tab_unselected.png\"); background-position: top left; font-size: 10px; font-family: Arial; text-align: center; width:" + (width/2) + "px; height: 18px;' class='bcTab2'><div style='background-image: url(\"" + bcImageLocation + "tab_unselected.png\"); background-position: top right; height: 18px; margin: 0 0 0 5px; padding: 0; line-height: 18px;'><a href='#bcTab2' onClick='bcActivateTab(\"bcContent2\"); return false;' style='color:#597daf; text-decoration:none;'><b>ACTIVITY</b></a></div></td>"
                   + "</tr></table>";
                   
        }

        var contentContainer = "<div class='contentContainer' style='width: " + (width - 2) + "px; height: 98px; background: #f7fbfe; border-style:solid; border-width:1px; border-top: 0px; border-color:#aaaaaa;'>";
        var tabContainer = "<div class='tabContainer' style='width: " + (width - 2) + "px; height:84px; position:relative; overflow: hidden;'>";

        var tab1 = "<table border='0' cellpadding='10' cellspacing='0' class='bcContent1' height='84' style='width: " + ((width - 2) * 3) + "px; position:relative; table-layout:fixed; word-wrap:break-word;'><tr><td class='bcContent1Element1' style='width:" + (width - 2) + "px; margin:0; padding:0; vertical-align:top;'></td><td class='bcContent1Element2' style='width:" + (width - 2) + "px; margin:0; padding:0; vertical-align:top;'></td><td class='bcContent1Element3' style='width:" + (width - 2) + "px; margin:0; padding:0; vertical-align:top;'></td></tr></table>";
        var tab2 = "<table border='0' cellpadding='10' cellspacing='0' class='bcContent2' height='84' style='width: " + ((width - 2) * 3) + "px; position:relative; table-layout:fixed; word-wrap:break-word; display:none;'><tr><td class='bcContent2Element1' style='width:" + (width - 2) + "px; margin:0; padding:0; vertical-align:top;'></td><td class='bcContent2Element2' style='width:" + (width - 2) + "px; margin:0; padding:0; vertical-align:top;'></td><td class='bcContent2Element3' style='width:" + (width - 2) + "px; margin:0; padding:0; vertical-align:top;'></td></tr></table>";

        var endDiv = "</div>";

        var nav = "<div class='nav' style='text-align: center; font-size: 10px; font-family: Arial; position: relative; height: 10px; display: none;'><b><a href='#prev' class='prevbutton' onClick='return bcAnimateTab(false);' style='color:#597daf; text-decoration:none; display:none;'>&lt; PREV</a> | <a href='#next' class='nextbutton' onClick='return bcAnimateTab(true);' style='color:#597daf; text-decoration:none;'>NEXT &gt;</a></b></div>";

        var footer = "<div style='height: 26px; font-family: Arial; font-size: 11px; background-color: #13568e;'><a href='" + data.href + "?utm_source=" + uid + "&utm_medium=footer&utm_campaign=widget' style='color: #fff; text-decoration: none; display: block; position: relative; top: 5px; left: 5px; font-weight: bold;'>View My Social Resume &raquo;</a></div>";

        jQuery("#bcwidget" + width + "-" + uid + " .bcContent").html(cssstyle + spacer + personal + spacer + tabs + contentContainer + tabContainer + tab1 + tab2 + endDiv + nav + endDiv + footer);

        if (0 == data.topIdeas.length) {
        
          bcMaxIndex["bcContent1"] = data.recentActivity.length - 1;
          bcMaxIndex["bcContent2"] = data.topIdeas.length - 1;

          if (0 == data.recentActivity.length) {
            bcWriteContent("bcContent1Element1", "I don't have any activity right now but check out my <a href=\"" + data.href + "?utm_source=" + uid + "&utm_medium=noactivity&utm_campaign=widget\">Social Resume</a>", data.href + "?utm_source=" + uid + "&utm_medium=noactivity&utm_campaign=widget", false, 1, 0, 0);
          }
          for (var j = 0; j < data.recentActivity.length && j < 3; ++j) {
              bcWriteContent("bcContent1Element" + (j + 1), data.recentActivity[j].activity, data.recentActivity[j].href, false, -1, data.recentActivity[j].replies, data.recentActivity[j].recommendations);
          }
          if (0 == data.topIdeas.length) {
            bcWriteContent("bcContent2Element1", "I haven't selected my Top Ideas yet but check out my <a href=\"" + data.href + "?utm_source=" + uid + "&utm_medium=notopidea&utm_campaign=widget\">Social Resume</a>", data.href + "?utm_source=" + uid + "&utm_medium=notopidea&utm_campaign=widget", false, 1, 0, 0);
          }
          for (var i = 0; i < data.topIdeas.length && i < 3; ++i) {
              bcWriteContent("bcContent2Element" + (i + 1), data.topIdeas[i].idea, data.topIdeas[i].href, true, i + 1, data.topIdeas[i].replies, data.topIdeas[i].recommendations);
          }

        } else {
        
          bcMaxIndex["bcContent1"] = data.topIdeas.length - 1;
          bcMaxIndex["bcContent2"] = data.recentActivity.length - 1;

          if (0 == data.topIdeas.length) {
            bcWriteContent("bcContent1Element1", "I haven't selected my Top Ideas yet but check out my <a href=\"" + data.href + "?utm_source=" + uid + "&utm_medium=notopidea&utm_campaign=widget\">Social Resume</a>", data.href + "?utm_source=" + uid + "&utm_medium=notopidea&utm_campaign=widget", false, 1, 0, 0);
          }
          for (var i = 0; i < data.topIdeas.length && i < 3; ++i) {
              bcWriteContent("bcContent1Element" + (i + 1), data.topIdeas[i].idea, data.topIdeas[i].href, true, i + 1, data.topIdeas[i].replies, data.topIdeas[i].recommendations);
          }
          if (0 == data.recentActivity.length) {
            bcWriteContent("bcContent2Element1", "I don't have any activity right now but check out my <a href=\"" + data.href + "?utm_source=" + uid + "&utm_medium=noactivity&utm_campaign=widget\">Social Resume</a>", data.href + "?utm_source=" + uid + "&utm_medium=noactivity&utm_campaign=widget", false, 1, 0, 0);
          }
          for (var j = 0; j < data.recentActivity.length && j < 3; ++j) {
              bcWriteContent("bcContent2Element" + (j + 1), data.recentActivity[j].activity, data.recentActivity[j].href, false, -1, data.recentActivity[j].replies, data.recentActivity[j].recommendations);
          }
        
        }
        
        bcActivateTab("bcContent1");
    });
}
