var thebody = false;
var dts = false;
var dds = false;
var lastf = false;

var theloc = false;

var isSafari = ((parseInt(navigator.productSub)>=20020000) && (navigator.vendor.indexOf('Apple Computer')!=-1));
var DEF_VAL = 'enter ZIP code';

function externalLinks() {
    var links = thebody.getElementsByTagName('a');
    var linknum = links.length;
    for(var i = 0; i <linknum; i++) {
        var thelink = links[i];
        if(thelink.rel && thelink.rel == 'nofollow') {
            thelink.target = '_blank';
        }
    }
}

function addEvent(obj, evType, fn, useCapture) {
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        //alert("Handler could not be attached");
    }
}

function showMenu() {
    var dls = document.getElementsByTagName('dl');
    if(dls.length > 0) {
        var thedl = dls[0];
        dts = thedl.getElementsByTagName('dt');
        dds = thedl.getElementsByTagName('dd');
        for(x = 0; x < dds.length; x++) {
            var thedt = thedl.getElementsByTagName('dt')[x];
            var thedd = thedl.getElementsByTagName('dd')[x];
            var theul = thedd.getElementsByTagName('ul')[1];
            var result = '';
            var lis = theul.getElementsByTagName('li');
            for(i = 0; i < lis.length; i++) {
                var kind = lis[i].getElementsByTagName('span')[0].firstChild.data;
                var hasit = lis[i].getElementsByTagName('span')[1].firstChild.data;
                result += (result != '' ? ' ' : '') + hasit.substr(2) + i;
                if(hasit == ': yes') lis[i].className = 'a';
            }
            thedt.className = result;
            thedd.className = result;
        }
        var fas = document.getElementById('f').getElementsByTagName('a');
        for(i = 0; i < fas.length; i++) {
            if(i < 1) {
                fas[i].rel = ' ';
                fas[i].className = 's';
                lastf = fas[i];
            }else fas[i].rel = 'yes' + (i - 1);
            addEvent(fas[i], 'click', switchTab, false);
        }
    }
}

var canstuff = false;

function switchTab() {
    if(this.firstChild && this.firstChild.data) {
        canstuff = true;
        document.title = this.firstChild.data.replace('all', '') + theloc.replace('Apple', '');
        var h1t = thebody.getElementsByTagName('h1')[0].getElementsByTagName('a')[0];
        h1t.replaceChild(document.createTextNode(document.title), h1t.firstChild);
    }
    lastf.className = '';
    this.className = 's';
    filterStores(this.rel);
    lastf = this;
}

function filterStores(what) {
    for(i=0; i < dts.length; i++) {
        var oldclass = dts[i].className;
        if(oldclass.indexOf(what) >= 0) {
            if(canstuff) {
                dts[i].style.opacity = '1';
                dts[i].style.height = 'auto';
                dts[i].style.margin = '0 0 -40px 0';
                dts[i].style.padding = '10px';
                dds[i].style.opacity = '1';
                dds[i].style.height = '90px';
                dds[i].style.padding = '40px 10px 10px 10px';
                dds[i].style.margin = '0 0 40px 0';
                dds[i].style.border = '1px solid #e7e7e7';
            }else{
                dts[i].style.display = 'block';
                dds[i].style.display = 'block';
            }
        }else{
            if(canstuff) {
                dts[i].style.height = '0px';
                dts[i].style.opacity = '0';
                dts[i].style.margin = '0';
                dts[i].style.padding = '0';
                dds[i].style.height = '0px';
                dds[i].style.opacity = '0';
                dds[i].style.padding = '0';
                dds[i].style.margin = '0';
                dds[i].style.border = '0';
            }else{
                dts[i].style.display = 'none';
                dds[i].style.display = 'none';
            }
        }
    }
}

function replaceSearchField()
{
    if (!document.getElementById)
        return;
    var searchForm = document.getElementsByTagName('form')[0];
    var searchField = searchForm.getElementsByTagName('input')[0];
    var searchButton = searchForm.getElementsByTagName('input')[1];
    if (isSafari) {
        searchField.setAttribute('type', 'search');
        searchField.setAttribute('autosave', 'in.apple-store');
        searchField.setAttribute('results', '5');
        searchField.setAttribute('placeholder', DEF_VAL);
        searchField.setAttribute('class', 'saf');
        searchButton.style.display = 'none';
    }else{
        addEvent(searchField, 'focus', focusSearch, false);
        addEvent(searchField, 'blur', blurSearch, false);
        if (searchField.value=='') searchField.value = DEF_VAL;
    }
    if(searchForm.className == 's') searchField.focus();
}

function focusSearch() {
    if (this.value==DEF_VAL) {
        this.value = '';
        this.setAttribute('class', 'focus');
    }
}

function blurSearch() {
    if (this.value=='') {
        this.value = DEF_VAL;
        this.removeAttribute('class', 'focus');
    }
}

function init() {
    thebody = document.getElementsByTagName('body')[0];
    theloc = thebody.getElementsByTagName('h1')[0].getElementsByTagName('a')[0].firstChild.data;
    showMenu();
    replaceSearchField();
    externalLinks();
}

addEvent(window, 'load', init, false);