$(document).ready(function(){

	var isiPad		= navigator.userAgent.match(/iPad/i) != null;
	var isiPhone	= navigator.userAgent.match(/iPhone/i) != null;
	var isiPod		= navigator.userAgent.match(/iPod/i) != null;
	var isAndroid	= navigator.userAgent.match(/Android/i) != null;

	// enable ColorBox
	// ===============
	$(".singleView").colorbox();
	$("a[rel='galleryView']").colorbox();
	// Only enables if browser is not one of the popular mobile browsers
	if(!isiPad && !isiPhone && !isiPod && !isAndroid) {
		$(".formView").colorbox({height:"75%", width:"75%", maxWidth:"940px", iframe:true, overlayClose:false});
		$(".demoViewOmnibus").colorbox({height:"535px", width:"675px", iframe:true, overlayClose:false});
		$("a[rel='demoViewOmnibus']").colorbox({height:"535px", width:"675px", iframe:true, overlayClose:false, current:"demo {current} of {total}"});
		$(".demoViewGeneration21").colorbox({height:"765px", width:"1025px", iframe:true, overlayClose:false});
		$("a[rel='demoViewGeneration21']").colorbox({height:"855px", width:"1075px", iframe:true, overlayClose:false, current:"demo {current} of {total}"});
		$(".demoView").colorbox({height:"765px", width:"1025px", iframe:true, overlayClose:false});
		$("a[rel='demoView']").colorbox({height:"765px", width:"1025px", iframe:true, overlayClose:false, current:"demo {current} of {total}"});
		$("a[rel='pressView']").colorbox({height:"75%", width:"75%", maxWidth:"940px", iframe:true, overlayClose:false, current:"release {current} of {total}"});
		$("a[rel='enewsView']").colorbox({height:"75%", width:"75%", maxWidth:"940px", iframe:true, overlayClose:false, current:"eNews {current} of {total}"});
	}
	
		
  // scroll spy logic
  // ================

  var activeTarget,
      $window = $(window),
      position = {},
      nav = $('body > .topbar li a'),
      targets = nav.map(function () {
        return $(this).attr('href');
      }),
      offsets = $.map(targets, function (id) {
        return $(id).offset().top;
      });


  function setButton(id) {
    nav.parent("li").removeClass('active');
    $(nav[$.inArray(id, targets)]).parent("li").addClass('active');
  }

  function processScroll(e) {
    var scrollTop = $window.scrollTop() + 10, i;
    for (i = offsets.length; i--;) {
      if (activeTarget != targets[i] && scrollTop >= offsets[i] && (!offsets[i + 1] || scrollTop <= offsets[i + 1])) {
        activeTarget = targets[i];
        setButton(activeTarget);
      }
    }
  }

  nav.click(function () {
    processScroll();
  });

  processScroll();

  $window.scroll(processScroll);


  // Dropdown example for topbar nav
  // ===============================

  $("body").bind("click", function (e) {
    $('a.menu').parent("li").removeClass("open");
  });

  $("a.menu").click(function (e) {
    var $li = $(this).parent("li").toggleClass('open');
    return false;
  });


  // Slideshow logic (must link to jquery.cycle.all.js)
  //	Used on omnibuslearning.com/clients.html.
  //	Commented out here and placed at bottom of that
  //	page because it threw errors on all other pages.
  // ==================================================
  
	/*$('.slideshow').cycle({
		fx: 'scrollLeft' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});*/


  // table sort example
  //	Commented out because it threw errors on all pages.
  //	Doesn't appear to be used anywhere as the tables
  //	are not sortable.
  // ====================================================

  /*$("#sortTableExample").tablesorter( {sortList: [[1,0]]} );*/


  // add on logic
  // ============

  $('.add-on :checkbox').click(function() {
    if ($(this).attr('checked')) {
      $(this).parents('.add-on').addClass('active');
    } else {
      $(this).parents('.add-on').removeClass('active');
    }
  });


  // Disable certain links in docs
  // =============================

  $('ul.tabs a, ul.pills a, .pagination a, .well .btn, .actions .btn, .alert-message .btn, a.close').click(function(e) {
    e.preventDefault();
  });

  // Copy code blocks in docs
  $(".copy-code").focus(function() {
    var el = this;
    // push select to event loop for chrome :{o
    setTimeout(function () { $(el).select(); }, 1);
  });


  // POSITION TWIPSIES
  // =================

  $('.twipsies.well a').each(function () {
    var type = this.title
      , $anchor = $(this)
      , $twipsy = $('.twipsy.' + type)

      , twipsy = {
          width: $twipsy.width() + 10
        , height: $twipsy.height() + 10
        }

      , anchor = {
          position: $anchor.position()
        , width: $anchor.width()
        , height: $anchor.height()
        }

      , offset = {
          above: {
            top: anchor.position.top - twipsy.height
          , left: anchor.position.left + (anchor.width/2) - (twipsy.width/2)
          }
        , below: {
            top: anchor.position.top + anchor.height
          , left: anchor.position.left + (anchor.width/2) - (twipsy.width/2)
          }
        , left: {
            top: anchor.position.top + (anchor.height/2) - (twipsy.height/2)
          , left: anchor.position.left - twipsy.width - 5
          }
        , right: {
            top: anchor.position.top + (anchor.height/2) - (twipsy.height/2)
          , left: anchor.position.left + anchor.width + 5
          }
      }

    $twipsy.css(offset[type])

  });
  
  	$(function() {
		// setup ul.tabs to work as tabs for each div directly under div.panes
		$("ul.tabs").tabs("div.panes > div");
	});
	
});
