/* sIFR setup */
if (typeof sIFR == "function" && !sIFR.UA.bIsIEMac && (!sIFR.UA.bIsWebKit || sIFR.UA.nWebKitVersion >= 100)) {
  sIFR.replaceElement("h1", named({sFlashSrc: "/includes/tglite.swf", sColor: "#A20000", sWmode: "transparent"}));
  sIFR.setup();
};

$(document).ready(function() {
   var backToTop = "<div class='backToTop' style='margin-top:1em;'><a href='#wrapper' style='font-weight:bold;'>Back to Top</a></div><hr />";
    $('#main_column a[name], #main_column a[id]').before(backToTop);
    $('.backToTop:first').remove();
    $('hr:last').remove();
    
    $("#main_column a[href*='#']").click(function(event){
        event.preventDefault();
        var full_url = this.href;
        var parts = full_url.split("#");
        var trgt = parts[1];
        
        var target_offset = $("#"+trgt).offset();
        var target_top = target_offset.top;
        $('html, body').animate({scrollTop:target_top}, 500);
    });

  $('form.rating :radio.star').rating();
  $('#rating_submit').css('display', 'none');
  $('#quick_links_drop').change(function() {
    window.location = $(this).val();
  });
  $('#byline_variants').html('<a href="#">Print</a> | <a href="#">Download PDF</a>');

//Hide the Advisor Client Contact
var theH2Text = $('#main_withAside h2').html();
if (theH2Text == null) {
    //do nothing
}
else if(theH2Text.match('Advisor') == 'Advisor'){
    $('#main_withAside a[href*=623.htm]').remove();
}
else if(theH2Text.match('Adviser') == 'Adviser'){
    $('#main_withAside a[href*=623.htm]').remove();
}
else{
    //do nothing
}

// Cookie setup on language variant
var cookieName = 'seic_homePref';
$('#language_drop').change(function() {
        var cookieValue = $(this).val();
        if (cookieValue == "/enUS/1470.htm") {
            //do nothing
        window.location = cookieValue;
        }
        else {
            $.cookie(cookieName, cookieValue, {
                path: '/',
                expires: 30
            });
        window.location = cookieValue;
        }    
  });

//Fix the disappearing footer bug
$('.bottom').addClass('fixed');
$('#footer').addClass('fixed');

//New Windows Scripts for PDFs
$("#content_inner a[href$='pdf']").click(function(){
        this.target="_blank";
    });

//Print to PDF setup
        var thePage = window.location;
        var theTitle = document.title;
        var addPDFLink = "<img src='http://www.seic.com/includes/pdfLink.gif' style='padding:0 5px 0 0'/><a href='http://pdfmyurl.com?url="+ thePage +"&--print-media-type&--filename="+ theTitle +"&--orientation=Portrait&--footer-right="+ thePage +"'>Download PDF</a>";
        $("#printPDF").append(addPDFLink);

//FAQs (show/hide dt/dtt)
$("dd").hide();

$("dt").click(function(){
        $("dd").hide();
        $(this).next().toggle();
        
        $('.bottom').addClass('fixed');
        $('#footer').addClass('fixed');
        $('.bottom').removeClass('fixed');
        $('#footer').removeClass('fixed');
        
    });


//Change the Contact Us link based on Current Page Keyword Meta Data
var theMetaData = $("meta[name='keywords']").attr('content');
    
    if (theMetaData.match('Banks') == 'Banks') {
        $("a[href$='contact-us.aspx']").attr('href', 'contact-us.aspx' + '?Type=Banks');
    }
    
    else if (theMetaData.match('Advisors') == 'Advisors') {
        $("a[href$='contact-us.aspx']").attr('href', 'contact-us.aspx' + '?Type=Advisors');
    }
    
    else if (theMetaData.match('Institutional') == 'Institutional') {
        $("a[href$='contact-us.aspx']").attr('href', 'contact-us.aspx' + '?Type=Institutions');
    }
    
    else if (theMetaData.match('Investment') == 'Investment') {
        $("a[href$='contact-us.aspx']").attr('href', 'contact-us.aspx' + '?Type=IMS');
    }
    
    else if (theMetaData.match('Individuals') == 'Individuals') {
        $("a[href$='contact-us.aspx']").attr('href', 'contact-us.aspx' + '?Type=WealthNetwork');
    }
    
    else{
        //do nothing
    }


//Lightbox Effect
$("#main_column a[href$='jpg']").click(function(){
        var positionFromTop = "";
        
        imageSource = $(this).attr("href");
        imageCaption = $(this).find("img").attr("title");
        
        if (typeof window.pageYOffset != 'undefined') {
            positionFromTop = window.pageYOffset;
        }
        else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
                positionFromTop = document.documentElement.scrollTop    ;
            }
        else if (typeof document.body.scrollTop != 'undefined') {
            positionFromTop = document.body.scrollTop;
        }
        
         var myWidth = 0, myHeight = 0;
          if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myWidth = window.innerWidth - 1;
            myHeight = window.innerHeight;
          } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
          } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
          }
    
        var amountforOffset = (myWidth - 800)/2
        
        $("#wrapper").before("<div id='lightbox' style='display:none;'><p><img src='" + imageSource + "' /></p><p id='caption'>" + imageCaption + "</p><p id='close' onclick='closeem()'>Close</p></div>");
        $("#lightbox").before("<div id='overlay'></div>").css("top", positionFromTop + 10).css("left", amountforOffset);
        
        $("#overlay").css("width", myWidth).css("height", myHeight + positionFromTop);
        $("#head_links").css("display", "none");
        
        $("#lightbox").fadeIn("slow");
        
        return false;
    });
});

