/*-------------------------------------------------
Title:    custom jQuery scripts
Updated:  March 26, 2010
------------------------------------------------- */

/* scaffolding resize
------------------------------------------------- */
Array.prototype.max = function() {
  var max = this[0];
  var len = this.length;
  for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
  return max;
}

Array.prototype.min = function() {
  var min = this[0];
  var len = this.length;
  for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
  return min;
}

function setsize(w) {
  // loop through children again
  $('#primary-main .content > div,#primary-main .content > table').each(function(n) {
    var elWidth = $(this).width() + parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right"));

    // if with is auto, set the content width to auto
    if ($(this).css("width") == "auto") {
      $('#content').css("width", "auto");
    }
    else {
      // if the element width is greater than or equal to the widest element, proceed with resizing logic
      if (elWidth >= w) {

        var dif = $(this).width() - $('#primary-main').width();
        var padding = parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right"));
        var newWidth = $('#content').width() + dif + padding + 'px';

        if ($(this).width() <= ($('#primary-main').width() + (padding + 1))) {
          $('#content').css("width", "auto");
        }
        if ($(this).width() > ($('#primary-main').width() - (padding + 1))) {
          $('#content').css("width", newWidth);
        }
      }
    }
  });
}

function resize() {
  var widths = new Array();

  // loop through children and put the widths of all elements into an array
  $('#primary-main .content > div,#primary-main .content > table').each(function(n) {
    if ($(this).css("width") !== "auto") {
      var i = $(this).attr("id");
      widths[n] = $(this).width() + parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right"));
      return widths;
    }
  });

  // find the widest width in the array
  var widest = widths.max();

  if ($('#masthead').width() > ($('#content').width() + parseInt($('#content').css("padding-left")) + parseInt($('#content').css("padding-right")))) {
    $('#content').css("width", "auto");
  }
  else {
    setsize(widest);
  }
};

$(document).ready(function() {
  if ($('body').hasClass("fluid")) {
    resize();

    var resizeTimer = null;

    $(window).bind('resize', function() {
      if (resizeTimer) clearTimeout(resizeTimer);
      resizeTimer = setTimeout(resize, 0);
    });
  }
});
/* -------------------------------------------------
end scaffolding resize */





/* Education Card
------------------------------------------------- */
//$(document).ready(function() {
//  if ($('body').hasClass('edu')) {
//    $('#ec_bubble').customFadeIn(750); // fade the bubble in

//    var bubbleTimer = setTimeout(
//    function() {
//      $('#ec_bubble').customFadeOut(750); // fade the bubble out
//      $('#ec_leftcol').css('z-index', '1000'); // re-set the left column's z-index
//      $('#ec_rightcol').css('z-index', '1000'); // re-set the left column's z-index
//      clearTimeout(bubbleTimer); // clear the timeout variable
//    },
//      5000 // time before bubble fades out
//    );
//  }
//});

function triggerEduCardBalloon() {
    $('#ec_bubble').customFadeIn(750); // fade the bubble in

    var bubbleTimer = setTimeout(
    function() {
      $('#ec_bubble').customFadeOut(750); // fade the bubble out
      $('#ec_leftcol').css('z-index', '1000'); // re-set the left column's z-index
      $('#ec_rightcol').css('z-index', '1000'); // re-set the left column's z-index
      clearTimeout(bubbleTimer); // clear the timeout variable
    },
      5000 // time before bubble fades out
    );

}
/* -------------------------------------------------
end Education Card */





/* Product Loader
------------------------------------------------- */
$(document).ready(function() {
  $('ul.products li a').click(function() {

    // clear the current "active" product icon
    $('ul.products li').each(function(index) {
      $(this).removeClass("active");
    });

    // set the new "active" product icon
    $(this).parent().addClass("active");

    // fade out the current product
    $('#primary-main div:first').fadeOut(600);

    // insert the selected product into the primary div and immediately hide it
    $('#primary-main').html($($(this).attr("href")).html());
    $('#primary-main div:first').hide();

    // fade in the new product
    $('#primary-main div:first').customFadeIn(600);
    return false;

  });
});
/* -------------------------------------------------
end Product Loader */





