﻿jQuery.fn.exists = function(){
	return jQuery(this).length>0;
}
jQuery.preloadImages = function() {
 for(var i = 0; i<arguments.length; i++)
 {
  jQuery("<img>").attr("src", arguments[i]);
 }
};

 $(document).ready(function(){
 
	$.preloadImages("http://hpc.kz/template/default/ru/images/sub-menu-bottom.png", "http://hpc.kz/template/default/ru/images/sub-menu-top.png", "http://hpc.kz/template/default/ru/images/sub-menu-center.png");
 
    $('.topmenu ul.level-0 li').hover(
        function() {
            $(this).addClass("active");
            //$(this).find('ul.level-1').stop(true, true);
            //$(this).find('ul.level-1').slideDown();
			$(this).find('ul.level-1').css("display","block");
        },
        function() {
            $(this).removeClass("active");
            //$(this).find('ul.level-1').slideUp('slow');
			$(this).find('ul.level-1').css("display","none");
        }
    );
    $('.topmenu ul.level-1 li').hover(	
        function() {			
            $(this).addClass("active");
            //$(this).find('ul.level-2').stop(true, true);
            //$(this).find('ul.level-2').slideDown();
			$(this).find('ul.level-2').css("display","block")
        },
        function() {
            $(this).removeClass("active");
            $(this).find('ul.level-2').css("display","none")
        }
    );	
	
	$('.left-level-1 a').click(
        function() {            
		//alert($(this).parent().attr("class"));
		  if($(this).parent().find('ul').exists()) {
			if($(this).parent().attr("class")!="left-level-1 active") {
				$(this).parent().attr("class","left-level-1 active");
				$(this).parent().css("background", "url(http://hpc.kz/template/default/ru/images/bgs.png) 0 -360px no-repeat");
				$(this).parent().find('ul').stop(true, true);
				$(this).parent().find('ul').slideDown();
			} else {
			    $(this).parent().attr("class","left-level-1");
				$(this).parent().css("background","none");
				$(this).parent().find('ul').slideUp('fast');
			}
		  }
        }
    );
	
	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	
	$("ul.topnav li span").click(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});
 
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
 

});
