var compareVal;
var clip = null;

$(document).ready(function() {
    if ($('.guideline-item').length > 0) {
        $('body').addClass('guideline');
    }
    /*lightbox *******/
    $(function() {
        $('.lightboximage a').lightBox({fixedNavigation:false});
        $('.guideline-item').each(function() {
            $(this).find('.col-left a').lightBox({fixedNavigation:true});
        });
        if ($('.screenshotsidebar .img-holder a').length > 0) {
            $('.screenshotsidebar .img-holder a').lightBox({fixedNavigation:false});
        }
    });
    /*scroller *******/
    $(function() {
        $('#scroller').loopedSlider();
    });
    /* gelijke hoogtes screenshots ****/
    equalHeight();
    /* navigatie paginas, styling ****/
    $(function() {
        $(".navigator span[title='Go to first page'],.navigator a[title='Go to last page'], .navigator a[title='Go to first page'], .navigator span[title='Go to last page']").hide();
        $(".navigator a[title='Go to previous page']").addClass("previous");
        $(".navigator span[title='Go to previous page']").addClass("disabeled-start");
        $(".navigator a[title='Go to next page']").addClass("next");
        $(".navigator span[title='Go to next page']").addClass("disabeled-end");
    });

    /* status yes/no toggle & status not available switch */
    $(function() {
        $('.status span a').each(function() {
            $(this).click(function() {
                $(this).parent().find('.on').removeClass('on').addClass('noton');
                $(this).removeClass('noton').addClass('on');
            });
        });
        /* status not available switch */
        $('.checklink').each(function() {
            $(this).click(function() {
                $(this).toggleClass('is-checked');
            });
        });
    });
    /* funky admin hover styles */
    $(function() {
        $('.showactive').mouseenter(function() {
            $(this).css("background-color", "#fefedc");
        });
        $('.showactive').mouseleave(function() {
            $(this).css("background-color", "#F3F3F3");
        });
    });
    /* select url on focus */
    $(".share input,.share2 input").focus(function() {
        /* Select input field contents */
        this.select();
    });

    if ($('.screen-checklist').length > 0) {
        prepLayout();
    }

    /* set the height of the overlayer */
    if ($('.screen-checklist .na').length > 0) {
        setOverLayHeight($(this));
    }
    if ($('.checklink').length > 0) {
        $('.checklink').live('click', function() {
            $(this).parents('.results-block .float-stop').addClass('na');
            setOverLayHeight();
        });
    }

    matchCols.init('.equalize h3');

    if ($('#project-overview .myprojects .editable').length > 0) {
        $('.editable').bind('mouseenter', function() {
            $(this).parents('.project-item').find('.editable').addClass('editable-hover');
            $(this).parents('.project-item').find('.editable').append('<span class="edit-icon"></span>');
        });
        $('.editable').bind('mouseleave click', function() {
            $(this).parents('.project-item').find('.editable').removeClass('editable-hover');
            $('span.edit-icon').remove();
        });
    }
    /* toggle design patterns / best practices */
    if ($('.guideline-item').length > 0) {
        $('.guideline-content').hide();
        $('.guideline-item .toggleBlock').live('click', function() {
            $(this).parents('.guideline-item').find('.guideline-content').toggle();
            if ($(this).text() == 'Show More') {
                $(this).text('Show Less');
            } else {
                $(this).text('Show More');
            }
            return false;
        });
    }

    appendClearAfterLastOnRow();

    initClip();
});

function appendClearAfterLastOnRow() {
    if ($('.lastonrow').length > 0) {
        $('.lastonrow').after('<div class="clear"></div>')
    }
}

function prepLayout() {
    $('.screen-checklist').find('div.blocks-item:first').addClass('first');
    $('.screen-checklist').find('div.blocks-item:last').addClass('last');
}

function setOverLayHeight() {
    $('.na').each(function() {
        $(this).find('.is-checked').addClass('on');
        var wrapHeight = $(this).height();
        $(this).find('.na-overlay').css('height', wrapHeight + 'px');
        $(this).find('.is-checked').addClass('on');
    });
}

function removeMarginRight() {
    /* remove margin-left on 4th, 8th, 12th ..etc */
    var items = $('.project-item').length;
    for (var i = 3; i < items; i = i + 4) {
        $('.project-item:eq(' + i + ')').css("margin-right", "0");
    }
}

function equalHeight() {
    $('.equal').equalHeight();
}

function fixScreenshotListLayout() {
    matchCols.init('.equalize h3');
    equalHeight();
}

var matchCols = {
    init: function(matchClass) {
        /* reset the start compare value */
        var compareVal = 0;
        /* get the heightest value */
        $(matchClass).each(function() {
            if ($(this).height() > compareVal) {
                compareVal = $(this).height();
            }
        });
        /* set the heightest value */
        $(matchClass).each(function() {
            $(this).parent().height(compareVal);
        });
    }
}

/* callback function for annotation tool to go to the previous page */
function goBack() {
    history.go(-1);
}

/* called by the framework if screenshot modal window is closed */
function afterScreenshotUploadCallback() {
    fixScreenshotListLayout();
    appendClearAfterLastOnRow();
}

function initClip() {
    ZeroClipboard.setMoviePath(contextPath + '/js/ZeroClipboard.swf');

    $('.share-button').mouseover(function() {
        clip = new ZeroClipboard.Client();
        clip.setHandCursor(true);
        /* get the text that needs to be copied */
        var clipText = $(this).parent().find('input[type=text]').val();
        /* set the clipboard text */
        clip.setText(clipText);
        /* reposition the flash movie */
        if (clip.div) {
            clip.receiveEvent('mouseout', null);
            clip.reposition(this);
        } else clip.glue(this);
        clip.addEventListener('complete', function(client, text) {
            alert("Copied text to clipboard: " + text);
        });
    });
}