//Lightbox Additional Function
function closeem(){
    $("#lightbox").remove();
    $("#overlay").remove();
    $("#head_links").css("display", "block");
    return false;
}

function queryString(ji) {
    urlLocal = window.location.search.substring(1);
    gy = urlLocal.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

//Frequently Asked Questions
$(".faq").attr("class", "faqJS");
$(".faqJS dd").hide();
$(".faqJS dt").click(function(){
        $(".faqJS dd").hide();
        $(this).next().toggle();
        
        $('.bottom').addClass('fixed');
        $('#footer').addClass('fixed');
        $('.bottom').removeClass('fixed');
        $('#footer').removeClass('fixed');
        
    });


/*----------------------------------------------------------------------
 * setFocus( [Object o] )
 *   Sets the focus on the specified object, by 
 *   default the first element of the first form on
 *   the page is selected. Ignores hidden fields.
 *
 * author:    Arkadiusz Gruszowski, Oct 11 2005
 * modified:  Arkadiusz Gruszowski, Oct 01 2007
 */
function setFocus( o ) {
    
    if( o != undefined )
    {                                                    // If object has been specified.
        try {                                            // Try the function in case object cannot be focused
            o.focus();
            o.select();
        } catch(e) {}
    } 
    else if( document.forms.length > 0)                  // If at least 1 form is present.
    {
        var l = document.forms[0].elements.length;       // Store length for faster loop execution.
        for(var i=0; i < l; i++) {
            var element = document.forms[0].elements[i];
            if( element.type != "hidden" ) {             // If the element is of type hidden ignore.
                try {
                    element.focus();
                    element.select();
                } catch(e) {}
                break;                                   // Break if first focusable field is found.
            }
        }
    }
    
}

/* trim whitespace off the right-hand side of a string */
function rtrim(trim_str) {
  var trimmed_str = (trim_str + '').replace(/\s+$/, '');
  return(trimmed_str);
}

function objoin(obj) {
  var joinstr = '';
  for (keyword in obj) {
    joinstr = joinstr + '[' + keyword + '], ';
  }
  return(joinstr);
}

/* gets all unique category groups and keywords out of the elements provided */
function getFilterGroupCategories(obj_jq) {
  var keywords = new Object();
  obj_jq.map(function() {
    var keyword_array = $(this).html().split(',');
    var i = 0;
    while (i < keyword_array.length) {
      var cat_array = keyword_array[i].split(':');
      if (cat_array[0] == 'Categories') {
        var key_array = cat_array[1].split('.');
        var cat_id = rtrim(key_array[0]);
        if (!keywords[cat_id]) {
          keywords[cat_id] = new Object();
        }
        if (key_array.length > 1) {
          keywords[cat_id][rtrim(key_array[1])] = 1;
        }
      }
      i += 1;
    }
  });
  return(keywords);
}

/* gets all unique category keywords in the business unit out of the elements provided */
function getFilterBunitCategories(obj_jq, bunit) {
  var keywords = new Object();
  obj_jq.map(function() {
    var keyword_array = $(this).html().split(',');
    var i = 0;
    while (i < keyword_array.length) {
      var cat_array = keyword_array[i].split(':');
      var cat_id = rtrim(cat_array[0]);
      if (cat_id == 'Categories') {
        var key_array = cat_array[1].split('.');
        var key_id = rtrim(key_array[0]);
        if (key_array.length > 1 && key_id == bunit) {
          keywords[rtrim(key_array[1])] = 1;
        }
      }
      i += 1;
    }
  });
  return(keywords);
}

/* gets all unique category keywords out of the elements provided */
function getFilterCategories(obj_jq) {
  var keywords = new Object();
  obj_jq.map(function() {
    var keyword_array = $(this).html().split(',');
    var i = 0;
    while (i < keyword_array.length) {
      var cat_array = keyword_array[i].split('.');
      var group_array = cat_array[0].split(':');
      var group_id = rtrim(group_array[0]);
      if (group_id == 'Categories') {
        keywords[rtrim(group_array[1])] = 1;
      }
      i += 1;
    }
  });
  return(keywords);
}

/* gets all unique content type keywords out of the elements provided */
function getFilterTypes(obj_jq) {
  var keywords = new Object();
  obj_jq.map(function() {
    var keyword_array = $(this).html().split(',');
    var i = 0;
    while (i < keyword_array.length) {
      var cat_array = keyword_array[i].split('.');
      var group_array = cat_array[0].split(':');
      var group_id = rtrim(group_array[0]);
      if (group_id == 'Content Types') {
        keywords[rtrim(group_array[1])] = 1;
      }
      i += 1;
    }
  });
  return(keywords);
}

/* gets all unique years out of the elements provided */
function getFilterDates(obj_jq) {
  var keywords = new Object();
  var rxp_date = new RegExp("(\\d\\d\\d\\d)");
  obj_jq.map(function() {
    var matches = rxp_date.exec($(this).html());
    if (matches) {
      var year = matches[1];
      keywords[year] = 1;
    }
  });
  return(keywords);
}

/* populates a select drop-down with the keywords provided */
function populateSelect(keywords, last_insert) {
  for (keyword in keywords) {
    var keyvalue;
    switch (keyword) {
    case 'Advisor':
      keyvalue = 'Financial Advisors';
      break;
    case 'IMS':
      keyvalue = 'Investment Managers';
      break;
    case 'Institutions':
      keyvalue = 'Institutional Investors';
      break;
    case 'Private Banking':
      keyvalue = 'Private Banks';
      break;
    case 'Wealth':
      keyvalue = 'Affluent Individuals';
      break;
    default:
      keyvalue = keyword;
      break;
    }
    if (typeof(keywords[keyword]) != 'object') {
      last_insert = last_insert.after('<option value="' + keyword + '">' + keyvalue + '</option>');
    }
    else {
      var html = '<optgroup label="' + keyvalue + '">';
      for (subkey in keywords[keyword]) {
        html += '<option value="' + keyword + '.' + subkey + '">' + subkey + '</option>';
      }
      last_insert = last_insert.after(html + '</optgroup>');
    }
  }
}

/* builds an onChange event handler for filter drop-downs to hide/show relevant items */
function buildFilterChangeFunc(str_selector, func_filter) {
  var new_func = function() {
    var new_filter = $(this).val();
    if (new_filter == 'all') {
      $(str_selector).parent().parent().css('display', 'block');
    }
    else {
      $(str_selector).map(function() {
        var keywords = func_filter($(this));
        var check = new_filter.split('.');
        var check_pass = false;
        if (check.length > 1) {
          if (keywords[check[0]] && typeof(keywords[check[0]]) == 'object' && keywords[check[0]][check[1]]) {
            check_pass = true;
          }
        }
        else if (keywords[check[0]]) {
          check_pass = true;
        }
        if (check_pass) {
          $(this).parent().parent().css('display', 'block');
        }
        else {
          $(this).parent().parent().css('display', 'none');
        }
      });
    }
  };
  return new_func;
}