// build_thumbs_list.js
//

function toggleArray(arrID) {
    for (var i=0; i<arrID.length; i++) {
        var theID = arrID[ i ];
        theElement = dojo.byId(theID);
        dojo.html.toggleDisplay(theElement);
    }
}

// Pass multiple element ID's in an array
// to toggle their visibility
function switchArray(arrSwitch) {
    for (var i=0; i<arrSwitch.length; i++) {
        var arrID = arrSwitch[ i ];
        if (arrID.length != 2) {
            throw new Error('function switchArray called with improperly ' +
                            'formatted argument list.');}
        theElement = dojo.byId(arrID[ 0 ]);
        if ((arrID[ 1 ] != 'none') && (arrID[ 1 ] != 'block')) {
            throw new Error('function switchArray called with invalid ' +
                            'display type.');}
        dojo.html.setDisplay(theElement,arrID[ 1 ]);
    }
}

// Pass multiple element ID's in an array
// to turn on their visibility
function switchArrayOn(arrSwitch) {
    for (var i=0; i<arrSwitch.length; i++) {
        var theElement = dojo.byId(arrSwitch[ i ]);
        dojo.html.setDisplay(theElement,'block');
    }
}
// Pass multiple element ID's in an array
// to turn off their visibility
function switchArrayOff(arrSwitch) {
    for (var i=0; i<arrSwitch.length; i++) {
        var theElement = dojo.byId(arrSwitch[ i ]);
        dojo.html.setDisplay(theElement,'none');
    }
}

function _array_test(arr_in, test) {
    var ret_val = false;
    for (var i=0; i<arr_in.length; i++) {
        if (arr_in[ i ] == test) {
            ret_val = true;
            break;
        }
    }
    return ret_val;
}


dojo.require("ag.widget.Popper");
dojo.require("ag.widget.PrintablesPopper");

var g_parent_element;  // global...bad, bad
var g_product_line;    // more global badness

// Grab products
function grabProducts(arr_prods,arr_excluded,s_element,map_data,url_ajax){
    var prods   = new Array();
    var paths   = new Array();
    var request = new Object();
    var the_url = khost + '/productinfo.pd?';
    map_product_line = {'ag':'ecards','ec':'ecards','dl':'downloads',
                        'cnp':'projects','inv':'invites'};
    g_parent_element = s_element;
    
    // See if an extra data object is passed in.
    if (arguments.length > 3) {
        // Parse extra data
        dojo.debug(dojo.io.argsFromMap(map_data));
        for (var datum in map_data) {
            request[datum] = map_data[datum];
        }
        if ('site' in map_data) { g_product_line = map_product_line[request['site']]; }
        else { g_product_line = 'products'; }
        
        if (g_product_line === undefined) { g_product_line = 'products'; }
    }
    // See if an overriding ajax url is passed in.
    if (arguments.length > 4) { the_url = url_ajax; }

    var prods_list = eval(arr_prods);
    dojo.debug("prods_list: " + prods_list);
    for (var i=0; i<prods_list.length; i++) {
        var prod = prods_list[i];
        if (!(prod[0]&&prod[1]&&prod[0]!==""&&prod[1]!=="")) { continue; }
        if (_array_test(arr_excluded,prod[ 0 ])) { continue; }
        prods = prods.concat(prod[0]);
        paths = paths.concat(prod[1]);
    }
    // Don't continue if no product numbers
    if (prods.length === 0) { return; }
    //if (prods.length != paths.length) { throw some_error; )
    
    var prod_nums = prods.join(',');
    var path_nums = paths.join(',');
    dojo.debug("prod_nums: " + prod_nums + " | path_nums: " + path_nums);
    request["prodnum"]   = prod_nums;
    request["path"]      = path_nums;
    request["pd_nogzip"] = 1;
    dojo.debug("request: " + dojo.io.argsFromMap(request));
    dojo.io.bind({
                    url:the_url,
                    load:createProductWidget,
                    mimetype:"text/html",
                    method: 'POST',
                    content:request
                 });
}

function createProductWidget(type, data, evt){
    dojo.debug("starting createProductWidget");
    var root   = dojo.byId(g_parent_element);
    var uber_root = root.parentNode; 
    
    var p_elem = document.createElement('p');
    var p_text = 'You have no recently viewed ' + g_product_line;

    p_elem.className = "agi-desc";
    if (data.length > 0) {
        var no_rv = dojo.byId('no_rv');
        dojo.html.setDisplay(no_rv,'none');
        p_text = 'You recently viewed these ' + g_product_line + ':';
    }
    p_elem.appendChild(document.createTextNode(p_text));
    if (uber_root !== null) {
        uber_root.insertBefore(p_elem,root);
    }

    dojo.debug("Data Length: " + data.length);
    root.innerHTML = data.toString();

    //Add the newly grabbed products numbers to the products array,
    //and re-initialize the poppers.
    productNodes = dojo.html.getElementsByClass('product', null,'li');
    dojo.debug('products.length: ' + products.length);
    for( p in productNodes ){
        productNode = productNodes[p];
        dojo.debug('appending ' + productNode.id);
        products.push(productNode.id);
        dojo.debug('pushed ' + productNode.id);
    }
    dojo.debug('products: ' + products);

    initPoppers();
    dojo.debug('second popper init');
}
