function NewsPanel(blockid){
    this.blockid = blockid;
    this.listNews = null;
    this.count = 0;
    this.countAll = 0;
    this.path = "";
    this.sinhro = 0;
    this.page = 0;
}
var actionsNews=0;
NewsPanel.prototype.loadP = function(page){
    var myThis = this,i=0;
    myThis.listNews = new Array();
    if(!page) page = 0;
    this.page = page;
    this.sinhro = 0;
    Post(URL.news,{'page':page},function(json){
       try{
            myThis.countAll = json.count;
            myThis.count = json.data.length;
           for(i = 0; i < myThis.count;i++){
               try{
                    myThis.listNews.push(new News(json.data[i].title, json.data[i].text, new Photo(json.data[i].Photo.min,json.data[i].Photo.max),json.data[i].id));
               }catch(e){
                   alert("errorNewsPanel#2: "+e);
               }
           }
       }catch(e){
               alert("errorNewsPanel#1: "+e);
       }
       myThis.sinhro = 1;
    },'json');
    return myThis;
};

NewsPanel.prototype.templateDrawRight = function(){
    var html = "<td>", i = 0;
   // html += '<div class="newsBlockButton" onclick="newsPanel.loadP('+parseInt(this.page-1)+').draw()">Еще новости</div><div class="clear">&nbsp;</div>';

    html += '<div id="newsElementBlock_0" class="newsBlock newsBlockActive" onclick="newsPanel.clickToPhoto(0,this);"><div class="photoMin" onclick="window.location.href=\'/news/show/id/'+this.listNews[0].id+'\'" style="'+this.templatePhoto(0)+'"></div><div class="titleMin">'+this.listNews[0].title+'</div></div>';
    for(i = 1; i < 5; i++){
        if(!this.listNews[i]) break;
        html += '<div id="newsElementBlock_'+i+'" class="newsBlock"  onclick="newsPanel.clickToPhoto('+i+',this);"><div class="photoMin" onclick="window.location.href=\'/news/show/id/'+this.listNews[i].id+'\'" style="'+this.templatePhoto(i)+'"></div><div class="titleMin">'+this.listNews[i].title+'</div></div>';
    }
    if(this.countAll > (this.page+1)*5)
        html += '<div class="newsBlockButton" onclick="newsPanel.loadP('+parseInt(this.page+1)+').draw()">Еще новости</div> <div class="clear">&nbsp;</div>';
    else
        html += '<div class="newsBlockButton" onclick="newsPanel.loadP(1).draw()">Еще новости</div> <div class="clear">&nbsp;</div>'; 
    return html+"</td>";
    
}

NewsPanel.prototype.templatePhoto = function(id,size,tm){
   // if(!id) return "";
    var bi = 'background-image: ', end = ");";
    if(tm){bi = "";end = ")";}
    if(size) return bi+"url("+this.path+this.listNews[id].photo.max+end;
    return  bi+"url("+this.path+this.listNews[id].photo.min+end;
}


NewsPanel.prototype.draw = function(){
    var myThis = this;
    if(this.sinhro==0){
        setTimeout(function(){
            myThis.draw();
        },500);
        return;
    }
    
    var html = '<table cellpadding="0" cellspacing="0"><tr>', num = 0;
    html += '<td class="left"><div id="newsPanelLogo" style="'+this.templatePhoto(num,"max")+'">&nbsp;</div>';
    html += '<div class="hotNewsBottom">';
    html += '<span id="newsPanelTitle" ><a href="/news/show/id/'+this.listNews[0].id+'">'+this.listNews[0].title+'</a></span>';    
    html += '<div id="newsPanelText">'+this.listNews[0].text+'</div>';
    html += '</div>';
    html += '</td>';
    html += this.templateDrawRight();
    html += '</tr></table>'
    this.drawHTML(html);
};

NewsPanel.prototype.drawHTML = function(html){
    window.document.getElementById(this.blockid).innerHTML = html;
};

NewsPanel.prototype.moreNews = function(){
    if(actionsNews==0)
    {
        actionsNews=1;
        newsPag=newsPag+1;
        Post('/news/shownews',{page:newsPag,type:typeNews,typeQuery:'ajax'},function(json)
            { 
              var n=json['data'].length;
              if(n>0)
              {
                   var html = "";
                   for(var i=0;i<n;i++)
                  {
                      if(i%2==1)
                        html +='<div class="NewsVoidBlock leftMargin">';
                      else
                        html +='<div class="NewsVoidBlock">'; 
                    html+='<div class="title">\
                            <a class="title" href="/news/show/id/'+json['data'][i]['id']+'.html" title="'+json['data'][i]['title']+'"> '+json['data'][i]['title']+'</a>\n\
                           </div>';
                    
                    if(json['data'][i]['foto']!='')
                    {
                        html+='<div class="foto" style="background: url('+json['fotopath']+'imgshow.php?src='+json['data'][i]['foto']+'&object=news&model=small) center no-repeat;">&nbsp;</div>\
                                <div class="txt">'+json['data'][i]['contents']+'</div>';
                    }
                    else
                    {
                        html+='<div class="txtBig">'+json['data'][i]['contents']+'</div>';
                    }
                    
                    html+='<div class="clear"></div>\n\
                            </div>';

                  }
                  window.document.getElementById('showContent').innerHTML+= html;
                  if(json['nextPage']==0)
                    window.document.getElementById('showButtonMore').style.display='none'; 
                
                 actionsNews=0;
              }
              else
                   window.document.getElementById('showButtonMore').style.display='none';

            },'json');
    }
};

NewsPanel.prototype.clickToPhoto = function(id, element){
    try{
        
        var newClass = "newsBlock newsBlockActive";
        for(var i = 0; i < 5; i++){
            try{
                window.document.getElementById("newsElementBlock_"+i).setAttribute("class","newsBlock");
                window.document.getElementById("newsElementBlock_"+i).setAttribute("className","newsBlock");
            }catch(e){
                break;
            }
        }
        window.document.getElementById("newsPanelLogo").style.backgroundImage = this.templatePhoto(id, "max", 1);
        window.document.getElementById("newsPanelTitle").innerHTML = '<a href="/news/show/id/'+this.listNews[id].id+'">'+this.listNews[id].title+'</a>';
        window.document.getElementById("newsPanelText").innerHTML = this.listNews[id].text;
        element.setAttribute("class", newClass); 
	element.setAttribute("className", newClass);
    }catch(e){
        alert("errorNewsPanel#3: "+e);
    }
};


var newsPanel = new NewsPanel("newsPanel");
//newsPanel = newsPanel.loadP();
