// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function window_load() {

    try{
        var el = $(Cookie.get('menu'));
        if(el){
            tabdropdown.init('bluemenu');
            el.addClassName('selected');
            el.addClassName('default');
        }else{
            tabdropdown.init('bluemenu', 0);            
        }
    }catch(e){
        alert(e.message);
    }
}
 
/*
 *  Uses the scriptaculous Builder object to create a login form which is then shown using
 *  the Modalbox library.  Three observers are set on the form:
 *      login focus - removes error message and resizes modal form
 *      password focus - removes error message and resizes modal form
*/
function openLogin(){
    try{
        var node = Builder.node("div", {className: "login_popup"}, [
            Builder.node( "p",{id: "errmsg", className: "error", style: "display: none"}, "Unable to log you in with the credentials supplied!"),
            Builder.node("form", { id: "login_form" } ),
            Builder.node("p", "Username"),
            Builder.node("input", {type: "text", id: "login", name: "login"}, ""),
            Builder.node("p", " Password "),
            Builder.node("input", {type: "password", id: "password", name: "remember"}, ""),
            Builder.node("br"),
            Builder.node("br"),
            Builder.node("span", "Remember Me    "),
            Builder.node("input", {type: "checkbox", id: "remember", name: "remember"}, ""),
            Builder.node("br"),
            Builder.node("br"),
            Builder.node("input", {type: "hidden", name: "authenticity_token", id: "authenticity_token", value: "'" + authenticity_token + "'" }, "" ),
            Builder.node("input", {type: "button", value: "Submit", id: "submitButton", onclick: "submitButtonClick()"}, "")
        ]);
        Modalbox.show(node, {title: "Log In", width: 300,
                      afterLoad: setObservers, onHide: removeObservers });
    }catch(e){
        alert (e.message);
    }
}

/*
 *   Sets up observers on the modal form
 */
function setObservers (){
    //alert("settng observers");
    try{
        //$('submitButton').observe('click', submitButtonClick );
        $('login').observe( 'focus', removeErrorOnFocus );
        $('password').observe( 'focus', removeErrorOnFocus );
    }catch(e){
        alert(e.message);
    }
}

/*
 *   Removes observers on the modal form
 */
function removeObservers (){
    $('login').stopObserving( 'focus', removeErrorOnFocus );
    $('password').stopObserving( 'focus', removeErrorOnFocus );
}

function removeErrorOnFocus(){
    if($('errmsg').visible()) {
        $('errmsg').hide();
        Modalbox.resizeToContent();
    }
}

function submitButtonClick(){
    new Ajax.Request('/session',
      {
        parameters: 'login='+ $('login').value + '&password=' + $('password').value + '&authenticity_token=' + authenticity_token,
        evalScripts:true,
        method:'post',
        onSuccess: function(transport){
          Modalbox.hide();
        },
        onFailure: function(){
	    alert("Cannot log you in with those credentionals!");
            //Modalbox.resizeToInclude($('errmsg'), {afterResize: showErrMessage });
        }
      });
}

/*
 * Shows the error message after failed login
 */
function showErrMessage(){
    new Effect.Appear($('errmsg'));
}

/*
 * Sets a cookie for the id of the menu item.
 * This cookie is read when the new page is loaded
 * so that the correct menu item is highlighted.
 */
function set_menu_cookie(menuItem){
    try{
        Cookie.set('menu', $(menuItem).identify(), null );
    }catch(e){
        alert(e.message);
    }
}

/*
 * Sets a cookie for the id of the parent menu item's
 * id.  This cookie is read when the new page is loaded
 * so that the correct menu item is highlighted.
 *
 */
function set_menu_cookie_from_child(relNameForParent){
    try{
        var els = $$('a[rel="' + relNameForParent + '"]');
        var p = els[0].ancestors()[0];
        Cookie.set('menu', $(p).identify(), null );
    }catch(e){
        alert(e.message);
    }
}

