var lbsWebsite = 'http://www.london.edu/'
var xmlHttpLogin;
var searchUl;

function GetXmlHttpLoginObject() {
	var loc=null;
	try {
	  // Firefox, Opera 8.0+, Safari
	  loc=new XMLHttpRequest();
	}
	catch (e) {
	  // Internet Explorer
	  try {
	    loc=new ActiveXObject("Msxml2.XMLHTTP");
  	  }
	  catch (e) {
		loc=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
	return loc;
}

ulcount = 0;
hoverIE6 = function() {
	var containers = Array("nav");
	for (var containerCursor = 0 ; containerCursor < containers.length ; containerCursor++ )
	{
		//if (document.getElementById(containers[containerCursor])){
			var sfEls = document.getElementById(containers[containerCursor]).getElementsByTagName("LI");
			for (var i=0; i<sfEls.length; i++) {
				sfEls[i].onmouseover=function() {
					this.className+=" sfhover";
				}
				sfEls[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
				}
			}
		//}
	}
}
if (window.attachEvent) window.attachEvent("onload", hoverIE6);
function fileDownload( o ) {
    o.target = "_blank";
    var url = o.href.substring(o.href.indexOf(".london.edu") + 11);
    var filename = url.substring(url.lastIndexOf("/") + 1);
    dcsMultiTrack('DCS.dcsuri',url,'WT.ti',filename);
}


function lbs_dropdown( o ) {

    var s = o;
    
    var ddDiv = document.createElement('div');
    if(s.className = 'defaultSelect') {
        ddDiv.className = 'ddDiv defaultSelect';
    } else {
        ddDiv.className = 'ddDiv';
    }
    
    ddDiv.innerHTML = s.options[s.selectedIndex].innerHTML;
    
    ddDiv.onfocus = function() {
        ulcount++;
    
        var ul = document.createElement('ul');
        options = s.getElementsByTagName('option');
        for(j = 0; j < options.length; j++) {
            li = document.createElement('li');
            li.innerHTML = options[j].innerHTML;
            li.className = 'pseudoSelect';
            li.setAttribute('id','i' + ulcount + '_' + j);
            li.onmouseover = function() {
                this.style.cursor = 'pointer'
                this.style.background = '#a3a6ab';
                this.style.color = '#000000';
            }
            li.onfocus = function() {
                this.style.cursor = 'pointer'
                this.style.background = '#a3a6ab';
                this.style.color = '#000000';
            }
            li.onmouseout = function() {
                this.style.cursor = 'default'
                this.style.background = '#ffffff';
                this.style.color = '#0033cc';
            }
            li.onclick = function() {
                id = this.getAttribute('id');
                index = id.substring(id.lastIndexOf('_') + 1);
                s.selectedIndex = index;
                ddDiv.innerHTML = this.innerHTML;
                if(s.onchange) s.onchange();
                this.parentNode.parentNode.blur();
            }
            ul.appendChild(li);
        }
        
        dropdown = document.createElement('div');
        dropdown.style.border = '1px solid black';
        dropdown.style.width = '250px';
        dropdown.style.height = '140px';
        dropdown.style.overflow = 'scroll';
        dropdown.style.backgroundColor = '#ffffff';
        
        dropdown.style.zIndex = 100;
        dropdown.appendChild(ul);

        height = o.style.height;

        curleft = curtop = 0;
        c = ddDiv;
        if(c.offsetParent) {
            do {
                curleft += c.offsetLeft;
                curtop += c.offsetTop;
            } while(c = c.offsetParent)
        }
    
        dropdown.style.position = 'absolute';
        dropdown.style.left = curleft;
        dropdown.style.top = curtop + 20;
    
        document.getElementsByTagName('body')[0].appendChild(dropdown);
    
        dropdown.focus();
        dropdown.onblur = go_away;
        
    }
    
    s.parentNode.appendChild(ddDiv);
    s.style.display = 'none';
}

function go_away() {
    this.style.display = 'none';
}

function getLogin() {
    if (xmlHttpLogin.readyState==4) { 
        var status = xmlHttpLogin.responseXML.getElementsByTagName('status')[0].getAttribute('value');
        if(status == '1') {
            var navIdLoc = searchUl.innerHTML.indexOf('navId=') + 6;
            var navId = searchUl.innerHTML.substring(navIdLoc);
            navId = navId.substring(0, navId.indexOf('"'));
        
            searchUl.innerHTML = "";
            
            var name = xmlHttpLogin.responseXML.getElementsByTagName('name')[0].getAttribute('value');
            
            var welcomeLi = document.createElement('li');
            var welcomeSpan = document.createElement('a');
            var notLi = document.createElement('li');
            var notA = document.createElement('a');
            
            welcomeSpan.className = 'clickThrough';
            welcomeSpan.innerHTML = 'Welcome, ' + name;
            welcomeSpan.href = lbsWebsite + 'register/do?navId=' + navId;
            welcomeLi.appendChild(welcomeSpan);
            searchUl.appendChild(welcomeLi);
            
            notA.className = 'clickThrough';
            notA.href = lbsWebsite + 'register/do?navId=' + navId + '&mode=Logout';
            notA.innerHTML = '(not ' + name + '?)';
            notLi.appendChild(notA);
            searchUl.appendChild(notLi);
        }
    }
}

function checkLogin() {
    xmlHttpLogin=GetXmlHttpLoginObject();
    xmlHttpLogin.onreadystatechange=getLogin;
    xmlHttpLogin.open("get",lbsWebsite + "assets/includes/registercheck/login.xml");
    xmlHttpLogin.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpLogin.send( null );
}

$(document).ready(function() {

    searchUl = $('#search ul').get(0);
    checkLogin();

    $('.infoDropdown').accordion({
        active: false, 
        alwaysOpen: false, 
        autoHeight: false,
        header: "h4.ddHeading"
    });

    $('.noscript').hide();

    if($.browser.msie) {
   
        selects = document.getElementsByTagName('select');
        for(i = 0; i < selects.length; i++) {
            if(selects[i].className != 'noFormatting') {
                lbs_dropdown(selects[i]);
            }
        }
    }
});