$().ready(function(){


    // adding effects to search buttons
    $("#skattesubmit, #search .button").hover(function(){
        $(this).attr("src", "/sites/all/themes/abcnyheter/img/button_search_hover.gif");
    }, function(){
        $(this).attr("src", "/sites/all/themes/abcnyheter/img/button_search.gif");
    });
    $("#skattesubmit, #search .button").mousedown(function(e){
        e.preventDefault();
        $(this).attr("src", "/sites/all/themes/abcnyheter/img/button_search_press.gif");
    });


    // change form text and so on
    var taxstring = "Søk i skattelistene";
    var abcstring = "Søk i ABC Nyheter";

    $("#skattetext, #q").focus(function(){
        if (($(this).val() == abcstring) || ($(this).val() == taxstring)) {
            $(this).addClass('focused').val('');
        }
    }).blur(function(){
        if ($(this).val() == "") {
            $(this).removeClass('focused');
            switch ($(this).attr("id")) {
                case "q":
                    $(this).val(abcstring);
                    break;
                case "skattetext":
                    $(this).val(taxstring);
                    break;
            }
        }
    });
    
    // i want to break long titles, but stop after 5px reduction
    /*
    $(".field_forside_tittel a").each(function(){
        var parent = this.parentNode, i=0;
        while((this.offsetWidth > parent.offsetWidth) && i<5) {
            var font_size = parseInt($(this).css("font-size"));
            $(this).css("font-size", (font_size - 1) + "px")
            i++;
        }
    });
    */
});

;// markup for toggling labels
var commentsVisible,
commentsCollapsed,
commentsToggle = $("<p id='commentsVisibleToggle' class='comment_show-hide'></p>"),
expandCollapseAll = $("<p id='expandCollapseAll' class='comment_show-hide'></p>").click(function(){
    $(this).toggleVisibility("expand_collapse_all");
}),
prCommentToggle = $("<p class='prCommentToggle comment_show-hide'></p>").click(function(){
    $(this).toggleVisibility('comment');
});

$().ready(function(){
    
    // adding print even to print button
    $(".print-page").each(function(){
        $(this).click(function(e){
            e.preventDefault();
            window.print();
        });
    });
    
    // get current comment statuses
    if(document.getElementById("comments") && commentScript) {
        var hasComments = true,
        comments = $("#comments");

        if(window.commentsHidden) {
            comments.addClass("comments_hidden");
        }
        
        (window.commentsHidden == undefined || window.commentsHidden) ? commentsVisible = false : commentsVisible = true;
        ($(".comment:first").hasClass("comment_collapsed")) ? commentsCollapsed = true : commentsCollapsed = false;

        if(commentsCollapsed){
             comments.addClass("comments_collapsed");
        }

        // set up the comments toggle
        
        if(commentsCollapsed)
            comments.children("h3:first").after(expandCollapseAll
            .text(commentsExpandText));
        else
            comments.children("h3:first").after(expandCollapseAll
            .text(commentsCollapseText));
        
        if(commentsVisible)
            comments.children("h3:first").after(commentsToggle
            .text(commentsHideText));
        else
            comments.children("h3:first").after(commentsToggle
            .text(commentsShowText));
            
        commentsToggle.click(function(){
            $(this).toggleVisibility('all');
        });
        comments.find(".comment").each(function(){
            $(this).prepend(prCommentToggle.clone(true).text((commentsCollapsed) ? commentShowText : commentHideText));
        });
        
        // do stuff when we're scrolling to a comment
        if ((window.location.hash.search(/comment/) != -1) && !commentsVisible) $(commentsToggle).click();
        
        // when clicking on a "latest 5 comments" comment, expand fs.
        var last5 = $(".latest_comments:first");
        if(last5) {
            last5.find("a").click(function(){
                if(!commentsVisible) {
                    $(commentsToggle).click()
                }
            })
        }
    }
    
    /* add fixgap hack to all right aligned images */
    $(".image_align_right").each(function(){
        $(this).before("<div class='fixgap'><!-- --></div>");
    });

    /* add a width on left aligned images */
    $(".caption").each(function(){
        var captionedImage = $(this).find("img").get(0); 
        if(captionedImage){
            var width = captionedImage.clientWidth;
            if (width == 0) {
                // should something fail, never set width to 0
                return;
            }
            $(this).css("width", width + "px");
        }
    });

    $('a.comment_link').click(function(){
        var what = $("#comments");
        if (what.hasClass('comments_hidden')) {
            what.removeClass("comments_hidden").find(".comments").removeClass("comments_collapsed");
            commentsToggle.text(commentsHideText);
        }
    });
    
});

jQuery.fn.extend({
    toggleVisibility: function(scope){
        // all is the whole field, comment is pr comment, and expand_collapse_all
        return $(this).each(function(){
            if(scope == 'all') {
                var what = $(this).parent();
                if(what.hasClass("comments_hidden")) {
                    what.removeClass("comments_hidden").find(".comments").removeClass("comments_collapsed");
                    commentsToggle.text(commentsHideText);
                }
                else {
                    what.addClass("comments_hidden").find(".comments");
                    commentsToggle.text(commentsShowText);
                }
            }
            else if(scope == 'comment') {
                var what = $(this).parent();
                if(what.hasClass("comment_collapsed")) {
                    what.removeClass("comment_collapsed");
                    what.find(".prCommentToggle").text(commentHideText);
                }
                else {
                    what.addClass("comment_collapsed");
                    what.find(".prCommentToggle").text(commentShowText);
                }
            }
            else if(scope == 'expand_collapse_all') {
                if($(this).text() == commentsCollapseText) {
                    $("#comments").addClass("comments_collapsed").find(".comment").addClass("comment_collapsed").find(".prCommentToggle").text(commentShowText);
                    $(this).text(commentsExpandText);
                }
                else {
                    $("#comments").removeClass("comments_collapsed").find(".comment").removeClass("comment_collapsed").find(".prCommentToggle").text(commentHideText);
                    $(this).text(commentsCollapseText);
                }
            }
        });
    }
});

/* Push megaboard down below the right column
 * 
 */

function findPos(obj){
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
        return [curleft, curtop];
    }
}

$().ready(function(){
    var d = document, megaBoard = $(".ad_megaboard:first"), rightCol = $("#sidebar-right");
    if(!megaBoard) return;
    var megaBoardPosTop = findPos(megaBoard.get(0))[1], rightColBottom = findPos(rightCol.get(0))[1] + rightCol.get(0).clientHeight;
    
    if (megaBoardPosTop < rightColBottom) {
        var heightDiff = rightColBottom - megaBoardPosTop;
        megaBoard.css("margin-top", (heightDiff + 10) + "px");
    }
});

;