var temp_json={}

function FullStory_AB_callBack(transpot) 
{
    try
    {
        var tempText = transpot.responseText.gsub(/\s/,' ');
        FullStory_AB = ('/*-secure-\n'+tempText+'\n*/').evalJSON();
    }catch(e)
    {
        //alert(e);
    }
    FullStory_AB.info = info;
    
    /*print start*/
    //$('printArea').update(templates.FullStory_AB_print.evaluate(FullStory_AB));
    $('printArea').update($('forPrint').innerHTML);
    /*print end*/
    
    FullStory_AB.commentsHtml = "";
    temp_json.comment_table_id = 1;
    temp_json.comment = "#{comment}";
    temp_json.userid = "#{userid}";
    temp_json.datPost = "#{datPost}";
    //alert(FullStory_AB.ads)
    
    FullStory_AB.comments.each(
    function(comment){
        var temp_comment_template = new Template(templates.Comments.evaluate(temp_json));
        FullStory_AB.commentsHtml += temp_comment_template.evaluate(comment);
        temp_json.comment_table_id = temp_json.comment_table_id + 1;
    });
    var show_comment_paging = "";
    var temp_total_comment = parseInt(FullStory_AB.TotalComments,10);
    if (temp_total_comment > 0) {
		show_comment_paging = show_comment_paging + "Pages: ";
        var comment_p_no = Math.ceil(temp_total_comment / 10);
        for (var temp_com_no = 1; temp_com_no <= comment_p_no; temp_com_no++) {
            show_comment_paging = show_comment_paging + "<a href=\'javascript:DisplayComment("+temp_com_no+","+temp_total_comment+")\' id=\'comment_pages_"+temp_com_no+"\'>"+temp_com_no+"</a> ";
        }
    }
    //alert(show_comment_paging);
    FullStory_AB.total_Comments = show_comment_paging;
    FullStory_AB.comment = templates.Comment.evaluate(FullStory_AB);
    
    
    FullStory_AB.sectionNewsShow = FullStory_AB.sectionNews.length > 0?"show":"notShow";
    FullStory_AB.sectionNewsHtml = "";
    FullStory_AB.sectionNews.each(
    function(news){
        news.ParentCategory = FullStory_AB.ParentCategory;
        FullStory_AB.sectionNewsHtml += templates.SubNews.evaluate(news);
    });
    //alert(FullStory_AB.relatedNews.length)
    FullStory_AB.relatedNewsShow = FullStory_AB.relatedNews.length > 0?"show":"notShow";
    FullStory_AB.relatedNewsHtml = "";
    FullStory_AB.relatedNews.each(
    function(news){
        FullStory_AB.relatedNewsHtml += templates.SubNews.evaluate(news);
    });
    
    
    FullStory_AB.leftSectionShow = FullStory_AB.relatedNewsShow == "notShow" && FullStory_AB.sectionNewsShow == "notShow"?"notShow":"show";
    
    // share news
    FullStory_AB.shareDetail = "";
    FullStory_AB.shareNews.each(
    function(share){
        
        share.t_url = share.url;
        var urlTemplate = new Template(share.t_url);
        share.URL = encodeURIComponent(FullStory_AB.locationURL);
        share.Title = encodeURIComponent(FullStory_AB.Headline).replace(/'/g,"%27").replace(/"/g,"%22");
        share.Description = encodeURIComponent(FullStory_AB.LeadIN).replace(/'/g,"%27").replace(/"/g,"%22");
        share.f_url = urlTemplate.evaluate(share);
        FullStory_AB.shareDetail += templates.ShareDetail.evaluate(share);
        
    });
    FullStory_AB.share = templates.Share.evaluate(FullStory_AB);
    
    FullStory_AB.pageHtml = '<div class="page_link">';
    
    if(FullStory_AB.totalPage != 1)
    {
        if(FullStory_AB.currentPage != 1)FullStory_AB.pageHtml += ' <a href="javascript:chinadaily()" onClick="changeFullstoryPage('+(parseInt(FullStory_AB.currentPage)-1)+',\'FullStory_AB\',\''+FullStory_AB.contentID+'\')">Previous Page</a> ';
        for(var i=1;i<=FullStory_AB.totalPage;i++)
        {
            if(i==FullStory_AB.currentPage)
            {
                FullStory_AB.pageHtml += ' <font class="page_on">'+i+'</font> ';
            }else
            {
                FullStory_AB.pageHtml += ' <a href="javascript:chinadaily()" onClick="changeFullstoryPage('+i+',\'FullStory_AB\',\''+FullStory_AB.contentID+'\')">'+i+'</a> ';
            }
        }
        if(FullStory_AB.currentPage != FullStory_AB.totalPage)FullStory_AB.pageHtml += ' <a href="javascript:chinadaily()" onClick="changeFullstoryPage('+(parseInt(FullStory_AB.currentPage)+1)+',\'FullStory_AB\',\''+FullStory_AB.contentID+'\')">Next Page</a> ';
    }
    FullStory_AB.pageHtml += "</div>";
    
    if(FullStory_AB.album.length != 0)
    {
        /*var linkNode;
        if (document.createElement &&
        (linkNode = document.createElement('link'))) {
        linkNode.rel = "image_src";
        linkNode.href = "/templates/default/en/images/china_daily_logo.jpg";
        document.getElementsByTagName('head').item(0).appendChild(linkNode);}*/
        
        FullStory_AB.showPic = "show";
        FullStory_AB.showPicObject = FullStory_AB.album[FullStory_AB.currentPicIndex];
        FullStory_AB.showMore = FullStory_AB.album.length > 1?"show":"notShow";
        //$(FullStory_AB.contentID).update(templates.FullStory_AB_h.evaluate(FullStory_AB));
        if(FullStory_AB.showPicObject.photoSize == 'h')$(FullStory_AB.contentID).update(templates.FullStory_AB_h.evaluate(FullStory_AB));
        else if (FullStory_AB.showPicObject.photoSize == 's')$(FullStory_AB.contentID).update(templates.FullStory_AB_s.evaluate(FullStory_AB));
        else if (FullStory_AB.showPicObject.photoSize == 'v')$(FullStory_AB.contentID).update(templates.FullStory_AB_v.evaluate(FullStory_AB));
    }else
    {
        FullStory_AB.showPic = "notShow";
        $(FullStory_AB.contentID).update(templates.FullStory_AB_h.evaluate(FullStory_AB));
    }
        if(info.initAdsFullstory == undefined)
        {
            initAds(FullStory_AB.adsInfo);
            info.initAdsFullstory = true;
        }else
        {
            window.clearTimeout(adsFullstoryTimeOut)
            showNextAds('Ads_fullstory');
        }
      
      // for full story top nav start
      $("top_nav_fullstory").innerHTML = "> " + FullStory_AB.CategoryDes;
      $("top_nav_fullstory").href = "" + FullStory_AB.CategoryName + ".html";
      //$("top_nav_fullstory").style.display = "block";
      // for full story top nav end
      
      // for open the comment first page start
      if (temp_total_comment > 0) {
        DisplayComment(1,temp_total_comment);
      }
      // for open the comment first page end
}

function changeFullstoryPage(page,blockName,contentID)
{
    callBlockJSONService(path+blockName+".html",{success:contentID},FullStory_AB_callBack,contentID,{currentPageNo:page,currentPicIndex:FullStory_AB.currentPicIndex});
    $('btop').scrollTo();
}

function changePic(val)
{
    if(val == 1)
    {
        FullStory_AB.currentPicIndex = FullStory_AB.currentPicIndex +1 >= FullStory_AB.totalPic?0:FullStory_AB.currentPicIndex + 1;
    }else
    {
        FullStory_AB.currentPicIndex = FullStory_AB.currentPicIndex -1 == -1?FullStory_AB.totalPic-1:FullStory_AB.currentPicIndex - 1;
    }
    FullStory_AB.showPicObject = FullStory_AB.album[FullStory_AB.currentPicIndex];
    if(FullStory_AB.showPicObject.photoSize == 'h')$(FullStory_AB.contentID).update(templates.FullStory_AB_h.evaluate(FullStory_AB));
    else if (FullStory_AB.showPicObject.photoSize == 's')$(FullStory_AB.contentID).update(templates.FullStory_AB_s.evaluate(FullStory_AB));
    else if (FullStory_AB.showPicObject.photoSize == 'v')$(FullStory_AB.contentID).update(templates.FullStory_AB_v.evaluate(FullStory_AB));
    $('btop').scrollTo();
}


div_share = 0;
function share()
{
    clearTimeout(div_share);
    resizeLayer();
    $('sharePanel').show();
    Event.observe(window, 'resize',resizeLayer); 
}

function resizeLayer()
{
    var obj = $('shareImg').positionedOffset();
    var top = obj.top+15;
    var left = obj.left;
    //alert(top+","+left);
    $('sharePanel').setStyle({left:left+"px",top:top+"px",display:$('sharePanel').getStyle("display")});
    window.setTimeout(resizeLayer,100);
}

function closeShare()
{
    //$('sharePanel').hide();
    Event.stopObserving(window, 'resize',resizeLayer);
    div_share = setTimeout("$('sharePanel').hide()",500);
}

function viewComment()
{
    $('comments').show();
}

function addComment()
{
    if (MemberLogin.status != "member")
    {
        //alert("Please Login First");
        var eW = Element.getWidth('login_layer');
        var wW = window.innerWidth?window.innerWidth:document.documentElement.clientWidth;
        var offsetWidth = parseInt((wW - eW - 50));
        offsetWidth = offsetWidth < 0?0:offsetWidth;
        offsetWidth = offsetWidth /2;
        $('login_layer').l = offsetWidth;
        var st=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
	    $('login_layer').t = st;
	    $('login_layer').show();
        $('Username').focus();
        resizeCentreLayer('login_layer');
        disableScreen();
    }
    else
    {
        $('commentStatus').hide();
        $('commentInput').value = "";
        $('addComment').show();
    }
}

function closeLoginLayer()
{
    $('login_layer').hide();
    enableScreen();
}

function submitComment()
{
    if($('commentInput').value == ""){alert('Please write some comment');return;}
    callBlockJSONService('/en/services/AddComment.html','',submitCommentCallback,'',{comment:$('commentInput').value});
}

function submitCommentCallback(transpot)
{
    result = transpot.responseText.evalJSON();
    $('addComment').hide();
    var statusHtml = result.status == 'success'?"Your comment is successfully submitted. It may take some time to appear.":"fail";
    $('commentStatus').update(statusHtml);
    $('commentStatus').show();
}

function emailArticleResize()
{
    resizeCentreLayer('email_article');
}

function emailArticle()
{
    win=open('/en/site_info/emailArticle.html?id='+info.requestID, 'emailArticle', 'width=670,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no', "_blank");
	if(win!=null)win.focus();
    /*var eW = Element.getWidth('email_article');
    var wW = window.innerWidth?window.innerWidth:document.documentElement.clientWidth;
    var offsetWidth = parseInt((wW - eW - 50));
    offsetWidth = offsetWidth < 0?0:offsetWidth;
    offsetWidth = offsetWidth /2;
    $('email_article').l = offsetWidth;
    var st=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
	$('email_article').t = st;
	$('email_article').show();
    resizeCentreLayer('email_article');
    disableScreen();*/
}

function closeEmailArticle()
{
    $('email_article').hide();
    enableScreen();
}

function loginAddComment()
{
    afterLoginCallBack = fullstoryMemberCallback;
    go_login();
    return false;
}

function fullstoryMemberCallback(transpot)
{
    if (MemberLogin.status == "member")
    {
        enableScreen();
        addComment();
        $('login_layer').hide();
        afterLoginCallBack = "";
    }
}

function DisplayComment(page,total)
{

    for (var i=1; i <= total; i++) {
        $('comment_table_'+i).hide();
    }
    
    var last_id = parseInt(page,10) * 10;
    for (var count=1; count <= 10; count++) {
        if ($('comment_table_'+last_id) != null) {
            $('comment_table_'+last_id).show();
        }
        last_id = last_id - 1;
    }
  
    var comment_p_no = Math.ceil(total / 10);
    for (var temp_com_no = 1; temp_com_no <= comment_p_no; temp_com_no++) {
        $("comment_pages_"+temp_com_no).removeClassName("page_on");
    }
    $("comment_pages_"+page).addClassName("page_on");
    
}

//Event.observe(window, "load", loadContent); 
function replace_all_sp(data) {
    //return data.replace(/$/g,"%24").replace(/&/g,"%26").replace(/'/g,"%27").replace(/"/g,"%22").replace(/\+/g,"%2B").replace(/,/g,"%2C").replace(/\//g,"%2F").replace(/:/g,"%3A").replace(/;/g,"%3B").replace(/\?/g,"%3F").replace(/@/g,"%40").replace(/\n\r/g," ").replace(/\n/," ");
    return data.replace(/&/g,"%26").replace(/'/g,"%27").replace(/"/g,"%22").replace(/\+/g,"%2B").replace(/,/g,"%2C").replace(/\//g,"%2F").replace(/:/g,"%3A").replace(/;/g,"%3B").replace(/\?/g,"%3F").replace(/@/g,"%40").replace(/\n\r/g," ").replace(/\n/," ");
}

function printFullstory() {
    //alert($("printArea").innerHTML);
 }

