               Drupal.behaviors.slider = function (context){
                    var row=0;
                    var boxWidth = $(".box").outerWidth();
                    var boxHeight = $(".box").outerHeight();
                    /* positioning backgrounds */
                    $(".rower").each(function(){
                        var box=0;
                        $(this).find(".box").each(function(){
                            $(this).find(".slide").each(function(){
                                $(this).css("background-position", "-"+box*boxWidth+"px -"+row*boxHeight+"px");
                            });
                            box++;
                        });
                        row++;
                    });

                    images = $("#b1_1").find(".slide");
                    var nyt=1;
                    /* Setting auto-advance every 7 seconds */
                    var auto;
                    auto=setInterval(function(){
                        nyt=slide(nyt);
                    },5000);
                    $(".lastcont").click(function(){
                        window.location =  $(this).find("a").attr("href");
                    });
                    $(".lastone").click(function(){
                        window.location =  $(this).parent().find("a").attr("href");
                    });
                }

                function slide(nyt){
                    if(nyt==images.length){
                        nyt=1;
                    }
                    else nyt ++;
                    var row=1;
                    var delays = new Array();
                    var timeDelay = 100;
                    for(var b = 1; b<=20; b++){
                        delays[b] = timeDelay;
                        timeDelay = timeDelay+100;
                    }
                    $(".rower").each(function(){
                        /*works but no coolness*/
                        var box=1;
                        $(this).find(".box").each(function(){
                            //var delay = (300*(row-1))+(500*box);
                            index = rand(delays.length);
                            delay = delays[index];
                            delays.splice(index, 1);
                            var visible = $(this).find(":visible");
                            var nextvisible = $("#r"+row+"_"+box+"_"+nyt);
                            $(this).queue(function(){
                                //$(visible).delay(delay).slideUp(1000);
                                //$(nextvisible).delay(delay).slideDown(500);
                                $(visible).delay(delay+100).fadeOut(1000);
                                $(nextvisible).delay(delay).fadeIn(400);
                                $(nextvisible).find("*").delay(delay).fadeIn(400);
                                $(this).dequeue();
                            });
                            box++;
                        });
                        row++;
                        /*end of works, but no coolness*/
                    });
                    return nyt;
                }
                function checkRightView(obj){
                    var ret = 0;
                    var classesToCheck = new Array();
                    classesToCheck[0]=".designerview";
                    classesToCheck[1]=".timelineview";
                    classesToCheck[2]=".newsview";
                    for(x in classesToCheck){
                        ret+= $(obj).parents(classesToCheck[x]).size();
                    }
                    return ret;
                }