/* Content Cyclers
------------------------------------------------- */
jQuery.fn.customCycler = function(n, rate) {
  var cycler = $(this);

  // initiate ad cyclers
  if (cycler.hasClass("manual")) {
    cycler.cycle({
      fx: 'fade',
      timeout: 0,
      prev: '#prev' + n,
      next: '#next' + n,
      pager: '#paginate' + n,
      pagerAnchorBuilder: paginator,
      cleartype: true
    });
  }
  else {
    if (rate == "ad") {
      cycler.cycle({
        fx: 'fade',
        //timeout: 30000, // 30 seconds
        timeout: 3600000, // 1 hour
        prev: '#prev' + n,
        next: '#next' + n,
        prevNextClick: prevNextAd,
        pager: '#paginate' + n,
        pagerClick: selectAd,
        pagerAnchorBuilder: paginator,
        cleartype: true
      });
    }

    // initiate default cyclers
    else if (rate == "slow") {
      cycler.cycle({
        fx: 'fade',
        timeout: 15000, // 15 seconds
        prev: '#prev' + n,
        next: '#next' + n,
        pager: '#paginate' + n,
        pagerAnchorBuilder: paginator,
        cleartype: true
      });
    }

    // initiate default cyclers
    else if (rate == "fast") {
      cycler.cycle({
        fx: 'fade',
        timeout: 7000, // 7 seconds
        prev: '#prev' + n,
        next: '#next' + n,
        pager: '#paginate' + n,
        pagerAnchorBuilder: paginator,
        cleartype: true
      });
    }

    // initiate default cyclers
    else {
      cycler.cycle({
        fx: 'fade',
        timeout: 11000, // 11 seconds
        prev: '#prev' + n,
        next: '#next' + n,
        pager: '#paginate' + n,
        pagerAnchorBuilder: paginator,
        cleartype: true
      });
    }
  }

  $('div.cycler').each(function(index) {
    $('#paginate' + n).append($('#next' + n));
  });

  function writeAdBlock(el, tile) {
    // ad code block
    var node = '<iframe src="http://ad.doubleclick.net/adi/cboe.dart/' + zone + ';sz=300x250;tile=' + tile + ';ord=' + ord + '" width="300" height="250" valign="top" align="right" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"><a href="http://ad.doubleclick.net/jump/cboe.dart/' + zone + ';sz=300x250;tile=' + tile + ';ord=' + ord + '" target="_new"><img src="http://ad.doubleclick.net/ad/cboe.dart/' + zone + ';sz=300x250;tile=' + tile + ';ord=' + ord + '" width="300" height="250" border="0" alt="AD"></a></iframe>';

    // loop through siblings and remove ad block
    $(el).siblings().each(function() {
      $(this).html("");
    });

    // set ad block for active slide
    $(el).html(node);
  };

  function prevNextAd(isNext, slideIndex, slideNode) {
    var tile = tiles[slideIndex];

    writeAdBlock(slideNode, tile);
  };

  function selectAd(slideIndex, slideNode) {
    var tile = tiles[slideIndex];

    writeAdBlock(slideNode, tile);
  };

  // paginator function
  function paginator(idx, slide) {
    return '<div class="unit"><a href="#">' + (idx + 1) + '</a></div>';
  };
};

$(document).ready(function() {
  $('div.cycler').each(function(index) {
    var n = index + 1;
    if ($('#cycler' + n).parent().hasClass("active")) {
      if ($('#cycler' + n).hasClass("ad")) {
        $('#cycler' + n).customCycler(n, "ad");
      }
      else if ($('#cycler' + n).hasClass("slow")) {
        $('#cycler' + n).customCycler(n, "slow");
      }
      else if ($('#cycler' + n).hasClass("fast")) {
        $('#cycler' + n).customCycler(n, "fast");
      }
      else {
        $('#cycler' + n).customCycler(n, "default");
      }
    }
    else if ($('#cycler' + n).hasClass("manual")) {
      $('#cycler' + n).customCycler(n, "default");
    }
  });
});
/* -------------------------------------------------
end Content Cycler */



