//Gestine PNG con JS
//Attivazione CufOn
//OnFocus input

$(function(){
    
    $("#subnav li").hover(function(){
        $(this).toggleClass("hover");
    }, function(){
        $(this).toggleClass("hover");
    });
    $(".pagin li:last").addClass("last");
    $(".delme").focus(function(){
        if ($(this).is('input')) {
            var orival = $(this).val();
            $(this).val("");
        }else{
            var orival = $(this).html();
            $(this).html("");
        }

        $(this).blur(function(){
            if($(this).is('input')){
                if ($(this).val() != "" && $(this).val() != orival) {
                    $(this).removeClass('delme').unbind('blur').unbind('focus');
                }else{
                    $(this).val(orival);
                }
            }else{
                if ($(this).html() != "" && $(this).html() != orival) {
                    $(this).removeClass('delme').unbind('blur').unbind('focus');
                }else if ($.browser.safari && $(this).val() != "" && $(this).val() != orival) {
                    $(this).removeClass('delme').unbind('blur').unbind('focus');
                }else{
                    $(this).html(orival);
                }
            }

        });
    });

    $(".categories li a:not(.children li a)").click(function(){
        //$(this).parent().toggleClass("open");
        var ul = $(this).siblings("ul");
        var time = ul.children().size();
        if(time < 2){
            time = 150;
        }else if(time < 3){
            time = 300;
        }else if(time < 6){
            time = 600;
        }else{
            time = 1000;
        }
        ul.slideToggle(time);
        return false;
    });
    $(".tab.menu a").click(function(){
        $(".tab.menu a").removeClass("active");
        var rel = $(this).addClass("active").attr("rel");
        $(".the-most-read-comment > div:not(.tab)").removeClass("active");
        $(".the-most-read-comment ."+rel).addClass("active");
    });
    $.wdbox.settings.window_title = "";
    $.wdbox.settings.window_interaction = "<a class = 'closeLink'><span>&otimes;</span> chiudi</a>";
    $("form[name=newsletterform]").submit(function(){
        $.post($.template_url+"/parts/process-email.php", {from : $(this).find("input[name=from]").val()}, function(res){
            $(".message p").html(res);
            $(".message").css({backgroundColor : '#fff'}).slideDown(500).animate({backgroundColor : '#FFFFE0'}, 500);
            $(".message-close").click(function(){
                $(".message").slideUp(500);
            });
        });
        
        return false;
    });
    $(".ajaxPicture a, .ajaxPicture-caption .caption, .page .post .entry a:has(img)").live("click", function(){
        var href = $(this).attr("href");
        var alertContent = "<img class='picf' src='"+href+"'/><div class='wdbox-caption'>"+$(this).attr("title")+"</div>";
        var img = false;
        if(img = $(this).find('img')){
            var linkText = "Leggi l'articolo completo";
            var linkToPost;
            if(linkToPost = img.siblings(".linktopostholder").val()){
                alertContent+="<div class='link-to-post'><a href=\""+linkToPost+"\">";
                var title;
                if(title = img.attr("alt")){
                    linkText+=" \""+title+"\"";
                }
                alertContent+= linkText+"</a></div>";
            }
        }
        $.wdbox.alert(alertContent);

        $(".wdbox .picf").load(function(){
            var picw = $(".wdbox .picf").width();
            $(".wdbox .wdbox_content").width(picw);
            $.wdbox.centerBox({
                top : 20
            });
        });
        return false;
    });
    if($.browser.version < 7){
        var clear = $.template_url+"/images/clear.gif"; //path to clear.gif
        //alert(clear);
        pngfix();
    }

});