//remove conflict with other libraries
jQuery.noConflict();

function buildMenu()
{
	//MENU MEMORY - recursive find and hide menus
	
	//determine query string
	var pathname = window.location.pathname;
	var search = window.location.search;
	//if there is ?id=1&params=...(Ajax search form - cut &params... part)
	if(search.indexOf('&') > -1)
		search = search.substring(0,search.indexOf('&'));
	
	pathname += search;
	
	/*
	var pathname = window.location.pathname;
	//site has friendly URL-s if there is .html(Ajax search form - cut extension)
	if(pathname.indexOf('.') > -1)
		pathname = pathname.substring(0,pathname.indexOf('.'));*/
	
	//search <li> elements
	jQuery('.three-level-menu,#right ul.static').children().each(function()
	{
		//if(jQuery(this).find('a[href=' + pathname + ']').size() == 0)
		if(jQuery(this).find('a[href=' + pathname + ']').size() == 0)
			jQuery(this).find('ul').hide(); //first level search is false
		else
		{
			jQuery(this).find('ul').show(); //first level search is false
			jQuery(this).children(":eq(0)").addClass('selected');
			jQuery(this).find('ul').children().each(function()
			{
				if(jQuery(this).find('a[href=' + pathname + ']').size() == 0)
					jQuery(this).find('ul').hide(); //second level search is false
				else
				{
					jQuery(this).children(":eq(0)").addClass('selected');
					//target is in third level, nothing more to hide
				}
			});
		}

	});
	
	//MENU CLICK EVENT
	jQuery('.three-level-menu').find('a').click(function(){//attach animation events 
		
		//REMOVE FOCUS FROM <a> element
		jQuery('#hidden-focus').focus();
/*		 
		//MENU ANIMATION 
		var submenu = jQuery(this).find('ul:eq(0)'); //if there is submenu, slide it down and slide others up
		if (submenu.size() > 0)
		{
			//MENU APPEARANCE EFFECT
			//select item - finds <a> tag and adds class selected
			jQuery(this).children(":eq(0)").addClass('selected');
			
			//remove selected items elsewhere recursively
			jQuery(this).siblings().find('a').removeClass('selected');
			
			//MENU MOVEMENT EFFECT
			submenu.slideDown();
			jQuery(this).siblings().find('ul').slideUp();
		}*/
	});
	
	//EXCEPTIONS FOR EMPTY FOLDERS - MAKE THEM EXPAND NOT LINK TO PAGE
	var i;
	var fldLength = folderExceptions.length;
	
	var exceptionsPath = window.location.pathname;
	if (exceptionsPath.indexOf('php') == -1)
		exceptionsPath += 'index.php';
	
	for(i=0; i<fldLength; i++)
	{
		jQuery('.three-level-menu').find('li a[href=' + exceptionsPath + '?id=' + 
		folderExceptions[i] + ']').click(function(){//attach animation events 
				
			//MENU ANIMATION 
			var submenu = jQuery(this).parent().find('ul:eq(0)'); //if there is submenu, slide it down and slide others up
			if (submenu.size() > 0)
			{
				//MENU APPEARANCE EFFECT
				//select item - finds <a> tag and adds class selected
				jQuery(this).addClass('selected');
				
				//remove selected items elsewhere recursively
				jQuery(this).parent().siblings().find('a').removeClass('selected');
				
				//MENU MOVEMENT EFFECT
				submenu.slideDown();
				jQuery(this).parent().siblings().find('ul').slideUp();
			}
			return false;
		});
		
		//EXCEPTIONS FOR BREADCRUMBS - DISABLE LINKS FOR EMPTY FOLDERS
		jQuery('#breadcrumbs').find('a[href=' + exceptionsPath + '?id=' + 
		folderExceptions[i] + ']').each(function(){
			jQuery(this).css({'text-decoration': 'none', 'cursor': 'default'});
			jQuery(this).click(function(){ return false;});
		});
	}
}

//var slidingNow = false;

