
//TODO: Wrap tUI in jQuery closure and use noConflict to
//      allow for other javascript libraries to work together.

var tUI = {
		
				init: function() {

					//Indentify browser type
					tUI.identifyBrowser();
					
					//Init Login form(s)
					tUI.forms.fixLogin();
					
					
					//Init Quotes
					var quotesExists = parseInt($(".quotes_data").size());
					if(quotesExists)
					{
						if ($.browser.msie && $.browser.version == 6)
						{
							var waitTime = 90 * 1000; // 1.5 min
							window.setTimeout(tUI.quotes, waitTime);
						}
						else
						{
							tUI.quotes();
						}					
					}
				
					//Create Gallery
					var galleryExists = parseInt($(".gallery_container").size()); 
					if (galleryExists)
					{
						if ($.browser.msie && $.browser.version == 6)
						{
							var waitTime = 60 * 1000; // 1 min
							window.setTimeout(tUI.gallery.init, waitTime);
						}
						else
						{
							tUI.gallery.init();
						}
					}						

				}, //tUI.init

				identifyBrowser: function() {
					var userAgent = navigator.userAgent.toLowerCase();

					if ($._browser === undefined)
					{
						// Figure out what browser is being used
						//Depeciated as of JQuery 1.3 but kept alive by me. Very useful to me.
						//Changed to $._browser instead of $.browser to avoid any conflicts
						$._browser = {
							version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
							safari: /webkit/.test( userAgent ),
							opera: /opera/.test( userAgent ),
							msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
							mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
						};
					}

					//add browser class to document - helps with CSS browser targeting
					var msie = ($._browser.msie) ? "ie" : false;
					var mozilla = ($._browser.mozilla) ? "mozilla": false;
					var safari = ($._browser.safari) ? "safari": false;
					var browser = (msie || mozilla || safari);
					var browserVer = browser + parseInt($._browser.version);
					var platform = null;
						
					$("body")
						.addClass(browser)
						.addClass(browserVer);
					
					if ( navigator.platform.substr(0, 3).toLowerCase() !== "win" ) {
						platform = 'macOS';
						$('body').addClass('macOS');
					}

					//add the version for these very specific cases
					var specific_class = (platform == null) ? "": platform + '_'; 
					specific_class += (browser + '_v' + $._browser.version.replace(/\./g, "_"));
					$('body').addClass(specific_class);

				}, //identifyBrowser
				
				forms: {
					fixLogin: function() {
						var loginFormSelector = ".login form input:text, .login form input:password";
						var $loginFormElements = $(loginFormSelector);
						$loginFormElements
							.bind("keydown",
								function(e)
								{					
									var ENTER = 13;
									switch(e.keyCode)
									{
										case ENTER:
											e.preventDefault();
											$(this)
												.parents("form")
												.eq(0)
												.submit();
											break;
									}					
								}
							); // end bind						
					} //tUI.forms.fixLogin
					
				}, //tUI.forms


				jqPlugins: {

					//Array of loaded plugins
					loaded: [],


					load: function(pluginName, srcURL, callback) {

						$.getScript(srcURL, callback);

						//Store the name of the loaded plugin
						tUI.jqPlugins.loaded.push(pluginName);
					},

					isLoaded: function(pluginName) {
						var status = false;
						var loadedPlugins = tUI.jqPlugins.loaded;

						for(plugin in loadedPlugins)
						{
							if (plugin === pluginName)
							{
								status = true;
								break;
							}
						}
						return status;
					}

				}, //tUI.jqPlugins

				gallery: {
					onMouseOutOpacity: 0.67,

					init: function() {
					
						//Styles set in javascript so that if javascript isn't enabled then the
						//browser won't display controls that need javascript
						var showOnlyJSControls = $("<style type='text/css'>div.gallery_content{display:block;} div.noscript{display: none;}</style>");
						if (!($.browser.msie && $.browser.version == 6))
						{
							$("head").append(showOnlyJSControls);
						}


						//Tab functionality
						var $tabs = $(".gallery_tabs li");						
						var $gallery = $(".gallery");						

						$tabs.each(
							function(index) 
							{
								//For each tab assign onClick functionality
								$(this)
									.click(
										function(e)
										{
											
											//preventDefault behavior
											e.preventDefault();

											//remove the highlight from the currently selected tab
											$tabs
												.filter(".selected")
												.removeClass("selected");
											
											//mark the tab clicked as selected
											$(this).addClass("selected");

											//Hide the last gallery, now show the selected gallery
											//var index is used in a closure and thus the value is perserved
											$gallery
												.filter(".selected")
												.removeClass("selected")
												.end()
												.eq(index)
												.addClass("selected");
												
											//For bookmarking purposes click the thumbnail with class selected
											$gallery
												.filter(".selected")
												.find(".thumbs li")
												.filter(".selected")
												.find("a")
												.click();
										}
									); //end click								
							}
						); //end $tabs.each

						//Set initial style for thumbnails
						tUI.gallery.styleImgThumbs();

						//Show the selected default gallery						
						$tabs
							.filter(".selected")							
							.click();            
					}, //tUI.init

					styleImgThumbs: function() {
						$('ul.thumbs li').css('opacity', tUI.gallery.onMouseOutOpacity)
							.hover(
								function () {
									$(this).not('.selected').fadeTo('fast', 1.0);
								}, 
								function () {
									$(this).not('.selected').fadeTo('fast', tUI.gallery.onMouseOutOpacity);
								}
							);
					},
					
					loadSpecificImage: function(tabIndex, imageIndex) {
						
						//Get selected the desired tab
						
						//var tabIndex = parseInt(tUI.misc.queryParams.get("t"));
						//var imageIndex = parseInt(tUI.misc.queryParams.get('i'));
						tabIndex = parseInt(tabIndex);
						imageIndex = parseInt(imageIndex);
						
						var $tabs = $(".gallery_container .gallery_tabs li");
						var $thumbnail = $(".gallery_container .gallery").filter(".selected").find(".thumbs li");											
						
						if (!isNaN(tabIndex) && !isNaN(imageIndex))
						{
							$tabs.eq(tabIndex).click();
							$thumbnail.eq(imageIndex).find("a").click();							
						}
					},
					
					bookmarkURL: function() {
						var $galleryContainer = $(".gallery_container");
						var tabPrefix = 't';
						var imagePrefix = 'i';
											
						//Get the active tab (visible gallery)
						var selectedTab = $galleryContainer.find(".gallery_tabs li.selected")
						 var tabIndex = $galleryContainer.find(".gallery_tabs li").index(selectedTab);
									
						//Get the active image
						 var selectedImage =  $galleryContainer.find(".gallery").filter(".selected").find(".thumbs li.selected");
						 var imageIndex = $galleryContainer.find(".gallery").filter(".selected").find(".thumbs li").index(selectedImage);
									 
						 //Set the location address for Facebook
						 location.hash = tabPrefix + tabIndex + imagePrefix + imageIndex;
					},
					
					shareOnFacebook: function(func) {
						
						//Bookmark the image
						tUI.gallery.bookmarkURL();
									 
						 //call the Facebook function
						 if (typeof(func) == "function")
						 {
						 	func();
						 }
						 
						 return false;
							
					} // gallery.shareOnFacebook

				}, //tUI.gallery

				quotes: function() {
					var $currQuote = null;
					var $quotes = $(".block_press_clipping .quotes_data li");
					var quote = null;
					var source = null;
					var q = '&quot;';
					var dash = "&ndash;";
					var duration = 10 * 1000; // # of seconds x 1 millisecond 

					//Load quote
					$currQuote = $quotes.filter(".selected").next();

					//Choose the next quote or select the first one
					$currQuote = ($currQuote.size()) ? $currQuote: $quotes.filter(":first");

					quote = $currQuote.find(".quote").html();
					source =  $currQuote.find(".source").html();

					$(".block_press_clipping .quotes p")
						.fadeOut("normal", 
							function()
							{
								$(".block_press_clipping .quotes .quote").html(q+quote+q);
								$(".block_press_clipping .quotes .quote_source").html(dash+source);
							}
						)
						.fadeIn("normal");
						
						//Rotate quotes
						window.setTimeout(tUI.quotes, duration);

					//Mark the current quote as selected
					$quotes
						.filter(".selected")
						.removeClass("selected");

					$currQuote.addClass("selected");
									
				} //quotes				
				
}; //tUI


$(document).ready(
	function()
	{						
		tUI.init();
	}
);
