$(document).ready(function() {
    ui = new UI();
    //ordering of the following calls is IMPORTANT!!!
    tk._init(isLoggedIn, isDrivebyUser); //does not require templates to be ready, but is required for tk.setting("ab_searchcontacts_displayMode") to return a value other than false    
    tk.rmi_loadJSTemplates(dependency_init);
    pymt._init();

    $.localScroll.defaults.axis = 'xy';
    $("#contentPanel_home").localScroll({
		target: '#home_scrollable', // could be a selector or a jQuery object too.
		queue:true,
		duration:750,
		hash:true,
		onBefore:function( e, anchor, $target ){
                    if(anchor.getAttribute("Id") != "home_FAQ" && $("#home_FAQ_iframe").is(":visible")) {
                        $("#home_FAQ_iframe").fadeOut("fast");
                    }
			// The 'this' is the settings object, can be modified
		},
		onAfter:function( anchor, settings ){
                    if(anchor.getAttribute("Id") == "home_FAQ") {
                        $("#home_FAQ_iframe").fadeIn("fast", function(){$("#home_FAQ_iframe").css("background", "none");});
                        $.log(anchor, "anchor " + anchor + " id:" + anchor.getAttribute("Id"), true);
                    }
			// The 'this' contains the scrolled element (#content)
		}
	});

});

function dependency_init(e) {
    if(typeof(IE6UPDATE_OPTIONS) != "undefined") {setTimeout("alert('Please update your browser.')", 600000)};
    if(!e.ok) {
        alert("We encountered and error. The page needs to reload.");
        window.location.reload(true);
        return;
    }

    $.setSnippetLib(e.data);
    $("#contentPanel_addressbook").snippet("ab_panel", {
        displayMode:tk.setting("ab_searchcontacts_displayMode"),
        orderby:tk.setting("ab_searchcontacts_orderby")
        });
	
    ab._init();
    lman._init();
	
    ui.setLoginElements();
	
    if(confStatus.length > 0) {
        switch(confStatus) {
            case "confirmfailed" :
                ui.statusMessage(confMessage, "error");
                break;
            case "confirmed" :
                ui.statusMessage(confMessage, "green");
                break;
            case "alreadyconfirmed" :
                if((isLoggedIn) && !(isDrivebyUser)) return;
                setTimeout(function(){ui.modalLogin();}, 1000);
            default :
                ui.statusMessage(confMessage);
        }
    }
    ui.displayMain("home");

    if(!tk.loggedIn()) {
      // ui.modal("modal__dev_welcome", {}, "register");
    } else {
        setTimeout(function(){
            if(typeof tk.userData == "undefined") {
                setTimeout(this, 1000);
            } else {
                var link = $("#reportABugLink").attr("href");
                $("#reportABugLink").attr("href", link + "&entry_7=" + encodeURIComponent(tk.userData.email));
            }

        }, 1000);
    }
}


function initPermanentTabs(e) {
    alert("initPermanentTabs depreciated!")
    tk.log($.inspect(e));
}



/**
 * callback funciton for FCKEditor (required by FCK api)
 */
function FCKeditor_OnComplete( editorInstance ) {
    editorInstance.ResetIsDirty();
    lman.autoSaveDraft(editorInstance.Name.substring(4));
    lman.setEditorAreaMargins();
    editorInstance.Events.AttachEvent("OnAfterSetHTML", function() {
        lman.setEditorAreaMargins();
    })
        
}

//DEBUG FUNCTIONALITY (RELOAD TEMPLATES @ RUNTIME)
function reloadTemplates() {
    tk.rmi_loadJSTemplates(function(e) {
        $.setSnippetLib(e.data);
    });
}
/*
function beta(number) {
    var a = number;
    var fonze = function() {
       alert(a);
    }
    a++;
    return(fonze);
}

var bill = beta(4);
beta(9);
bill();*/
