/**
 * JS for ZŠ Bitovská theme
 */

jQuery('html').addClass('js');

jQuery(document).ready(function(){

	// add/hide search value (and add title)
	var searchLabel = jQuery('label[for="search"]');
	var searchInput = jQuery('#search');
	var labelText = searchLabel.text();
	searchLabel.remove();
	if (searchInput.val() == '') {
		searchInput.val(labelText);
	}
	searchInput.focus(function(){
		if (jQuery(this).val() == labelText) jQuery(this).val('').attr('title', labelText);
	}).blur(function(){
		if (jQuery(this).val() == '') jQuery(this).val(labelText).removeAttr('title'); 
	});
	
	
	// hover menu
	var hoverIntentConfig = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: slideDown, // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: slideUp // function = onMouseOut callback (REQUIRED)    
	};
	
	
	jQuery('#nav > li').not(".current_page_item, .current_page_parent, .current_page_ancestor").find("ul").hide();
	jQuery('#nav > li:has(ul > li)').not(".current_page_item, .current_page_parent, .current_page_ancestor").hoverIntent( hoverIntentConfig );


/// !!!!!!!!!!!!! Fix focus/blur!	
	jQuery('#nav > li:has(ul > li)').not(".current_page_item, .current_page_parent, .current_page_ancestor").find('a').focus(function(){ slideDown(false); }).blur(function(){ slideUp(false); });
	//jQuery('#nav > li:has(ul > li)').not(".current_page_item").find('a').focus(function(){ console.log('focused'); }).blur(function(){ console.log('unfocused'); });

	function slideDown(state) { 
		if (!state) {
			//console.log('focused');
			if (jQuery(this).next("ul").hasClass('active') || jQuery(this).parents('ul').hasClass('active')) {
				
			} else {
				jQuery(this).next("ul").addClass('active').slideDown("slow");
			}
			
		}
		else {
			jQuery(this).find("ul").addClass('active').slideDown("slow");
		}
	}
	
	function slideUp(state) {
		if (!state) {
			//console.log('unfocused');
			jQuery(this).next("ul").removeClass('active').slideUp("slow");
		}
		else {
			jQuery(this).find("ul").removeClass('active').slideUp("slow");
		}
	}
	
	
	// add print page link to footer
	jQuery('#footerInner .alignright').append('<li><a href="#">' + PRINT_PAGE + '</a></li>');
	
	
	// move calendar caption to nav table (function added to jQuery file)
	moveCaption();
	
	// move sidebar calendar title to calendar inner content container
	var calhead = jQuery('#wp-calendar-outer h2');
	calhead.remove();
	jQuery('#wp-calendar').prepend(calhead);
	
	
	// add yellow class to classes list
	//jQuery('.ie6 ul.tridy li:nth-child(2n), .ie7 ul.tridy li:nth-child(2n), .ie8 ul.tridy li:nth-child(2n)').addClass('alt');
	jQuery('ul.tridy li').each(function(){
		var item = jQuery(this);
		if (item.find('a').attr('title').match(/A/i)) item.find('a').addClass('yellow');
		if (item.find('a').attr('title').match(/(1|2)\.A/i)) item.addClass('block');
		if (item.find('a').attr('title').match(/(1|2)\.B/i)) item.find('a').addClass('yellow');
	});
	
	
	// create Družina submenu
	if ((jQuery('body.page-id-51').length > 0) || (jQuery('body.parent-pageid-51').length > 0)) {
		var subMenu = jQuery('#subpages .subpages').clone();
		jQuery('#subpages .subpages').remove();
		var druzinaMenu = ['cerna', 'cervena', 'fialova', 'hneda', 'modra', 'oranzova', 'zelena', 'zluta'];
		var newSubMenu = jQuery('<ul class="subpages images clearfix subpagesshow"/>');
		subMenu.find('a').each(function(){
			var item = jQuery(this);
			jQuery.each(druzinaMenu, function(i){
				var _regex = new RegExp(druzinaMenu[i], "i");
				if (item.attr('href').match(_regex)) {
					item.addClass(druzinaMenu[i]).append('<span/>');
					newSubMenu.append(item.parent());
				}
			});
		});
		
		subMenu.find('li:last').addClass('last');
		subMenu.insertAfter(jQuery('#subpages .subpages-head')).addClass('subpagesshow');
		newSubMenu.insertAfter(subMenu);
	}
	
	
	
	
	// trigger superbox (for pop-up content-based modals)
	 jQuery.superbox.settings = {
		closeTxt: "Zavřít",
		loadTxt: "",
		nextTxt: ">",
		prevTxt: "<",
		boxWidth: "800",
		boxHeight: "540"
	};
	jQuery.superbox(); 
});
