﻿var bDiagOutEnabled = false;

var hover_stars = 0;

    function srs_toggle(ixStar) {
        nStars = ixStar;
        var sPostParams = 'op=stars&post_id=' + admCurrentPostId + '&data=' + nStars;

        diag_out(sPostParams);

        sbtvAjax.Post('/ajax/ACToolbar.aspx', sPostParams, srs_handleResponse);
    }

    function srs_handleResponse(sResponse) {
        var aStarSeg = sResponse.split('<!--CONTENT_MARKER-->')[1].split('|');
        var updated_stars = aStarSeg[0];
        var rating_count = aStarSeg[1];
        srs_display(updated_stars, rating_count);
    }


    function srs_display(display_stars, rating_count) {
        nStars = display_stars;
        var bp = (20 * (nStars-5)).toString() + 'px 0px';
        var div_stars = document.getElementById('srs_stars');
        diag_out('srs_display() bp >' + bp + '<  rating_count >' + rating_count + '<');
        div_stars.style.backgroundPosition = bp;

        var sRating = '';
        
        switch (rating_count) {
            case '':
                sRating = 'Not yet rated';
                break;
            case '1':
                sRating = 'Rated once';
                break;
            default:
                sRating = 'Rated ' + rating_count + ' times';
                break;
        }
        div_stars.title = sRating + '. Click a Star to add your rating';
    }
    
    var actPostId = 0;
    var actPostTitle = '';
    var actPanelOp = '';

    var divFavorite;
    var divComment;
    var divNumbComments;

    var divShowComments;
    var divShowMyComment;

    var linkShowComments;
    var linkHideComments;

    var divShare;
    var divEmbed;

    var sCurrent = '';

    function actInit() {
        divFavorite = document.getElementById('favorite');
        divComment = document.getElementById('comment');

        divShowComments = document.getElementById('commentSection');
        divShowMyComment = null;
        divNumbComments = document.getElementById('numberOcomments');

        linkShowComments = document.getElementById('showAllLink');
        linkHideComments = document.getElementById('hideAllLink');

        divShare = document.getElementById('share');
        divEmbed = document.getElementById('embed');
        
    }

    function actFave() {
        actInit();
        
        if(sCurrent != divFavorite){
            sCurrent = divFavorite;

            var sParams = 'op=fave&post_id=' + actPostId;
            sbtvAjax.Post('/ajax/ACToolbar.aspx', sParams, handleACToolbarResponse);
        }
        else{
            sCurrent = '';
        }
        
        close_unselected();
    }

    function actComment() {
        actInit();
        
        if(sCurrent!= divComment){
            sCurrent = divComment;
            
            actSetPostFromAdm();

            var sParams = 'op=comment&post_id=' + actPostId;
            sbtvAjax.Post('/ajax/ACToolbar.aspx', sParams, handleACToolbarResponse);
        }
        
        else{
            sCurrent='';
        }
        
        close_unselected();
    }

    function sSubmitNewComment() {
        actInit();
        
        var txtComment = document.getElementById('makeComment').value;
        
        actSetPostFromAdm();
        
        var escapeTxtComment=escape(txtComment);
        
        var sParams = 'op=savecomt&post_id=' + actPostId + "&data=" + escapeTxtComment;
        
        sbtvAjax.Post('/ajax/ACToolbar.aspx', sParams, handleACToolbarResponse);
    }

    function showAllComments() {

        actInit();
        
        if(divShowComments.style.display=='block')
        {
            divShowComments.style.display = 'none';
        }
        else
        {
            var sParams = 'op=showcomt&post_id=' + actPostId;
            sbtvAjax.Post('/ajax/ACToolbar.aspx', sParams, handleShowCommentResponse);
        }
    }

    function actShare() {
        actInit();
        if(sCurrent != divShare){
            sCurrent = divShare;

            var divShareTextLink = document.getElementById('shareTextLink');

            actSetPostFromAdm();
            var sTextLink = site_domain_root + 'topics/' + actPostId;

            divShareTextLink.value = sTextLink;
        }
        else{
            sCurrent = '';
        }
        
        close_unselected();

    }

    function sbtv_taf_onmouseover(btn) {
        STTAFFUNC.showHoverMap(btn, '2009041615514-1', site_domain_root + 'topics/' + actPostId, actPostTitle);
    }

    function sbtv_taf_onclick(btn) {
        STTAFFUNC.cw(btn, { id: '2009041615514-1', link: site_domain_root + 'topics/' + actPostId, title: actPostTitle });
    }

    function actEmbed() {
        actInit();
        if(sCurrent!=divEmbed){
            sCurrent = divEmbed;

            actSetPostFromAdm();

            var divEmbedCode = document.getElementById('shareEmbedCode');
            var divTextLink = document.getElementById('shareTextEmbedLink');
            var divHomeEmbedCode = document.getElementById('shareHomeEmbedCode');
            
            var sEmbedTextLink = '<a href="' + site_domain_root + 'topics/' + actPostId + '">'
                + actPostTitle
                + '</a>';
            
            var sEmbedCodeText = '<iframe src="' + site_domain_root + 'Pages/Embed/EmbedPost.aspx?post_id=' + actPostId + '" style="width:210px;height:195px;" frameborder="0" scrolling="no" ></iframe>';
            
            var sEmbedHomeLink = '<iframe src="' + site_domain_root + 'Pages/Embed/EmbedPost.aspx" style="width:210px;height:195px;" frameborder="0" scrolling="no" ></iframe>';

            divTextLink.value = sEmbedTextLink;
            divEmbedCode.value = sEmbedCodeText;
            divHomeEmbedCode.value = sEmbedHomeLink;
            
        }
        else{
            sCurrent='';
        }
        
        close_unselected();
    }

    function close_unselected() {
        actInit();
        if (sCurrent == '') {
            divComment.style.display = 'none';
            divEmbed.style.display = 'none';
            divFavorite.style.display = 'none';
            divShare.style.display = 'none';
        }

        if (sCurrent == divComment) {
            divComment.style.display = 'block';
            divEmbed.style.display = 'none';
            divFavorite.style.display = 'none';
            divShare.style.display = 'none';
        }

        if (sCurrent == divEmbed) {
            divComment.style.display = 'none';
            divEmbed.style.display = 'block';
            divFavorite.style.display = 'none';
            divShare.style.display = 'none';
        }

        if (sCurrent == divFavorite) {
            divComment.style.display = 'none';
            divEmbed.style.display = 'none';
            divFavorite.style.display = 'block';
            divShare.style.display = 'none';
        }

        if (sCurrent == divShare) {
            divComment.style.display = 'none';
            divEmbed.style.display = 'none';
            divFavorite.style.display = 'none';
            divShare.style.display = 'block';
        }
    }

    function select_all(currentTxtBox) {
        
        if (sCurrent == divShare) {
            document.getElementById('shareTextLink').focus();
            document.getElementById('shareTextLink').select();
        }
        if (sCurrent == divEmbed) {
            document.getElementById(currentTxtBox).focus();
            document.getElementById(currentTxtBox).select();
        }
        else {
        
        }
    }
    
    function actSetPostFromAdm() {

        actPostId = admCurrentPostId;
        actPostTitle = admCurrentPostTitle;

    }

    function handleACToolbarResponse(sResponse) {
        actInit();
        var aResponseSeg;
        if (sCurrent == divComment) {
            aResponseSeg = sResponse.split(sSbtvAjaxContentMarker);
            divComment.innerHTML = aResponseSeg[1];
        }
        if (sCurrent == divFavorite) {
            aResponseSeg = sResponse.split(sSbtvAjaxContentMarker);
            divFavorite.innerHTML = aResponseSeg[1];
        }
        else {
        }
    }

    function showAllComments() {
        actInit();
        actSetPostFromAdm();
        
        if (divShowComments.style.display == 'block') {
            divShowComments.style.display = 'none';
        }
        else {
            var sParams = 'op=showcomt&post_id=' + actPostId;
            sbtvAjax.Post('/ajax/ACToolbar.aspx', sParams, handleShowCommentResponse);
        }
    }

    function handleShowCommentResponse(sResponse) {
        actInit();
        
        var aResponseSeg;

        aResponseSeg = sResponse;
        
        divShowMyComment = document.getElementById('displayMyComment');
        
        if (divShowMyComment != null) {

            if (divShowMyComment.style.display = 'block') {
            
                divShowMyComment = document.getElementById('displayMyComment');
                divShowMyComment.style.display = 'none';
            }
            
            else{
                
            }
        }
        else {

        }
        linkShowComments.style.display = 'none';
        linkHideComments.style.display = 'block';
        divShowComments.style.display = 'block';
        divShowComments.innerHTML = aResponseSeg;
    }

    function hideAllComments() {
        actInit();
        linkShowComments.style.display = 'block';
        linkHideComments.style.display = 'none';
        divShowComments.style.display = 'none';
    }

    function updateACTbar() {
        actInit();
        actSetPostFromAdm();
        var sParams = 'op=updtact&post_id=' + actPostId;
        sbtvAjax.Post('/ajax/ACToolbar.aspx', sParams, handleUpdateACTResponse);
        diag_out('updateACTbar() sParams >' + sParams + '<');
    }

    function handleUpdateACTResponse(sResponse) {

        var aResponseSeg;
        var star_count;

        var aResponseSeg = sResponse.split('<!--CONTENT_MARKER-->');
        aActSeg = aResponseSeg[1].split('|');

//        diag_out('  admCurrentClipType=' + admCurrentClipType);
//        diag_out('  ixCurrentClip=' + ixCurrentClip + ' PostId=' + admCurrentPostId);
//        diag_out('handleUpdateACTResponse() stars:' + aActSeg[0] + ' comments:' + aActSeg[1]);
        var div_tbl = document.getElementById('actoolbar_tbl');
        if (admCurrentClipType.indexOf('Ad') != -1) {
            div_tbl.style.display = 'none';
        }
        else {
            div_tbl.style.display = 'block';
            srs_display(aActSeg[0],aActSeg[1]);
            divNumbComments.innerHTML = aActSeg[2];
        }
    }

    function handleACTStarsResponse(sResponse) {
        var aResponseSeg;
        aResponseSeg = sResponse.split('|');

        aStars = aResponseSeg[0].split('<!--CONTENT_MARKER-->');
        srs_display(aStars[1]);
    }

    var sDiagOutMsg = '';
    var sDiagOutClear =
        '<div'
        + ' style="'
        + 'height:14px;'
        + 'width: 298px;'
        + 'color: white;'
        + 'font: bold 11px arial;'
        + '"'
        + '>'

        + '<div'
        + ' style="'
        + 'float: left;'
        + 'height:14px;'
        + 'width: 80px;'
        + 'padding: 0px 5px 0px 5px;'
        + 'text-align: center;'
        + 'cursor: pointer;'
        + '"'
        + ' onclick="diag_out_clear()"'
        + '>'
        + 'clear'
        + '</div>'

        + '<div'
        + ' id="toggle_diag_out"'
        + ' style="'
        + 'float: left;'
        + 'height:14px;'
        + 'width: 120px;'
        + 'padding: 0px 5px 0px 5px;'
        + 'text-align: center;'
        + 'cursor: pointer;'
        + '"'
        + ' onclick="diag_out_toggle()"'
        + '>'
        + '</div>'

        + '</div>';


    function diag_out(msg) {
        if (bDiagOutEnabled != true) {
            return;
        }
        if (msg == '') {
            sDiagOutMsg = '';
        }
        else
        {
            sDiagOutMsg =
            '<tr>'
            + '<td>'
            + msg
            + '</td>'
            + '</tr>'
            + sDiagOutMsg
        }
//        document.getElementById('sbtv_home_link').innerHTML =
        document.getElementById('diag').innerHTML =
        '<div'
        + ' style="'
        + 'border:1px red solid;'
        + 'height:248px;'
        + 'background-color: white;'
        + 'border: 2px red solid;'
        + '"'
        + '>'
        + '<div'
        + ' style="'
        + 'height:234px;'
        + 'background-color: white;'
        + 'overflow: auto;'
        + '"'
        + '>'
        + '<table style="border-collapse:collapse;">'
        + ((sDiagOutMsg != '') ? sDiagOutMsg : '<tr><td><div style="color:#a0a0a0;">(empty)</div></td></tr>')
        + '</table>'
        + '</div>'
        + sDiagOutClear
        + '</div>';

        diag_out_toggle_update();
        
    }

    function diag_out_clear() {
        diag_out('');
    }

    function diag_out_toggle() {
        bDiagOutEnabled = !bDiagOutEnabled;
        diag_out_toggle_update();
    }

    function diag_out_toggle_update() {
        document.getElementById('toggle_diag_out').innerHTML = (bDiagOutEnabled ? 'disable' : 'enable') + ' diag_out'
    }