var j = jQuery.noConflict();

function getElem(id) {
   return document.getElementById(id);
}

function stopProp(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

/**
 * toggle between the two tabs in the embedded watchlist,
 * @argument tab_to_select {string} "list" or "overview"
 */
function toggleWatchlistTabs(tab_to_select) {
    var tabs = ["list", "overview"];
    
    for (t in tabs) {
        // set style of all tabs to "not selected"
        var tab_node = getElem("embedded_watchlists_tab_" + tabs[t]);
        if (tab_node != null) {
            tab_node.className = "nav_item";
        }
        
        // hide all tabs initially
        var content_node = getElem("embedded_watchlists_content_" + tabs[t]);
        if (content_node != null) {
            j(content_node).hide();
        }
    }
    
    // set style of newly selected tab
    var selected_tab = getElem("embedded_watchlists_tab_" + tab_to_select);
    if (selected_tab != null) {
        selected_tab.className = "nav_item selected";
    }

    // show contents of newly selected tab
    var selected_content = getElem("embedded_watchlists_content_" + tab_to_select);
    if (selected_content != null) {
        j(selected_content).show();
    }
}

/**
 * toggle show the dropdown contents revealing the public watchlists
 */
function toggleWatchlistDropdown() {
    var watchlist_dropdown_options = getElem("watchlist_dropdown_options");
    if (watchlist_dropdown_options != null) {
        j(watchlist_dropdown_options).toggle();
    }
}

/**
 * go to specific page of a specified watchlist
 * @argument watchlist_id {int} id of the watchlist
 * @argument p {int} page to show
 */
function paginateWatchlist(watchlist_id, p) {
    var embedded_watchlist_page = getElem("embedded_watchlist_page_" + watchlist_id);
    if (embedded_watchlist_page != null) {
        
        // hide the currently selected page
        var page = getElem("watchlist_section_" + watchlist_id + "_" + embedded_watchlist_page.value);
        if (page != null) {
            j(page).hide();
        }
        
        // show the new page
        page = getElem("watchlist_section_" + watchlist_id + "_" + p);
        if (page != null) {
            j(page).show();
        }
        
        // store the new page value
        embedded_watchlist_page.value = p;
    }
}

/**
 * go to specific page of a specified watchlist
 * @argument watchlist_id {int} id of the watchlist
 * @argument watchlist_name {string} name of watchlist to show in dropdown
 */
function setWatchlistDest(watchlist_id, watchlist_name) {
    // show the watchlist name in the dropdown
    var selected_text = getElem("selected_watchlist_text");
    if (selected_text != null) {
        j(selected_text).html(watchlist_name);
    }

    // close the watchlist dropdown
    toggleWatchlistDropdown();
    
    // switch over to list view
    toggleWatchlistTabs("list");
    
    // hide the contents of the formerly selected watchlist
    var selected_id = getElem("selected_watchlist_id");
    if (selected_id != null) {
        var selected_watchlist = getElem("embedded_watchlist_section_" + selected_id.value);
        if (selected_watchlist != null) {            
            j(selected_watchlist).hide();
        }
        selected_id.value = watchlist_id;
    }
    
    // show the contents of newly selected watchlist
    var watchlist = getElem("embedded_watchlist_section_" + watchlist_id);
    if (watchlist != null) {
        j(watchlist).show();
    }
    
    // make overview tab active
    var overview_link = getElem("overview_link");
    if (overview_link != null) {
        j(overview_link).hide();
    }
    
    var overview_link_disabled = getElem("overview_link_disabled");
    if (overview_link_disabled != null) {
        j(overview_link_disabled).show();
    }
    
}

/**
 * show the overview of a watchlist item
 * @argument item_uri {string} uri of the item to show
 * @argument item_type {string} type of the item to show, "company", "person", or "industry"
 */
function showItemOverview(item_uri, item_type) {
    var loading_item = getElem("loading_item");
    if (loading_item != null) {
        j(loading_item).show();
    }
    
    // retrive the watchlist item
    var params = {item_uri:item_uri, item_type:item_type};
    var url = "/tracker/embedded_watchlist_item";
    j.ajax({
        type: "POST",
        url: url,
        data: params, 
        cache: false,
        error:
            function() {
            },
        success:
            function(data){
                if (data != "-1") {
                    // make overview tab active
                    var overview_link = getElem("overview_link");
                    if (overview_link != null) {
                        j(overview_link).show();
                    }
                    
                    var overview_link_disabled = getElem("overview_link_disabled");
                    if (overview_link_disabled != null) {
                        j(overview_link_disabled).hide();
                    }
    
                    // toggle over to the overview tab
                    toggleWatchlistTabs("overview");

                    var embedded_watchlists_content_overview_data = getElem("embedded_watchlists_content_overview_data");
                    if (embedded_watchlists_content_overview_data != null) {
                        j(embedded_watchlists_content_overview_data).html(data);
                    }
                } else {
                    alert("Error loading " + item_type + ". Please try again.");
                }
                
                if (loading_item != null) {
                    j(loading_item).hide();
                }
            }
   });

}

