var Y = YAHOO.util, Dom = Y.Dom, Event = Y.Event;

window.tests = {
	
	"make" : function(){
		var ul;
		for ( var i = 0; i < 250; i++ ) {
			ul = document.createElement('ul');
			Dom.addClass(ul, 'fromcode');
			Dom.setAttribute(ul, 'id', 'setid'+i);
			document.body.appendChild(ul);
			ul.innerHTML = '<li>one</li><li>two</li><li>three</li>';
		}
		return Dom.getElementsByClassName('fromcode', 'ul').length;
	},
	
	"indexof" : function(){
		// No indexof method for arrays in YUI
		var target, uls, index, i, j, len;
		for ( i = 0; i < 20; i++ ) {
			target = Dom.get('setid150');
			uls = document.getElementsByTagName('ul');
			index = 0;
			for ( j = 0, len = uls.length; j < len; j++ ) {
				if ( uls[j] === target ) {
					index = j;
					break;
				}
			}
		}
		return index;
	},
	
	"bind" : function(){
		var uls = document.getElementsByTagName('ul'), lis = [];
		for(var i = 0, len = uls.length; i < len; i++) {
			Dom.getChildrenBy(uls[i], function(li) {
				if(li.nodeName === 'LI') {
                    lis.push(li);
                }
			});
		}
		Event.on(lis, 'click', function(){});
		return lis.length;
	},
	
	"attr" : function(){
		return Dom.batch(document.getElementsByTagName('ul'), function(el){
			return Dom.getAttribute(el, 'id');
//            return el.id;
		}).length;
	},
	
	"bindattr" : function(){
        var uls = document.getElementsByTagName('ul'), lis = [], subscriber = function(){};
		for(var i = 0, len = uls.length; i < len; i++) {
			Dom.getChildrenBy(uls[i], function(li) {
				if(li.nodeName === 'LI') {
                    Event.on(li, 'mouseover', subscriber);
                    Dom.setAttribute(li, 'rel', 'touched');
                    Event.removeListener(li, 'mouseover', subscriber);
                    lis.push(li);
                }
			});
		}
		return lis.length;
	},

	"table": function(){
		// Making sure to help IE with DOM manipulation wrt tables
		var _, table, i;
		for ( i = 0; i < 40; i++ ) {
            _ = document.createElement('div');
            _.innerHTML = '<table class="madetable"><tbody><tr><td>first</td></tr></tbody></table>';
			table = _.getElementsByTagName('table')[0];
			document.body.appendChild(table);
			Dom.insertBefore(
                document.createElement('td'),
                table.getElementsByTagName('td')[0]);
		}
        var tds = [];
        Dom.batch(document.getElementsByTagName('tr'), function(tr) {
            Dom.getChildrenBy(tr, function(td) {
                if (td.nodeName === 'TD') {
                    tds.push(td);
                }
            });
        });
        return tds.length;
    },
	
	"addanchor" : function(){
        var lis = [], a = document.createElement('A');
        Dom.setAttribute(a, 'href', 'http://example.com');
        a.appendChild(document.createTextNode('link'));

        Dom.batch(Dom.getElementsByClassName('fromcode'), function(ul) {
            Dom.getChildrenBy(ul, function(li) {
				if(li.nodeName === 'LI') {
                    li.appendChild(a.cloneNode(true));
                    lis.push(li);
                }
			});
        });
		return lis.length;
	},

	"append" : function(){
		var div, rets = [];
		for ( var i = 0; i < 500; i++ ) {
			div = document.createElement('div');
			Dom.setAttribute(div, 'rel', 'foo2');
			document.body.appendChild(div);
        }
        Dom.batch(document.getElementsByTagName('DIV'), function(div) {
            if ((Dom.getAttribute(div, 'rel') + '').indexOf('foo2') === 0) {
                rets.push(div);
            }
        });
        return rets.length;
    },
	
	"addclass-odd" : function(){
        var divs = document.getElementsByTagName('div'), i, len, total = 0;
        for(i = 0, len = divs.length; i < len; i++) {
            Dom.addClass(divs[i], 'added');
            if(i % 2 === 1) {
                Dom.addClass('odd');
                total++;
            }
        }
        return total;
	},
	
	"style": function(){
		return Dom.getElementsByClassName('added', 'div', document, function(node) {
            Dom.setStyle(node, 'backgroundColor', '#ededed');
            Dom.setStyle(node, 'color', '#fff');
        }).length;
	},
	
	"removeclass": function(){
		return Dom.getElementsByClassName('added', 'div', document, function(div) {
            Dom.removeClass(div, 'added');
        }).length;
	},
	
	"sethtml": function(){
		return Dom.batch(document.getElementsByTagName('div'), function(div){
			div.innerHTML = '<p>new content</p>';
		}).length;
	},
	
	"insertbefore" : function(){
        var els = Dom.getElementsByClassName('fromcode', 'ul'), rets = [], anchors = [];
        for(var i = 0, len = els.length; i < len; i++) {
            anchors = els[i].getElementsByTagName('A');
            for(var j = 0, m = anchors.length; j < m; j++) {
                rets.push(anchors[j]);
            }
        }
		Dom.batch(rets, function(a){
			var p = document.createElement('p');
			p.innerHTML = 'A Link';
			Dom.insertBefore(p,a);
		});
        return rets.length;
	},
	
	"insertafter" : function(){
        var els = Dom.getElementsByClassName('fromcode', 'ul'), rets = [], anchors = [];
        for(var i = 0, len = els.length; i < len; i++) {
            anchors = els[i].getElementsByTagName('A');
            for(var j = 0, m = anchors.length; j < m; j++) {
                rets.push(anchors[j]);
            }
        }
		Dom.batch(rets, function(a){
			var p = document.createElement('p');
			p.innerHTML = 'After Link';
			Dom.insertAfter(p,a);
		});
        return rets.length;
	},
	
	"destroy": function(){
        var total = 0;
        Dom.getElementsByClassName('fromcode', 'ul', document, function(el) {
            el.parentNode.removeChild(el);
            total++;
        });
		return total;
	},
	
	"finale": function(){
		var body = document.body;
		while ( body.firstChild ) {
			body.removeChild(body.firstChild);
		}
		return body.getElementsByTagName('*').length;
	}
	
};
