if (!org) var org = {};
if (!org.moddular) org.moddular = {};


org.moddular.Util = {
    createDelegate: function(obj, method) {
        return function() {
            return obj[method].apply(obj, arguments);
        }
    },

    setCookie: function(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = '; expires=' + date.toGMTString();
        } else {
            var expires = '';
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    },

    getCookie: function(name) {
        var nameEQ = name + '=';
        var ca = document.cookie.split(';');
        for (var i = 0;i < ca.length; ++i) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(nameEQ) == 0) {
                return c.substring(nameEQ.length, c.length);
            }
	}
        return null;
    },

    findPos: function(obj) {
        var curLeft = 0;
        var curTop = 0;
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                curLeft += obj.offsetLeft;
                curTop += obj.offsetTop;
                obj = obj.offsetParent;
            }
        }
        return {x: curLeft, y: curTop};
    }
};

org.moddular.ResizeHandle = function(target) {
    this.pos = org.moddular.Util.findPos(target);
    this.target = target;
    this.handle = this.createHandle();
};
org.moddular.ResizeHandle.prototype.createHandle = function() {
    var div = document.createElement('div');
    div.setAttribute('title', 'Drag up and down to resize this textarea');
    div.className = 'drag-handle';
    div.style.left = (this.pos.x - 15) + 'px';
    div.style.top = (this.pos.y + this.target.offsetHeight - 25) + 'px';

    div.appendChild(document.createTextNode('\u2195'));

    div.onmousedown = org.moddular.Util.createDelegate(this, 'startDrag');
    document.addEventListener('mouseup', org.moddular.Util.createDelegate(this, 'stopDrag'), false);

    this.target.parentNode.appendChild(div);

    return div;
};

org.moddular.ResizeHandle.prototype.startDrag = function() {
    document.onmousemove = org.moddular.Util.createDelegate(this, 'drag');
};
org.moddular.ResizeHandle.prototype.stopDrag = function() {
    document.onmousemove = null;
};
org.moddular.ResizeHandle.prototype.drag = function(e) {
    var mouseY = parseInt(e.pageY);
    var totalY = document.body.scrollTop + document.body.offsetHeight;
    if (mouseY > this.pos.y + 50) {
        this.handle.style.top = (mouseY - 10) + 'px';
        this.target.style.height = (mouseY - this.pos.y + 7) + 'px';
    }
    if (mouseY > totalY - 50) {
        document.body.scrollTop += 50;
    }
};


org.moddular.Search = function(fieldId, checkboxId, resultsId) {
    this.field = document.getElementById(fieldId);
    this.value = this.field.value;
    this.check = document.getElementById(checkboxId);
    this.comments = (this.check.checked) ? 1 : 0;
    this.commentChange = false;
    this.results = document.getElementById(resultsId);

    this.findEnabled = true;
    this.setupCheckbox();

    this.waiting = false;
    this.xmlhttp = null;
    if (location.hash.length) {
        this.field.value = location.hash.substring(1);
        this.trySearch();
    }
    this.check.onclick = org.moddular.Util.createDelegate(this, 'switchComments');
    this.field.onkeyup = org.moddular.Util.createDelegate(this, 'trySearch');
};
org.moddular.Search.prototype.trySearch = function() {
    if (this.findEnabled && !this.waiting && this.field.value.length > 1 && (this.field.value != this.value || this.commentChange)) {
        this.value = this.field.value;
        this.waiting = true;
        this.commentChange = false;
        this.results.innerHTML = '<p>Searching for &#8220;' + this.value + '&#8221;</p>';
        this.doSearch();
    }
};
org.moddular.Search.prototype.doSearch = function() {
    this.xmlhttp = new XMLHttpRequest();
    this.xmlhttp.open('GET', 'http://www.moddular.org/redesign/xml/search/' + escape(this.value) + '/' + this.comments, true);
    this.xmlhttp.onreadystatechange = org.moddular.Util.createDelegate(this, 'response');
    this.xmlhttp.send(null);
};
org.moddular.Search.prototype.response = function() {
    if (this.xmlhttp.readyState == 4) {
        location.hash = this.field.value;
        this.results.innerHTML = this.xmlhttp.responseText;
        this.waiting = false;
        this.xmlhttp = null;
        this.trySearch();
    }
};
org.moddular.Search.prototype.switchComments = function() {
    this.commentChange = true;
    this.comments = (this.check.checked) ? 1 : 0;
    org.moddular.Util.setCookie('include_comments', this.comments, 7);
    this.trySearch();
};
org.moddular.Search.prototype.setupCheckbox = function() {
    var label = document.createElement('label');
    label.htmlFor = 'toggle-find';
    label.appendChild(document.createTextNode('Find as you type:'));
    var input = document.createElement('input');
    input.setAttribute('id', 'toggle-find');
    input.setAttribute('type', 'checkbox');

    input.onclick = org.moddular.Util.createDelegate(this, 'toggleFind');
    this.toggle = input;

    this.check.parentNode.appendChild(label);
    this.check.parentNode.appendChild(input);
    this.findEnabled = (org.moddular.Util.getCookie('find') == 1) ? 1 : 0;
    if (this.findEnabled) {
        input.setAttribute('checked', 'checked');
    }
};
org.moddular.Search.prototype.toggleFind = function() {
    this.findEnabled = (this.toggle.checked) ? 1 : 0;
    org.moddular.Util.setCookie('find', this.findEnabled, 7);
};