﻿ var _docId = 0;
 var _parentId = 0;
 var _resources = "";
 var _itemsCommands = "";
 var _discAct = "0";
 var _discList = "";
 var _pageWidthCount=0;
 
 $(document).ready(function() {

   
					var myData = {};

					$.ajax({
					
							url: "_discList.aspx",
							global: false,
							type: "POST",
							data: myData,
							dataType: "json",
							contentType: "application/json; charset=utf-8",
							beforeSend: function(){
							$('#disscusionList').text("nahrávám list...");
							ShowRotator();
							},
							success: function(data) {
							
								_resources = data.result.resources[0];
						
								$('#rotator').html("");							
								$('#disscusionList').text("");
								GetDiscList(data.result.discList[0].item);
						
								$('.discListItem').click(function(e){
									$('#page').text("stránka: 1");
									  var discName = $('#discName').text();
									  var discAct = e.target.id;
									  
										$('#discName').text(e.target.innerHTML);
										$("#disscusionList a:contains("+e.target.innerHTML+")").text(discName);
										$("#disscusionList a:contains("+discName+")").attr('id',_discAct);
										
										if($('#discName').text()=="Diskuse") 
										{
											_discAct = data.result.discList[0].item[0].id;
										}
										else
										{
											_discAct = discAct;
										}
										
									  
									  loadData(_discAct.replace('disc',''),1,1,"")
									  
									  
									});
									
							
								if(_docId==0)
								  {
										$('#discName').text(data.result.discList[0].item[0].name);
										loadData(data.result.discList[0].item[0].id,1,0,"");
									}
									
									$('#pager').after("<div id='instruction' style='clear:right;'>"+data.result.instruction[0].item+"</div>");
								}
								
																					
							});   
							
							
							$('#actionRefresh').click(function(e){
									RefreshDisc();
							});
														
 });
 
 
 function RefreshDisc(){
  
  
	$('#f_search').attr('value','');
	$('#f_only').attr('value','');
	$('#f_detail').attr('value','');																
	loadData(_docId,1,0,"");
	
	
 }
 

 function GetDiscList(items){

        $.each(items, function(index){
          _discList += this.id + ",";
          
					if(index!=0)
					$("<span><img width='8px' height='8px' src='design/images/list-pic2.png' /></span><a href='#' id='disc"+this.id+"' class='discListItem'  style='color: #2196a0;'>"+this.name+"</a>").appendTo("#disscusionList");
			  });	
			   	

 }

 var changeDiscFilter = 0;

 function setDiscFilter(docId, cpi, filter, detail) {

     changeDiscFilter = 1;
     loadData(docId, cpi, filter, detail);
 }
 
 function loadData(docId,cpi,filter,detail){
					_docId = docId; 
					_parentId = 0;
					
					var _search = "";
					if($('#f_search').attr('value')+""!="undefined") 
						_search = $('#f_search').attr('value');
						
					var _only = "";
					if($('#f_only').attr('value')+""!="undefined") 
						_only = $('#f_only').attr('value');

					var _detail = detail;
						
					var _mark = "";
					if($('#f_mark').attr('value')+""!="undefined") 
						_mark = $('#f_mark').attr('value');

					var _ignore = "";
					if($('#f_ignore').attr('value')+""!="undefined") 
						_ignore = $('#f_ignore').attr('value');
						
					if(_search == "" && _only == "" && _detail == "")	filter = 0;
					
					var myData = { 'docid': docId, 'cpi': cpi, 'filter': filter, 'search': _search, 'only': _only, 'detail': _detail, 'mark': _mark, 'ignore': _ignore, 'change': changeDiscFilter };

					changeDiscFilter = 0;

					$.ajax({
					    url: "_discItems.aspx?" + new Date().getTime(),
					    global: false,
					    type: "POST",
					    data: myData,
					    dataType: "json",
					    contentType: "application/json; charset=utf-8",
					    timeout: 20000,
					    error: function(e) {
					        $('#rotator').html("");
					        $('#disscusionDetail').html(_resources.errorTrans);
					    },
					    beforeSend: function() {
					        ShowRotator();
					    },
					    success: function(data) {
					        $("#prevnext").remove();
					        $("#prevnextTop").remove();
					        $("#changeWidthD").remove();
					        $('#disscusionDetail').html("");

					        $("#pageinfo").remove();

					        if (data.msg) {
					            $("#disscusionDetail").html(data.msg);
					            return false;
					        }
					        _itemsCommands = BuildItemsCommands(data.result.commands[0]);

					        switch (filter) {
					            case 0:
					                if (data.result.discItems[0].itemCount != 0) {
					                    if (data.result.discItems[0].pageCount > 1) {
					                        WritePager(data.result.discItems[0].pageCount, cpi);
					                    }

					                    WriteRow(data.result.discItems[0].item, 'disscusionDetail')

												;
					                }
					                else {
					                    $('#disscusionDetail').html("");
					                }
					                break;
					            case 1:
					                if (!data.result.discItems[0].item) {
					                    $('#pager').html("");
					                    $('#pagerTop').html("");
					                    $('#rotator').html("");
					                    $('#disscusionDetail').append("<div style='margin-top:10px;margin-bottom:10px;'>" + _resources.noData + "</div>");
					                    return;
					                }
					                WriteFakePager();
					                WriteRow(data.result.discItems[0].item, 'disscusionDetail');

					                break;
					        }
					        $('#rotator').html("");

					        $('.findItem').click(function(e) {
					            var det = e.target.innerHTML.replace('#', '');
					            $('#f_detail').attr('value', det);
					            setDiscFilter(_docId, 1, 1, det);
					            //loadData(_docId,1,1,det);
					        });

					        $('#nextPage').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#nextPageFirst').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#nextPageTop').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#nextPageTopFirst').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });


					        $('#prevPage').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#prevPageLast').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#prevPageTop').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#prevPageTopLast').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $('#goToPage').click(function(e) {
					            loadData(_docId, e.target.hash.replace('#', ''), 0, "")
					        });

					        $("#msginfo").load("_newMsgInfo.aspx?dummy=" + new Date().getTime());

					        $('#changeWidth').click(function(e) {
					        });

					        $('#changeWidth').attr('checked', (ReadCookie('changeWidth') == 'true') ? true : false);
					        $('#checkedWidth').attr('value', (ReadCookie('changeWidth') == 'true') ? true : false);
					        ChangePageWidth();

					        $('.itemPrint').click(function(e) {
					            var printId = e.target.parentNode.parentNode.parentNode.id;
					            var myPrintHtml = $('#disscusionDetail').find('#' + printId);

					            $("#main").hide();
					            $("body").css("background-image", "none");

					            $("<div id='printCover'></div>").appendTo("body");
					            $("<h2>" + document.title + "</h2>").appendTo("#printCover");
					            $("<div id='disscusionDetailPrint'></div>").appendTo("#printCover");
					            $("<div id='" + printId + "' class='discItem'>" + myPrintHtml[0].innerHTML + "</div>").appendTo("#disscusionDetailPrint");
					            $("<a id='backPrint' href='#'>" + _resources.backPrint + "</a>").appendTo("#printCover");
					            $("#disscusionDetailPrint .itemPrint").remove();

					            $('#backPrint').click(function(e) {
					                $("#printCover").remove();
					                $("#main").show();
					                $("body").css("background-image", "url(design/images/background.png)");
					            });

					            eval("window.print();");
					        });

					    }



					});
							
  
 
  }
  
  function ChangePageWidth(write)
  {   		
		if ($('#changeWidth').attr('checked'))		
		{
			$('#diskuseBody').attr('class','diskuze');
			$('#checkedWidth').attr('value','false');			
		}
		else
		{
			$('#diskuseBody').attr('class','');
			$('#checkedWidth').attr('value','true');			
		}						
		WriteCookie('changeWidth',$('#changeWidth').attr('checked'),3);
  }
 
	function WriteRow(items,parentId){
	
      $.each(items, function(){
			          
			  var myStyle = parentId =='disscusionDetail' ? 'discitem' : 'discitem-sub';
				$("<div class='"+myStyle+"' id='"+this.id+"'/>").appendTo("#"+parentId+"");
				
				if(this.mark)
				{
					if(this.mark=='item')
						$('#'+this.id).css('border-color','#2495a5');
						$('#'+this.id).css('background-color','#ffcccc');
				}
				
				if(!this.ignore)
				{
					var html = "<div class='discitem-header-left'>";
					html += "	<a class='itemAuthor' style='color: #2196a0;font-size:12px;' href='messages.aspx?akce=new&recipient="+this.author+"' title='"+_resources.send+"'>"+decodeURIComponent(this.author)+"</a>";
					html += "</div>";
					html += "<div class='discitem-header-right'>";
					html += "	<a class='itemPrint' style='color: #2196a0;' href='#' title='"+_resources.print+"'>"+_resources.print+"</a>";
					html +=		_itemsCommands.replace('userinfo',this.author).replace('hostinfo',this.host);
					html += "	<span class='itemDatum'>"+this.date+"</span>";
					html += "	<span class='itemId'>#"+this.id+"</span>";
					html += "</div>";
					
					
					$("<div id='discitem-header'/>").html(html).appendTo("#"+this.id+"");
					
					var htmlContent = this.postContent;
					
					htmlContent = ShowHashShortCut(htmlContent);
					
					var extrainfo = "";
					if(this.ip)
					{
						extrainfo = "<div style='margin-bottom:3px;'>" + this.ip + " , " + this.host + "</div>"
					}
					
		  		$("<div class='itemText' />").html(extrainfo+decodeURIComponent(htmlContent)).appendTo("#"+this.id+"");
											
				}					
					
		  		if(this.item!=null)
		  		{
			  		 WriteRow(this.item,this.id);
		  		}
		  		
		  		
		  
		  });	
		  	  	
  }
  
  function WriteFakePager()
  {  
			$("<div id='prevnext' style='text-align:center;' />").appendTo("#pager");
			$("<div id='prevnextTop' style='text-align:center;' />").appendTo("#pagerTop");			
	}
	

  function WritePager(pages,cpi){
  
			if(pages=="")
				return;
			
			$("<div id='prevnext' style='text-align:center;' />").appendTo("#pager");
			$("<div id='prevnextTop' style='text-align:center;' />").appendTo("#pagerTop");
			
			var isCounterView= false;
			if(parseInt(cpi)!=1 && parseInt(pages)>1)
			{			
				isCounterView=true;
				$("<span><a href='#1' id='nextPageFirst'><img src='design/images/prev-first.png' />&nbsp;"+_resources.first+"&nbsp;</a></span>").appendTo("#prevnext");
				$("<span><a href='#"+(parseInt(cpi)-1)+"' id='nextPage'><img src='design/images/prev.png' />&nbsp;"+_resources.prev+"&nbsp;</a></span>").appendTo("#prevnext");			
				$("<span style='color:#5bb1c0;'>&nbsp;"+ cpi +" z "+ pages + "&nbsp;</span>").appendTo("#prevnext");	
				$("<span><a href='#1' id='nextPageTopFirst'><img src='design/images/prev-first.png' />&nbsp;"+_resources.first+"&nbsp;</a></span>").appendTo("#prevnextTop");
				$("<span><a href='#"+(parseInt(cpi)-1)+"' id='nextPageTop'><img src='design/images/prev.png' />&nbsp;" + _resources.prev + "&nbsp;</a></span>").appendTo("#prevnextTop");					
				$("<span style='color:#5bb1c0;'>&nbsp;"+ cpi +" z "+ pages + "&nbsp;</span>").appendTo("#prevnextTop");
			}
			if(parseInt(pages)>1 && parseInt(pages)!=parseInt(cpi))
			{
				if(!isCounterView)					
				{
					$("<span style='color:#5bb1c0;'>&nbsp;"+ cpi +" z "+ pages + "&nbsp;</span>").appendTo("#prevnext");
				}
				$("<span><a href='#"+(parseInt(cpi)+1)+"' id='prevPage'>&nbsp;"+_resources.next+"&nbsp;<img src='design/images/next.png' /></a></span>").appendTo("#prevnext");					
				$("<span><a href='#"+pages+"' id='prevPageLast'>&nbsp;"+_resources.last+"&nbsp;<img src='design/images/next-last.png' /></a></span>").appendTo("#prevnext");					
				if(!isCounterView)
				{	
					$("<span style='color:#5bb1c0;'>&nbsp;"+ cpi +" z "+ pages + "&nbsp;</span>").appendTo("#prevnextTop");		
				}
				$("<span><a href='#"+(parseInt(cpi)+1)+"' id='prevPageTop'>&nbsp;"+_resources.next+"&nbsp;<img src='design/images/next.png' /></a></span>").appendTo("#prevnextTop");					
				$("<span><a href='#"+pages+"' id='prevPageTopLast'>&nbsp;"+_resources.last+"&nbsp;<img src='design/images/next-last.png' /></a></span>").appendTo("#prevnextTop");					
			}
			
     $("<p id='pageinfo'><a href='' id='goToPage'>Přejdi na stránku</a>&nbsp;<input type='text' class='inputText' onchange='getPageChange()' size='3' id='getPage' /></p>").appendTo("#pager");
	}
  
  function getPageChange()
  {
		document.getElementById('goToPage').href = '#' + document.getElementById('getPage').value;
  } 
 
  function ShowHashShortCut(content){
			var regexp = "(\%20)(\%23)(\\d+)";				
			var reg = new RegExp(regexp, "g");		
			content = content.replace(reg,"<a href='#' class='findItem'>#$3</a>");				
			
			return content;
  }
  	

	function BuildItemsCommands(commands){

     var html = "";
     
     if(commands==null)
			return html;
     
     $.each(commands.item, function(){
      
	     html += "<a class='"+this.name+"' style='"+this.style+"' href='#"+this.hash+"' title='"+this.title+"'>"+this.text+"</a>";
	     
	     if(this.script!="")
	     {
					if(document.getElementById(this.scriptId))
						$("#"+this.scriptId).remove();
					
					  var newjs=document.createElement('script');
					  newjs.id=this.scriptId;
						newjs.type='text/javascript';
						newjs.src='design/js3/'+this.script+'?dummy=' + new Date().getTime();
						document.getElementsByTagName('head')[0].appendChild(newjs);	     
					
       }
	     
	   });
	   
	   return html;

	}
	

function WriteCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		
	}
	return null;
}

