﻿var Immediacy = function() {


    // Another thing that has to be done
    var add_parameters_to_email_links = function() {
        $('#header a[href$=contactus.php]').each(function() { // links in the header whose href ends with contactus.php
            var a = $(this);
            var href = a.attr('href');
            if (href.match(/\?/)) {
                href = href += '&dmabury=true';
            } else {
                href = href += '?dmabury=true';
            }
            a.attr('href', href);
        })
    } (); // Remember to put parentheses here if you want the function to run automatically

    // Basically all that this does is create a set of named blocks that
    // each run immediately. The name hints a bit at what the block does, and the
    // parentheses make it go. The Immediacy function isn't run until the page loads,
    // so it is safe to make these functions run as soon as possible.




    var pad_bottom_list_element = function() {
        $("li.styleLevel1:last").css({ paddingBottom: '5px' });
        $("li.styleLevel2:last").css({ paddingBottom: '5px' });
        //$(".sectionMenuSelected").css({ paddingBottom: '0' });
    } ();


    var last_list_item = function() {
        $("#rightNav ul li:last").css({
                                    "border-bottom": '1px solid #ccc',
                                    "padding-bottom" : "9px"});
    } ();



    var create_principal_investing_investments_table = function() {
        var investmentsTable = "<table class='principal_investing_investments'>";
        investmentsTable += "<tr>";
        investmentsTable += "<td class='firstheader'>&nbsp;</td>";
        investmentsTable += "<td class='header'>Company</td>";
        investmentsTable += "<td class='header'>Since</td>";
        investmentsTable += "<td class='header'>Website</td>";
        investmentsTable += "</tr>";
        $('table.ImmTS_Investments td').each(
            function(i) {
                var html = $(this).html();
                var lastColIndex = 4;

                //alert(i);
                if (i % 5 == 0) {
                    investmentsTable += "<tr>\n";
                    investmentsTable += "<td rowspan='2'>\n"; // image holder
                    investmentsTable += html;
                    investmentsTable += "</td>\n";
                } else if ((i + 1) % 5 == 0) {
                    investmentsTable += "</tr>\n";
                    investmentsTable += "<tr>\n";
                    investmentsTable += "<td colspan='3' class='description'>\n"; // description holder
                    investmentsTable += html;
                    investmentsTable += "</td>\n";
                    investmentsTable += "</tr>\n";
                } else {
                    investmentsTable += "<td class='cell'>\n";
                    investmentsTable += html;
                    investmentsTable += "</td>\n";
                }

            });

        investmentsTable += "</table>";
        $('table.ImmTS_Investments').replaceWith(investmentsTable);
    } ();

    var create_principal_investing_active_investments_table = function() {
        var investmentsTable = "<table class='principal_investing_investments'>";
        investmentsTable += "<tr>";
        investmentsTable += "<td class='header'>Active Investments</td>";
        investmentsTable += "<td class='firstheader'>&nbsp;</td>";
        investmentsTable += "<td class='firstheader'>&nbsp;</td>";
        investmentsTable += "<td class='firstheader'>&nbsp;</td>";
        investmentsTable += "</tr>";
        $('table.ImmTS_Active_Investments td').each(
            function(i) {
                var html = $(this).html();
                var lastColIndex = 4;

                //alert(i);
                if (i % 5 == 0) {
                    investmentsTable += "<tr>\n";
                    investmentsTable += "<td rowspan='2'>\n"; // image holder
                    investmentsTable += html;
                    investmentsTable += "</td>\n";
                } else if ((i + 1) % 5 == 0) {
                    investmentsTable += "</tr>\n";
                    investmentsTable += "<tr>\n";
                    investmentsTable += "<td colspan='3' class='description'>\n"; // description holder
                    investmentsTable += html;
                    investmentsTable += "</td>\n";
                    investmentsTable += "</tr>\n";
                } else {
                    investmentsTable += "<td class='cell'>\n";
                    investmentsTable += html;
                    investmentsTable += "</td>\n";
                }

            });

        investmentsTable += "</table>";
        $('table.ImmTS_Active_Investments').replaceWith(investmentsTable);
    } ();




    var right_align_submits = function() {
        $('form').each(function() {
            var form = $(this);
            form.find(':submit').each(function() {
                if (!$(this).hasClass("noalign")) {
                    $(this).wrapAll("<div id='submitbuttons'></div>");
                }
            })
        });
    } ();





    var split_site_map = function() {
        var headerCount = 0;
        $('.sitemap > li').each(function(i) {
            if ($(this).hasClass('sitmp0')) {
                headerCount++;
            }

            if (headerCount < 4) {
                $(this).appendTo($('.sitemap1'));
            } else if (headerCount >= 4 && headerCount <= 7) {
                $(this).appendTo($('.sitemap2'));
            } else {
                $(this).appendTo($('.sitemap3'));
            }
        })
    } ();



    var populateSearch = function() {
        try {
            var queryString = window.location.href.split("?");

            var query = queryString[1].split("=");

            var term = query[1];

            if (query[0] == "terms") {
                $('.textTerms').val(term);
            }
        } catch (err) {

        }

    } ();




    var unlink_section_tools = function() {
        /*
        <div id="breadcrumbs">
        <span class="Path">
        <a href="/section_tools.aspx">Section Tools</a>
        </span>
        */
        var toUnlink = "section_tools.aspx";
        if ($(".Path")) {
            $(".Path > a").each(function(i) {
                ($(this).attr("href").indexOf(toUnlink) != -1) ? $(this).replaceWith("Section Tools") : null;
                //(toUnlink.match(this)) ? $(this).parents("li").addClass("selectedTab") : $(this).parents("li").removeClass("selectedTab");
            });
        }

    } ();


};




