domain = 'http://www.etftips.com/';


// code to execute when document is loaded
$(function() {
    $('.admin_box_login input[@type=image]').mouseover(imgover).mouseout(imgout);
    $('#body_admin form input').not('[@type=submit],[@type=button]').focus(select_form_row).blur(deselect_form_row);
    $('#body_admin form textarea').focus(select_form_row).blur(deselect_form_row);
    $('.imgover').mouseover(imgover).mouseout(imgout);
    $('.bgover').mouseover(bgover).mouseout(bgout);

    if ($('.treeview').length > 0)
        $('.treeview').fileTree({
            script: domain + 'ajax_subcategories',
            multiFolder: false
        }, function(item) {
            document.location.href=item
        });

    $('.cat_parent_combo').change(category_change);

    if ($('#select_parents').length > 0)
        $('#select_parents').asmSelect({
            addItemTarget: 'bottom',
            animate: true,
            highlight: true,
            sortable: false
        });

    //clueTip
    if ($('#body_admin').length > 0)
        $('a[title]').cluetip({
            splitTitle: '|',
            arrows: true,
            dropShadow: false,
            cluetipClass: 'jtip'
        });
});

function category_change() {
    while ($(this).next().hasClass('cat_parent_combo')) $(this).next().remove();
    obj = $(this);
    if ($(obj).val() == 0) {
        while ($(obj).val() == 0 && $(obj).prev().hasClass('cat_parent_combo')) obj = $(obj).prev();
        $(obj).parent().find('input:first').attr('value', $(obj).val());
        return;
    }

    $.get(domain+'categories/ajax_categories_combo/'+$(this).val(), {}, function(result) {
        $(obj).after(result);
        $(obj).next().change(category_change);
        $(obj).parent().find('input:first').attr('value', $(obj).val());
    });
}

function get_data(ticker)
{
    $('#frm_tickers').after('<div id="loading" style="color: red">Loading.. Please wait</div>');
    $.get('import_stock/' + ticker, {}, function(response) {
        $('div#loading').remove();
        alert(response);
    });
}

function download_now(id)
{
    $.getJSON('home/download_now/'+id, function(data) {
        if (data['error']) { 
            alert(data['error_message']); return;
        }
        alert('Ticker data found. Imported '+data['imported_count']+' records.');
    });
}
