// JavaScript Document
/* 

Developed by
Herman Talvitie (PixelEye)
www.pixeleye.fi
Last update: September 15th, 2010

*/


 var firstTime = true; // This boolean is to inform is the user is visiting the site for the first time. It's important so that the user won't wait the first time for the submenus
  var userCheckDelayFirstTime = 100; // (Only the first time! This time checks for how long time the user has to have the mouse over the link, before anything happens
  var userCheckDelay = 600; // This time checks for how long time the user has to have the mouse over the link, before anything happens
  var slideUpSpeed = 350; // This time determines the speed of changing submenus
  var subMenu = false; // This Boolean determines if a submenu is down
  var currentMenu = null;
  var idName;
  
$(document).ready(function(){
	
	// Once the site has been loaded, let's hide all the submenus

  $(".subNAVI_tuotteet").hide();
  $(".subNAVI_arvio").hide();
  $(".subNAVI_tilaa").hide();
  $(".subNAVI_tuki").hide();
  $(".subNAVI_ajankohtaista").hide();
  $(".subNAVI_yhteystiedot").hide();
  

	// This function makes sure all the submenus slides up.
  
  function clearMenus(){
  $(".subNAVI_tuotteet").slideUp(slideUpSpeed);
  $(".subNAVI_arvio").slideUp(slideUpSpeed);
  $(".subNAVI_tilaa").slideUp(slideUpSpeed);
  $(".subNAVI_tuki").slideUp(slideUpSpeed);
  $(".subNAVI_ajankohtaista").slideUp(slideUpSpeed);
  $(".subNAVI_yhteystiedot").slideUp(slideUpSpeed);
  };
 
// This function controls how the submenu is displayed
  function menuSlide (target){
	  
	  // Let's first make sure whether the user is using the menu for the first time...
	  if(firstTime == true && target != "etusivu" && target != "tilaa" && target != "ajankohtaista") {
		
		  idName = window.setTimeout(subMenuDown, userCheckDelayFirstTime);
		  firstTime = false;
		  
	  } else {
		  
		  idName = window.setTimeout(subMenuDown, userCheckDelay);
		  
	  };
	  
		

		// This function controls the display of the submenu
			function subMenuDown(){
				if(currentMenu == target){ // This one is to ensure the main link cannot be accessed twice
					
					return;
					
				} else { 
					clearMenus(); // This function clears all of the possible submenus being displayed
					currentMenu = target; // Let's assign the current link being used
					$(".subNAVI_"+target).delay(slideUpSpeed).slideDown();
					};
			};
	  
  };
  
  
// This function makes sure the timer goes out, so that the submenus won't come down
	function clearTimer (){
			window.clearTimeout(idName);
	};

// 
	$(".etusivu").mouseover(function (){menuSlide ("etusivu")});
	$(".tuotteet").mouseover(function (){menuSlide ("tuotteet")});
	$(".arvio").mouseover(function (){menuSlide ("arvio")});
	$(".tilaa").mouseover(function (){menuSlide ("tilaa")});
	$(".tuki").mouseover(function (){menuSlide ("tuki")});
	$(".ajankohtaista").mouseover(function (){menuSlide ("ajankohtaista")});
	$(".yhteystiedot").mouseover(function (){menuSlide ("yhteystiedot")});
	
	$(".etusivu").mouseout(function (){clearTimer ()});
	$(".tuotteet").mouseout(function (){clearTimer ()});
	$(".arvio").mouseout(function (){clearTimer ()});
	$(".tilaa").mouseout(function (){clearTimer ()});
	$(".tuki").mouseout(function (){clearTimer ()});
	$(".ajankohtaista").mouseout(function (){clearTimer ()});
	$(".yhteystiedot").mouseout(function (){clearTimer ()});
  
});


// This controls the mouseover effect for main navigation

$(function() {
    $('.rollover').hover(function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    }, function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    });
});
