/*
 * PMCT.nl javascript v0.6 // July/16/2009
 *
 */
var hiding_submenus;
 
$(document).ready(function() {		
	bindMainMenu();
	if($('.news-ticker').length) showNewsTicker();
	bindTooltips();
});


/* binding main-menu items */
function bindMainMenu() {	
	if($('.main-menus .current_page_ancestor').length&&!$('.main-menus .current_page_ancestor:first').hasClass('current_page_parent')) {		
		showSubMenu($('.main-menus .current_page_ancestor a:first')); //level3
	} else if($('.main-menus .current_page_ancestor').length&&$('.main-menus .current_page_ancestor:first').hasClass('current_page_parent')) {
		showSubMenu($('.main-menus .current_page_parent a:first'));	// level2
	} else if($('.main-menus .current_page_item').length&&!$('.main-menus .current_page_ancestor').length&&$('.main-menus .current_page_item ul').length) {
		showSubMenu($('.main-menus .current_page_item a:first'));	// level1
	};
		
};

/* showing sub-menus from main-menu */
function showSubMenu(item) {	
	var submenus_content = item.next('ul').html();
		
	submenus_content = "<ul>"+submenus_content+"</ul>";
	$('.sub-menus').html(submenus_content);
	$('.sub-menus li:last').css('border','0px');
	$('.sub-menus li:last').css('padding-right','0px');
	var submenus_width = $('.sub-menus').width();
	
	var item_width = item.width();
	var item_pos = item.offset();
	var item_top_pos = item_pos.top + 46;
		
	var item_left_pos;
	if($('.main-menus .pagenav li:first').hasClass('current_page_item')||$('.main-menus .pagenav li:first').hasClass('current_page_parent')) {
		item_left_pos = item_pos.left;		
	} else item_left_pos = item_pos.left - ((submenus_width - item_width)/2) + 10;
	
	if($('.menubar').offset().left>item_left_pos) item_left_pos = $('.menubar').offset().left;
	
	var cssProp = {
		'position' : 'absolute',
		'top' : item_top_pos,
		'left' : item_left_pos
	};
	$('.sub-menus').css(cssProp);
		
	$('.sub-menus').fadeIn('fast');
};

/* news ticker */

function showNewsTicker() {
	var table_width = $('.news-ticker-articles').width();
	$('.news-ticker').css('opacity', '0').css('visibility', 'visible');
	$('.news-ticker').animate({'opacity': '1'}, 'slow');
	var table_max_left = 0 - table_width;	
	var table_left;
	
	function scrollNews() {
		table_left = $('.news-ticker-articles').css('left');
		table_left = table_left.substring(0, table_left.length-2);
		if(table_left<table_max_left) {			
			$('.news-ticker-articles').css('left', '350px');			
		};
		$('.news-ticker-articles').animate({'left': '-=10px'}, 'fast', 'linear', scrollNews);
	};
		
	scrollNews();
	
	$('.news-ticker').hover(function() {
		$('.news-ticker-articles').stop();
	}, scrollNews);
};

/* tooltip functionality */

var hideTooltip, hidingTooltip;

function bindTooltips() {
	if($('.show-tooltip').length) {
		$('.show-tooltip').hover(showTooltip, hideTooltip);
		$('.tooltip').hover(keepTooltipAlive, hideTooltip);
	};
};

function showTooltip(event) {
	if($(this).find('span').length) {		
		if(hidingTooltip) {
			clearTimeout(hideTooltip);
			hidingTooltip = false;
		};
		$('.tooltip').hide();		
		
		var x = event.pageX+7;
		var y = event.pageY+7;
		var tooltip_text = $(this).find('span').html();
		
		$(this).addClass('active-tooltip');
		
		$('.tooltip').html(tooltip_text);		
		
		$('.tooltip').css({
			'left' : x,
			'top' : y
		});
		
		$('.tooltip').show('fast');
	};
};

function hideTooltip() {		
	hideTooltip = setTimeout(function() {				
			$('.tooltip').fadeOut('slow');
			hidingTooltip = false;
	}, 1000);
	hidingTooltip = true;	
};

function keepTooltipAlive() {
	if(hidingTooltip) {
		clearTimeout(hideTooltip);
		hidingTooltip = false;
	};
};
