function deleteNode( nodeName ) {
  targetElement = document.getElementById(nodeName);
  if( targetElement == null ) {
    return false;
  }
  else {
    removeChildren( targetElement );

    return true;
  }
}

function removeChildren( parentNode ) {
    if( !parentNode || !parentNode.childNodes ) {
      return false;
    }

    nodeList = parentNode.childNodes;   
    node = nodeList[0];
    while( node != null ) {
      parentNode.removeChild(node);
      node = nodeList[0];
    }
}


function TB_Callback(){
    try{
//        tb_remove();
//        tb_show(null, '/images/loadingAnimation.gif', false);
        if (typeof(TB_CbFn) == 'function') TB_CbFn();
    } catch(e) {}
}

var TB_CbFn = null;
//set handlers for secured links and forms
jQuery(function($) {

    //focus in username
    var sn = function(){
        var u = $('#username')
        if (u.is(':visible')) {
            clearInterval(snid);
            u.focus();
        }
    }
    var snid = window.setInterval(sn, 200);
    
    $('.secured').each(function() {
        var obj = $(this);

        if ($(this).is('form')) {
            $(this).submit(function() {
                set_callback_submit(this);
                show_login_form();
                return false;
            })
        }

        if ($(this).is('a')) {
            $(this).removeAttr('onclick').click(function() {
                if (this.href.indexOf('/login/') == -1) 
                    set_callback_click(this)
                else
                    default_callback(false);
                show_login_form();
                return false;
            })
        }
    })
    
    var show_login_form = function() {
        tb_show(null,'/login/?iframe=1&TB_iframe=true&height=400&width=475',false);
    }
    var set_callback_click = function(link) {
        TB_CbFn = function() {
            window.location.href = link.href;
        }
    }
    var set_callback_submit = function(form) {
        TB_CbFn = function() {
            $(form).unbind('submit').submit();
        }
    }
    var default_callback = function(withmsg) {
        if (typeof(withmsg) == 'undefined') withmsg = false;
        TB_CbFn = function(){
            if(withmsg) {
                $('.login_box').html('<div class="h2">Login</div><p>Login successful, wait for redirect...</p>');
            } else { //spinner
                $('#sf_guard_auth_form').css({'background':'transparent url(/images/icons/spinner.gif) center no-repeat'});
            }
            window.location.reload();
        };
    }
    default_callback(window == top.window); //message only when not in iframe
   
})
function show_f(id) {
    jQuery('#'+id).toggle();
    return false;
}

function fb_logout(link){
    if(typeof(FB) != 'undefined' && FB.Connect.get_status().result == FB.ConnectState.connected) {
        var href = (typeof(link) == 'object') ? link.href : link;
        FB.ensureInit( function() {FB.Connect.logoutAndRedirect(href);} );
        return false;
    }
    return true;
}

function fb_login(){
    FB.ensureInit( function() {FB.Connect.requireSession(TB_Callback);} );
    return false;
}
