// JavaScript Document
/* 
jqeury main file in /js/jquery/jquery-1.6.1.min */
//var $j = jQuery.noConflict(); 

jQuery(function($){
      // Use jQuery with $(...)
      // alert("jqeury");
	  //jQuery("#sidenav .level-top.active").hide();
	  

	
	
	/* ------------------ STYLE SELECT ELEMENT ----------------------- */
	jQuery("select.change").sb({ fixedWidth: true });
	
 	/* ---------- LEFT NAVIGATION ------------- 
	  
	  if the current item is second level, then remove its first level elements
	  */
	  /* if no level 2 is active then switch colors */
	  if (jQuery("#sidenav li.level2.active").length > 0) {
		  //alert("aa");
		  jQuery("#sidenav li.level2:not(.active)").addClass("level2-withchildren");
		  
		  if ((jQuery("#sidenav .level2.active").children("ul")).length > 0)
		  	jQuery("#sidenav li.level2.active").addClass("active-children");
	  }
	  
	  if (jQuery("#sidenav li.level1.active").length > 0) {
		  //alert("aa");
		  if ((jQuery("#sidenav .level1.active").children("ul")).length > 0)
		  	jQuery("#sidenav li.level1.active").addClass("active-children");
	  }
	  
	 // $classNames = jQuery("#sidenav .level2.active").attr("class");
	/*
	  if (jQuery("#sidenav .level2.active").length >0)
	  	{
			//alert(jQuery("#sidenav .level2.active").children("a").text());
			// if the second level has more children that need to be shown, then we need to replace menus
			if ((jQuery("#sidenav .level2.active").children("ul")).length > 0)
			{
				
			// copy the active li element 
			$li = jQuery("#sidenav .level2.active");
			$toremove = $li.closest("ul").closest("li");
			//alert($toremove.html());
			$li.removeClass("level2").addClass("level1 level-top");
			$toremove.before($li);
			$toremove.remove();
	
			jQuery("#sidenav .level-top.active").addClass("normalli");
			}
			else {
			jQuery("#sidenav .level-top.active").addClass("normalli")
			}
		}
		else 
		{
			// alert("else");
			//jQuery("#sidenav .level-top.active").show();
			jQuery("#sidenav .level-top.active").addClass("normalli");
		}
		*/	
	

	/* ------------------ TOP NAVIGATION --------------------
	add image and tagline
	*/
	jQuery("#nav li.level-top").each(function(){
		// get image
		$image = jQuery(this).children("a.level-top").attr("ref");
		$tagline = jQuery(this).children("a.level-top").children("em").text();
		$imageLi = jQuery('<li class="imageli"></li>');
		$imageLi.append('<img src="' + $image + '" width="130" height="60" />');
		$imageLi.append('<h5>'+ $tagline +'</h5>');
		jQuery(this).children("ul").append($imageLi);
		//alert($tagline);
		});
	
	/*
	
	jQuery("#nav li").mouseenter(function(){
		jQuery(this).addClass("hover");
		});
	
	jQuery("#nav li").mouseleave(function(){
		jQuery(this).removeClass("hover");
		});
	*/


	jQuery("#nav ul.level1 li").mouseenter(function(){
		   //jQuery(this).closest("li.level-top").addClass("spec");
			if (jQuery(this).children("ul").length >0 ){
				jQuery(this).closest("ul.level1").css("padding-right","170px");
				}
			else
				jQuery(this).closest("ul.level1").css("padding-right","0");	
			});
	
	jQuery("#nav ul.level1").mouseleave(function(){
			jQuery(this).closest("ul.level1").css("padding-right","0");
			//jQuery(this).closest("li.level-top").removeClass("spec");
			});
	
	/* ------------------ TOP NAVIGATION --------------------
	make 2 and 3 level subnavigation have the same height
	*/
	
	jQuery("#nav li li").mouseenter(function(){
		/* set bg for big ul */
		
		/* change image and tagline  */
		//jQuery('.top-custservice').text(Math.random());
		//alert(jQuery(this).closest("li.level-top").find(".imageli").find("img"));
		
		// old - beofore mousemove
		if (!jQuery(this).hasClass("imageli")){
			jQuery(this).closest("li.level-top").find(".imageli").find("img").attr("src", jQuery(this).children("a").attr("ref"));
			jQuery(this).closest("li.level-top").find(".imageli").find("h5").text(jQuery(this).children("a").children("em").text());
			}
		else {
		
		}
		

		/* end change image and tagline */									
		//alert("hover on");
		$currentHeight = jQuery(this).closest("ul").height() + parseInt(jQuery(this).closest("ul").css("padding-bottom"));
		$childHeight = jQuery(this).children("ul").height() + parseInt(jQuery(this).children("ul").css("padding-bottom"));
		//alert($currentHeight + " : "+ $childHeight);
		// alert("prvi: "+ jQuery(this).closest("ul").height() + ", drugi: "+ jQuery(this).children("ul").height());
		if ($currentHeight > $childHeight)	
			jQuery(this).children("ul").height( $currentHeight - parseInt(jQuery(this).children("ul").css("padding-bottom")) );
		else if ($currentHeight  < $childHeight)
			jQuery(this).closest("ul").height($childHeight - parseInt(jQuery(this).closest("ul").css("padding-bottom")) ) ;
		}, function(){});
	
	// change image and tagline
	// set to last element that is hovered (current hovered, or parent over)
	jQuery("#nav li ul").mousemove(function(){
		$overelements = jQuery("#nav").find("li.over:not(.imageli)");
		
		if ($overelements.length > 0)
			{
			//jQuery('.top-custservice').text($overelements.length);
			$lastel = (jQuery("#nav").find("li.over:not(.imageli)")).last();
			jQuery("#nav .imageli").find("img").attr("src", $lastel.children("a").attr("ref"));
			jQuery("#nav .imageli").find("h5").text($lastel.children("a").children("em").text());				
			}
		else
			return;

		//jQuery('.top-custservice').text($lastel.children("a").children("em").text() );
		/*
		if ($lastel!=null && $lastel!="" && $lastel.children("a").children("em")!=""  && $lastel.children("a").children("em")!=null){
			//alert($lastel.children("a").children("em").text());
			jQuery('.top-custservice').text($lastel.children("a").children("em").text() );
			jQuery("#nav .imageli").find("img").attr("src", $lastel.children("a").attr("ref"));
			jQuery("#nav .imageli").find("h5").text($lastel.children("a").children("em").text());		 
			}
			*/
		});
	

	// when hover top link set that first image
	jQuery("#nav li a.level-top").mouseenter(function(){
		$lastel = (jQuery("#nav").find("li.over:not(.imageli)")).last();											
		//jQuery('.top-custservice').text($lastel.children("a").children("em").text() );
		jQuery("#nav .imageli").find("img").attr("src", jQuery(this).attr("ref"));
		jQuery("#nav .imageli").find("h5").text(jQuery(this).children("em").text());		 
		});	

	  
	 
		
		/* ----------------- PRETTY PHTO - LIGHTBOX ----------- */
    	jQuery("a[rel^='prettyPhoto']").prettyPhoto();

		
  });
