var jq = jQuery.noConflict();
     
jq(function(){  
    var lang = jq('div#languages .active ').attr('href').match(/^[a-z]+/);
	if(lang == null) lang = 'pl';
    jq.get('banner_'+lang+'.htm', function(data){
        jq('.mySlider').html(data);
        
        
        var timerId;
        var active = 1;
        var lastActive = 1;
        var controls = 25;
        var height = 0;
        var visibleHeight = jq('.mySlider #bannerButtons').height();
    
        jq('.mySlider ul li').each(function(){
        
            height += jq(this).height();
        })

        var limitTop = (height - visibleHeight) + controls;

        function resetHeaders(){
            jq('.mySlider .blocks .simpleShow h3').each(function(){
                jq(this).css('margin-top', '-82px'); 
            });
            jq('.mySlider .blocks .simpleShow h4').each(function(){
                jq(this).css('right', '-2000px'); 
            });
        }
  
        function slideLeft(elem){
        
            var currentId = jq(elem).parent().parent().attr('id').match(/[\d]+/);

            if(jq('.submenu .active').attr('id') != 'n' + currentId)
            {
                resetHeaders();
                return;
            }
        
            jq(elem).animate({
                "right": "+=2000"
            },'slow', function(){
                if(!jq(this).next().length)
                {
                    setTimeout(function(){
                        simpleShow(jq('li.active').next());  
                    }, 2000);
                    return;
                };
                slideLeft(jq(this).next());
            });
        };
        
        function simpleShow(currentLi){
            if(jq(currentLi).length == 0)
            {
                jq('.mySlider  ul').animate({
                    top: "0px"
                },500);
                simpleShow(jq('li#n1'));
                return;
            }
            resetHeaders();
            var obj = jq('.mySlider .blocks#d'+jq(currentLi).attr('id').substr(1));
            var number = obj.attr('id').substr(1);
        
            var position = jq(currentLi).position();
        
            var parentHeight = jq(currentLi).parent().parent().height();

            if(position.top + jq(currentLi).height()  > parentHeight)
            {
                jq('.mySlider ul').animate({
                    top: "-"+jq(currentLi).height()+"px"
                    },500);
            }
        
            //var moveBy = (((number - 1) * heightWhole) * -1);
            if(active != number){
                jq('.mySlider .blocks').each(function(){
                    jq(this).css('z-index','1');
                });
                jq('.mySlider .blocks#d'+active).css('z-index','2');
            
                lastActive = active;
            
                jq('.mySlider .submenu li').each(function() {
                    jq(this).removeClass('active');
                });
            
            
                jq(currentLi).addClass('active');
            
                active = number;
            
                resetHeaders();
        
                jq('.mySlider .blocks#d'+number).css('height','0px');
                jq('.mySlider .blocks#d'+number).css('z-index','3');
                jq('.mySlider .blocks#d'+number).animate({
                    height: "400px" 
                },500);
            
                jq('.blocks#d'+lastActive+' .simpleShow h3, .blocks#d'+lastActive+' .simpleShow h4').removeAttr("style");
            
                setTimeout(function(){
                    jq('.mySlider .blocks#d'+number+' .simpleShow h3').animate({
                        "margin-top": "+=82"
                    },
                    2000,
                    function(){
                        var currentId = jq(this).parent().parent().attr('id').match(/[\d]+/);
                    
                        if(jq('.submenu .active').attr('id') != 'n' + currentId)
                        {
                            resetHeaders();
                            return;
                        }
                    
                        jq('.mySlider .blocks#d'+number+' .simpleShow h4:first').animate({
                            "right": "+=2000"
                        },
                        2000, 
                        function(){

                            var currentId = jq(this).parent().parent().attr('id').match(/[\d]+/);

                            if(jq('.submenu .active').attr('id') != 'n' + currentId)
                            {
                                resetHeaders();
                                return;
                            }
                            slideLeft(jq(this).next()); 
                        })
                    })
                }
                ,600);
             
            }
        }
    
        jq('.mySlider .arrUp').hover(
            function() {
                timerId = setInterval(function() {
                    var top = parseInt(jq('.mySlider ul').css('top').substr(0,jq('.mySlider ul').css('top').length-2));
                    if(top + 1 < 0){
                        top += 1;
                        jq('.mySlider  ul').css('top', top+"px");
                    }
                }, 10);
            },
            function() { 
                clearInterval(timerId); 
            }
            );
            
        jq('.mySlider .arrDown').hover(
            function() {
                timerId = setInterval(function() {
                    var top = parseInt(jq('.mySlider ul').css('top').substr(0,jq('.mySlider ul').css('top').length-2));
                    if(top - 1 > limitTop * -1){
                        top -= 1;
                        jq('.mySlider ul').css('top', top+"px");
                    }
                }, 10);
            },
            function() { 
                clearInterval(timerId); 
            }
            );
              
        resetHeaders();
    
        jq('.mySlider li#n1').addClass('active');
        
        jq('.mySlider .blocks#d1 .simpleShow h3').animate({
            "margin-top": "+=82"
        },1300,
        function(){
            jq('.mySlider .blocks#d1 .simpleShow h4:first').animate({
                "right": "+=2000"
            },'slow', function(){
                slideLeft(jq(this).next());
            })
        });
                
        jq('.mySlider ul li').click(function(){
            simpleShow(jq(this));
        });
    });
});