String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};







$(document).ready(function() {
    Immediacy();

    var currURL = window.document.location.toString();
    if (currURL.match("analyst_bio") || currURL.match("page=708")) {
        $("#breadcrumbs > span.selected").html($(".subheader:first").html());
    }

    if (currURL.match("company.aspx")) {
        $("#breadcrumbs > span.selected").html($(".subheader:first").html());
    }




    /************************************************/



    /* Email Page */
    $("#buttonClose").bind('click', function(e) {
        $("#ddEmailPage").hide();
    });

    $("#emailPage").bind('click', function(e) {
        var offset = $(this).offset();
        var left = (offset.left - 40) + "px";
        var top = (offset.top + 20) + "px";

        $("#txtPageURL").val(document.location.href);
        $("#txtPageTitle").val(document.title);

        $("#ddEmailPage").css({
            position: 'absolute',
            zIndex: 5000,
            left: left,
            top: top
        });
        $("#ddEmailPage").show();
    });

    $("#sendEmail").bind('click', function(e) {
        //Error checking
        var hasErrors = false;
        var errorList = new Array();

        function test(src) {
            var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
            var regex = new RegExp(emailReg);
            return regex.test(src);
        }

        //Validate From Address
        if (test($("#senderEmail").val())) {
            hasErrors = false;
        } else {
            hasErrors = true;
            errorList.push("Your Email address is not valid");
        }

        //Validate recipient address(s)
        var emailAddresses = $("#recipientEmail").val();

        //Remove spaces
        emailAddresses = emailAddresses.trim();

        //Parse commas
        var parsedAddresses = emailAddresses.split(",");
        if (parsedAddresses.length > 5) {
            errorList.push("Too many email addresses entered for recipient");
            hasErrors = true
        } else {

            for (var i in parsedAddresses) {
                if (!test(parsedAddresses[i])) {
                    hasErrors = true;
                    errorList.push("One of more of the recipient email addresses you entered is invalid.");
                }
            }
        }


        //No errors, send email
        if (!hasErrors) {
            try {
                pageTracker._trackPageview("www.stephens.com/emailPage");
            } catch (error) { }
            var sendData = "txtPageURL=" + $("#txtPageURL").val() + "&pageTitle=" + $("#txtPageTitle").val() + "&pageURL=" + $("#txtPageURL").val() + "&senderEmail=" + $("#senderEmail").val() + "&recipientEmail=" + emailAddresses;
            $.ajax(
                {
                    type: "GET",
                    url: "email_page.aspx",
                    data: sendData,
                    success: function() {
                        $("#ddEmailPage").html("<img src='images/email_icon.gif' /><br/><span>Your email has been sent.</span>");
                        $("#ddEmailPage").fadeOut(7000);
                    }
                }
            );
        } else {
            var errorString = "";
            for (var i in errorList) {
                errorString = errorString + "&bull; " + errorList[i] + "<br/>";
            }
            $("#errorList").html(errorString);
            $("#errorList").show();
        }
    });
    /**********************************************/








    /*    
    (currURL.match("/wealth_management")) ? $("#mnuWealthManagement").addClass("mnuSelected") : $("#mnuWealthManagement").removeClass("mnuSelected");
    (currURL.match("/investment_banking")) ? $("#mnuInvestmentBanking").addClass("mnuSelected") : $("#mnuInvestmentBanking").removeClass("mnuSelected");       
    */

    /*
    if ($(".accordionContainer")) {
        
    $(".accordionContainer > li > ul").each(function() {
    var me = $(this);
    me.hide();
    $(this).parents("li").toggle(function() {
    me.show("slow");
    }, function() { 
    me.hide("slow");
    });
    });
    }
    */

    if ($(".tabs")) {
        $(".tabs > li > a").each(function(i) {
            (currURL.match(this)) ? $(this).parents("li").addClass("selectedTab") : $(this).parents("li").removeClass("selectedTab");
        });
    }

});
