langTimeout = null
resizeTimeout = null
b0Timeout = null
b4Timeout = null

scrollParams = {
    resize:false,
    scrollbarHeight: 12,
    showArrows: true,
    arrowSize: 12
    }


$(function(){

    /* === INIT === */        
        
    ie6WidthAdjust()/* ie6 hack */ 
    $('div.scrollingPanel').jScrollHorizontalPane(scrollParams);

    $(window).resize(function(e){            
                ie6WidthAdjust()
                applyScrolling()
        })    

    /*floatBlock($('div.row2')).smoothFloat();*/
        
    var list = $('ul.bannerList a')
        
    $.each( 
        $('ul.bannerList li > a'), 
        function() { 
            imghover(this) 
        } 
    );
    
    $('#menu3-li-6 a').hover(
        function(){
            $('div.langSelection').slideDown('slow')  
            clearTimeout(langTimeout)          
        },
        function(){langTimeout = setTimeout(function(){$('div.langSelection').slideUp('slow')},1000)}
        )
        
    $('div.langSelection').hover(
        function(){
            $(this).slideDown('slow')
            clearTimeout(langTimeout)
        },
        function(){langTimeout = setTimeout(function(){$('div.langSelection').slideUp('slow')},1000)}
        )
        
    $('#b0 a').hover(
        function(){
            clearTimeout(b0Timeout)
            $('#b0 a').stop(true,false)
            $('#b0 a').animate( { width:"379px"}, 600 )            
        },
        function(){b0Timeout = setTimeout(function(){$('#b0 a').animate( { width:"228px"}, 600 )},1000)}
        )
        
    $('#b4 a').hover(
        function(){
            clearTimeout(b4Timeout)
            $('#b4 a').stop(true,false)
            $('#b4 a').animate( { width:"379px"}, 600 )            
        },
        function(){b4Timeout = setTimeout(function(){$('#b4 a').animate( { width:"228px"}, 600 )},1000)}
        )
     
     $('a#single_01').fancybox();   // RIGA b6 BOX  

});


function ie6WidthAdjust(){
    /* ie6 hack */
    if ($.browser.msie && $.browser.version.substr(0,1)<8) {
        $('div.dx').css('width','')        
        $('div.scrollingPanel').css('width','')
        if($(window).width()<1060){
            $('div.dx').css('width',(1060-290)+'px')        
            $('div.scrollingPanel').css('width',(1060-290)+'px')
            }
        }         
}

function applyScrolling(){
    jScrollPaneRemove($('div.scrollingPanel'))
    
    setTimeout(function(){
        /*alert($('div.scrollingPanel').outerWidth())*/
        $('div.scrollingPanel').jScrollHorizontalPane(scrollParams)
    },500);
    //floatBlock($('div.row2')).smoothFloat();
}


function jScrollPaneRemove (element){
        $this = $(element);
        var $c = $this.parent();
        if ($c.is('.jScrollPaneContainer')) {
            $this.css(
                {
                    'top':'',
                    'height':'',
                    'width':'',
                    'padding':'',
                    'overflow':'',
                    'position':''
                }            
            );
            // TODO :: $this.attr('style', $this.data('originalStyleTag'));
            $c.after($this).remove();
        }   
}


function floatBlock(div){
    
    if(this.moveLoop){
        clearInterval(this.moveLoop)
        this.moveLoop = null
        div.css({left: 0})
    }
    
    div.viewPortWidth = div.parent().parent().outerWidth();
    window["floatBox"] = div;
    div.bannerScrollPosition = div.position().left 
    
    div.move = function(x){
        if(x>0) div.css({left: x});
        }

    div.smoothFloat=function(){
        var scrollPosition = $('div.scrollingPanel').position().left 
        var dist = this.width() - (this.viewPortWidth - scrollPosition);
        dist = (dist>0) ? 0 : dist;
        if (dist<=0) this.bannerScrollPosition += (-dist - this.bannerScrollPosition)/10;
        this.move(this.bannerScrollPosition);
    }
                       
    this.moveLoop = setInterval( "window['floatBox'].smoothFloat()", 30 );
    
    return div
}


/* === banners hoover === */
function imghover(aTag){
    aTag=$(aTag)        
    
    var imgTag =  $('img',aTag) 
    var titleTag = $('span.h3',aTag)
    aTag.css('width', imgTag.width()+'px')
    
    imgTag.after('<div class="velina" />')
    var velinaTag = $('div.velina',aTag)
    velinaTag.originalOpacity = velinaTag.css('opacity')
    
    imgTag.wrap('<div class="imgOffPlaceholder" />');
    
    var off = imgTag.parent()
    
    var offName =  imgTag.attr('src').slice(0,-4) + '_o' + imgTag.attr('src').slice(-4);
    
    off.css( 'background-image', 'url('+offName+')' );

    imgTag.css('opacity',0)              

    aTag.hover( 
        function(){
            var imgTag =  $('img',aTag)
            var titleTag = $('span.h3',aTag)
            //var descrTag = $('p',aTag) 
            var velinaTag = $('div.velina',aTag)  
            
            imgTag.stop(true, true)
            imgTag.animate({opacity: 1});
            
            titleTag.stop(true, true)
            titleTag.animate({color: '#000'});
            }, 
        function(){
            var imgTag =  $('img',aTag)
            var titleTag = $('span.h3',aTag)
            
            imgTag.stop(true, true)
            imgTag.animate({opacity: 0});
            
            titleTag.stop(true, true)
            titleTag.animate({color: '#666'});
            }
        )
    
}