Drupal.behaviors.coolnessprods = function (context){
                    /*displays captions and hides them on mouseout*/
                    
                    $(".view-prodotti .element, .view-contenuti .element").hover(function(){
                        if(checkRightView($(this))==0){
                            $(this).find("p.alt").slideDown(100);
                            $(this).find("div.caption").slideDown(200);
                        }
                    }, function(){
                        if(checkRightView($(this))==0){
                            $(this).find("p").slideUp(100);
                            $(this).find("div.caption").slideUp(200);
                        }
                    });
                    /*
                    clicks on elements: shows nowon and hides others.
                    switches imgs if nowon is already visible.
                    */
                    $(".view-prodotti .element, .view-contenuti .element").click(function(){
                        var row = $(this).parents(".rower2");
                        var nowon = $(this).parents(".rower2").find(".nowon");
                        var img = $(this).find(".smallp");
                        var link = $(this).find(".views-field-title a").attr("href");

                        /* modifying nowon metadata*/
                        $(nowon).find("h2").text($(img).attr("title"));
                        $(nowon).find("p").text($(img).attr("alt"));
                        $(nowon).find(".linker").attr("href", link);

                        /*hides this .element*/
                        $(this).hide(200);
                        $(this).parents(".rower2").find(".element").each(function(){
                            $(this).attr("style", "border:10px solid #fff; border-top:0;border-bottom:12px solid #fff; border-right:5px solid #fff;" );
                        });

                        /*calculate src*/
                        var src = $(img).attr("src");
                        src = src.replace("prodotti_view", "prodotti_viewbigger");

                        if(!$(nowon).is(':visible')){
                            /*shows up .nowon and puts there the image + alt and title. hides this .element*/
                            $(img).clone().appendTo($(nowon));
                            
                            $(nowon).find(".smallp").attr("src", src).removeClass("smallp").removeClass("element").animate({"width":$(nowon).width(), "height":$(nowon).height()-20},1000).addClass($(this).attr("class"));
                            $(nowon).show(1000, function(){
                                if($(row).attr("id").replace("row","")!=1){
                                    var offset = $(nowon).offset();
                                    $.scrollTo(offset.top,1000);
                                    $(nowon).find(".closenower").show();
                                }
                                if($(nowon).find("p").height()>20){
                                    $(nowon).find(".bottomalt").css("height", $(nowon).find("p").height());
                                }else{
                                    $(nowon).find(".bottomalt").css("height",20);
                                }
                            });
                        }
                        else{
                            /*switches between the images, alt and titles. hides this element and shows up the other one.*/
                            var nowonimg = $(nowon).find("img");
                            var elementclasses = $(nowonimg).attr("class");
                            $(nowonimg).removeAttr("class");
                            $(nowonimg).fadeOut(200, function(){
                                $(nowonimg).remove();
                                $(img).clone().appendTo($(nowon)).hide();
                                $(nowon).find(".smallp").attr("src", src).removeClass("smallp").removeClass("element").animate({"width":$(nowon).width(), "height":$(nowon).height()-20},1000).addClass($(this).attr("class"));
                                $(nowon).find("img").fadeIn(300);
                            });
                            elementclass = elementclasses.split(" ");
                            for(i=0; i<elementclass.length; i++){
                                if(elementclass[i].indexOf("img")==0){
                                    $("#id"+elementclass[i].substr(3,elementclass[i].length)).show(500);
                                }
                            }
                            if($(nowon).find("p").height()>20){
                                $(nowon).find(".bottomalt").css("height", $(nowon).find("p").height());
                            }else{
                                $(nowon).find(".bottomalt").css("height",20);
                            }
                        }
                        /*checks whether other .nowon are visible in other rows. if true, hides them showing up the right .element*/
                        var id = $(this).parents(".rower2").attr("id");
                        $(".rower2").each(function(){
                            if(id!=$(this).attr("id")){
                                if($(this).find(".nowon").is(":visible")){
                                    $(this).find(".nowon .closenower").click();
                                }
                            }
                        });
                    });
                    /*closes the current .nowon element and shows up the right .element*/
                    $(".view-prodotti .closenower, .view-contenuti .closenower").click(function(){
                        $(this).hide();
                        var nowon = $(this).parents(".rower2").find(".nowon");
                        $(this).parent().hide(500);
                        var elementclasses = $(nowon).find("img").attr("class");
                        elementclass = elementclasses.split(" ");
                        for(i=0; i<elementclass.length; i++){
                                if(elementclass[i].indexOf("img")==0){
                                    $("#id"+elementclass[i].substr(3,elementclass[i].length)).show(500);
                                }
                            }
                        $(this).parent().find("img").remove();
                        $(this).parents(".rower2").find(".element").each(function(){
                            $(this).attr("style", " ");
                        });
                    });
                }
                Drupal.behaviors.menuer = function(context){
                    var urlreq = window.location.href;
                    var prodottiClass = "products";
                    var divaniClass = "sofas";
                    var poltroneClass = "armchairs";
                    var complementiClass = "complements";
                    var chisiamoClass = "who_we_are";
                    if(Drupal.locale.strings){
                        prodottiClass = "prodotti";
                        divaniClass = "divani";
                        poltroneClass = "poltrone";
                        complementiClass = "complementi";
                        chisiamoClass = "chi_siamo";
                    }
                    
                    if((urlreq.indexOf("/prodotti")>0)){
                        if((urlreq.indexOf("/"+prodottiClass+"/"+divaniClass)>0)||(urlreq.indexOf("/prodotti/couches")>0)){
                            $("#block-menu-primary-links .menu .expanded."+prodottiClass).each(function(){
                                $(this).find("ul.menu li").each(function(){
                                    if(!$(this).hasClass(divaniClass)){
                                        $(this).removeClass("expanded").addClass("collapsed");
                                        $(this).find("li").hide();
                                    }
                                });
                            });
                        }
                        else if((urlreq.indexOf("/prodotti/"+poltroneClass)>0)){
                            $("#block-menu-primary-links .menu .expanded."+prodottiClass).each(function(){
                                $(this).find("ul.menu li").each(function(){
                                    if(!$(this).hasClass(poltroneClass)){
                                        $(this).removeClass("expanded").addClass("collapsed");
                                        $(this).find("li").hide();
                                    }
                                });
                            });
                        }
                        else if((urlreq.indexOf("/prodotti/"+complementiClass)>0)){
                            $("#block-menu-primary-links .menu .expanded."+prodottiClass).each(function(){
                                $(this).find("ul.menu li").each(function(){
                                    if(!$(this).hasClass(complementiClass)){
                                        $(this).removeClass("expanded").addClass("collapsed");
                                        $(this).find("li").hide();
                                    }
                                });
                            });
                        }
                        else{
                            $("#block-menu-primary-links .menu .expanded."+prodottiClass+" .expanded").removeClass("expanded").addClass("collapsed");
                            $("#block-menu-primary-links .menu .expanded."+prodottiClass+" .collapsed .menu").hide();
                        }
                        //tolgo il rosso a prodotti
                        /*$("#block-menu-primary-links .menu .expanded."+prodottiClass+" a:first").css({
                            backgroundImage:"none",
                            backgroundColor: "#9D8D85"
                        });*/
                        //metto il rosso a chi lo deve avere
                        $("#block-menu-primary-links .menu .expanded."+prodottiClass+" .expanded a:first").addClass("active");
                    }
                    else if((urlreq.indexOf("/prodotto")>0)){
                        $("#block-menu-primary-links .menu .expanded."+prodottiClass+" ul.menu li").each(function(){
                            if(!$(this).hasClass("active-trail")){
                                $(this).removeClass("expanded").addClass("collapsed");
                                $(this).find("ul.menu").hide();
                            }
                        });
                    }
                    else{
                        $("#block-menu-primary-links .menu .expanded."+prodottiClass).removeClass("expanded").addClass("collapsed");
                        $("#block-menu-primary-links .menu .collapsed."+prodottiClass+" ul.menu").hide();
                    }
                    var menuitem = $("#block-menu-primary-links .menu .expanded."+chisiamoClass);
                    if(!$(menuitem).hasClass("active-trail")){
                        $("#block-menu-primary-links .menu .expanded."+chisiamoClass).removeClass("expanded").addClass("collapsed");
                        $("#block-menu-primary-links .menu .collapsed."+chisiamoClass+" ul.menu").hide();
                        $($("#block-menu-primary-links .menu .collapsed."+chisiamoClass+" a")).click(function(event){
                            if($(this).parent().hasClass("collapsed")){
                                $(this).parent().addClass("expanded").removeClass("collapsed");
                                $(this).parent().find("ul.menu").slideDown();
                                if(!$(this).parent().hasClass("leaf")){
                                    event.preventDefault();
                                }
                            }
                            else{
                                if(!$(this).parent().hasClass("leaf")){
                                    event.preventDefault();
                                }
                                $(this).parent().addClass("collapsed").removeClass("expanded");
                                $(this).parent().find("ul.menu").slideUp();
                            }
                        });
                    }
                    else{
                        $("#block-menu-primary-links .menu ."+chisiamoClass+" a:first").click(function(event){
                            event.preventDefault();
                            if($(this).parent().hasClass("expanded")){
                                $(this).parent().addClass("collapsed").removeClass("expanded");
                                $(this).parent().find("ul.menu").slideUp();
                            }else{
                                $(this).parent().addClass("expanded").removeClass("collapsed");
                                $(this).parent().find("ul.menu").slideDown();
                            }
                        });
                    }
                    //tolgo il rosso ai menu che hanno active tra i figli
                    $("#block-menu-primary-links .menu .expanded").each(function(){
                        if($(this).find("li a.active").size()>0){
                            $(this).find("a:first").css({
                                backgroundImage:"none",
                                backgroundColor: "#9D8D85",
                                color: "#fff"
                            });
                        }
                    });
                    /*opens up login form*/
                    loginPosition();                
                    var opened = false;
                    $("#block-user-0 h3").click(function(){
                        if(opened==false)
                            opened = loginOpen();
                        else
                            opened = loginClose(true);
                    });
                    $(window).resize(function(){
                        opened = loginPosition();
                    });
                    /*posiziona il footer*/
                    footerPosition();
                    $(window).resize(function(){
                        footerPosition();
                    });
                    $("#block-rossifelice-3 a").attr("target","_blank");
                }
