﻿/**
 * Stream.News Namespace
 * Used for the news menu on the about stream page.
 * Developed by Soho Pros., Inc.
 *
 * All private variables and functions begin with _.  They should not be referenced or called from outside of Stream.Page.
 * -----------------------------------------------------------------------------------------------------------------------
 */
Stream.News = {
    /***** Variables *****/
    _leaders: [],
    _isLeadershipOpen: false,
    _articles: [],
    _curPage: 1,
    _maxArticlesInList: 20,
    _isArticleOpen: false,
    _isLocation: false,
    _location: 0,
    _dallas: 1000,
    _austin: 2000,
    _houston: 3000,
    _sanantonio: 4000,
    _isNewsPage: false,
    _isLeadershipPage: false,
    /***** End Variables *****/
    
    /***** Functions ******/
    /**
     * function init()
     * Called on window.onload, will call the webservices to get the articles and leadership people
     */
    init: function() {
        if (!SN._isLocation) {
            if (SN._isLeadershipPage) {
                wsAbout.getLeadershipPeople(SN._getLeadershipSuccess, SN._getLeadershipFailure);
            }
            else {
                wsAbout.getArticles(SN._getArticlesSuccess, SN._getArticlesFailure);
            }
        }
        else {  
            if (SN._isLeadershipPage) {
                wsAbout.getLeadershipPeopleByLocation(SN._location, SN._getLeadershipSuccess, SN._getLeadershipFailure);
            }
            else {
                wsAbout.getArticlesByLocation(SN._location, SN._getArticlesSuccess, SN._getArticlesFailure);
            }
        }
    },
    
    
    /**
     * function _getLeadershipSuccess(result)
     * A private callback function that is called after successful execution fo wsAbout.getLeadershipPeople.
     * Will create the table with the list of people in the leadership positions of the company nationwide.
     * @param result - A list of leadeshipPeople objects defined in App_Code/Classes/News.vb
     */
    _getLeadershipSuccess: function(result) {
        if (result) {
            SN._leaders = result;
            
            // create the table and the table header
            var tbl = document.createElement('table');
            tbl.id = 'tblArticleList';
            tbl.border = 0;
            tbl.cellSpacing = 0;
            tbl.cellPadding = 0;
            tbl.style.width = '800px';
            var tbdy = document.createElement('tbody');
            tbl.appendChild(tbdy);
            var hdRow = document.createElement('tr');
            var th1 = document.createElement('th');
            th1.className = 'leadershipTblHdrCell';
            th1.appendChild(document.createTextNode('Name'));
            hdRow.appendChild(th1);
            var th2 = document.createElement('th');
            th2.className = 'leadershipTblHdrCell';
            th2.style.paddingLeft = '15px';
            th2.appendChild(document.createTextNode('Title'));
            hdRow.appendChild(th2);
            var th3 = document.createElement('th');
            th3.className = 'leadershipTblHdrCell';
            th3.style.paddingLeft = '15px';
            th3.appendChild(document.createTextNode('Location'));
            hdRow.appendChild(th3);
            var th4 = document.createElement('th');
            th4.className = 'leadershipTblHdrCell';
            th4.style.paddingLeft = '15px';
            th4.appendChild(document.createTextNode('V-card'));
            th4.style.textAlign = 'center';
            hdRow.appendChild(th4);
            var th5 = document.createElement('th');
            th5.className = 'leadershipTblHdrCell';
            th5.style.paddingLeft = '15px';
            th5.style.textAlign = 'center';
            th5.appendChild(document.createTextNode('E-mail'));
            hdRow.appendChild(th5);
            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 = 'leadershipTblCell';
                td1.style.color = '#0084a9';
                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 = 'leadershipTblCell';
                td2.style.paddingLeft = '15px';
                td2.appendChild(document.createTextNode(result[i].title));
                tr.appendChild(td2);
                var td3 = document.createElement('td');
                td3.className = 'leadershipTblCell';
                td3.style.paddingLeft = '15px';
                td3.appendChild(document.createTextNode(result[i].office));
                tr.appendChild(td3);
                var td4 = document.createElement('td');
                td4.className = 'leadershipTblCell';
                td4.style.paddingLeft = '15px';
                td4.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>';
                    td4.innerHTML = vcard;
                } else {
                    td4.innerHTML = '&nbsp;';
                }
                tr.appendChild(td4);
                var td5 = document.createElement('td');
                td5.className = 'leadershipTblCell';
                td5.style.paddingLeft = '15px';
                td5.style.textAlign = 'center';
                var a = '<a href=\"mailto:' + result[i].email + '\"><img src=\"http://www.streamrealty.com/images/envelope.gif\" alt=\"email\" /></a>';
                td5.innerHTML = a;
                tr.appendChild(td5);
                tbdy.appendChild(tr);
            }
            
            $('leadershipContent').appendChild(tbl);
        }
    },
    
    _getLeadershipFailure: function(result) {
    
    },
    
    showLeadership: function() {
        var b = navigator.appVersion; 
        var version = b.charAt(22);
        if ((ie) && (version == 6)) {
            $('leadership').style.zIndex = -1000;
            new Effect.Appear('leadership', { duration: 0, from: 0, to: 0.01 });
            if ($('leadership').clientHeight < (subCatContainer.offsetHeight - 45)) {
                $('leadership').style.height = (subCatContainer.offsetHeight - 45) + 'px';
            }
            $('leadership').style.zIndex = 1000;
        }   
        else {
            $('leadership').style.minHeight = (subCatContainer.offsetHeight - 45) + 'px';
        }
        new Effect.Appear('leadership', { duration: 0.5, from: 0, to: 1 });
        SN._isLeadershipOpen = true;
        return false;
    },
    
    closeLeadership: function() {
        new Effect.Fade('leadership', { duration: 0.2, from: 0.99, to: 0 });
        SN._isLeadershipOpen = false;
        return false;
    },
    
    _getArticlesSuccess: function(result) {
        if ((result) && (SN._isNewsPage)) { 
            SN._articles = result;
            
            // Loop through the articles and add them
            var lastArticle = (result.length > SN._maxArticlesInList) ? SN._maxArticlesInList : result.length;
            SN._createArticleTable(0, SN._maxArticlesInList);
            
                        
            // Build pager
            if (result.length > SN._maxArticlesInList) {
                SN._curPage = 1;
                $j( '#backNextWrapper' ).css( 'display', 'inline' );
                $j( '#next' ).css( 'display', 'inline' );
                $j( '#pager' ).text( '1 - ' + SN._maxArticlesInList + ' of ' + result.length + ' articles' );
            }
            else {
                $j( '#pager').text( '1 - ' + result.length + ' of ' + result.length + ' articles' );
            }
        }
    },
    
    _getArticlesFailure: function(result) {
        alert('error');
    },
    
    articlesBack: function() {
        if (!SN._isArticleOpen) {
            var nextArticle = (SN._maxArticlesInList * --SN._curPage) - SN._maxArticlesInList;
            var lastArticle = nextArticle + SN._maxArticlesInList;
            //if (lastArticle > SN._articles.length) lastArticle = SN._articles.length;
            
            SN._createArticleTable(nextArticle, lastArticle);
            
            //var tbl = SN._createArticleTable(nextArticle, lastArticle);
            
            //if ( $j( '#tblArticleList' ) ) $j( '#recentArticles' ).empty();
            //$j( '#recentArticles' ).append( tbl );
            
            // Build pager
            if (nextArticle > 0) {
                $j( '#back' ).css( 'display', 'inline' );
            }
            else {
                $j( '#back' ).css( 'display', 'none' );
            }
            $j( '#pager' ).text( (nextArticle + 1) + ' - ' + lastArticle + ' of ' + SN._articles.length + ' articles' );
            $j( '#next' ).css( 'display', 'inline' );
        }
    },
    
    articlesNext: function() {
        if (!SN._isArticleOpen) {
            var nextArticle = (SN._maxArticlesInList * SN._curPage);
            var lastArticle = SN._maxArticlesInList * ++SN._curPage;
            if (lastArticle > SN._articles.length) lastArticle = SN._articles.length;
            
            SN._createArticleTable(nextArticle, lastArticle);
            
            //var tbl = SN._createArticleTable(nextArticle, lastArticle);
            
            //if ( $j( '#tblArticleList') ) $j( '#recentArticles' ).empty();
            //$j( '#recentArticles' ).append( tbl );
            
            // Build pager
            if (lastArticle < SN._articles.length) {
                $j( '#next' ).css( 'display', 'inline' );
            }
            else {
                $j( '#next' ).css( 'display', 'none' );
            }
            $j( '#pager' ).text( (nextArticle + 1) + ' - ' + lastArticle + ' of ' + SN._articles.length + ' articles' );
            $j( '#back').css( 'display', 'inline' );
        }
    },
    
    _createArticleTable: function(nextArticle, lastArticle) {
        $j( '#tblArticleList1' ).remove();
    
        var index = nextArticle;
        var tbl1 = document.createElement('table');
        tbl1.id = 'tblArticleList1';
        tbl1.border = 0;
        tbl1.cellSpacing = 0;
        tbl1.cellPadding = 0;
        tbl1.style.width = '435px';
        tbl1.style.margin = '0 auto';
        var tbdy = document.createElement('tbody');
        tbl1.appendChild(tbdy);
        
        for (var i = 0; i < SN._maxArticlesInList / 2; i++) { 
            if ( index < SN._articles.length ) {
                var tr = document.createElement('tr');
                tr.className = 'articleListRow'; 
                var td1 = document.createElement('td');
                td1.className = 'articleListTblCell';
                td1.style.color = '#666';
                td1.style.paddingLeft = '0px';
                td1.style.paddingRight = '10px';
                td1.valign = 'top';
                var date = '<span style=\"font-weight: bold\">' + SN._articles[index].dateList + '</span>';
                td1.innerHTML = date;
                tr.appendChild(td1);
                var pub = '<span style=\"font-style: italic;\">' + SN._articles[index].publication + '</span><br />';
                var a = '<a class=\"articleLink\" target=\"_blank\" style=\"color: #666;\" href=\"article.aspx?articleID=' + SN._articles[index].id + '\">' + SN._articles[index].title + '</a>';
                var td2 = document.createElement('td');
                td2.className = 'articleListTblCell';
                td2.style.color = '#666';
                td2.style.paddingLeft = '0px';
                td2.innerHTML = pub + a;
                tr.appendChild(td2);
                tbdy.appendChild(tr);
                index++;
            }
        }
        $j( '#recentArticlesLeft' ).empty().append( tbl1 );
        
        $j( '#tblArticleList2' ).remove();
        
        if ( SN._articles.length > lastArticle ) {
            var tbl2 = document.createElement('table');
            tbl2.id = 'tblArticleList2';
            tbl2.border = 0;
            tbl2.cellSpacing = 0;
            tbl2.cellPadding = 0;
            tbl2.style.width = '435px';
            tbl2.style.margin = '0 auto';
            var tbdy2 = document.createElement('tbody');
            tbl2.appendChild(tbdy2);
            for (var i = SN._maxArticlesInList / 2; i < SN._maxArticlesInList; i++) {
                var tr = document.createElement('tr');
                tr.className = 'articleListRow'; 
                var td1 = document.createElement('td');
                td1.className = 'articleListTblCell';
                td1.style.color = '#666';
                td1.style.paddingLeft = '0px';
                td1.style.paddingRight = '10px';
                td1.valign = 'top';
                var date = '<span style=\"font-weight: bold\">' + SN._articles[index].dateList + '</span>';
                td1.innerHTML = date;
                tr.appendChild(td1);
                var pub = '<span style=\"font-style: italic;\">' + SN._articles[index].publication + '</span><br />';
                var a = '<a class=\"articleLink\" target=\"_blank\" style=\"color: #666;\" href=\"article.aspx?articleID=' + SN._articles[index].id + '\">' + SN._articles[index].title + '</a>';
                var td2 = document.createElement('td');
                td2.className = 'articleListTblCell';
                td2.style.color = '#666';
                td2.style.paddingLeft = '0px';
                td2.innerHTML = pub + a;
                tr.appendChild(td2);
                tbdy2.appendChild(tr);
                index++;
            }
            $j( '#recentArticlesRight' ).empty().append(tbl2);
        }
    },
    
    showArticle: function(articleID) {  
        if (!SN._isArticleOpen) {
            $('articleHdr').innerHTML = SN._articles[articleID].title;
            $('publication').innerHTML = 'As seen in ' + SN._articles[articleID].publication;
            $('pubDate').innerHTML = SN._articles[articleID].pubDate;
            $('author').innerHTML = SN._articles[articleID].author;
            $('articleBody').innerHTML = SN._articles[articleID].body;
            var b = navigator.appVersion; 
            var version = b.charAt(22);
            if ((ie) && (version == 6)) {
                $('article').style.zIndex = -1000;
                new Effect.Appear('article', { duration: 0, from: 0, to: 0.01 });
                if ($('article').clientHeight < (subCatContainer.offsetHeight - 45)) { 
                    $('article').style.height = (subCatContainer.offsetHeight - 45) + 'px';
                }
                $('article').style.zIndex = 1000;   
            }   
            else {
                $('article').style.minHeight = (subCatContainer.offsetHeight - 45) + 'px';
            }
            new Effect.Appear('article', { duration: 0.5, from: 0, to: 1 });
            SN._isArticleOpen = true;
        }
        return false;
    },
    
    closeArticle: function() {
        new Effect.Fade('article', { duration: 0.2, from: 0.99, to: 0 });
        SN._isArticleOpen = false;
        return false;
    },
    
    showNewsLinks: function() {
        $('innerSubMenuDiv').hide();
        $('innerSubNewsMenu').show();
    },
    
    showDefaultLinks: function() {
        $('innerSubNewsMenu').hide();
        $('innerSubMenuDiv').show();
    }
};