$(document).ready(function(){
    

    $('#valutatable td.lastd a').each(function(){
        var graph=$(this).parent().parent().next().find('.valutagraph, .valuta_rates');
        var hh=graph.height();
        graph.css({height:0, display:'block'});
        $(this).click(function(e){
                
                //alert(hh)
                
                if (graph.hasClass('opened')) {
                    graph.animate({ paddingBottom:"0", "height": "0", "opacity": "0" }, { queue: false, duration: 400, complete: function(){
                        graph.removeClass('opened');
                    } });
                    
                }
                else {
                    graph.animate({ paddingBottom:"10px", "height": hh+"px", "opacity": "100" }, { queue: false, duration: 400, complete: function(){
                        graph.addClass('opened');
                    } });
                    
                }
            e.preventDefault();
        }); 
    }); 
    
    $('.boxcontainer div.valutabox, .boxcontainer div.bankbox').each(function(){
        var graph=$(this).next('.valutagraph');
        var pagerbox=$(this).next('.pagerbox');
        var pager=pagerbox.find('.pager');
        var hh=graph.find('.graph_groups').height();
        $(this).hover(
          function () {
            $(this).addClass('over');
          }, 
          function () {
            $(this).removeClass('over');
          }
        );

        $(this).find('img.opengraph').click(function(e){
            graph.animate({top:0},400);
            e.preventDefault();
        }); 
        
        $('.boxcontainer div.valutabox h2 a').click(function(e){
            e.preventDefault();
        }); 
        
        graph.click(function(e){
            graph.animate({top:'165px'},400);
            e.preventDefault();
        });
        
        graph.mouseenter(function(e){
            $(this).find('.graph_groups').animate({opacity:1, height:hh},200);
            e.preventDefault();
        })
        graph.mouseleave(function(e){
            $(this).find('.graph_groups').animate({opacity:0, height:0},200);
            e.preventDefault();
        });
        
        function checkEnd(l){
            if (l<=-((items.length-2)*145)){
                    n.css({opacity:0.5});
                    p.css({opacity:1});
                }
                else {
                    n.css({opacity:1});
                }
                
        
        if (l>=0){
                    n.css({opacity:1});
                    p.css({opacity:0.5});
                }
                else {
                    p.css({opacity:1}); 
                }
            
        }
        
        if (pager.length) {
            var l=pager.position().left;
            
            var items=pager.find('ul');
            pager.css({width:items.length*145});
            var n=pager.next().next('.pnext');
            var p=pager.next('.pprev');
            checkEnd(l);
            n.click(function(e){
                if (l>-((items.length-2)*145)){
                pager.animate({left:(l-145)+"px"},200,function(){
                    l=pager.position().left;
                    checkEnd(l);
                });
                }
                
                e.stopPropagation();
            });
            p.click(function(e){
                
                if (l<0){
                pager.animate({left:(l+125)+"px"},200,function(){
                    l=pager.position().left;
                    checkEnd(l);
                });
                }
                
                e.stopPropagation();
            });
        }
        $(this).find('.lnkbtn').click(function(e){
            pagerbox.css({zIndex:20}).animate({top:0},400);
                
            e.preventDefault();
        });
        
        pagerbox.click(function(e){
            pagerbox.animate({top:'100px'},400);
            e.preventDefault();
        });
         
    });  
    
    var gzindex=10;

    $('.valutagraph ul li img').click(function(e){
        
        if (!$(this).hasClass("checked")){
            $(this).addClass("checked");
            $("#"+$(this).attr("id").replace("_chk","_pic")).fadeIn().css({zIndex:gzindex++});
            this.src=this.src.replace("off","on");
        }
        else{
            $(this).removeClass("checked");
            $("#"+$(this).attr("id").replace("_chk","_pic")).fadeOut();
            this.src=this.src.replace("on","off");
        }
        
        e.stopPropagation();
    });
    
    
       
    
});