function footerPosition(){
    var top =$("#grid").height();
    if(top == null){
        top = $("#main").height();
    }

    top=top+40;
    $("#footer").attr("style", "top: "+top+"px")
}
function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
function loginPosition(){
    loginClose();
    var top = $(window).height();
    $("#block-user-0").css("top", top);
    if($(window).width()<1100){
        $("#block-user-0").css({
            "left":"0",
            "marginLeft":"0"
        });
        if($.browser.msie){
            $("#block-user-0").css({
                "left":"0",
                "marginLeft":"0",
                "top":top-17
            });
        }
    }
    else{
        $("#block-user-0").attr("style"," ");
        if($.browser.msie){
            $("#block-user-0").css("top", top);
        }
    }
    return false;
}
function loginOpen(){
    var top = $(window).height()+15;
    $("#user-login-form").slideDown();
    if($.browser.webkit){
        var percent = ((top-197)*100)/top;
        $("#block-user-0").animate({
            "top":percent+'%'
        });
    }
    else{
        $("#block-user-0").animate({
            "top":top-197
        });
    }
    $("#block-user-0 h3").css("background-position", "right -20px");
    return true;
}
function loginClose(animate){

    var top = $(window).height();
    $("#user-login-form").slideUp();
    if(animate==null){
        $("#block-user-0").css("top",top);
    }
    else{
        if($.browser.webkit){
            $("#block-user-0").animate({
                "top":100+'%'
            });
        }else{
            $("#block-user-0").animate({"top":top});
        }
    }
    $("#block-user-0 h3").css("background-position", "right 0");
    return false;
}