/* myCBOE Accordion
------------------------------------------------- */
$(document).ready(function() {
  $('#myAccordion').accordion({
    active: '.open',
    alwaysOpen: false,
    animated: "easeslide",
    autoheight: false,
    header: "div.toggle",
    change: function(event, ui) { accordionChange(event, ui); }    
  });

  $("#myAccordion div.toggle").click(function() {

    $('#myAccordion div.toggle').each(function(index) {
      $(this).addClass("closed");
      $(this).removeClass("open");
    });

    $(this).addClass("open");
  });
});

function accordionChange(event, ui) {
  if ($(event.newContent).attr("id") == "IF_IDCSnapShot") {
    reSizeMySnapShot();
  }
}

function reSizeMySnapShot() {
  try {
    var oFrame = document.getElementById("IF_IDCSnapShot");
    oFrame.style.height = 320; //oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
    oFrame.style.width = 310;  //oBody.scrollWidth + (oBody.offsetWidth - oBody.clientWidth);
    $('#IF_IDCSnapShot').width(310);
    $('#IF_IDCSnapShot').height(320);
  }
  //An error is raised if the IFrame domain != its container's domain
  catch (e) {
    window.status = 'Error: ' + e.number + '; ' + e.description;
  }
}

/* -------------------------------------------------
end myCBOE Accordion */





/* Tool Accordion
------------------------------------------------- */
$(document).ready(function() {
  $('#toolAccordion').accordion({
    active: '.primary-bar-on',
    alwaysOpen: false,
    animated: "easeslide",
    autoheight: false,
    header: "div.toggle"
  });

  $("#toolAccordion div.toggle").click(function() {

    $('#toolAccordion div.toggle').each(function(index) {
      $(this).addClass("primary-bar-off");
      $(this).removeClass("primary-bar-on");
    });

    $(this).addClass("primary-bar-on");
  });
});
/* -------------------------------------------------
end Tool Accordion */





/* Expand/Collapse Accordion
------------------------------------------------- */
jQuery.fn.expandAccordion = function() {

  var accordion = $(this);

  // tool accordion
  $(this).children('.content').slideDown();

  $(this).children('.primary-bar-off').each(function(index) {
    $(this).removeClass("primary-bar-off");
    $(this).addClass("primary-bar-on");
  });

  $(this).children('.primary-bar-on').click(function() {
    accordion.collapseAccordion();
    $(this).addClass("primary-bar-on");
    $(this).next('.content').slideDown();
    $(this).unbind("click");
  });

  // myCBOE accordion
  $(this).children('.sub_mod').slideDown();

  $(this).children('.closed').each(function(index) {
    $(this).removeClass("closed");
    $(this).addClass("open");
  });

  $(this).children('.open').click(function() {
    accordion.collapseAccordion();
    $(this).next('.sub_mod').slideDown();
    $(this).unbind("click");
  });
};

jQuery.fn.collapseAccordion = function() {

  // tool accordion
  $(this).children('.content').slideUp();
  $(this).children('.primary-bar-on').each(function(index) {
    $(this).removeClass("primary-bar-on");
    $(this).addClass("primary-bar-off");
  });

  // myCBOE accordion
  $(this).children('.sub_mod').slideUp();
  $(this).children('.open').each(function(index) {
    $(this).removeClass("open");
    $(this).addClass("closed");
  });

  $(this).accordion("activate", false);
};


jQuery.fn.expandAccordionByID = function(divID) {
  divID = '#' + divID;
  var accordion = $(divID);

  // tool accordion
  $(divID).children('.content').slideDown();

  $(divID).children('.primary-bar-off').each(function(index) {
    $(divID).removeClass("primary-bar-off");
    $(divID).addClass("primary-bar-on");
  });

  $(divID).children('.primary-bar-on').click(function() {
    accordion.collapseAccordion();
    $(divID).addClass("primary-bar-on");
    $(divID).next('.content').slideDown();
    $(divID).unbind("click");
  });

  // myCBOE accordion
  $(divID).children('.sub_mod').slideDown();

  $(divID).children('.closed').each(function(index) {
    $(divID).removeClass("closed");
    $(divID).addClass("open");
  });

  $(divID).children('.open').click(function() {
    accordion.collapseAccordion();
    $(divID).next('.sub_mod').slideDown();
    $(divID).unbind("click");
  });
};

