var $j = jQuery.noConflict();

var login_ErrorTemplate = $j.template('<div class="error" id="error_${Id}">' +
                                            '<li>${Text}</li>' +
                                          '</div>');
$j(document).ready(function() {


    $j('#frmReg').submit(function() {
		_handle = $j('INPUT[@name="handle"]').val();
		if (_handle!=null && _handle.length < 5){ 
			var msg=new Object; 
			msg.Id=11111; 
			msg.Text="Please enter your Username.";
			$j('#errorWrapper').show();
			//See if error has already been added
			var err = $j('#error_' + msg.Id);
			if (err.length == 0) {$j('#errorWrapper').append(login_ErrorTemplate, msg);}
			else err.show();
			return false;
		}

        var path = '/api/Registration.svc/UsrFormReg?apikey=0566e181-5b76-44e7-a41d-e556635d5dee';
        if ($j.query.toString().length > 0) { path = path + $j.query.toString().replace('?', '&') }
        $j(this).ajaxSubmit({ url: path, dataType: "json", success: reg_showResponse });
        return false;
    });

    $j('#jqmPopup').jqm({ ajax: 'CPLanding/CPPageView.aspx?tempf=facebook/popups/LoginNotice.html&cpr=true', modal: true, toTop: true });
    $j('#jqmPopupReg').jqm({ ajax: 'CPLanding/CPPageView.aspx?tempf=facebook/FBRegLogin.html&cpr=true', toTop: true, modal: true });
    { $j('.fbConnectHelp').cluetip({ positionBy: 'bottomTop', tracking: true, width: 230, cluezIndex: 3500, dropShadow: false, local: true, hideLocal: true, showTitle: false }); }

    $j('.error').hide();
    $j('#errorWrapper').hide();
    $j('#dialog').jqm();

    $j(document).pngFix();
});

var user_logged_intoFB = true;
var app_is_authorized = false;
    
function FBConnect() {
    if (app_is_authorized) {fblogIn(); }
    if (!app_is_authorized) {
        $j('#jqmPopup').jqmShow(); FB.Connect.requireSession(function() {
                                                                $j('#jqmPopup').jqmHide();
                                                                fblogIn();
                                                            }); 
    }
    
}
function reg_showResponse(responseText, statusText) {
    if (responseText.PageRedirect != null && responseText.Success == 1)
        window.location.href = responseText.PageRedirect;

    $j('#errorWrapper').hide();
    $j('.error').hide();
    $j.each(responseText.Messages, function(i, msg) {
        $j('#errorWrapper').show();
        //See if error has already been added
        var err = $j('#error_' + msg.Id);
        if (err.length == 0) {$j('#errorWrapper').append(login_ErrorTemplate, msg);}
        else err.show();
    });

}
function fblogIn() {
    $j.ajax({ type: "GET",
        url: "/api/Login.svc/fblogin?apikey=6c85693e4698acb1745f769bef8a3e01",
        cache: false, dataType: "json",
        success: fblogin_showResponse
    });
}

function fblogin_showResponse(responseText, statusText) {
    if (responseText.PageRedirect != null && responseText.Success == 1) {
        window.location.href = responseText.PageRedirect;
    }
    else {
        $j.each(responseText.Messages, function(i, msg) {
            if (msg.Id == "1") {
                $j('#jqmPopupReg').jqmShow();
            }
        });
    }
}

function indx_onFBConnected(user_id) {
    app_is_authorized = true;
    
}

function indx_onFBNotConnected() {
    FB.ensureInit(function() {
        FB.Connect.get_status().waitUntilReady(function(status) {
            if (status == FB.ConnectState.userNotLoggedIn) {user_logged_intoFB = false;}
            if (status == FB.ConnectState.appNotAuthorized) { app_is_authorized = false; }
        });
    });

} 


