//alert("One more test, sorry");
var Events = {
    debug: function() { alert("hello"); },
    loaddlg: function() {
        
        if ($('#additionalBrands li').length > 5) {
                
            Ext.get("additionalBrands").hide();
            
            // Generate the HTML for the dialog
            var additionalBrands = document.getElementById("additionalBrands");
            var groupBaseBrand = document.getElementById("groupbaseBrand");
            var HTML = groupBaseBrand.innerHTML + additionalBrands.innerHTML;
            //additionalBrands.innerHTML = "";
            $('#additionalBrands').remove();
            
            HTML = "<ul style='list-style-type: none; padding-left: 3px;'>" + HTML + "</ul>"
            
            BrandDialog = new Ext.BasicDialog("allBrandsDialogs", {
                title: "Brands",
                autoCreate: true,
                closable: true,
                //animateTarget: "moreLink",
                resizeable: false,
                collapsible: false,
                height: 300, //Ext.get("allBrandsDialogList").getHeight() + 80,
                width: 300,
                modal: false,
                draggable: false,
                shadow: false
            });
            BrandDialog.addKeyListener(27, BrandDialog.hide, BrandDialog); // ESC can also close the dialog
            BrandDialog.hide();
            
            // Put our generated HTML into the dialog
            var id = BrandDialog.body.id;
            document.getElementById(id).innerHTML = HTML;
            
            // Regenerate the HTML for the shown 10 brands (sorting)
            var topTen = ["228", "33", "132", "208", "173", "204", "273", "137", "81", "151"]
            var badArtists = ["170", "180", "108", "209", "187"]
            var els = $('#allBrandsDialogs li')
            var base = $('#groupbaseBrand');
            var extras = [];
            if (els.length > 15) {  //$('#groupbaseBrand li').length >= 1000) {
                base.html("");
                els.each(
                    function(i) {
                        var li = $(this);
                        var href = li.children("a").attr("href");
                        var done = null;
                        $.each(topTen, function (i, val) {
                            var regex = new RegExp("[=_]" + val + "(_|$|&)");
                            if ( regex.exec(href) != null ) {
                                li.clone().appendTo("#groupbaseBrand");
                                done = true;
                            }
                        });
                        if (done == null) {
                            var regex = new RegExp("c=(46|24)(_|$|&)");
                            if ( regex.exec(location.search) == null ) {
                                var done = null;
                                $.each(badArtists, function (i, val) {
                                    var regex = new RegExp("[=_]" + val + "(_|$|&)");
                                    if ( regex.exec(li.children("a").attr("href")) != null ) {
                                        li.remove();
                                        done = true;
                                        //console.log(li.text());
                                    }
                                });
                                if (done == null) {
                                    extras.push(li);
                                }
                            } else {
                                extras.push(li);
                            }
                        }
                    }
                );
                if ( base.children('li').length < 10 ) {
                    for (i=base.children('li').length; i<11; i++) {
                        extras.shift().clone().appendTo("#groupbaseBrand");
                    }
                }
            }
            
            // Resize Dialog to it's body
            BrandDialog.setContentSize(Ext.get(id).getWidth() + 20, Ext.get(id).getHeight() + 30)
            
            // CSS
            $('#' + id).css("text-align", "left")
            $('#' + id).css("color", "#246A82")
            // Create More Link
            var dh = Ext.DomHelper;
            dh.append('groupbaseBrand', {tag: 'p', html: '<a id="moreLink" href="javascript:void(0);">More Brands</a>'});
            var moreLink = Ext.get("moreLink");
            moreLink.on("mouseover", function (e) { BrandDialog.show(); } );
            
            // Anchor to more link
            BrandDialog.alignTo("moreLink", "bl-r", [40, 0]);
            
            Ext.get("allBrandsDialogs").on("mouseout", function(e) {
                var ele = e.getTarget();
                var rel = e.getRelatedTarget();
                //console.log(ele.nodeName, ele.id, rel.id);
                if ( rel.id == "ext-gen10" && ele.id != "ext-gen4") { BrandDialog.hide(); }
            });
            
        }
        
        /*if ( $('.shoppingBot').length == 4 ) {
            $($('.shoppingBot')[1]).css("height", $($('.shoppingBot')[0]).css('height'));
            $($('.shoppingBot')[3]).css("height", $($('.shoppingBot')[2]).css('height'));
        }*/
    }
};

var BrandDialog;

//if (!Ext.isIE) {
//    Ext.EventManager.onDocumentReady(Events.loaddlg, Events, true);
//}
if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
    //pass
} else {
    Ext.EventManager.onDocumentReady(Events.loaddlg, Events, true);
}


