var SS = {
	
	init : function() {
		SS.tabs();
		SS.clearFields();
		SS.dropdown();
		SS.formatTable();
		SS.fancyBox();
		SS.slidesjs();
		SS.showHideInfo();
		
		if ( $('#nav-side #nav').children().text() === '' ) {
			$('#nav-side').css({
				background : 'none',
				paddingBottom : '50px'
			});
		}
	},
	
	/* jQuery Tabs for areas that are not defined by the system.	
		You'll notice there is an options configuration object that is used to provide all the defualt 
		options for the tabs such as html nodes to target as well as speed and classname. You can call the tabs
		function from any page
	*/
	tabs : function(options) {
		SS.options = {
			$nav : $('#tabbedNav li a'),
			$content : $('#tabbedContent > div'),
			className : 'active',
			speed : 'fast'
		};

		$.extend({}, options, SS.options);

		SS.options.$nav.live('click', function(e) {
			SS.options.$content.hide().filter(this.hash).fadeIn(SS.options.speed);
			SS.options.$nav.parent().removeClass(SS.options.className);
			$(this).parent().addClass(SS.options.className);
			e.preventDefault();
		}).filter(':eq(0)').click();
	},
	
	// Add/Remove Formfield Hints When User Clicks in TextField
	clearFields : function() {
		// Clear/Show Form Hints
		$('input').not('input:button').not('input:submit').each(function() {
		  $(this)
		    .data('default', $(this).val())
		    .focus(function() {
		      if($(this).val() === $(this).data('default') || '') {
		        $(this).val('');
		      }
		    })
		    .blur(function() {
		      var default_val = $(this).data('default');
		      if($(this).val() === '') {
		        $(this).val($(this).data('default'));
		      }
		   });
		});
	}, 
	
	// FANCYBOX PLUGIN
	fancyBox : function() {
		if ( $(".fancyPop").length > 0 ) {
			$.getScript('/ui/js/jquery.fancybox-1.3.4.pack.js', function() {
				$(".fancyPop").fancybox({
					'titlePosition'	: 'inside',
					'transitionIn'	: 'none',
					'transitionOut'	: 'none'
				});
			});
		}
	},
	
	// SlidesJS.com jQuery Slider Plugin
	slidesjs : function() {
		if ( $('#slides').length > 0 ) {
			$.getScript('/ui/js/lib/slides.min.jquery.js', function() {
				$('#slides').slides({
					play : 5000,
					effect : 'fade',
					autoHeight : 'true',
					fadeSpeed : 100
				});
			});
		}
	},
	
	// Add odd and even styling to tables with a class of formatTable
	formatTable : function() {
		// Add odd rows to tables with class of .formattable
		if ($('.formattable')) {
			$('.formattable tr:odd').addClass('odd');
			$('.formattable tr:first').addClass('trheader');
/*			$('.formattable tr').hover(function() {
				$(this).addClass('highlightCell');
			}, function () {
				$(this).removeClass('highlightCell');
			}); */
		}
	},
	
	// Dropdown Menu 
	dropdown : function() {
		
		function showMenu() {
			$(this).find('ul').stop(true, true).slideDown('fast');
		}

		function hideMenu() {
			$(this).find('ul').stop(true, true).fadeOut('fast');
		}
		
		var config = {
			over : showMenu,
			out : hideMenu,
			timeout : 'fast',
			interval : 100
		};
		
		$('#nav-main ul#nav li ul').css({ display : 'none', left : 'auto' });
		$('#nav-main ul#nav li').hoverIntent(config);
	},
	
//modified on/about oct. 1, 2011 -dcc
	showHideInfo : function() {
		if ( $('.showHideInfo').length < 0 ) return false;
		var $showHideInfo = $('.showHideInfo > div');
		$showHideInfo.hide();
		$('.showHideInfo h3').wrap("<a href=\"#\"></a>");
		$('.showHideInfo h3').parent("a").click(function(e) {
			$(this).children("h3").toggleClass("showHideInfoOpen");
			var currentDiv = $(this).parent("div").children("div");
			//when shift+click, toggle div without collapsing other open divs
			if (e.shiftKey == false) {
				$showHideInfo.not(currentDiv).slideUp('fast');
				$showHideInfo.not(currentDiv).parent("div").find("h3").removeClass("showHideInfoOpen");
			}
			//when ctrl+click, expand all divs
			if (e.ctrlKey == true) {
				$showHideInfo.not(currentDiv).slideDown('fast');
				$showHideInfo.not(currentDiv).parent("div").find("h3").addClass("showHideInfoOpen");
			}
			currentDiv.slideToggle('fast');
			e.preventDefault();
		});
	}
	
};