jQuery.fn.collapseAccordionByID = function(divID) {
  divID = '#' + divID;

  // tool accordion
  $(divID).children('.content').slideUp();
  $(divID).children('.primary-bar-on').each(function(index) {
    $(divID).removeClass("primary-bar-on");
    $(divID).addClass("primary-bar-off");
  });

  // myCBOE accordion
  $(divID).children('.sub_mod').slideUp();
  $(divID).children('.open').each(function(index) {
    $(divID).removeClass("open");
    $(divID).addClass("closed");
  });

  $(divID).accordion("activate", false);
};
/* -------------------------------------------------
end Expand/Collapse Accordion */





/* Tabs
------------------------------------------------- */
(function($) {
  $.fn.customFadeIn = function(speed, callback) {
    $(this).fadeIn(speed, function() {
      if (jQuery.browser.msie)
        $(this).get(0).style.removeAttribute('filter');
      if (callback != undefined)
        callback();
    });
  };
  $.fn.customFadeOut = function(speed, callback) {
    $(this).fadeOut(speed, function() {
      if (jQuery.browser.msie)
        $(this).get(0).style.removeAttribute('filter');
      if (callback != undefined)
        callback();
    });
  };
})(jQuery);

$(document).ready(function() {
  $('.module-tabs > li > a').click(function() {

    if ($(this).parent().parent().hasClass("product-tabs")) {
      if ($(this).parent().attr("class") !== "button") {
        $(this).parent().siblings("li").each(function(index) {
          $(this).removeClass("active");
        });
        $(this).css("outline", "none");
        $(this).parent().addClass("active");

        $(this).parent().parent().parent().siblings("div.boxer").each(function(index) {
          $(this).hide();
        });

        $($(this).attr("href")).customFadeIn();

        // clear the current "active" product icon
        $('ul.products li').each(function(index) {
          $(this).removeClass("active");
        });

        // set the new "active" product icon
        $($(this).attr("href") + ' ul.products li:first a').parent().addClass("active");

        // fade out the current product
        $('#primary-main div:first').fadeOut(600);

        // insert the selected product into the primary div and immediately hide it
        $('#primary-main').html($($($(this).attr("href") + ' ul.products li:first a').attr("href")).html());
        $('#primary-main div:first').hide();

        // fade in the new product
        $('#primary-main div:first').customFadeIn(600);
        return false;
      } else {
        return false;
      }
    }
    else {
      if ($(this).parent().attr("class") !== "button") {
        $(this).parent().siblings("li").each(function(index) {
          $(this).removeClass("active");
        });
        $(this).css("outline", "none");
        $(this).parent().addClass("active");

        $(this).parent().parent().parent().siblings("div.boxer").each(function(index) {
          $(this).hide();
        });

        $($(this).attr("href")).customFadeIn();
        return false;
      } else {
        return false;
      }
    }
  });


  $('.tabs > div.navrow > div > div > a').click(function() {

    $(this).blur();

    if ($(this).parent().hasClass("modtab_on") || $(this).parent().hasClass("modtab_tv_on") || $(this).parent().hasClass("modwhitetab_on") || $(this).parent().hasClass("modblutab_on")) {
      return false;
    }
    else {

      /* module tabs */
      if ($(this).parent().hasClass("modtab_off")) {
        $(this).parent().siblings("div").each(function(index) {
          if ($(this).hasClass("modtab_on")) {
            $(this).removeClass("modtab_on");
            $(this).addClass("modtab_off");
          }
        });

        $(this).parent().removeClass("modtab_off");
        $(this).parent().addClass("modtab_on");
      }

      /* tv module tabs */
      if ($(this).parent().hasClass("modtab_tv_off")) {
        $(this).parent().siblings("div").each(function(index) {
          if ($(this).hasClass("modtab_tv_on")) {
            $(this).removeClass("modtab_tv_on");
            $(this).addClass("modtab_tv_off");
          }
        });

        $(this).parent().removeClass("modtab_tv_off");
        $(this).parent().addClass("modtab_tv_on");
      }

      /* white module tabs */
      if ($(this).parent().hasClass("modwhitetab_off")) {
        $(this).parent().siblings("div").each(function(index) {
          if ($(this).hasClass("modwhitetab_on")) {
            $(this).removeClass("modwhitetab_on");
            $(this).addClass("modwhitetab_off");
          }
        });

        $(this).parent().removeClass("modwhitetab_off");
        $(this).parent().addClass("modwhitetab_on");
      }

      /* blue module tabs */
      if ($(this).parent().hasClass("modblutab_off")) {
        $(this).parent().siblings("div").each(function(index) {
          if ($(this).hasClass("modblutab_on")) {
            $(this).removeClass("modblutab_on");
            $(this).addClass("modblutab_off");
          }
        });

        $(this).parent().removeClass("modblutab_off");
        $(this).parent().addClass("modblutab_on");
      }



      // hide other tabs & remove "active" class
      $(this).parent().parent().parent().siblings("div.content").each(function(index) {
        $(this).removeClass("active");
        $(this).hide();
      });


      // show active tab & add "active" class
      $($(this).attr("href")).addClass("active");
      if (($(this).parent().hasClass("modtab_tv_on")) || ($(this).parent().hasClass("modtab_tv_off"))) {
        return;
      }
      $($(this).attr("href")).customFadeIn(1000);

      // find sibling cyclers and stop them!
      $($(this).attr("href")).siblings("div.content").find("div.cycler").each(function(index) {
        $(this).cycle('stop');

        // removes pagination elements (otherwise will be re-built if/when tab is re-focused
        $('#paginate' + $(this).attr("id").substring(6) + ' div.unit').remove();
      });



      // find nested cyclers and activate them!
      $($(this).attr("href") + ' div.cycler').each(function(index) {

        var n = $(this).attr("id").substring(6);

        // activate ad cyclers
        if ($(this).hasClass("ad")) {
          $(this).customCycler(n, "ad");
        }

        // activate slow cyclers
        else if ($(this).hasClass("slow")) {
          $(this).customCycler(n, "slow");
        }

        // activate fast cyclers
        else if ($(this).hasClass("fast")) {
          $(this).customCycler(n, "fast");
        }

        // activate default cyclers
        else {
          $(this).customCycler(n, "default");
        }

        $('#paginate' + n).append($('#next' + n));
      });

      return false;
    }
  });
});
/* -------------------------------------------------
end Tabs */





