function ajaxSearch() {
    pretendthis = this;
    this.levels = new Array(), this.leftLinks = new Array(), this.leftMenus = new Array();
    this.createLeft = function (link, category, class1, class2, class3, opt) { this.leftLinks.push(new this.leftLink(link, category, class1, class2, class3, opt)); }
    this.createMenu = function (menu) { this.leftMenus.push(new this.wrapMenu(menu)); }
    this.createCat = function (category, key1, key2) { this.levels.push(new this.catHash(category, key1, key2)); }
    this.getCat = function (category) { for (i in this.levels) { if (this.levels[i].category === category) { return this.levels[i]; }; } };
};

ajaxSearch.prototype.updateLevels = function (key1, key2, key3, key4) { //function is wrong needs replacing abovebeyond this point      
    var headID = document.getElementsByTagName("head")[0];
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.href = '/css/barbour.css';
    cssNode.media = 'screen'; 
    var cssNode2 = document.createElement('link');
    cssNode2.type = 'text/css';
    cssNode2.rel = 'stylesheet';
    cssNode2.href = '/css/style3.css';
    cssNode2.media = 'screen'; 
    
    this.sPrice = 'clear';

    var bsFlag = false;

    var thisSplit, thisHash = this.hash.replace(key1, '');
    thisSplit = thisHash.split(key2);
    for (var i in this.levels) {
        for (var j = 0; j < thisSplit.length; j++) {
            var hashBlock = thisSplit[j].split(key3);
            if (this.levels[i].category == hashBlock[0]) {          // goes thru whole leftlink array oO?!!!
                var thisHash = hashBlock[1].split(key4);
                if (hashBlock[0] == 'price') {
                    this.sPrice = thisHash;
                } else {
                    this.levels[i].cHash.splice(0, this.levels[i].cHash.length);
                    for (var y = 0; y < thisHash.length; y++) { this.levels[i].addToLevel(thisHash[y]); }
                    for (var m = 0; m < this.leftLinks.length; m++) {
                        if (this.leftLinks[m].category == this.levels[i].category) {
                            if (this.leftLinks[m].category == 'brand' && this.levels[i].cHash.length < 3) {

                                if(this.levels[i].cHash[1].toLowerCase()==='barbour-clothing'){
                                bsFlag=true;
                                }

                            }
                            if (this.searchHash(this.levels[i], this.leftLinks[m]) == true) {
                                this.leftLinks[m].status = 1;
                                this.leftLinks[m].link.className = this.leftLinks[m].active;
                            } else { if (this.leftLinks[m].status != 2) { this.leftLinks[m].resetLink(); } }
                        }
                    }
                }
            }
        }
    }
    //if(bsFlag){headID.appendChild(cssNode);}else{while(headID.childNodes.length>0){Dom.remove(headID.firstChild);}headID.appendChild(cssNode2);}
};
ajaxSearch.prototype.getPrice = function () { return this.sPrice; }
ajaxSearch.prototype.getHash = function () { return this.hash; }
ajaxSearch.prototype.updateHash = function (hash) { this.storeHash = hash; this.hash = this.storeHash; };
ajaxSearch.prototype.checkHash = function (hash) { if (this.storeHash !== hash) { return 1; } else { return 0; } };
ajaxSearch.prototype.intervaler = function (todo, time) { clearInterval(this.interval); this.interval = setInterval(todo, time); };
ajaxSearch.prototype.timer = function (todo, time) { clearTimeout(this.timeout); this.timeout = window.setTimeout(todo, time); };
ajaxSearch.prototype.submitHash = function (hash) {
    var bLevel = "";
    this.url = ''
    if (this.getCat('pageNum').cHash == '') { this.url = "&ppg=1"; }
    window.location.hash = hash + this.url;
}
ajaxSearch.prototype.createHash = function (hash) {
    this.hash = new String();
    for (i in this.levels) {
        for (j in this.levels[i].cHash) {
            if (j > 1) { this.hash += this.levels[i].split2; } // not in place yet should be for final
            this.hash += this.levels[i].cHash[j];
        }
    } return this.hash;
}
ajaxSearch.prototype.searchHash = function (obj, hashCell) { // alert(obj.cHash+"____"+hashCell.id);           
    for (i in obj.cHash) {
        if (obj.cHash[i].length == hashCell.id.length) { if (obj.cHash[i].match(hashCell.id)) { return true; } }
    } return false;
}
ajaxSearch.prototype.catHash = function (hash, split1, split2) {
    this.category = hash;
    this.prevLength = hash.length; //something
    this.split1 = split1;
    this.split2 = split2;
    this.cHash = new Array();
    this.baseLevel = 0;
    this.clearHash = function (pr) { this.cHash = new Array(); } //create reset links funtion}  
    this.addToLevel = function (hash) {
        this.cHash[0] = this.split1;
        if (this.split2.length < 1 && this.cHash.length > 1) { this.cHash.pop(); }
        this.cHash.push(hash); //+this.split2;      
    };
    this.removeFromLevel = function (hash) { //duplicated functionality between here and search function?          
        var thisLength = hash.toString().length;
        for (i in this.cHash) {
            if (this.cHash[i].length == thisLength) {
                if (this.cHash[i].match(hash)) { this.cHash.splice(i, 1); }
            }
        }
        if (this.cHash.length == 1) { this.cHash = new Array(); }
    };
};
ajaxSearch.prototype.catHash.prototype.wipeLevel = function () {
    for (var i = 0; i < this.cHash.length; i++) { this.cHash.splice(i, 1); } this.cHash = new Array();
}
ajaxSearch.prototype.catHash.prototype.setBase = function (pr) { this.baseLevel = pr; }
ajaxSearch.prototype.leftLink = function (link, cat, class1, class2, class3, opt) {
    this.category = cat;
    this.id = link.id.substr(5);
    this.link = link;
    this.status = 0;
    this.normal = class1;
    this.active = class2;
    this.inactive = class3;
    if (opt != true) {
        if (this.link.className == class2) { this.status = 1; }
        if (this.link.className == class3) { this.status = 2; }
    }
    this.resetLink = function () { this.status = 0; this.link.className = this.normal; }
    this.clickerd = function () {
        if (this.status == 0) {
            this.status = 1;
            this.link.className = this.active;
        } else if (this.status == 1) {
            this.status = 0;
            this.link.className = this.normal;
        } else if (this.status == 2) { }
        return this.status;
    }
}
ajaxSearch.prototype.searchBases = function (pass) { for (i in this.levels) { if (this.levels[i].baseLevel == pass) { return true; } } return false; }
ajaxSearch.prototype.setBases = function (pass) { for (i in this.levels) { this.levels[i].setBase(pass); } }
ajaxSearch.prototype.appendHash = function (hashCell) {
    this.activeLevel = this.getCat(hashCell.category);
    if (this.searchHash(this.activeLevel, hashCell) && this.activeLevel.category != 'pageNum') {
        if (this.activeLevel.category != 'price' && this.activeLevel.category != 'pageNum' && this.activeLevel.category != 'sort') { this.getCat('price').clearHash(); }
        this.activeLevel.removeFromLevel(hashCell.id);
        var page = this.getCat('pageNum'); // add exception property array to be kept seperate
        page.addToLevel('1');
    }
    else if (this.activeLevel.category == 'perPage') {
        this.activeLevel.cHash = []; // :? :/:/
        this.activeLevel.addToLevel(hashCell.id);
        if (this.activeLevel.category != 'pageNum') { var page = this.getCat('pageNum'); page.addToLevel('1'); }
    }
    else {
        if (this.activeLevel.category != 'price' && this.activeLevel.category != 'pageNum' && this.activeLevel.category != 'sort') { this.getCat('price').clearHash(); }
        this.activeLevel.addToLevel(hashCell.id);
        if (this.activeLevel.category != 'pageNum') { var page = this.getCat('pageNum'); page.addToLevel('1'); }
    };
}
function sortCollection(data, s) {// replace this function NOWWWWWWWW
    deptArr = [], typeArr = []; colourArr = [], brandArr = [], sizeArr = [], priceArr = [];
    var temp = data.split("#split#")
    deptArr = temp[0];
    deptArr = deptArr.split("~split~");
    brandArr = temp[1];
    brandArr = brandArr.split("~split~");
    typeArr = temp[2];
    typeArr = typeArr.split("~split~");
    colourArr = temp[3];
    colourArr = colourArr.split("~split~");
    sizeArr = temp[4];
    sizeArr = sizeArr.split("~split~");
    priceArr = temp[5];
    priceArr = priceArr.split("~split~");
    if (priceArr.length > 1) { pricySlide.setPrices(priceArr); } //update price slider min/max        
    updateLinks(deptArr, aSrch.getCat('department'));
    updateLinks(brandArr, aSrch.getCat('brand'));
    updateLinks(typeArr, aSrch.getCat('type'));
    updateLinks(colourArr, aSrch.getCat('colour'));
    updateLinks(sizeArr, aSrch.getCat('size'));
    var getPage = getElementsByClassName(document, "a", "plist_all");
    var getNavs = getElementsByClassName(document, "a", "pageCount");
    for (var i = 0; i < aSrch.leftLinks.length; i++) {
        if (aSrch.leftLinks[i].category == 'perPage') {
            aSrch.leftLinks.splice(i, 1);
        }
        if (aSrch.leftLinks[i].category == 'pageNum') {
            aSrch.leftLinks.splice(i, 1);
        }
    }
    for (var i = 0; i < getPage.length; i++) {
        getPage[i].onclick = function () { return false; }
        aSrch.createLeft(getPage[i], "perPage", "plist_all", "plist_all", "plist_all", true);
        eval("Event.add(getPage[i],'mouseup',function(){aSrch.submitHash(aSrch.createHash(aSrch.appendHash(aSrch.leftLinks[" + aSrch.leftLinks.length + "-1])));});");
    }
    for (var i = 0; i < getNavs.length; i++) {
        getNavs[i].onclick = function () { return false; }
        aSrch.createLeft(getNavs[i], "pageNum", "pageCount", "pCt", "pageCount", true);
        eval("Event.add(getNavs[i],'mouseup',function(){aSrch.submitHash(aSrch.createHash(aSrch.appendHash(aSrch.leftLinks[" + aSrch.leftLinks.length + "-1])));});");
    }
}

