//jQuery OpenID Plugin 1.1 Copyright 2009 Jarrett Vance http://jvance.com/pages/jQueryOpenIdPlugin.xhtml
//Modified by GL 6/17/2009
/*
  - Add a buffer to save the pattern for sever side code handling
  - Modify clicking the direct class li will not submit the page 
*/
$.fn.openid = function() {

    var $this = $(this);
    var $usr = $this.find('input.openid_username');
    var $id = $this.find('input.openid_identifier');
    var $front = $this.find('div:has(input.openid_username)>span:eq(0)');
    var $end = $this.find('div:has(input.openid_username)>span:eq(1)');
    var $usrfs = $this.find('fieldset:has(input.openid_username)');
    var $idfs = $this.find('fieldset:has(input.openid_identifier)');
    var $buff = $this.find('input.openid_providerpattern');

    var submitusr = function() {
        if ($usr.val().length < 1) {
            $usr.focus();
            return false;
        }
        $id.val($front.text() + $usr.val() + $end.text());
        return true;
    };

    var submitid = function() {
        if ($id.val().length < 1) {
            $id.focus();
            return false;
        }
        return true;

    };
    var direct = function() {
        $usrfs.css('display', 'none');
        $idfs.css('display', 'none');

        $('#openid_error_label').hide();
        ResizeOpenIdPopUpBox('small');

        var $li = $(this);

        $($('.openIdProvider')[0]).val($li.attr('name'));

        $li.parent().find('li').removeClass('highlight');
        $li.addClass('highlight');

        return true;
    };

    var openid = function() {
        $('#openid_error_label').hide();
        ResizeOpenIdPopUpBox('small');
        $('#LeaveConfirmationOpenId').css('display', 'none');

        var $li = $(this);

        $('.openIdProvider').val($li.attr('name'));

        $li.parent().find('li').removeClass('highlight');
        $li.addClass('highlight');
        $usrfs.hide();
        $idfs.show();
        $id.focus();
        $this.unbind('submit').submit(submitid);

        return false;
    };

    var username = function() {
        $('#openid_error_label').hide();
        ResizeOpenIdPopUpBox('small');
        $('#LeaveConfirmationOpenId').css('display', 'none');

        var $li = $(this);

        $('.openIdProvider').val($li.attr('name'));

        $li.parent().find('li').removeClass('highlight');
        $li.addClass('highlight');
        $idfs.hide();
        $usrfs.show();
        $this.find('label[for=openid_username] span').text($li.attr("title"));
        $front.text($li.find("span").text().split("username")[0]);
        $end.text("").text($li.find("span").text().split("username")[1]);
        $buff.val($front.text() + "{0}" + $end.text());
        $usr.focus();
        $this.unbind('submit').submit(submitusr);
        return false;
    };

    $this.find('li.direct').click(direct);
    $this.find('li.openid').click(openid);
    $this.find('li.username').click(username);
    $id.keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            return submitid();
        }
    });
    $usr.keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            return submitusr();
        }
    });
    $this.find('li span').hide();
    $this.find('li').css('line-height', 0).css('cursor', 'pointer');
    $this.find('li:eq(0)').click();
    return this;
};
