if (typeof window.console == 'undefined') window.console = { log: function() { } };

jQuery(function($) {
	
    (function() {
      // ROTATOR
      var duration = 5000;
      var fadeTime = 1250;
      var timer = null;
      var items = $('.rotator-item'); // find rotator items
      var currentIndex = 0;

      function startTimer() {
        clearTimer();
        timer = setTimeout(showNext, duration);
      }
      function clearTimer() {
        if (timer != null) {
          clearTimeout(timer);
          delete timer;
        }		
      }
      function showNext() {
        var next = currentIndex + 1;
        if (next >= items.length)
          next = 0;
        showItem(next);
      }
      function showItem(i) {
        clearTimer();
        
        // hide current, show this
        items.eq(currentIndex).fadeOut(fadeTime);
        items.eq(i).fadeIn(fadeTime, afterFade);
        
        // select button
        buttons.removeClass('selected');
        buttons.eq(i).addClass('selected');
        
        currentIndex = i;
      }
      function afterFade() {			
        startTimer();	
      }
      
      // create buttons
      var buttonHolder = $('#rotator-buttons');
      buttonHolder.html(''); // clear out
      items.each(function(i, el) {
				buttonHolder.append($('<a href="javascript:void(0);">' + (i+1) + '</a>'));
      });
      
			// link up buttons
      var buttons = $('#rotator-buttons a');
      buttons.each(function(i, j) {
        console.log(j, i);
        $(j).click(function(e) {
					console.log('clicked', this, i);
          showItem(i); 
          e.preventDefault();
          return false;
        });
      });
			buttons.eq(0).addClass('selected');
			
			// show first, hide all others
			items.hide();
			items.first().show();
			
      startTimer();	
     })();
     
     
    // validation
    $.updnValidatorCallout.attachAll();
     
    // FAQ
	// start up hide all defintions
	$('.dts-faq dd').hide();

	// toggle function
	$('.dts-faq dt').click(function() {
	   $(this).toggleClass('selected').next('dd').toggle(100);     
	});
	
	// open first item
	$('.dts-faq dt:first-child').each(function() {
	   $(this).addClass('selected').next('dd').toggle(100);  
	});    
	
	// Overlays
	$(".has-overlay[rel]").overlay({
		expose: { 
			color: "#FFFFFF", 
			loadSpeed: 200, 
			opacity: 0.75 
		}		 
	});
	
	// Tooltips
	$(".tooltipped a[title]").tooltip('.tooltip');
	
	// Faculty Tabs
	//if (jQuery.tabs) {
		$("ul.dts-tabs").tabs("div.tab-box");
	//}
	
	// Pagination
	if (jQuery.quickpaginate) {
		$(".paginate li").quickpaginate({ perpage: 4 });
		$("a.qp_next").attr("href", ".paginate #top");
		$("a.qp_prev").attr("href", ".paginate #top");
	}
	
	// Ellipsis
	$.fn.ellipsis = function(enableUpdating){
		var s = document.documentElement.style;
		if (!('textOverflow' in s || 'OTextOverflow' in s)) {
			return this.each(function(){
				var el = $(this);
				if(el.css("overflow") == "hidden"){
					var originalText = el.html();
					var w = el.width();
					var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute','width': 'auto','overflow': 'visible','max-width': 'inherit'
                    });
					el.after(t);
					
					var text = originalText;
					while(text.length > 0 && t.width() > el.width()){
						text = text.substr(0, text.length - 1);
						t.html(text + "...");
					}
					el.html(t.html());
					
					t.remove();
					
					if(enableUpdating == true){
						var oldW = el.width();
						setInterval(function(){
							if(el.width() != oldW){
								oldW = el.width();
								el.html(originalText);
								el.ellipsis();
							}
						}, 200);
					}
				}
			});
		} else return this;
	};
	
	
(function() {
  // create popup and attach to page
  var popup = $('<div id="media-popup"><div id="media-popup-content"></div><div id="media-popup-carrot-border-left"></div><div id="media-popup-carrot-left"></div><div id="media-popup-carrot-border-right"></div><div id="media-popup-carrot-right"></div></div>').appendTo(document.body),
    popupContent = $('#media-popup-content');

  $('.media-hover').hover(function() {
  
    var link = $(this),
      linkOffset = link.offset(),
      title = link.attr('data-title'),
      person = link.attr('data-person'),
      description = link.attr('data-description');

    // fill the popup with great info
    popupContent
      .html( '<b>'  + title + '</b><i>' + person + '</i>' + (description.length > 125 ? description.substring(0, 125) + '...' : description) + '')
    
    // position popup on the left or right  
    var left = 0,
      carrotNameToShow = 'left';
      carrotNameToHide = 'right';
    
    if (linkOffset.left + link.outerWidth() + popup.outerWidth() > $(window).width()) {
      left = linkOffset.left - popup.outerWidth() - 6;
      carrotNameToShow = 'right';
      carrotNameToHide = 'left';
    } else {
      left = linkOffset.left + link.outerWidth() + 6 ;
    }

    $('#media-popup-carrot-' + carrotNameToShow).show();
    $('#media-popup-carrot-border-' + carrotNameToShow).show();
    $('#media-popup-carrot-' + carrotNameToHide).hide();
    $('#media-popup-carrot-border-' + carrotNameToHide).hide();
      
    // animate the popup
    popup
      .css({top: linkOffset.top, left: left })
      .fadeIn(250);
            
  }, function() {
    popup
      .hide();
  });
  
})();// end of media popups

	
	
	
});