function updateLinks(resultsArray, level) {
    var linksArray = [];
    for (i in aSrch.leftLinks) {
        if (level.category == aSrch.leftLinks[i].category) {
            linksArray[linksArray.length] = aSrch.leftLinks[i];
        }
    }
    for (var i = 0; i < linksArray.length; i++) {
        linksArray[i].status = 2;
        for (j in resultsArray) {
            if (linksArray[i].id == resultsArray[j]) {
                if (linksArray[i].link.className == linksArray[i].active) {
                    linksArray[i].status = 1;
                }
                else//(array[i].link.className==array[i].normal)
                {
                    linksArray[i].status = 0;
                    linksArray[i].link.className = linksArray[i].normal;
                }
            }
        }
        if (aSrch.searchHash(level, linksArray[i]) == true) {
            if (linksArray[i].status != 0) {
                linksArray[i].status = 1;
                linksArray[i].link.className = linksArray[i].active;
            }

        }
        if (linksArray[i].status == 2) { linksArray[i].link.className = linksArray[i].inactive; }
    }
}
var aRequest;
ajaxSearch.prototype.httpGo = function (URL) {
    aRequest = createxmlHttp();
    var thisPage = $('thisContent');
    var thisMainPage = $('thisContent');
    var ajaxLoader = $('ajaxLoader');
    aRequest.onreadystatechange = function () {
        if (aRequest.readyState == 4 && aRequest.status == 200) {
            this.busy = false; var collectResponse;
            //alert(aRequest.responseText);
            collectResponse = aRequest.responseText.split("#splitmain#");
            if (collectResponse[0] != "") {
                try {
                    ajaxLoader.style.display = "none";
                    thisPage.style.opacity = 100 / 100;
                    thisPage.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
                    thisPage.style.filter = 'alpha(opacity=100)';
                } catch (e) { }
                thisMainPage.innerHTML = collectResponse[0];
            }
            try {
                if (collectResponse.length > 1) {
                    sortCollection(collectResponse[1]);
                }
            } catch (e) { }
        } else {
            ajaxLoader.style.display = "block";

            try {
                thisPage.style.opacity = .25;
                thisPage.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=25)';
                thisPage.style.filter = 'alpha(opacity=25)';
            } catch (e) { }
        } 
    }
    //alert(URL);
    aRequest.open("GET", URL, true);
    aRequest.send(null);
    this.busy = true;
}
ajaxSearch.prototype.wrapMenu = function (obj) {
    obj.style.display = "block";
    this.open = function () { (obj.style.display == "block") ? (obj.style.display = "none") : (obj.style.display = "block"); }
}
function createClearLeft(obj, idOffset) {
    var category = obj.id.substr(idOffset);
    obj.onclick = function () { return false; }
    Event.add(obj, 'mouseup', function () {
        for (var j in aSrch.levels) {
            if (aSrch.levels[j].category == category) {
                for (var i in aSrch.leftLinks) {
                    if ((aSrch.leftLinks[i].category == aSrch.levels[j].category) && (aSrch.leftLinks[i].status == 1)) {
                        aSrch.leftLinks[i].resetLink(0);
                        aSrch.appendHash(aSrch.leftLinks[i]);
                    }
                } break;
            }
        }
        aSrch.submitHash(aSrch.createHash(null));
    });
}
