var changeTimer = false;
var showTime = false;
var helper = false;

var pics = ['img_avionika', 'img_avto', 'img_med', 'img_tnp'];
var divs = ['avionika', 'avto', 'medtech', 'tnp'];
var currPic = 0;

function showPic(pic)
{   
    if($('#' + pics[pic]).hasClass('active')) return;
    
    clearInterval(changeTimer);
    changeTimer = false;
    
    currPic = pic;
    
    $('.active').hide().toggleClass('active');
    $('#' + pics[pic]).show().toggleClass('active');
            
    
    $('.one').filter('.hover').toggleClass('hover');
    $('.one').filter('.'  + divs[pic]).toggleClass('hover');    
}

function resetTimer()
{
    if(changeTimer)
        clearInterval(changeTimer);
    changeTimer = setInterval('changePic();', 7000);  
}

function changePic()
{
    if(currPic == 3)
        currPic = 0;
    else
        currPic++;
        
    $('.active').fadeOut(2000).toggleClass('active');
    $('#' + pics[currPic]).fadeIn(2000).toggleClass('active');
    
    $('.one').filter('.hover').toggleClass('hover');
    $('.one').filter('.'  + divs[currPic]).toggleClass('hover');
}


$(function(){        
        if(window.location.pathname.indexOf('avto') != -1)
            picId = 1;
        else if(window.location.pathname.indexOf('med') != -1)
            picId = 2;
        else if(window.location.pathname.indexOf('tnp') != -1)
            picId = 3;
        else
            picId = 0;
        
        showPic(picId);
        
		resetTimer();
        
        $("#sidebar .one").hover(
			function(){
				if(showTime) clearTimout(showTime);
                helper = $(this).children('.helper');
                showTime = setTimeout('helper.fadeIn(500); showTime = false;', 1000);
			},function(){
                if(showTime)
                {
                    clearTimeout(showTime);
                    showTime = false;
                }
				$(this).children('.helper').fadeOut(500);
			}
		);
        
		$("#sidebar .avionika").hover(function(){
            showPic(0);
		}, resetTimer);
		$("#sidebar .avto").hover(function(){
            showPic(1);            
		}, resetTimer);
		$("#sidebar .medtech").hover(function(){
            showPic(2);
		}, resetTimer);
		$("#sidebar .tnp").hover(function(){
		    showPic(3);          
		}, resetTimer);
		//$('#sidebar a').ifixpng(); 
});
