﻿/// <reference path="jquery-1.6.2-vsdoc.js" />
/// <reference path="modernizr-2.0.6-development-only.js" />

this.screenshotPreview = function () {
    /* CONFIG */

    xOffset = 0;
    yOffset = 15;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    /* END CONFIG */
    $("a.screenshot").hover(function (e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        var tmpUrl = $("img", this).attr("src");
        var fullUrl = this.rel;

        $("body").append("<p id='screenshot'><img width='300px' src='" + tmpUrl + "' alt='url preview' />" + c + "</p>");

        $(new Image())
            .load(function () {
                $("#screenshot img").attr("src", fullUrl);
            })
            .attr("src", fullUrl);

        $("#screenshot")
            .css("top", (e.pageY - xOffset) + "px")
            .css("left", (e.pageX + yOffset) + "px")
            .fadeIn("fast");
    },
    function () {
        this.title = this.t;
        $("#screenshot").remove();
    });
    $("a.screenshot").mousemove(function (e) {
        $("#screenshot")
            .css("top", (e.pageY - xOffset) + "px")
            .css("left", (e.pageX + yOffset) + "px");
    });
};

$(function () {
    if (!Modernizr.input.placeholder) {
        $('[placeholder]').focus(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur().parents('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });
    }

    $(document).bind("click", function (e) {
        var $clicked = $(e.target);
        if (!($clicked.is(".drop_box") || $clicked.parents().is(".drop_box"))) {
            $(".drop_box_content").hide();
        } else {
            var $content = $(".drop_box_content", $clicked.closest(".drop_box"));
            if ($content.is(":hidden")) {
                $($content).show();
            } else if ($clicked.is(".drop_box") || (!$clicked.parents().is(".drop_box") && $clicked.is(":not(.drop_box_content)"))) {
                $content.hide();
            }
        }
    });

    /*
    * Url preview script 
    * powered by jQuery (http://www.jquery.com)
    * 
    * written by Alen Grakalic (http://cssglobe.com)
    * 
    * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
    *
    */
    screenshotPreview();
});