function attachBannerAnimation()
{
	jQuery('.banner-home').mouseover(function(){
		//if(slidingNow == false){
			//slidingNow = true;
			//HIDING
			var correspondingUL = jQuery(this).next().next();
			jQuery(this).siblings('.banner-home').next().slideUp('fast');
			jQuery(this).parent().find('ul').not(correspondingUL).each(function(){
				if(jQuery(this).is(':visible')) jQuery(this).fadeOut(100);
			});
			var bn = jQuery(this);
			//SHOWING
			jQuery(this).next().slideDown('normal', function(){
				/*slidingNow = false;*/
			//adjustRightMenuHeights();
			});
			if(!correspondingUL.is(':visible')){ 
				bn.next().next().fadeIn(200);};

	});
	
	jQuery('.banner-main, #right ul').not('.static').mouseout(function(e){
			//if "to" element is from the menu do not mouseout
			var target = (e.relatedTarget) ? e.relatedTarget : e.toElement;
			if( jQuery(target).is('.banner-main') || jQuery(target).is('a')){
				return false;
			}
			//HIDING
			if(jQuery(this).is('ul'))
			{
				jQuery(this).fadeOut(80);
				jQuery(this).prev().slideUp('fast');
			}
			else
			{
				//hide ul if there is such
				if( jQuery(this).next().is('ul') ) jQuery(this).next().fadeOut(80);
				if( jQuery(this).next().next().is('ul') ) jQuery(this).next().next().fadeOut(80);
				
				jQuery(this).parent().find('.banner-home').next().slideUp('fast');
			}
	});
}

//Logo rotation and preloading

var currentIndex = 0; //start rotation
//var logos = ['abermed.jpg','exp_logistics.jpg','frontier.jpg','lewest.jpg','martek.jpg','sos.jpg','iridium.jpg'];
				
function rotateLogos()
{
	jQuery('#banner-background').css("background-image", 
	"url('" + SITE_ROOT + LOGOS_FOLDER + logos[currentIndex] + "')");
	currentIndex++;
	if (currentIndex == logos.length)
		currentIndex = 0;
}

//Banner rotation and preloading

var currentBannerIndex = 0; //start rotation
var banners = ['telemedics_home_aeroplane.jpg', 'telemedics_home_hospital.jpg', 'telemedics_home_ship.jpg', 'telemedics_home.jpg'];
				
function rotateBanners()
{
	jQuery('#bigBanner').attr("src", SITE_ROOT + IMAGES_FOLDER + banners[currentBannerIndex]);
	currentBannerIndex++;
	if (currentBannerIndex == banners.length)
		currentBannerIndex = 0;
	//console.log
}


function preloadImages()
{
	var preloadedImages = [];
	var i, preloadedLength;
	
	for (i = 0; i < banners.length; i++)
	{
		preloadedLength = preloadedImages.length;
		preloadedImages[preloadedLength] = new Image();
		preloadedImages[preloadedLength].src = SITE_ROOT + IMAGES_FOLDER + banners[i];
	}
	
	for (i = 0; i < logos.length; i++)
	{
		preloadedLength = preloadedImages.length;
		preloadedImages[preloadedLength] = new Image();
		preloadedImages[preloadedLength].src = SITE_ROOT + LOGOS_FOLDER + logos[i];
	}
	
}
//end logo section


function adjustRightMenuHeights()
{
	jQuery('#right ul').not('.static').each(function(){
		var ulHeight = jQuery(this).height();
		//jQuery(this).prev();
		var firstDiv = jQuery(this).prev().prev();
		//var firstDivHeight = firstDiv.height();
		//var secDivHeight = secDiv.height();
		var firstDivTop = firstDiv.position().top;
		var firstDivLeft = firstDiv.position().left;
		var liChildren = jQuery(this).children().size();
		jQuery(this).css({position: "absolute", top: firstDivTop + ( 86*2 - liChildren*20 ), left: firstDivLeft});
	});
	
	//jQuery('#right ul').hide();
	//jQuery('#right div.banner-main').hide();
	/*jQuery('#right ul').each(function(){
		jQuery(this).parent().css({height: "auto"});
	});*/
}

function printThisPage()
{
	var content = jQuery('#content');
	jQuery('body').html('');
	jQuery('body').prepend('<div id="goBack"><span onclick="location.reload()">&laquo; go back</span>'+
	'<span onclick="window.print()">print</span></div>');
	jQuery('body').append(content);
	jQuery('#content').css({margin: '50px auto', height: 'auto'});
	jQuery('html').css('background-color','#fff');
	
	window.print();
}

function toggleContent()
{
	jQuery('.partner-container>img').click(function () {
      jQuery(this).children('.partner-content hidden').toggle("slow");
    });
}

function tooltip()
{	
	/* CONFIG */		
		xOffset = 15;
		yOffset = 10;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	jQuery(".tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		jQuery("body").append("<p id='tooltip'>"+ this.t +"</p>");
		jQuery("#tooltip")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		jQuery("#tooltip").remove();
    });	
	jQuery(".tooltip").mousemove(function(e){
		jQuery("#tooltip")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px");
	});			
}

jQuery(function()
{
	buildMenu();
	attachBannerAnimation();
	adjustRightMenuHeights();
	setInterval("rotateLogos()", 3500);
	setInterval("rotateBanners()", 7000);
	preloadImages();
	toggleContent();
//	tooltip(); //show tooltips
});

