var resetTarget = new Array();
var resetCheck = null;
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////Drop Nav Menu Constructor & prototype////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function dropNav(oTarget) {
    this.oTarget = oTarget;
    this.aHeadLinks = new Array();
    this.aDrops = new Array();
}
dropNav.prototype.clearDrops = function() {
    for (i = 0; i < this.aDrops.length; i++) {
        if (!this.aDrops[i].active) {
            this.aDrops[i].oTarget.className = '';
            this.aDrops[i].bClicked = false;
            this.aDrops[i].shieldDown();
            if (this.aDrops[i].upDown) {
                this.aDrops[i].oList.style.zIndex = 20;
            }
        }
    }
}
dropNav.prototype.clearHighLights = function(currentSelection) {
    for (i = 0; i < this.aDrops.length; i++) {
        if (this.aDrops[i].oTarget.className != 'navSelected' && this.aDrops[i].oTarget != currentSelection) {
            this.aDrops[i].oTarget.className = '';
        }
    }
}
dropNav.prototype.initialise = function() {
    if (this.oTarget) {
        var aLists = this.oTarget.getElementsByTagName('li');
        for (i = 0; i < aLists.length; i++) {
            if (aLists[i].id) {
                var oDropList = new list(aLists[i]);
                oDropList.initialise();
                oDropList.oParent = this;
                this.aDrops.push(oDropList);
            }
        }
    }
    $('#topnav ul').hover(function() {
     //   $('div, iframe', this).removeClass('tabnavHide');
    },function() {
     //   $('div, iframe', this).addClass('tabnavHide');
        //$('#topnav ul li').removeClass('navSelected');
    });
}
//////////////////////////////////////////////////////////////////////////////////////
///////////////////////List Item Constructor & prototype//////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function list(oTarget) {
    this.oParent;
    this.oTarget = oTarget;
    this.active = false;
    this.bClicked = false;
}
list.prototype.initialise = function() {
    this.oLink = this.oTarget.getElementsByTagName('a')[0];
    if (this.oLink.innerHTML.toLowerCase().indexOf('0') >= 0 && this.oLink.innerHTML.toLowerCase().indexOf('mess') >= 0) {
        this.oLink.innerHTML = 'My mail';
    }
    this.oList = this.oTarget.getElementsByTagName('div')[0];
    this.Shield = this.oTarget.getElementsByTagName('iframe')[0];
    if (this.oList) {
        this.oLink.className = 'DDMenu';
        this.listLinks = this.oList.getElementsByTagName('li');
        this.setListLinks();
        this.oTarget.onmouseover = this.turnOn.bind(this);
    } else {
        this.oTarget.onmouseover = this.turnOn.bind(this);
        this.oTarget.onclick = this.followLink.bind(this);
    }
    this.oTarget.onmouseout = this.clearHighlights.bind(this);
}
list.prototype.killLink = function() {
    //debugger;
    return false;
}
list.prototype.followLink = function() {
    document.location.href = this.oLink.href;
    return false;
}
list.prototype.turnOn = function() {
    this.highLight();
    if (this.bClicked) {
        this.oParent.clearDrops();
        this.oParent.clearHighLights();
    } else {
        this.startResetCheck();
        this.oParent.clearDrops();
        this.oTarget.className = 'navSelected';
       // document.title = $('#topnav .topnav-places').is('navSelected');
        $('div', this.oTarget).addClass('opening');
        $('iframe', this.oTarget).addClass('opening');
        $(this.oTarget).animate({ margin: 0 }, 0, function() {
           // document.title = this.active;
            $('div', this.oTarget).removeClass('opening');
            $('iframe', this.oTarget).removeClass('opening');
        });
        if (this.oList) {
            this.height = this.oList.offsetHeight;
            this.width = this.oList.offsetWidth;
            this.shieldUp();
        }
        this.bClicked = true;
    }
    return false;
}
list.prototype.highLight = function() {
this.startResetCheck();
    
    this.active = true;
    this.oParent.clearHighLights();
    if (this.oTarget.className != 'navSelected') {
        this.oTarget.className = 'highLighted';
    }
}
list.prototype.clearHighlights = function() {
    this.active = false;
    //alert($(this).attr('id'));
    //$(this).hide();
    $('div', this.oTarget).removeClass('opening');
    $('iframe', this.oTarget).removeClass('opening');
    this.oParent.clearHighLights(this.oTarget);
    this.oTarget.className != '';
}
list.prototype.setListLinks = function() {
    for (l = 0; l < this.listLinks.length; l++) {
        this.listLinks[l].oParent = this;
        this.listLinks[l].shref = this.listLinks[l].getElementsByTagName('a')[0].href;


        if (this.listLinks[l].getElementsByTagName('a')[0].id) {
            var sId = this.listLinks[l].getElementsByTagName('a')[0].id;
            if (sId.indexOf('currentSelection') > -1) {
                this.nCurrentSelection = l;
            }
        }
        this.listLinks[l].onmouseover = function() {
            this.oParent.active = true;
        }
        this.listLinks[l].onmouseout = function() {
            this.oParent.active = false;

        }
        this.listLinks[l].onclick = function() {
            document.location.href = this.shref;
            return false;
        }
    }
}
list.prototype.shieldUp = function() {
    this.Shield.style.height = (this.height - 3) + 'px';
    this.Shield.style.width = this.width + 'px';
    this.Shield.style.zIndex = -10;

    this.Shield.removeAttribute('allowTransparency');

    if (this.nTopAdjust) this.Shield.style.top = this.nTopAdjust + 'px';
    this.Shield.style.visibility = 'visible';
    this.Shield.style.display = 'block';
}
list.prototype.shieldDown = function() {
    if (this.Shield) {
        this.Shield.style.visibility = 'hidden';
        this.Shield.style.display = 'none';
    }
}
list.prototype.startResetCheck = function() {
    resetTarget.push(this);
    if (resetCheck) {
        clearTimeout(resetCheck);
    }
    resetCheck = setTimeout(resetChecker, 1000);
}
//////////////////////////////////////////////////////////////////////////////////////
////////////////////Up-Down Drop Nav Constructor & prototype//////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function upDowndropNav(oTarget) {
    dropNav.call(this, oTarget);
}
upDowndropNav.prototype = new dropNav();
upDowndropNav.prototype.initialise = function() {
    if (this.oTarget) {
        this.upDown = true;
        var aLists = this.oTarget.getElementsByTagName('li');
        for (i = 0; i < aLists.length; i++) {
            if (aLists[i].id) {
                var oDropList = new upDownlist(aLists[i]);
                oDropList.oParent = this;
                oDropList.initialise();
                this.aDrops.push(oDropList);
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////////////////
///////////////////Up-Down List Item Constructor & prototype//////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function upDownlist(oTarget) {
    dropNav.call(this, oTarget);
}
upDownlist.prototype = new list();
upDownlist.prototype.turnOn = function() {
    this.oList.style.zIndex = 2000;
    if (this.bClicked) {
        this.oParent.clearDrops();
        this.oParent.clearHighLights();
    } else {
        this.startResetCheck();
        this.oParent.clearDrops();
        this.oTarget.className = 'navSelected';
        this.height = this.oList.offsetHeight;
        this.width = this.oList.offsetWidth;
        this.nTopAdjust = -((this.height / (this.listLinks.length)) * this.nCurrentSelection);
        this.oList.style.top = this.nTopAdjust + 'px';
        this.shieldUp();
        this.bClicked = true;
    }
    return false;
}
//////////////////////////////////////////////////////////////////////////////////////

function resetChecker() {
    var newReset = new Array()
    for (var i = 0; i < resetTarget.length; i++) {
        if (!resetTarget[i].active) {
            resetTarget[i].oParent.clearDrops();
            resetTarget[i].oParent.clearHighLights();
        } else {
            newReset.push;
        }
    }
    resetTarget = newReset;
}
