/////// Library functions ///////
function properCase(str) {
    var index;
    var tmpStr;
    var tmpChar;
    var preString;
    var postString;
    var strlen;
    tmpStr = str.toLowerCase();
    strLen = tmpStr.length;
    if (strLen > 0)  {
        for (index = 0; index < strLen; index++)  {
            if (index == 0)  {
            tmpChar = tmpStr.substring(0,1).toUpperCase();
            postString = tmpStr.substring(1,strLen);
            tmpStr = tmpChar + postString;
            }
            else {
                tmpChar = tmpStr.substring(index, index+1);
                if (tmpChar == " " && index < (strLen-1))  {
                tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
                preString = tmpStr.substring(0, index+1);
                postString = tmpStr.substring(index+2,strLen);
                tmpStr = preString + tmpChar + postString;
                     }
            }
        }
    }
    return tmpStr;
}

//  +   addOption(select_elem, label, value)
//
//  adds the new label value pair to a select / drop down menu
//  @params:
//              select_elem     id of the form element
//              label           label for the new value
//              value           new value to associate
//  @returns:   boolean
//              true    if the addition was successful
//              false   otherwise
//
//  if the cart takes long to load, cart line elements in DOM won't be found
//  so the function won't do the change, without error reporting in IE6
//
function addOption(select_elem, label, value) {
    if (typeof label == 'undefined') return false;

    //  + without this check IE6 breaks
    if (typeof select_elem == 'undefined' || select_elem == null) return false;

    var opt = new Option(label, value);
    try {
        select_elem.add(opt, null); // standards compliant
    } catch(ex) {
        select_elem.add(opt); // IE only
    }
    return true;
}

/**
 * This function is used to show the swatch name and image sample. The second
 * parameter is optional, if it is not defined 0 will be used.
 */
function showSwatchName(obj,left_margin) {
    var left = YAHOO.util.Dom.getX(obj);
    var sw = YAHOO.util.Dom.getElementsByClassName('color_name', 'div', obj);
    sw[0].style.display = 'block';
    YAHOO.util.Dom.setX(sw, left);

    left_margin = typeof(left_margin) != 'undefined' ? left_margin : 0;
    var sw2 = YAHOO.util.Dom.getElementsByClassName('swatch_zoom', 'div', obj);
    sw2[0].style.display = 'block';
    YAHOO.util.Dom.setX(sw2, left+left_margin);
}

// this hides all labels to avoid having more than one showing
function hideSwatchName(id) {
    var swatches = document.getElementById('swatches_' + id);
    var elements = YAHOO.util.Dom.getElementsByClassName('color_name', 'div', swatches);
    for (var i=0; i < elements.length; i++) {
        elements[i].style.display='none';
    };
    var elements = YAHOO.util.Dom.getElementsByClassName('swatch_zoom', 'div', swatches);
    for (var i=0; i < elements.length; i++) {
        elements[i].style.display='none';
    };
}

function changeColorText(color_name) {
    var text = document.getElementById('color_selected_text');
    if (text) text.innerHTML = properCase(color_name);
}

function addGCtoBag () {
    var color              = '0000';
    var elColor            = document.getElementById('color_id_0');
    if (elColor) color     = elColor.value;

    var amount             = '0000';
    var elAmount           = document.getElementById('size_select_0');
    if (elAmount) { amount = elAmount.value; };


    var qty                = '';
    var elQty              = document.getElementById('qty_select_0');
    if (elQty) { qty       = elQty.value; };

    var gc     = document.getElementsByTagName('input');
    var ids    = new Array();

    var post_colors = new Array();
    var post_sizes  = new Array();
    var post_qtys   = new Array();
    for (var i = 0; i < gc.length; i++) {
        if (gc[i].type == "checkbox"
            && gc[i].checked
            && amount != ''
            && qty != '') {
            ids[ids.length]                 = eval('gift_item_codes.' + gc[i].value + '[' + amount + ']');
            post_colors[post_colors.length] = '0000';
            post_sizes[post_sizes.length]   = '0000';
            post_qtys[post_qtys.length]     = qty;
        };
    };

    if (ids.length == 0 || qty.value == '') {
        return;
        // or do something
    };

    var postData = 'i='  + ids.join(',')
                 + '&c=' + post_colors.join(',')
                 + '&s=' + post_sizes.join(',')
                 + '&q=' + post_qtys.join(',');


    var sUrl = "/cart/ajaxadd";

    var callback = {
        success:function(o) {
            if (typeof weAreInCart != "undefined" && weAreInCart == true) {
                window.location.reload();
                return;
            }
            if (o.responseText == undefined) return;
            if (typeof wait != 'undefined') wait.hide();
            responses = o.responseText.split('<br />');
            open_header_cart(responses[0]);
            document.getElementById('shopping_bag_counter').innerHTML = responses[1];
            updateRecent();
        },
        failure:function(o) {
            if (o.responseText == undefined) return;
            open_header_cart("There was an error while adding the item to your bag.");
        }
    };

    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}



function updateLocalStore(){
    if(!isArray(skuSelect))
        skuSelect.availableInLocalStore();
}

// ==================== 
// = Catalog leftnav  = 
// ==================== 
YAHOO.namespace('shopko.catalog.tv'); 
YAHOO.shopko.catalog.tv.nav = { 
    oTree: null, 
    init : function(id){ 
        this.oTree = new YAHOO.widget.TreeView(id); 
        this.oTree.draw(); 
        //this.oTree.collapseAll();
    } 
}; 