var DC = {
	init : function() {
		DC.pdficons();
		DC.contentpop();
		DC.captionwidths();
		DC.myumucclick();
		DC.simpleformrequiredfields();
		DC.youtubepop();
		DC.formattable();
	},
	
	//add pdf icons after all the pdfs
/*	pdficons : function() {
		$('a[href$=".pdf"]').after(' <img src="/ui/images/icon_pdf_small.jpg" title="This is a PDF document" alt="This is a PDF document" />');
	}, */
	
	//add pdf icons after all the pdfs except empty a tags
	pdficons : function() {
		$('a[href$=".pdf"], a[href$=".PDF"]').each(function() {
			if ($(this).html() == "") {
				if (document.location.href.indexOf("csauthor.umuc.edu") > 0) {
					$(this).after(' <img src="/ui/images/icon_pdf_small_grey.jpg" title="This is a PDF document (empty A tag)" alt="This is a PDF document" />');
				}
			}
			else {
				$(this).after(' <img src="/ui/images/icon_pdf_small.jpg" title="This is a PDF document" alt="This is a PDF document" />');
				$("#content-area").width($("#content-area").width());
			}
		});
	},
	
	//like fancybox but this loads the content of a page
	contentpop : function() {
		if ( $(".contentpop").length > 0 ) { //this ensures it only loads the fancybox script if there's a use for it on the page
			$.getScript('/ui/js/jquery.fancybox-1.3.4.pack.js', function() {
				$(".contentpop").fancybox({
					'width' : '75%',
					'height' : '90%',
					'autoScale' : true,
					'transitionIn' : 'fade',
					'transitionOut' : 'fade',
					'type' : 'iframe',
					'centerOnScroll' : true, //for scrolling content, this makes sense in case people scroll the page behind the lightbox
					'titleShow' : false //not showing the title because it just doesn't work well with this setup
				});
			});
		}
	},
	
	//certain pages use these classes (like /visitors/events/art/exhibits/maril.cfm) to caption photos. this fixes the size of the div to match the size of the image.
	captionwidths : function() {
		$('.floatCaptionRight, .floatCaptionLeft').each(function() {
			$(this).width($(this).children("img").width());
		});
	},
	
	//just resizes this a bit, otherwise stretches to 100%, which is fine i guess. but this tightens things up
	myumucclick : function() {
		$('#myumucclick').each(function() {
			totalwidth = 0
			$(this).children('li').each(function() {
				totalwidth += ($(this).width() + 8+15+2);
			});
			$(this).width(totalwidth);
		});
	},
	
	simpleformrequiredfields : function() {
/*		$('.CS_Form_Required_Baseline').each(function() {
			$(this).next('td').children('input:last').after(' <img src="/ui/images/icon_required.gif" alt="Required Field" title="Required Field" />');
		});
		$('.CS_Form_Required').each(function() {
			$(this).next('td').children('table').children('tbody').children('tr:last').find('table').children('tbody').children('tr:last').children('td:last').after(' <img src="/ui/images/icon_required.gif" alt="Required Field" title="Required Field" />');
		}); */
		$('.CS_Form_Form').prepend("<p><img src=\"/form_generator/img/bullet_star.png\" /> indicates a required field.</p>");
		$('.CS_Form_Required, .CS_Form_Required_Baseline').each(function() {
			$(this).attr("title", "Required");
		});
	},
	
	youtubepop : function() {
		if ( $(".youtube").length > 0 ) { //this ensures it only loads the fancybox script if there's a use for it on the page
			$.getScript('/ui/js/jquery.fancybox-1.3.4.pack.js', function() {
				$(".youtube").click(function(event) {
					event.preventDefault();
					vidid = $(this).attr("class").split(" ")[1];
					$.fancybox({
						'padding'		: 0,
						'content'		: "<div id=\""+vidid+"\"><iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/"+vidid+"\" frameborder=\"0\" allowfullscreen></iframe></div>",
						'transitionIn'	: 'fade',
						'transitionOut'	: 'fade'
					});
				});
			});
		}
	},
	
	formattable : function() {
		// Add odd rows to tables with class of .formatted-table AND .alternating
		$('.formatted-table.alternating tr:even').addClass('even');
	}
}

jQuery(function($) {
	SS.init();
	DC.init();
});