/* Graph Tooltip
------------------------------------------------- */
$(document).ready(function() {
  $('tr.graph-tooltip').cluetip({
    width: 215,
    topOffset: -78,
    leftOffset: -491,
    splitTitle: '|',
    dropShadow: false,
    positionBy: 'fixed',
    onActivate: function(e) { $('#scaffold').append($('#cluetip')); return true; }
  });

  $('tr.graphFirstTab-tooltip').cluetip({
    width: 215,
    topOffset: -78,
    leftOffset: -445,
    splitTitle: '|',
    dropShadow: false,
    positionBy: 'fixed'
  });

  $('tr.graphNotFirstTab-tooltip').cluetip({
    width: 215,
    topOffset: -77,
    leftOffset: -185,
    splitTitle: '|',
    dropShadow: false,
    positionBy: 'fixed'
  });



  /*
  $('tr.graphCharts-tooltip').cluetip({
  width: 215,
  topOffset: $(this).parent.attr("class")
  nope valign: top,
  top: 0,
  top: 0%
  top: $(this).parent.attr("top")
  top: 100,
  leftOffset: 0,
  splitTitle: '|',
  dropShadow: false,
  positionBy: 'fixed'
  });
  */
});
/* -------------------------------------------------
end Graph Tooltip */




/* IE6 Transparency Fix
------------------------------------------------- */
$(document).ready(function() {
  $('.pngfix').each(function() {
    $(this).attr('writing-mode', 'tb-rl');
    $(this).css('background-image', 'none');
    $(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="path/to/image.png",sizingMethod="scale")');
  });
});
/* -------------------------------------------------
end IE6 Transparency Fix */



