$(function(){

    $(".open-popin").click(function(){
        var target = $(this).attr("href");
        if($(target).is(':visible')){
            $(".popin").hide();
        }else{
            $(".popin").hide();
            $(target).show();
        }
        return false;
    });
    
    $(document).click(function(e) {
        if(!$(e.target).parents(".popin").length) {
            $(".popin").hide();
        }
    });
    
    $(".change-account").click(function() {
        $(".user-screen").hide();
        $(".user-screen.login").show();
    });
    $(".signup-button").click(function() {
        $(".user-screen").hide();
        $(".user-screen.signup").show();
    });
    $(".login-form, .signup-form").submit(function() {
        $(".user").hide();
        $(".user-screen").hide();
        $(".user-screen.info").show();
    });

});

// Social Plug-in

(function() {

var uid = 0;

window.social = function(opts) {
    
    var opts = opts || {};
    opts.sel = opts.sel || $(".Ldt-Social");
    opts.url = opts.url || document.location.href;
    opts.text = opts.text || $("title").text();
    
    if (!opts.sel.length) {
        return;
    }
    
    opts.sel.addClass("Ldt-Social");
    
    if (opts.swf) {
        ZeroClipboard.setMoviePath(opts.swf);
    }
    
    var clipId = "Social-Clip-" + (++uid),
        clip,
        html = '<div class="Ldt-Social-Url-Container"><a href="#" target="_blank" class="Ldt-Social-Square Ldt-Social-Url" title="'
            + gettext("Share URL")
            + '"></a><span class="Ldt-Social-UrlPop"><input class="Ldt-Social-Input"/><div class="Ldt-Social-CopyBtn" id="'
            + clipId
            + '">'
            + gettext("Copy")
            + '</div></span></div><a href="http://www.facebook.com/share.php?'
            + $.param({ u: opts.url, t: opts.text })
            + '" target="_blank" class="Ldt-Social-Fb Ldt-Social-Ext" title="'
            + gettext("Share on Facebook")
            + '"></a><a href="https://twitter.com/intent/tweet?'
            + $.param({ url: opts.url, text: opts.text })
            + '" target="_blank" class="Ldt-Social-Twitter Ldt-Social-Ext" title="'
            + gettext("Share on Twitter")
            + '"></a><a href="https://plus.google.com/share?'
            + $.param({ url: opts.url, title: opts.text })
            + '" target="_blank" class="Ldt-Social-Gplus Ldt-Social-Ext" title="'
            + gettext("Share on Google+")
            + '"></a><a href="mailto:?'
            + $.param({ subject: opts.text, body: opts.text + ": " + opts.url })
            + '" target="_blank" class="Ldt-Social-Mail" title="'
            + gettext("Share via E-Mail")
            + '"></a></div>';
            
    opts.sel.html(html);

    opts.sel.find(".Ldt-Social-Url").click(function() {
        var _pop = opts.sel.find(".Ldt-Social-UrlPop");
        _pop.toggle();
        if (_pop.is(":visible")) {
            if (typeof clip == "undefined") {
                clip = new ZeroClipboard.Client();
                clip.setHandCursor( true );
                clip.glue(clipId);
                
                clip.addEventListener( 'onMouseUp', function() {
                    _pop.hide();
                    clip.hide();
                });
            }
            clip.show();
            clip.setText( opts.url );
            opts.sel.find(".Ldt-Social-Input").val(opts.url).focus();
        } else {
            clip.hide();
        }
        return false;
    });
    opts.sel.find(".Ldt-Social-Input").focus(function() {
        this.select();
    });
    opts.sel.find(".Ldt-Social-Ext").click(function() {
        window.open(
            opts.sel.find(this).attr("href"),
            "_blank",
            "height=300,width=450,left=100,top=100,toolbar=0,menubar=0,status=0,location=0");
        return false;
    });
};
}());