/**
* LGB JavaScript stuff.
* @author CNPP
*/
var LGB = {
	/**
	* Initialize.
	*/
	initReady: function() {
		// all
		LGB.initExternalLinks();
		LGB.initBodyTag();
		LGB.initSlider();
		LGB.initFolder();
		LGB.initItems();
		LGB.initSearch();
		LGB.validateForm();
		
		// template page
		if (jQuery("#container.page").size() > 0) {
			LGB.initTabs();
			LGB.initBlowup();
		}
		
		// template single
		if (jQuery("#container.single").size() > 0) {
			LGB.initIngredients();
			LGB.initBlowup();
		}
		
		// template faq
		/*
		if (jQuery("#container.page-item-36").size() > 0) {
			LGB.initToc();
			LGB.initScroller();
		}
		*/	

	},
	initLoad: function() {
		LGB.adjustItems();
	},
	
	/**
	* Initializes the external links.
	*/
	initExternalLinks: function() {
		var internal = "http://www.la-grande-bouffe.ch";
		jQuery("#content a, #marginal a").each(function(){
			var h = jQuery(this).attr("href");
			var hs = h.substring(0,1);
			if (h.indexOf(internal) < 0 && hs != "/" && hs != "#") {
				jQuery(this).addClass("external");
				jQuery(this).attr("target","_blank");
				try {
					var t = jQuery(this).attr("title");
					jQuery(this).attr("title","Externer Link: " + t);
				}
				catch (ex) {
				}
			}
		});
	},
	/**
	* Initializes the body tags.
	*/
	initBodyTag: function() {
		jQuery().bodyTag();
	},
	/**
	* Initializes the Visual.
	*/
	initVisual: function() {		
		// visual slider
		jQuery("#visualslider").visualSlider();
	},
	
	/**
	* Initializes the blowup.
	*/
	initBlowup: function() {
		jQuery(".blowup").blowup();
	},
	/**
	* Initializes the slider.
	*/
	initSlider: function() {
		jQuery("#slider").slider();
	},
	/**
	* Initializes the folder.
	*/
	initFolder: function() {
		jQuery("#marginal").folder();
	},
	/**
	* Initializes the toc.
	*/
	initToc: function() {
		jQuery(".toc").each(function(){
			jQuery(this).toc();
		});

	},
	/**
	* Initializes the toc.
	*/
	initScroller: function() {
		jQuery(".scroller").each(function(){
			jQuery(this).scroller();
		});
	},
	/**
	* Initializes the tabs.
	*/
	initTabs: function() {
		jQuery("#tabs").tabs({cookie:{expires:30},fx:{opacity:'toggle', duration:400}});	
	},
	/**
	* Initializes the ingredients.
	*/
	initIngredients: function() {
		// table
		jQuery("#ingredients table, #ingredients table td, #ingredients table th").width("").height("");	
	},
	/**
	* Initializes the search.
	*/
	initSearch: function() {
		// event
		jQuery("#searchtoggler").bind("click",toggleSearch);
		
		// init
		jQuery("#search").hide();
		if (jQuery("#container.search").size() > 0) {
			jQuery("#search").show();
			jQuery("#searchtoggler").addClass("expanded");
			jQuery("#searchtoggler").removeClass("collapsed");
		}
		
		/*
		* Toggles the search.
		*/
		function toggleSearch() {			
			if (jQuery(this).hasClass("expanded")) {
				jQuery("#search").slideUp(60);
				jQuery(this).addClass("collapsed");
				jQuery(this).removeClass("expanded");			
			}
			else {
				jQuery("#search").slideDown(180,function(){
					jQuery("#search #s").focus();
				});
				jQuery(this).removeClass("collapsed");
				jQuery(this).addClass("expanded");			
			}
			return false;			
		}	
	},
	/**
	* Validates a form.
	*/
	validateForm: function() {
		var vforms = jQuery(".validate");
		if (vforms.size() > 0 ) {
			// rules
			var rules = new ValidatorRuleCollection();
			
			rules.add('validator-required', 'Eingabe 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;nze folgende Angaben:"});	
		}
	},

	/**
	* Initializes the items.
	*/
	initItems: function() {
		
		// events
		jQuery(".items li").bind("click",function(){
			window.location.href = jQuery("a",this).attr("href");
		});
		jQuery(".items li").bind("mouseenter",function(){
			jQuery(this).addClass("hover");
		});
		jQuery(".items li").bind("mouseleave",function(){
			jQuery(this).removeClass("hover");
		});
	},
	adjustItems: function() {
		jQuery("#content .items li .info").each(function(){
				try {
					var elTitle = jQuery("h3",this);
					var h = jQuery(this).height() - elTitle.height();
					jQuery("p",this).css({"overflow":"hidden","height":h});	
				} 
				catch (e) {
					// ignore	
				}
		});	
		jQuery("#marginal .items li").each(function(){
				try {
					var elTitle = jQuery("h4",this);
					var h = jQuery(this).height() - elTitle.height();
					jQuery("p",this).css({"overflow":"hidden","height":h});	
				} 
				catch (e) {
					// ignore	
				}
		});	
	}
}
jQuery(document).ready(function(){
	LGB.initReady();
});
jQuery(window).bind("load",function(){
	LGB.initLoad();
});


