$(document).ready(function() {
  //Set the opacity of all images to 0
	$('div.img div').css({opacity: 0.0}).hide();
	
	//Get the first image and display it (gets set to full opacity)
	$('div.img div:first').show().css({opacity: 1.0}).addClass('show');
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	var interval = setInterval('rotate()',4000);
  initTabs('.tabset',interval);
  initSearchBox('.search-form .txt');
});
function rotate() {	
	//Get the first image
	var current = ($('div.img div.show')?  $('div.img div.show') : $('div.img div:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.img div:first') :current.next()) : $('div.img div:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.show()
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000, function() { $(this).hide(); })
	.removeClass('show');
	
}
function initTabs(h_list,interval) {
	$(h_list).each(function(_ind, _el) {
		var btn_h = $(_el);
		var _btns = $(_el).find('a.tab');
		_btns.each(function(_ind, _el) {
			this._box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			_el.onclick = function() {
        clearInterval(interval);
        if(!this._box.hasClass('show')){
          var current = ($('div.img div.show')?  $('div.img div.show') : $('div.img div:first'));
          var next = this._box;
          next.css({opacity: 0.0})
          .addClass('show')
          .show()
          .animate({opacity: 1.0}, 1000);

          //Hide the current image
          current.animate({opacity: 0.0}, 1000, function() {$(this).hide();})
          .removeClass('show');
        }
				return false;
			}
		});
	});
}
function submitHMCSearch() {
    var query = $('#hmcq').val();
    var site = 'ewp-all';
    var searchUrl = 'http://www.pennstatehershey.org/web/guest/home/search/hmcsearch/' + site + '/' + escape(query);
    window.location.href = searchUrl;
}
function initSearchBox(selector){
  var $input = $(selector);
  var label_text = 'Search';
  $input.val(label_text);
  $input.focus(function() {
    if (this.value == label_text) {
      $(this).removeClass('placeholder').val('');
    };
  }).blur(function() {
    if (this.value == '') {
      $(this).addClass('placeholder').val(label_text);
    };
  }); 
}
