/**
* JFlume JavaScript stuff.
* @author CNPP
*/
var JFlume = {
	
	/**
	* Initialize.
	*/
	initOnReady : function() {	
		
		// context
		var ctxPage = jQuery("#page");
		var ctxHeader = jQuery("#header");
		var ctxSidebar = jQuery("#sidebar");
		var ctxContent = jQuery("#content");
		
		// init
		JFlume.initNavigation(ctxHeader);	
		JFlume.initImages(ctxContent);
		JFlume.initTables(ctxContent);
		JFlume.initHoverable(ctxPage);
		JFlume.initLinks(ctxPage);

	},
	initOnLoad : function() {	
		
		// content
		var ctxContent = jQuery("#content");
		var ctxSidebar = jQuery("#sidebar");
		
		// init
		JFlume.initItems(ctxContent);
		JFlume.initNavigator(ctxSidebar);
	},
	

	
	/**
	* Initializes the navigation.
	*/
	initNavigation: function(ctx) {	
		
		// reference
		var elsNav = jQuery("#navigation ul li a",ctx);
		var elSection = jQuery("#navigation_section",ctx);
		var elSectionTitle = jQuery("#navigation_section_title",ctx);
		
		// vars
		var currentSection = jQuery(elSectionTitle).html();
		var classTemporary = "temporary";
		var timerReset = null;
		var reset = false;
		
		// hover intent
		var config = {    
			 sensitivity: 100,
		     over: updateSection, // function = onMouseOver callback (REQUIRED)     
		     out: clearSection // function = onMouseOut callback (REQUIRED)    
		};
		jQuery(elsNav).hoverIntent(config);
		
		// fluff titles
		jQuery(elsNav).attr("title",null);
		
		
		/**
		* Shows/hides the menu.
		*/
		function updateSection() {
			
			// update section
			if (! jQuery(this).parent().hasClass("current")) {
				
				// disable reset
				reset = false;
				clearTimeout(timerReset);
				resetTimer = null;
				
				// title
				var stitle = jQuery(this).html();
				jQuery(elSectionTitle).addClass(classTemporary);
				jQuery(elSectionTitle).html(stitle);
				
				// animate
				var swidth = jQuery(elSectionTitle).width();
				jQuery(elSectionTitle).css("left",-swidth+"px");
				jQuery(elSectionTitle).animate({
				    left: 0
				  	}, 
					120);
				
			}
			
		}
		function clearSection() {
			// reset text
			jQuery(elSectionTitle).html("");
			jQuery(elSectionTitle).removeClass(classTemporary);
			jQuery(elSectionTitle).css("left",0);
			timerReset = window.setTimeout(resetSection,900);
			reset = true;
		}
		function resetSection() {
			if (reset) {
				jQuery(elSectionTitle).css("left",0);
				jQuery(elSectionTitle).html(currentSection);
			}
		}
		
	},
	
	/**
	* Initializes the navigator.
	*/
	initNavigator: function(ctx) {
		
		// navigator
		jQuery('#navigator').each(function(i,el){
			
			// scroll panel
			jQuery(el).css({"overflow-y":"auto"});
			jQuery(el).jScrollPane({"showArrows":true});
		});
		
	},
	
	
	/**
	* Initializes the items.
	*/
	initItems: function(ctx) {
		

		// items
		jQuery(".items",ctx).each(function(i,items){
			
			// masonry
			jQuery(items).masonry({
			  columnWidth: 220, 
			  itemSelector: '.item' 
			});
			
		});
		
	},
	
	
	/**
	* Initialize the hoverable.
	*/
	initHoverable: function(ctx) {
			
		// elements
		jQuery(".hoverable, #navigator li, #branding, .images ul li",ctx).each(function(i,els){
			
			// check
			if (jQuery("a",els).length <= 0) {
				jQuery(els).addClass("nolink");
				return;
			}
		
			// events
			jQuery(els).bind("mouseenter",function(){
				jQuery("img",this).animate({opacity: 0.85}, 180);
				jQuery(this).addClass("hover");
			});
			jQuery(els).bind("mouseleave",function(){
				jQuery("img",this).animate({opacity: 1.0}, 300);
				jQuery(this).removeClass("hover");
			});
			jQuery(els).bind("click",function(){
			
				// modal
				if (jQuery(this).hasClass("modal")) {
					return true;
				}		
				// follow link
				var h = jQuery("a",this).attr("href");
				if (h == null) {
					h = jQuery(this).attr("href");
				};
				if (h != null) {
					window.location.href = h;
					return true;
				};
			});
		});
		
	},
	
	/**
	* Initializes the images.
	*/
	initImages: function(ctx) {	
		
		// gallery
		jQuery("#gallery",ctx).each(function(i,el){
			jQuery(el).gallery();
		});
		
		// modal
		jQuery(".images",ctx).each(function(i,el){
			jQuery("ul li a",el).nyroModal({
				bgColor:'#F5F5F5',
				minHeight:'100',
				hideContent:function hideModal(elts, settings, callback) {
				  elts.wrapper.hide().animate({opacity: 0}, {complete: callback, duration: 80}); 
				},
				showBackground:function showBackground(elts, settings, callback) {
					elts.bg.css({opacity:0}).fadeTo(300, 0.75, callback);
				}
			});
		});
	},
	
	
	/**
	* Initializes the tables.
	*/
	initTables: function(ctx) {	
		
		// page
		if (jQuery("#page").hasClass("template_page")) {
			
			// fluff tables
			jQuery("table",ctx).css({"width":"auto","height":"auto"})
		}
	},
	

	/**
	* Initialize the links.
	*/
	initLinks: function(ctx) {	
		
		// external
		jQuery(".link_external",ctx).each(function(i,el){
			// attrs
			var lhref = jQuery(el).attr("href");
			jQuery(el).attr("title","Externer Link " + lhref);
			jQuery(el).attr("target","_new");
		});
		
		// print
		jQuery(".link_print",ctx).bind("click",function(){
			window.print();
		});
	}
}
jQuery(document).ready(function(){
	JFlume.initOnReady();
});
jQuery(window).bind("load",function(){
	JFlume.initOnLoad();
});
