$.slider = {}
$.slider.stopped = "false";
$.slider.wait = "false";
//*****slide function to toggle the header content on the home page******************************//
function sliderControls(){
	
	$('.event:eq(0)').show();
	$('#slider-nav li a:eq(0)').addClass('active');
	
	$('#slider-nav li a').click(function() {
	  var temp = $("#slider-nav li a").index($(this));
		$('#slider-nav li a').removeClass('active');
		$(this).addClass('active');
		$('.event').hide();
		$('.event:eq(' + temp + ')').fadeIn('slow');
		return false;
  });

	$('#slider').mouseenter(function() {                  
		//stop anim  
		$(".event").stop(true, true);
		$("#slider-nav li a").stop(true, true);
		$.slider.stopped = "true";
	});
	
	$('#slider').mouseleave(function() {       
		//start anim  
		$.slider.stopped = "false";
		$.slider.wait = "true";
	});
	
	if($('#slider-nav li a').length > 1){
		setTimeout('rotate();', 6000);
	}
}
	
	
function rotate() {	
	if($.slider.stopped == "false"){
		$.slider.wait = "false";
		var slider_size = $("#slider-nav li a").length - 1;
		var temp = $("#slider-nav li a").index($("#slider-nav li a.active"));
		var next = temp+=1;
		
		if(next <= slider_size){
			$('.event').hide();
			$('#slider-nav li a').removeClass('active');
			$('.event:eq(' + next + ')').fadeIn('slow');
			$('#slider-nav li a:eq(' + next + ')').addClass('active');
		}
		if(next > slider_size){
			$('.event').hide();
			$('#slider-nav li a').removeClass('active');
			$('.event:eq(0)').fadeIn('slow');
			$('#slider-nav li a:eq(0)').addClass('active');
		}
	}
	setTimeout('rotate();', 6000);
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnNext').click(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
	$('#btnSubmit').click(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
function populateDefaultValues(){
  $(':input').each(function(){
    if($(this).val() == ''){
      $(this).val($(this).attr('title'));      
    }
  });
}
//*****Directory page function************************************************************************//
function directoryControls() {
	if($('#directory-content').length){
		//Directory Nav Toggler
		$('#directory-nav li.people a').click(function() {
			$('#directory-nav li a').removeClass('active');
			$(this).addClass('active');
			$('#map-slide').hide();
			$('#department-slide').hide();
			$('#people-slide').show();
			return false;
		});
		$('#directory-nav li.department a').click(function() {
			$('#directory-nav li a').removeClass('active');
			$(this).addClass('active');
			$('#map-slide').hide();
			$('#department-slide').show();
			$('#people-slide').hide();
			return false;
		});
		$('#directory-nav li.map a').click(function() {
			$('#directory-nav li a').removeClass('active');
			$(this).addClass('active');
			$('#map-slide').show();
			$('#department-slide').hide();
			$('#people-slide').hide();
			return false;
		});
		// Department Tab Toggler
		$('#department-slide ul li a.toggle').click(function() {
			if($(this).hasClass('active')){
				$(this).removeClass('active');
				$(this).siblings('div').removeClass('active');
			}else{
				$(this).addClass('active');
				$(this).siblings('div').addClass('active');
			}
			return false;
		});
		//People Tab Letter Toggler
		$('#people-slide ul.alpha-nav li:not(.first) a').click(function() {
			var letter = $(this).text();
			$('#people-slide ul.alpha-nav li a').removeClass('active');
			$(this).addClass('active');
			$('#people-slide table.active').addClass('hidden');
			$('#people-slide table.active').removeClass('active');
			$('#people-slide table.' + letter).addClass('active').removeClass('hidden');
			return false;
		});
		$('#people-slide ul.alpha-nav li.first a').click(function() {
			$('#people-slide ul.alpha-nav li a').removeClass('active');
			$(this).addClass('active');
			$('#people-slide table.hidden').addClass('active');
			$('#people-slide table.hidden').removeClass('hidden');
			return false;
		});
	}
}
/* Adjust sidebar to be the max height */
function sidebarHeight(){
	if($('iframe').length){
		$('iframe').load(function(){
			$('#sidebar').height(938);
		});
	}
	else{
		if($('#sidebar').height() < $('#main').height()){
			$('#sidebar').height($('#main').height());
		}
	}
}
/* Add classes to navigation elements to maintain design and functionality */
function navClasses(){
	if($('#breadcrumbs').length){
		$('#breadcrumbs > li:last-child').addClass('last');
	}
	if($('#mainNav').length){
		$('#mainNav li ul > li:last-child').addClass('last');
	}
	if($('#leftbar ul').length){
		$('#leftbar ul > li:last-child').addClass('last');
	}
}
//*****faculty Switcher**************************************************//
function facultySelectChange() {
  if ($('#slctFacultyMembers').length < 1) return false;
  $('#slctFacultyMembers').change(function() {
    var site = $('#slctFacultyMembers option:selected').val();
    window.location.href = (site);
  });
}
//*****Trim Slider Titles to Correct Size**********************************//
function trimSliderTitles(){
	if($('#slider-nav li a').length){
		var numTitles = $('#slider-nav li a').length;
		
		switch(numTitles)
		{
		case 1:
			$('#slider-nav').addClass('one');
			$('#slider-nav li a').each(function(){
				if($(this).text().length > 125){
					// get the text within the a
					// remove leading and trailing spaces
					// get first 20 characters
					// separate characters into an array of words
					// remove the last full or partial word
					// combine into a single string and append "..."
					var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
					var shortText = shortText.substring(0, 25).split(" ").slice(0, -1).join(" "); 
					$(this).text(shortText);
					$(this).append("&hellip;");
				}
			});
		break;
		case 2:
			$('#slider-nav').addClass('two');
			$('#slider-nav li a').each(function(){
				if($(this).text().length > 63){
					// get the text within the a
					// remove leading and trailing spaces
					// get first 63 characters
					// separate characters into an array of words
					// remove the last full or partial word
					// combine into a single string and append "..."
					var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
					var shortText = shortText.substring(0, 25).split(" ").slice(0, -1).join(" "); 
					$(this).text(shortText);
					$(this).append("&hellip;");
				}
			});
		break;
		case 3:
			$('#slider-nav').addClass('three');
			$('#slider-nav li a').each(function(){
				if($(this).text().length > 42){
					// get the text within the a
					// remove leading and trailing spaces
					// get first 42 characters
					// separate characters into an array of words
					// remove the last full or partial word
					// combine into a single string and append "..."
					var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
					var shortText = shortText.substring(0, 42).split(" ").slice(0, -1).join(" "); 
					$(this).text(shortText);
					$(this).append("&hellip;");
				}
			});
		break;
		case 4:
			$('#slider-nav').addClass('four');
			$('#slider-nav li a').each(function(){
				if($(this).text().length > 31){
					// get the text within the a
					// remove leading and trailing spaces
					// get first 31 characters
					// separate characters into an array of words
					// remove the last full or partial word
					// combine into a single string and append "..."
					var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
					var shortText = shortText.substring(0, 31).split(" ").slice(0, -1).join(" "); 
					$(this).text(shortText);
					$(this).append("&hellip;");
				}
			});
		break;
		case 5:
			$('#slider-nav').addClass('five');
			$('#slider-nav li a').each(function(){
				if($(this).text().length > 25){
					// get the text within the a
					// remove leading and trailing spaces
					// get first 25 characters
					// separate characters into an array of words
					// remove the last full or partial word
					// combine into a single string and append "..."
					var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
					var shortText = shortText.substring(0, 25).split(" ").slice(0, -1).join(" "); 
					$(this).text(shortText);
					$(this).append("&hellip;");
				}
			});
		break;
		}
	}
}
//*****Trim bread crumb text****************************************************************//
function trimBreadCrumbTitles(){
	$('#breadcrumbs > li > a').each(function(){
		if($(this).text().length > 20){
			// get the text within the a
			// remove leading and trailing spaces
			// get first 20 characters
			// separate characters into an array of words
			// remove the last full or partial word
			// combine into a single string and append "..."
			var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
			var shortText = shortText.substring(0, 25).split(" ").slice(0, -1).join(" "); 
			$(this).text(shortText);
			$(this).append("&hellip;");
		}
	});
}
//*****OPTIONS formating text strings in mensu*********************************************//
function optionsTextFormat(){
	if($('#footerNav').length){
		$('#footerNav li.addmission ul.subnav li').html(function(index, html){ 
			return html.replace(/OPTIONS - for Adult Learners/g,"OPTIONS - <br />for Adult Learners");
		});
		$('#footerNav li.academics ul.subnav li').html(function(index, html){ 
			return html.replace(/OPTIONS - for Adult Learners/g,"OPTIONS - <br />for Adult Learners");
		});	
	}
	if($('#mainNav').length){
		$('#mainNav li.addmission ul.subnav li').html(function(index, html){ 
			return html.replace(/OPTIONS - for Adult Learners/g,"OPTIONS - <br />for Adult Learners");
		});
		$('#mainNav li.academics ul.subnav li').html(function(index, html){ 
			return html.replace(/OPTIONS - for Adult Learners/g,"OPTIONS - <br />for Adult Learners");
		});	
	}
}
//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget(){
	$('a[target]').not('a[target=_self]').removeAttr('target').addClass('newwindow');
  $('a[target]').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  $('a.newwindow').live('click', function(){
    window.open($(this).attr('href'));
    return false;
  });
}
//*****Mask function from http://digitalbush.com/projects/masked-input-plugin**************************************************//
function mask() {
	if($('#txtMsg').length){
  	$('#txtMsg').mask('(999) 999-9999');
	}
	if($('#txtZip').length){
		$('#txtZip').mask('99999');
	}
	if($('#txtPhone').length){
		$('#txtPhone').mask('(999) 999-9999');
	}
	if($('#txtCellPhone').length){
		$('#txtCellPhone').mask('(999) 999-9999');
	}
	if($('#txtWorkPhone').length){
		$('#txtWorkPhone').mask('(999) 999-9999');
	}
	if($('#txtPreferredPhone').length){
		$('#txtPreferredPhone').mask('(999) 999-9999');
	}
	if($('#txtDOB').length){
		$('#txtDOB').mask('99/99/9999');
	}
	if($('#txtSSN').length){
		$('#txtSSN').mask('999-99-9999');
	}
	if($("#inputID").length){
		$("#inputID").keyup(function() {
			$("#inputID").val($(this).val().toLowerCase().replace(' ','_'));
		});
	}
}
//*****jQuery form validation from http://bassistance.de/jquery-plugins/jquery-plugin-validation/**************************************************//
function validate(){
  if($('#formId').length == 0){ return; }
  $('#formId').validate({
    //add the error class to the label and element
    highlight: function(element, errorClass) {
		  if ($(element).is(':radio')) {
		    $(element).parent().find('h4').addClass(errorClass);
		  } else {
        $(element).addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
      }
    },
    //remove error class from label and element once valid
    unhighlight: function(element, errorClass) {
		  if ($(element).is(':radio')) {
		    $(element).parent().find('h4').removeClass(errorClass);
		    $(element).parent().prev('h4').addClass('valid');
		  } else {
        $(element).removeClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
      }
    },
    //add the green box and checkmark for valid fields
    success: function(label) {
		  if (label.prev().is(':checkbox')) {
        label.next().addClass("valid");
        label.addClass("valid").text("");
      } else {
        label.prev().addClass("valid");
        label.addClass("valid").text("");
      }
    },
    onfocusout: false,
    onkeyup: false,
    onclick: false,
    //do not use the title attribute as the error message
    ignoreTitle: true,
    //make the error text wrapped in a span
    errorElement: 'span',
    //javascript instead of class specified rules
    rules: {
      'agreeToTerms': 'required'
    },
    //messages other than the default 'X'
    messages: {
		  'agreeToTerms': '<br />X Please select one of the options above'
		},
    // the errorPlacement has to take the h2 of radio buttons into account
		errorPlacement: function(error, element) {
		  if ( element.is(':radio') )
		    error.appendTo( element.parent() );
		  else
				error.insertAfter(element);
		}
  });
}
//*****News menu toggler controls******************************************//
function newsToggle(){
	if($('ul.news').length){
		$('ul.news > li > a').click(function(){
			if($(this).parent('li').children('ul').length){
				$('ul.news > li > a').not(this).parent('li').children('ul').hide();
				$(this).parent('li').children('ul').toggle();
				return false;
			}
		});
		$('ul.news > li > ul > li > a').click(function(){
			if($(this).parent('li').children('ul').length){
				$('ul.news > li > ul > li > a').not(this).parent('li').children('ul').hide();
				$(this).parent('li').children('ul').toggle();
				return false;
			}		
		});
	}
}
//*****Tab States for the Directory / Load into proper tag
function directoryTabChoice(){
	if($('body#directory').length){
		var tab_value = getQuerystring('tab');
		
		if(tab_value == "people"){
			$('#directory-nav li a').removeClass('active');
			$('#directory-nav li.people a').addClass('active');
			$('#map-slide').hide();
			$('#department-slide').hide();
			$('#people-slide').show();
		}
		else if(tab_value == "department"){
			$('#directory-nav li a').removeClass('active');
			$('#directory-nav li.department a').addClass('active');
			$('#map-slide').hide();
			$('#department-slide').show();
			$('#people-slide').hide();
		}
		else if(tab_value == "map"){
			$('#directory-nav li a').removeClass('active');
			$('#directory-nav li.map a').addClass('active');
			$('#map-slide').show();
			$('#department-slide').hide();
			$('#people-slide').hide();
		}
	}
}
//***************************************strips query string values based on what key you use*****//
//***************************************ex: var tab_value = getQuerystring('tab');***************//
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
	return default_;
  else
	return qs[1];
}
//*****Replace FLV embeds with Flowplayer**********************************//
function flvFlowplayer()
{
	var srcParam = $('object param[name="src"]');
	var src = srcParam.val();
	if(src && /.flv$/.test(src)){
		var objectEl = srcParam.parent();

		// get the width and height
		var width = objectEl.attr('width');
		var height = objectEl.attr('height');

		// add flowplayer
		$.getScript('/js/flowplayer-3.2.6.min.js', function(){
			var playerMarkup = '<div id="video-area" style="width: ' + width + 'px; height: ' + height + 'px;"></div>';
			objectEl.before(playerMarkup);
			objectEl.remove();
			flowplayer('video-area', 'http://builds.flowplayer.netdna-cdn.com/56587/28757/flowplayer.commercial-3.2.5-2.swf?0.8489860161207616', {
				clip: src
			});
		});
	}
}
//*****Load all functions**************************************************//
$(document).ready(function(){
	replaceAlign();
	replaceTarget();
	strictNewWindow();
	sliderControls();
	populateDefaultValues();
  clearDefaultValue();
	directoryControls();
	sidebarHeight();
	facultySelectChange();
	navClasses();
	trimSliderTitles();
	trimBreadCrumbTitles();
	optionsTextFormat();
	mask();
	validate();
	newsToggle();
	directoryTabChoice();
	flvFlowplayer();
	$('a[href=/infocenter/calendar/], a[href=/calendar/]').addClass('newwindow');
	$('a[href=/athletics/], a[href=/studentlife/athletics/]').addClass('newwindow');
});

