$(function() {
	// flexslider (frontpage)
	$('.fp-slider').flexslider( {
		animation: 'slide',
		directionNav: false,
		controlsContainer: '.flex-container',
		pauseOnHover: true
	});
	// anti-spam
	$('.anti-spam').each(function(i) {
		$(this).html($(this).html().replace('(at)','@').replace('(dot)','.'));
		$(this).attr('href', 'mailto:' + $(this).html());
	});
	// fancybox
	$('.utel-send-mail').fancybox( {
		'width': 650,
		'height': 500,
		'padding': 20,
		'overlayColor': '#000',
		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe',
		'titleShow': false,
		'hideOnOverlayClick': false
	});
	$('a.screenshot').fancybox( {
		'overlayColor': '#000',
		'titlePosition': 'inside',
		'titleFormat': screenshotTitle,
		'titleFromAlt': true 
	});
	function screenshotTitle(title, currentArray, currentIndex, currentOpts) {
		return '<div class="screenshot-title">' + title + '</div>';
	}
	// tooltips
	$('#links img').poshytip( {
		className: 'tip-utel',
		showTimeout: 1,
		alignTo: 'target',
		alignX: 'center',
		offsetY: 5,
		slide: false,
		allowTipHover: false
	});
	// tabs
	$('#tabs>div').hide();
	$('#tabs>div:first').show();
	$('#tabs ul li:first').addClass('tabs-selected');
	$('#tabs>ul>li>a').click(function() {
		$('#tabs ul li').removeClass('tabs-selected');
		$(this).parent().addClass('tabs-selected');
		var currentTab = $(this).attr('href');
		$('#tabs>div').hide();
		$(currentTab).show();
		return false;
	});
	// mega menu
	function megaHoverOver() {
		$(this).children(":first").addClass('hover');
		$(this).find(".sub").show();
		jQuery.fn.calcSubWidth = function() {
			colWidth = 0;
			$(this).find("ul").each(function() { 
				colWidth += $(this).width();
			});
		};
		if ($(this).find(".column").length > 0) {
			var totalWidth = 0;	
			$(this).find(".column").each(function() {	
				$(this).calcSubWidth();
				totalWidth += colWidth;
			});
			$(this).find(".sub").css({'width': totalWidth + 80});
			$(this).find(".column:last").css({'padding':'0'});
		} 
		else { 
			$(this).calcSubWidth(); 
			$(this).find(".sub").css({'width': rowWidth});
		}
	}
	function megaHoverOut() {
		$(this).children(":first").removeClass('hover');
		$(this).find(".sub").hide();
	}
	$("nav#primary>ul>li").hover(megaHoverOver, megaHoverOut);
	// sidebar floating
	if ($("aside").length > 0) {
		var sb_top = $('aside').offset().top - parseFloat($('aside').css('marginTop').replace(/auto/, 0));
		var sb_height = $('aside').height();
		$(window).scroll(function (event) {
			var y = $(this).scrollTop();
			if ((y + 20 >= sb_top) && (sb_height + sb_top < $(window).height())) {
				$('aside').addClass('fixed');
			} else {
				$('aside').removeClass('fixed');
			}
		});
		$('#content').css('min-height', sb_height + 'px');
	}
	// auto complete search
	$('#search-field').autocomplete({ 
		serviceUrl: '/ext/autocomplete.php',
		minChars: 2,
		maxHeight: 500,
		width: 222,
		zIndex: 9999,
		deferRequestBy: 0,
		noCache: false, 
		onSelect: function(value, data){ document.location.href = data; },
	});
	// fix search form
	$('#search-form').attr('action', '/search/');
});
