jQuery(document).ready(function() {

	/*
	// jQuery Events Debugging
	$.fn.listHandlers = function(events, outputFunction) {
	    return this.each(function(i){
	        var elem = this,
	            dEvents = jQuery(this).data('events');
	        if (!dEvents) {return;}
	        $.each(dEvents, function(name, handler){
	            if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
	               $.each(handler, function(i,handler){
	                   outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
	               });
	           }
	        });
	    });
	};
	*/
	if ($(".obrazec2") != null) $(".obrazec2").colorbox({iframe: "true", width: "100%", height: "100%"});

	var urlDirFull = jQuery.url.attr("directory");
	jQuery.noConflict();

	/* --- main menu --- */

	jQuery.fn.initMainMenu = function(fullUrlDir) {
		jQuery.noConflict();
		jQuery("a[href != '#']", this).each(function() {
			// consider the id in the url when comparing
			if (jQuery(this).attr("class") == "m-m-useid"){
				// find matching url
				var urlDir = fullUrlDir;
				var aHref = jQuery(this).attr("href"); // trim IDs from url (example. "123758363528/")
			// ignore the id in the url when comparing
			} else {
				// find matching url
				var urlDir = fullUrlDir.replace(/all\/|[_\d]+\/.*/g, ''); // trim IDs from url (.../all/34235435234623/313/123123/...)
				var aHref = jQuery(this).attr("href").replace(/all\/|[_\d]+\/.*/g, ''); // trim IDs from url (.../all/34235435234623/313/123123/...)
			}
		
			// the page url is the same as the menu link, so open the menu
			if (urlDir == aHref) {
				jQuery(this).parents("li").each(function() {
					var tmp = jQuery(this).attr('class');
					var aLevels = tmp.match(/[0-9]+/); // get number at the end of the string
					//if (aLevels != undefined) {
					var level = aLevels[aLevels.length-1];				
					jQuery(this).addClass("menu-level"+level+"-open");
					//}
				});
				jQuery(this).parents("ul").show();
				jQuery(this).next("ul").show();
			}
		});
	}

	jQuery("#main-menu span").click(function(e) {
		//closeList("main-menu", "menu-level", 1);
		jQuery.noConflict();
		var liEl = jQuery(this).parents("li:first"); // li element
		var liSibl = liEl.siblings(); // sibling li elements
		
		// find out li level
	   	var tmp = liEl.attr('class');
	   	var aLevels = tmp.match(/[0-9]+/);
	   	var tgtlevel = aLevels[aLevels.length-1]; // tgt level
		
		liSibl.find("ul").slideUp('fast'); // hide all of the siblings' uls
		liEl.find("ul ul").slideUp('fast'); // hide child uls
		liEl.find("ul:first").slideToggle('fast'); // toggle the current submenu
		
		liSibl.removeClass("menu-level"+tgtlevel+"-open"); // remove the 'm-m-levelX-open' class from all the siblings of the clicked li
		liSibl.find('li').each(function() {
			var tmp = jQuery(this).attr('class');
			var aLevels = tmp.match(/[0-9]+/); // get number at the end of the string
			var level = aLevels[aLevels.length-1];
			jQuery(this).removeClass("menu-level"+level+"-open"); // remove the 'm-m-levelX-open' class from all the children of the siblings of the clicked li
		});
		liEl.find('li').each(function() {
			var tmp = jQuery(this).attr('class');
			var aLevels = tmp.match(/[0-9]+/); // get number at the end of the string
			var level = aLevels[aLevels.length-1];
			jQuery(this).removeClass("menu-level"+level+"-open"); // remove the 'm-m-levelX-open' class from all the children of the clicked li
		});
		
		liEl.toggleClass("menu-level"+tgtlevel+"-open"); // toggle class 'm-m-levelX-open' of the clicked li
		
		// prevent jumping to the top of the page for invalid (#) links
		if (liEl.find("a").attr("href") == '#') {
			e.preventDefault();
		}
	});

	/* --- END main menu --- */


	
	
	/* --- anchor click scrolling --- */
	
	jQuery('a[href^=#]').click(function(e) {
		jQuery.noConflict();
		// scroll to anchor if there is actually more after the # sign in the link
		if (jQuery(this).attr('href').length > 1) {
			scrollToElementName = jQuery(this).attr('href').substr(jQuery(this).attr('href').indexOf("#")+1);
			scrollTo = jQuery("a[name="+scrollToElementName+"]").offset().top;
			jQuery('html, body').animate({scrollTop: scrollTo}, 500);
			e.preventDefault();
		}
	});
	
	/* --- END anchor click scrolling --- */	


	/*
	if (jQuery("#body-entry").length) {
		jQuery("#main-menu").initMainMenu('/prvi_menu/podmenu_1/');
	} else { */
		jQuery("#main-menu").initMainMenu(urlDirFull);
	//}
	

	/* --- window resizing --- */
	
	jQuery(window).resize(function() {
		jQuery.noConflict();
		// resize elements, reposition elements, ...
	});
	
	/* --- END window resizing --- */

	// List all handlers for all events of all elements:
	//jQuery('*').listHandlers('*', console.info);

});
