var com=(com)?com:{};
    com.wc=(com.wc)?com.wc:{};
    com.wc.registry=(com.wc.registry)?com.wc.registry:{};

com.wc.registry.gridget = function(affiliateId, theme, size, inline, divToAppend){
    var retailerId = ["10810","10855","10074","10275","10910","14050"];
    var retailerName = ["Bloomingdale's", "Macy's", "Crate and Barrel", "Williams-Sonoma", "Pottery Barn", "Bed Bath %26 Beyond"];
    var cssUrl="http://images.della.com/images/partners/gridget/css/";
//    var cssUrl="./"; //for local testing
    var id_prefix = "com_wc_registry_"+theme+size;

    var h=document.getElementsByTagName('head')[0];
    var l = document.createElement('link');
    l.setAttribute('rel', 'stylesheet');
    l.setAttribute('type', 'text/css');
    l.href = cssUrl+theme+size+'.css';
    h.appendChild(l);

    var div = document.createElement('div');
    div.id = id_prefix;
    div.className = id_prefix;
    var p = document.createElement('p');
    p.id = id_prefix+'_header';
    p.className = id_prefix+'_header';
    p.innerHTML = 'Create a registry!';
    var ul = document.createElement('ul');
    ul.id = id_prefix+'_ul';
    ul.className = id_prefix+'_ul';
    var html = '';
    for(var i=0;i<retailerId.length;i++){
        html = html + '<li class="'+id_prefix+'_li'+'"><a target="_blank" href="http://reg.weddingchannel.com/retailer/WidgetLanding.jsp?a=' + affiliateId + '&r=' + retailerId[i] + '&rn=' + encodeURI(retailerName[i]) + '" class="retailer r'+(i+1)+'"><span id="'+id_prefix+'text">'+ retailerName[i] +'</span></a></li>';
    }
    ul.innerHTML = html;
    div.appendChild(p);
    div.appendChild(ul);

    if(inline){
        var tempWrapper = document.createElement('div');
        tempWrapper.appendChild(div);
        document.write(tempWrapper.innerHTML);
    }
    else {
        //Wait for element to render to prevent null pointer
        //in case calling script is placed before target id
        com.wc.registry.drawElement = function(){
            if(!document.getElementById(divToAppend)){
                setTimeout(com.wc.registry.drawElement, 100);
                return;
            }
            //clear out incoming div just in case
            var d = document.getElementById(divToAppend);
            com.wc.registry.removeChildren(d);
            //now append gridget content
            d.appendChild(div);
        };
        com.wc.registry.drawElement();
    }
};

com.wc.registry.removeChildren=function(e){
    while(e.hasChildNodes()){
        e.removeChild(e.lastChild);
    }
};
