//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('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]').addClass('newwindow').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  $('a.newwindow').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
}
//*****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('');
      }
    });
  });
}
//*****IE rounded corners from http://dillerdesign.com/experiment/DD_roundies/**************************************************//
function rounded() {
  DD_roundies.addRule('');
}
	//**************************************************Toggle FAQ Question/Answer**********************************************************//
function toggleFaq() {
  if (!$('ul.toggle').length > 0) return false;
	$('ul.toggle h3').addClass('inactive');
	$('ul.toggle div.toggle').hide();
  $('ul.toggle h3').click(function () {
		$(this).toggleClass('active');
		$(this).toggleClass('inactive');
    $($(this).next('div.toggle')).slideToggle();
  });
}
//********************************directory tab switcher*********************************//
function tabSwitch() {
  if ($('.tabSwitcher').length > 0) {;
    $('div.tabWrap').each(function (i) {
      $(this).hide();
    });
    $('ul.tabs li a').each(function (i) {
      $(this).attr('id','callTab' + i);
    });
    $('ul.tabs + div.tabWrap').show();  
  	$('ul.tabs li a').click(function(){
      $(this).parent().parent().children('li').each(function (i) {
        $(this).removeClass('selected');
      });
      $(this).parent().parent().parent().children('div').each(function (i) {
        $(this).hide();
      });
      $(this).parent().addClass('selected');
      var position = $(this).attr('id').substr(7,2) - 0;
  	  $('div.tabWrap:eq('+position+')').show();
  	  return false;
  	});
  }
}
//*****Alternate table row class**************************************************//
function alternateTableRow() {
  $('table').each(function(i) {
    $(this).find('tr:odd').addClass('odd');
    $(this).find('tr:even').addClass('even');
  });
}
//*****Home Slideshow controls*********************************************//
function homeSlideshow(){
	var active = $('.active').index();
	var length = ($('.slide').length - 1);

	if(active < length){
		$('.slide:eq(' + (active) + ') a img').fadeOut(3000);
		$('.slide:eq(' + (active) + ')').removeClass('active').fadeOut(3000);
		$('.slide:eq(' + (active + 1) + ') a img').fadeIn(3000);
		$('.slide:eq(' + (active + 1) + ')').fadeIn(3000).addClass('active');
	}
	else{
		$('.slide:eq(' + (active) + ') a img').fadeOut(3000);
		$('.slide:eq(' + (active) + ')').removeClass('active').fadeOut(3000);
		$('.slide:eq(0) a img').fadeIn(3000);
		$('.slide:eq(0)').fadeIn(3000).addClass('active');
	}

	setTimeout("homeSlideshow()", 6000);
}
//****************************************************Modal Window Controls and animations*************************************//
function showModal(){
	$('#play-video').click(function(){
		$('#modal-wrap').animate({
				opacity: 'show'
			}, 400, function() {
				$('#modal-wrap .head').delay(400).animate({
					opacity: "show"
				}, 400, function() {
						$('#modal-wrap .modalContent').animate({
							height: "toggle"
						}, 300, 'swing');
					}
				);
			}
		);
		
		return false;
	});
	if($('#SLUHthis_is_sluh').length){
		flowplayer("video-flowplayer", {src: "http://builds.flowplayer.netdna-cdn.com/56587/28757/flowplayer.commercial-3.2.5-4.swf", wmode: 'opaque'}, {
			// player configuration goes here
			clip: {
				url: 'http://www.sluh.org/flash/admissions/2010-This-Is-SLUH.mp4',
		    onStart: function(clip) {pageTracker._trackEvent("This is SLUH", "Start", clip.url);},// track start event for this clip
        onPlay: function(clip) {pageTracker._trackEvent("This is SLUH", "Play", clip.url);},
        onPause: function(clip) {pageTracker._trackEvent("This is SLUH", "Pause", clip.url, parseInt(this.getTime()));},// track pause event for this clip. time (in seconds) is also tracked
        onResume: function(clip) {pageTracker._trackEvent("This is SLUH", "Resume", clip.url, parseInt(this.getTime()));},
        onSeek: function(clip) {pageTracker._trackEvent("This is SLUH", "Seek", clip.url, parseInt(this.getTime()));},
        onStop: function(clip) {pageTracker._trackEvent("This is SLUH", "Stop", clip.url, parseInt(this.getTime()));},// track stop event for this clip. time is also tracked
        onFinish: function(clip) {pageTracker._trackEvent("This is SLUH", "Finish", clip.url);},// track finish event for this clip
        onMute: function(clip) {pageTracker._trackEvent("This is SLUH", "Mute", clip.url, parseInt(this.getTime()));},
        onUnmute: function(clip) {pageTracker._trackEvent("This is SLUH", "Unmute", clip.url, parseInt(this.getTime()));},
        onFullscreen: function(clip) {pageTracker._trackEvent("This is SLUH", "Full Screen", clip.url, parseInt(this.getTime()));},
        onFullscreenexit: function(clip) {pageTracker._trackEvent("This is SLUH", "Full Screen Exit", clip.url, parseInt(this.getTime()));}
			},
			canvas: {
				backgroundGradient: 'none'
			}
		});
	}
	if($('#SLUHadmissions.admissions').length){
		flowplayer("video-flowplayer", {src: "http://builds.flowplayer.netdna-cdn.com/56587/28757/flowplayer.commercial-3.2.5-4.swf", wmode: 'opaque'}, {
			// player configuration goes here
			clip: {
				url: 'http://www.sluh.org/flash/admissions/SLUHAdmissions10.mp4',
		    onStart: function(clip) {pageTracker._trackEvent("Admissions", "Start", clip.url);},// track start event for this clip
        onPlay: function(clip) {pageTracker._trackEvent("Admissions", "Play", clip.url);},
        onPause: function(clip) {pageTracker._trackEvent("Admissions", "Pause", clip.url, parseInt(this.getTime()));},// track pause event for this clip. time (in seconds) is also tracked
        onResume: function(clip) {pageTracker._trackEvent("Admissions", "Resume", clip.url, parseInt(this.getTime()));},
        onSeek: function(clip) {pageTracker._trackEvent("Admissions", "Seek", clip.url, parseInt(this.getTime()));},
        onStop: function(clip) {pageTracker._trackEvent("Admissions", "Stop", clip.url, parseInt(this.getTime()));},// track stop event for this clip. time is also tracked
        onFinish: function(clip) {pageTracker._trackEvent("Admissions", "Finish", clip.url);},// track finish event for this clip
        onMute: function(clip) {pageTracker._trackEvent("Admissions", "Mute", clip.url, parseInt(this.getTime()));},
        onUnmute: function(clip) {pageTracker._trackEvent("Admissions", "Unmute", clip.url, parseInt(this.getTime()));},
        onFullscreen: function(clip) {pageTracker._trackEvent("Admissions", "Full Screen", clip.url, parseInt(this.getTime()));},
        onFullscreenexit: function(clip) {pageTracker._trackEvent("Admissions", "Full Screen Exit", clip.url, parseInt(this.getTime()));}
			},
			canvas: {
				backgroundGradient: 'none'
			}
		});
	}
}
function closeModal(){
	//Close Modal Button
	$('#modal-wrap #modal #close-modal').click(function(event){
		var target = jQuery(event.target);
		var x = $('#modal-wrap .modalContent').queue('fx');
		var y = $('#modal-wrap .head').queue('fx');
		var z = $('#modal-wrap').queue('fx');

		if(target.is('#close-modal') && !x.length && !y.length && !z.length) {
			//if($('html').hasClass('video')){
//				var video = document.getElementById('video');
//				video.pause();
//			}
			$('#modal-wrap .modalContent').animate({
					height: 'toggle'
				}, 300, 'swing', function() {
					$('#modal-wrap .head').animate({
						opacity: "hide"
					}, 400, function() {
							$('#modal-wrap').animate({
								opacity: "hide"
							}, 200);
						}
					);
				}
			);
		}
		
		return false;
	});
	//Clicking on other stuff to close the modal
	$('#modal-wrap').click(function(event){
		var target = jQuery(event.target);
		var x = $('#modal-wrap .modalContent').queue('fx');
		var y = $('#modal-wrap .head').queue('fx');
		var z = $('#modal-wrap').queue('fx');

		if(target.is('#modal-wrap') && !x.length && !y.length && !z.length) {
			//if($('html').hasClass('video')){
//				var video = document.getElementById('video');
//				video.pause();
//			}
			$('#modal-wrap .modalContent').animate({
					height: 'toggle'
				}, 300, 'swing', function() {
					$('#modal-wrap .head').animate({
						opacity: "hide"
					}, 400, function() {
							$('#modal-wrap').animate({
								opacity: "hide"
							}, 200);
						}
					);
				}
			);
			return false;
		}
	});
	//Escape key closes modal if pressed and modal is visible
	$(document).keydown(function(event) {
		if(event.keyCode == 27 && $('#modal-wrap:visible').length){
			var x = $('#modal-wrap .modalContent').queue('fx');
			var y = $('#modal-wrap .head').queue('fx');
			var z = $('#modal-wrap').queue('fx');
			
			if(!x.length && !y.length && !z.length) {
				//if($('html').hasClass('video')){
//					var video = document.getElementById('video');
//					video.pause();
//				}
				$('#modal-wrap .modalContent').animate({
						height: 'toggle'
					}, 300, 'swing', function() {
						$('#modal-wrap .head').animate({
							opacity: "hide"
						}, 400, function() {
								$('#modal-wrap').animate({
									opacity: "hide"
								}, 200);
							}
						);
					}
				);
			}
		}
	});
}
//*****Move the margin for two line did you know's to fit*************************************//
function twoLineDidYouKnow() {
  if ($('#didYouKnow .contentContainer').height() > 20) {
    $('#didYouKnow .contentContainer').css("margin-top", '34px');
  }
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  clearDefaultValue();
  //rounded();
  toggleFaq();
  tabSwitch();
  alternateTableRow();
	showModal();
	closeModal();
	setTimeout("homeSlideshow()", 6000);
	twoLineDidYouKnow();
});
