/**
 * Zentralbibliothek Javascript stuff.
 * @author NOSE
 * 
 * @version 1.0.0 initial version
 *
 */
var Zentralbibliothek = {

	/**
	 * Initialize.
	 */
	initialize : function(){
		window.DEBUG = false;
		jQuery.log("initialize");
		
		// global
		Zentralbibliothek.initBodyTags();
		Zentralbibliothek.initTables();
		Zentralbibliothek.initDropdown();
		Zentralbibliothek.initSearch();
		Zentralbibliothek.initLivesearch();
		Zentralbibliothek.initTabs();
		Zentralbibliothek.initToc();
		Zentralbibliothek.initScroller();
		Zentralbibliothek.initInlineGallery();
		Zentralbibliothek.initOverlay();
		Zentralbibliothek.validateForm();
		Zentralbibliothek.initExternalLinks();

		
		// home
		if (jQuery("#container.home").size() > 0) {
			Zentralbibliothek.initCatalogs();
		}
		
	},
	/**
	* Initializes the body tags.
	*/
	initBodyTags: function() {
		jQuery.log("initBodyTags");
		
		// browser flag
		var bc = "";
		if (jQuery.browser.msie) {
			bc = "msie";
		}
		else if (jQuery.browser.mozilla) {
			bc = "mozilla";	
		}
		else if (jQuery.browser.safari){
			bc = "safari";
		}
		jQuery("body").addClass(bc);
	},
	/**
	* Initializes the tables.
	*/
	initTables: function() {
		jQuery.log("initTables");
		
		// blank tr for ie
		jQuery("table").each(function(){
			// foot
			if (jQuery("tfoot", this).size() > 0) {
				jQuery("thead tr:last-child, tfoot tr:last-child",this).addClass("blank");	
			}
			else {
				jQuery("thead tr:last-child, tbody tr:last-child",this).addClass("blank");
			}
		
		});
	},
	/**
	* Initializes the dropdowns.
	*/
	initDropdown: function() {
		jQuery.log("initDropdown");
		jQuery(".dropdown").each(function(){
			jQuery(this).dropdown();
		});
	},
	/**
	* Initializes the search.
	*/
	initSearch: function() {
		jQuery.log("initSearch");
		jQuery("#searchTerm").fieldEnhancer("searchForm","#searchForm",{fieldValue:"Suche"});
		
		// search result page
		if (jQuery("#searchOptionsToggler").size() > 0) {
			jQuery("#searchOptionsToggler").toggler("#searchOptions",{msgCollapsed:"Erweiterte Suche",msgExpanded:"Einfache Suche"});
		}
	},
	/**
	* Initializes the livesearch.
	*/
	initLivesearch: function() {
		jQuery.log("initLivesearch");
		jQuery(".livesearch").each(function(){
			jQuery(this).liveSearch();
		});
	},
	/**
	* Initializes the tabs.
	*/
	initTabs: function() {
		jQuery.log("initTabs");
		if (jQuery.browser.msie) {
			// ie bugs: no fading (cleartype rendering error), adding an option -> magically prevents ie6 crash
			jQuery(".tabs > ul.selector").tabs({selected:0});
		}
		else {
			jQuery(".tabs > ul.selector").tabs({ fx: { opacity: 'toggle' } });
		}
	},
	/**
	* Initializes the scroller.
	*/
	initScroller: function() {
		jQuery.log("initScroller");
		jQuery(".scroller").each(function(){
			jQuery(this).scroller();
		});
	},
	/**
	* Initializes the table of contents.
	*/
	initToc: function() {
		jQuery.log("initToc");
		jQuery(".toc").each(function(){
			jQuery(this).toc();
		});
	},
	/**
	* Validates a form.
	*/
	validateForm: function() {
		jQuery.log("validateForm");
		var vforms = jQuery(".validate");
		if (vforms.size() > 0 ) {
			// rules
			var rules = new ValidatorRuleCollection();
			
			rules.add('validator-required', 'Wert erforderlich.', function(fieldValue, fieldObj) {
				if (fieldValue == "")	return false;
				return true;
			});
			
			rules.add('validator-email', 'Ung&uuml;ltige Email Adresse.', function(fieldValue, fieldObj) {
				if (fieldValue == "")	return true;
				return /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(fieldValue);
			});
			// validator
			jQuery(vforms).validationAideEnable(rules, {showSummary:true,summaryMessage:"Bitte erg&auml;nzen sie folgende Angaben:"});	
		}
	},
	/**
	* Initializes the overlay.
	*/
	initOverlay: function() {
		jQuery.log("initOverlay");
		var tbWidth = 630;
		var tb_pathToImage = "images/loading.gif";
		
		// prepare
		jQuery(".overlay").each(function(el) {
			var h = jQuery(this).attr("href");
			
			// inline
			if (jQuery(this).hasClass("inline")) {
				var hn =  "#TB_inline?width="+tbWidth+"&inlineId="+h.replace(/#/, "");
				jQuery(this).attr("href",hn);
			}
			// iframe: ?keepThis=true&TB_iframe=true&height=300&width=630
			else {
				var pf = "?";
				if (h.indexOf("height") > 0) {
					pf = "&";
				}
				var hn =  h + pf + "keepThis=true&TB_iframe=true&width="+tbWidth;
				jQuery(this).attr("href",hn);
			}
		});
		
		// thickbox
		tb_init('a.overlay'); // pass where to apply thickbox
		imgLoader = new Image(); // preload image
		imgLoader.src = tb_pathToImage;
	},
	/**
	* Initializes the inline gallery.
	*/
	initInlineGallery: function() {
		jQuery.log("initInlineGallery");
		jQuery(".gallery").each(function(el) {
			jQuery(this).inlineGallery();
		});
	},
	/**
	* Initializes the external links.
	*/
	initExternalLinks: function() {
		jQuery.log("initExternalLinks");
		jQuery(".external").each(function(el) {
			var t = jQuery(this).attr("title");
			jQuery(this).attr("title","Externer Link "+ t);
			jQuery(this).attr("target","_blank");
		});
	},
	/**
	* Initializes the catalog login and search.
	*/
	initCatalogs: function() {
		jQuery.log("initCatalogs");
		// login 
		var actionLoginNebis = "http://opac.nebis.ch:80/F/";
		var actionLoginIds = "https://biblio.unizh.ch:443/F/";
		var formLogin = jQuery("#catalogLogin");
		jQuery(formLogin).attr("target","_blank");
		jQuery("#catalogLoginNebis",formLogin).attr("checked","checked");
		jQuery("#catalogLoginNebis",formLogin).bind("click",function(){
			 jQuery(formLogin).attr("action",actionLoginNebis);													 
		});
		jQuery("#catalogLoginIds",formLogin).bind("click",function(){
			 jQuery(formLogin).attr("action",actionLoginIds);													 
		});
		
		// search
		var actionSearchNebis = "http://opac.nebis.ch/F/";
		var actionSearchIds = "https://biblio.unizh.ch/F/";
		var actionSearchNebisIds = "http://ml.metabib.ch/V/";
		var formSearch = jQuery("#catalogSearch");
		jQuery(formSearch).attr("target","_blank");
		var catalogSearchFunc = jQuery("#catalogSearchFunc",formSearch);
		var catalogSearchSubmit = jQuery("#catalogSearchTerm",formSearch);
		jQuery("#catalogSearchNebis",formSearch).attr("checked","checked");
		jQuery("#catalogSearchNebis",formSearch).bind("click",function(){
			 jQuery(formSearch).attr("action",actionSearchNebis);
			 jQuery(catalogSearchFunc).attr("value","find-b");
			 jQuery(catalogSearchSubmit).attr("name","request");
		});
		jQuery("#catalogSearchIds",formSearch).bind("click",function(){
			 jQuery(formSearch).attr("action",actionSearchIds);	
			 jQuery(catalogSearchFunc).attr("value","find-b");
			 jQuery(catalogSearchSubmit).attr("name","request");
		});
		jQuery("#catalogSearchNebisIds",formSearch).bind("click",function(){
			 jQuery(formSearch).attr("action",actionSearchNebisIds);
			 jQuery(catalogSearchFunc).attr("value","meta-1-check");
			 jQuery(catalogSearchSubmit).attr("name","find_request_2");
		});
		
		// preselect
		jQuery("#catalogSearchNebis",formLogin).attr("checked","checked");
		jQuery(catalogSearchFunc).attr("value","find-b");
		jQuery(catalogSearchSubmit).attr("name","request");
		
	}
	
}
jQuery(document).ready(function(){
	Zentralbibliothek.initialize();
});
