﻿(function($){
$(function() {
    var loginPopup = function(from, fromUrl, stayInBox) {
        var href = $("a#myAccount[href*=Login]").attr('href');
        if( from ) {
            href += '?from=' + from;
        } else {
            href += '?fromUrl=' + fromUrl;
        }
        if( stayInBox ) {
            href += "&amp;stayInBox=1";
        }
        $("a#myAccount[href*=Login]").attr('href', href );
        $("a#myAccount[href*=Login]").trigger('click');
    };

    if(!$("a#myAccount[href*=Login]").length)
        return;
    
    // unbind other click handlers, such as fancybox (rather than living with them peacefully).
    $('.need-login').unbind('click').click(function(e) {
        var stayInBox = $(this).hasClass('login-fbox');
        loginPopup(null, $(this).attr('href'), stayInBox);
        return false;
    });
    
})
})(jQuery);

