/**
 * Stream.People Namespace
 * Used for the people page on the new stream website.
 * Developed by Soho Pros., Inc.
 *
 * All private variables and functions begin with _.  They should not be referenced or called from outside of Stream.People.
 * -------------------------------------------------------------------------------------------------------------------------
 */

Stream.People = {
    _isSearching: false,
    _searchResults: [],
    city: '',
    _fName: null,
    _lName: null,
    _office: null,
    _dept: null,
    _isLoaded: false,

    init: function() { 
        $j(window).history(function(e, hash) { 
            if (hash == '') { 
                $j('#peopleFinder').fadeOut(200);
                setTimeout("$j('#divBackGround').show()", 200);
                setTimeout("$j('#peopleSearch').show()", 200);
                ddlSearchLocations.style.display = 'block';
                ddlSearchDepartments.style.display = 'block';
                $('birdTrail').innerHTML = '<a href=\"../default.aspx\">Home</a>&nbsp;&nbsp;&nbsp;>>&nbsp;&nbsp;&nbsp;<a href=\"default.aspx\">People</a>&nbsp;&nbsp;&nbsp;>>&nbsp;&nbsp;&nbsp;<a href=\"http://streamrealty.com/people/people-finder.aspx\">People Finder</a>';
            }
            else {
                $j('#peopleFinder').fadeIn(500);
                $j('#peopleSearch').hide();
                $j('#divBackGround').hide();
                
                var fnameIndex = hash.indexOf('fname=');
                if (fnameIndex != -1) {
                    var str = hash.substring(fnameIndex);
                    SPeople._fName = str.substring(6, str.indexOf('-'));
                } else SPeople._fName = '';
                
                var lnameIndex = hash.indexOf('lname=');
                if (lnameIndex != -1) {
                    var str = hash.substring(lnameIndex);
                    SPeople._lName = str.substring(6, str.indexOf('-'));
                } else SPeople._lName = '';
                
                var offIndex = hash.indexOf('off=');
                if (offIndex != -1) {
                    var str = hash.substring(offIndex );
                    SPeople._office = str.substring(4, str.indexOf('-'));
                }
                
                var deptIndex = hash.indexOf('dept=');
                if (deptIndex != -1) {
                    var str = hash.substring(deptIndex);
                    SPeople._dept = str.substring(5);//, str.indexOf('-'));
                }
                SPeople._doSearch();
            }
        });
        
        if (!SPeople._isLoaded) {
            var url = '' + window.location;
            //alert(url);
            var fnameIndex = url.indexOf('fname=');
            var doSearch = false;
            if (fnameIndex != -1) {
                var str = url.substring(fnameIndex);
                tbSearchFName.value = str.substring(6, str.indexOf('-'));
                SPeople._fName = str.substring(6, str.indexOf('-'));
                doSearch = true;
            } else SPeople._fName = '';
                
            var lnameIndex = url.indexOf('lname=');
            if (lnameIndex != -1) {
                var str = url.substring(lnameIndex);
                tbSearchLName.value = str.substring(6, str.indexOf('-'));
                SPeople._lName = str.substring(6, str.indexOf('-'));
                doSearch = true;
            } else SPeople._lName = '';
            
            var offIndex = url.indexOf('off=');
            if (offIndex != -1) {
                var str = url.substring(offIndex );
                SPeople._office = str.substring(4, str.indexOf('-'));
                doSearch = true;
            }
            
            var deptIndex = url.indexOf('dept=');
            if (deptIndex != -1) {
                var str = url.substring(deptIndex);
                SPeople._dept = str.substring(5);//, str.indexOf('-'));
                doSearch = true;
            }
                
            
            if (doSearch) { 
                SPeople._doSearch();
                new Effect.BlindUp('divBackGroundCover', { duration: 0 });
            } else {
                new Effect.BlindUp('divBackGroundCover', { duration: .5 });
                setTimeout("Effect.Appear('peopleSearch', { duration: 1, from: 0, to: .90 })", 500);
            }
        }
    },
    
    search: function() {
        if (!SPeople._isSearching) {
            SPeople._isSearching = true;
            btnSearch.disabled = true;
            SPeople._fName = tbSearchFName.value;
            SPeople._lName = tbSearchLName.value;
            SPeople._office = ddlSearchLocations.options[ddlSearchLocations.selectedIndex].value;
            SPeople._dept = ddlSearchDepartments.options[ddlSearchDepartments.selectedIndex].value;
            SPeople._doSearch();
        }
        return false;
    },
    
    _doSearch: function() {
        wsPeople.getPeopleBySearch(SPeople._fName, SPeople._lName, SPeople._office, SPeople._dept, SPeople._searchSuccess, SPeople._searchFailure);
        return false;
    },
    
    _searchSuccess: function(result) {
        if (result) {
            $('peopleFinderContent').innerHTML = '';
            
            if (result.length > 0) {
                // create the table and the table header
                var tbl = document.createElement('table');
                tbl.id = 'tblArticleList';
                tbl.style.width = '800px';
                tbl.style.margin = '0';
                tbl.border = 0;
                tbl.cellSpacing = 0;
                tbl.cellPadding = 0;
                tbl.style.marginTop = '0px';
                tbl.style.marginBottom = '25px';
                var tbdy = document.createElement('tbody');
                tbl.appendChild(tbdy);
                var hdRow = document.createElement('tr');
                var th1 = document.createElement('th');
                th1.className = 'peopleFinderTblHdrCell';
                th1.appendChild(document.createTextNode('Name'));
                hdRow.appendChild(th1);
                var th2 = document.createElement('th');
                th2.className = 'peopleFinderTblHdrCell';
                th2.style.paddingLeft = '15px';
                th2.style.width = '200px';
                th2.appendChild(document.createTextNode('Title'));
                hdRow.appendChild(th2);
                var th3 = document.createElement('th');
                th3.className = 'peopleFinderTblHdrCell';
                th3.style.paddingLeft = '15px';
                th3.appendChild(document.createTextNode('Department'));
                hdRow.appendChild(th3);
                var th4 = document.createElement('th');
                th4.className = 'peopleFinderTblHdrCell';
                th4.style.paddingLeft = '15px';
                th4.appendChild(document.createTextNode('Location'));
                hdRow.appendChild(th4);
                var th5 = document.createElement('th');
                th5.className = 'peopleFinderTblHdrCell';
                th5.style.paddingLeft = '15px';
                th5.appendChild(document.createTextNode('V-card'));
                th5.style.textAlign = 'center';
                th5.style.width = '45px';
                hdRow.appendChild(th5);
                var th6 = document.createElement('th');
                th6.className = 'peopleFinderTblHdrCell';
                th6.style.paddingLeft = '15px';
                th6.style.width = '40px';
                th6.appendChild(document.createTextNode('E-mail'));
                th6.style.textAlign = 'center';
                hdRow.appendChild(th6);
                tbdy.appendChild(hdRow);
                
                // loop through all of the people and add them
                for (var i = 0; i < result.length; i++) {
                    var tr = document.createElement('tr');
                    var td1 = document.createElement('td');
                    td1.className = 'peopleFinderTblCell';
                    var link = '<a onmouseover=\"this.style.textDecoration = \'underline\';\" onmouseout=\"this.style.textDecoration = \'none\';\" style=\"color: #0084a9; font-weight: normal; text-decoration: none;\" href=\"bio.aspx?aid=' + result[i].aid + '\">' + result[i].name + '</a>';
                    td1.innerHTML = link;
                    tr.appendChild(td1);
                    var td2 = document.createElement('td');
                    td2.className = 'peopleFinderTblCell';
                    td2.style.paddingLeft = '15px';
                    td2.style.width = '200px';
                    td2.appendChild(document.createTextNode(result[i].title));
                    tr.appendChild(td2);
                    var td3 = document.createElement('td');
                    td3.className = 'peopleFinderTblCell';
                    td3.style.paddingLeft = '15px';
                    td3.appendChild(document.createTextNode(result[i].department));
                    tr.appendChild(td3);
                    var td4 = document.createElement('td');
                    td4.className = 'peopleFinderTblCell';
                    td4.style.paddingLeft = '15px';
                    td4.appendChild(document.createTextNode(result[i].office));
                    tr.appendChild(td4);
                    var td5 = document.createElement('td');
                    td5.className = 'peopleFinderTblCell';
                    td5.style.paddingLeft = '15px';
                    td5.style.textAlign = 'center';
                    if (result[i].vcard != null) {
                        var vcard = '<a style=\"text-decoration: none;\" target=\"_blank\" href=\"http://www.streamrealty.com/vcards/' + result[i].vcard + '\"><img src=\"http://www.streamrealty.com/images/vcard.jpg\" alt=\"" /></a>';
                        td5.innerHTML = vcard;
                    } else {
                        td5.innerHTML = '&nbsp;';
                    }
                    tr.appendChild(td5);
                    var td6 = document.createElement('td');
                    td6.className = 'peopleFinderTblCell';
                    td6.style.paddingLeft = '15px';
                    td6.style.textAlign = 'center';
                    var a = '<a href=\"mailto:' + result[i].email + '\"><img src=\"http://www.streamrealty.com/images/envelope.gif\" alt=\"email\" /></a>';
                    td6.innerHTML = a;
                    tr.appendChild(td6);
                    tr.onmouseover = function() { this.style.backgroundColor = '#f3f3f3'; return false; };
                    tr.onmouseout = function() { this.style.backgroundColor = 'white'; return false; };
                    tbdy.appendChild(tr);
                }
                $('peopleFinderContent').appendChild(tbl);
            }
            else {
                $('peopleFinderContent').innerHTML += 'No results found.';
            }
            
            var b = navigator.appVersion; 
            var version = b.charAt(22);
            if ((ie) && (version == 6)) {
                ddlSearchLocations.style.display = 'none';
                ddlSearchDepartments.style.display = 'none';
                $('peopleFinder').style.zIndex = -1000;
                new Effect.Appear('peopleFinder', { duration: 0, from: 0, to: 0.1 });
                if ($('peopleFinder').clientHeight < 500) {
                    $('peopleFinder').style.height = '500px';
                }
                $('peopleFinder').style.zIndex = 1000;
            }   
            else {
                $('peopleFinder').style.minHeight = '500px';
            }
            $j('#peopleFinder').fadeIn(500);
            $j('#peopleSearch').hide();
            $j('#divBackGround').hide();
            $('birdTrail').innerHTML += '&nbsp;&nbsp;&nbsp;>>&nbsp;&nbsp;&nbsp;Results';
        }
        
        SPeople._isLoaded = true;
        SPeople._isSearching = false;
        btnSearch.disabled = false;
        
        if ((SPeople._fName.length > 0) && (SPeople._lName.length > 0)) {
            $j.history.add('fname=' + SPeople._fName + '-lname=' + SPeople._lName + '-off=' + SPeople._office + '-dept=' + SPeople._dept);
        } else if ((SPeople._fName.length > 0) && (!SPeople._lName.length > 0)) {
            $j.history.add('fname=' + SPeople._fName + '-off=' + SPeople._office + '-dept=' + SPeople._dept);
        } else if ((!SPeople._fName.length > 0) && (SPeople._lName.length > 0)) {
            $j.history.add('lname=' + SPeople._lName + '-off=' + SPeople._office + '-dept=' + SPeople._dept);
        } else if ((!SPeople._fName.length > 0) && (!SPeople._lName.length > 0)) {
            $j.history.add('off=' + SPeople._office + '-dept=' + SPeople._dept);
        }
        
    },
    
    _searchFailure: function(error) {
        SPeople._isSearching = false;
        btnSearch.disabled = false;
    },
    
    closeSearch: function() {
        $j('#peopleFinder').fadeOut(200);
        setTimeout("$j('#divBackGround').show()", 200);
        setTimeout("$j('#peopleSearch').show()", 200);
        ddlSearchLocations.style.display = 'block';
        ddlSearchDepartments.style.display = 'block';
        $('birdTrail').innerHTML = SPeople.city + ' <a href=\"../people/default.aspx\">People</a>&nbsp;&nbsp;&nbsp;>>&nbsp;&nbsp;&nbsp;<a href=\"../people/people-finder.aspx\">People Finder</a>';
        $j.history.add('');
        return false;
    },
    
    showDayInLife: function() {
        if (ie) {
            ddlSearchLocations.style.display = 'none';
            ddlSearchDepartments.style.display = 'none';
        }
        new Effect.Appear('aDayInTheLife', { duration: 0.5, from: 0, to: 1 });
        return false;
    },
    
    closeDayInLife: function() {
        new Effect.Fade('aDayInTheLife', { duration: 0.2, from: 0.99, to: 0 });
        ddlSearchLocations.style.display = 'block';
        ddlSearchDepartments.style.display = 'block';
        return false;
    },
    
    showInTheCommunity: function() {
        if (ie) {
            ddlSearchLocations.style.display = 'none';
            ddlSearchDepartments.style.display = 'none';
        }
        new Effect.Appear('inTheCommunity', { duration: 0.5, from: 0, to: 1 });
        return false;
    },
    
    closeInTheCommunity: function() {
        new Effect.Fade('inTheCommunity', { duration: 0.2, from: 0.99, to: 0 });
        ddlSearchLocations.style.display = 'block';
        ddlSearchDepartments.style.display = 'block';
        return false;
    }

};