var UIList = {
    listArea: null,	
	listTitles: null,
    previewTitle: null,		
    previewDescription: null,
	issuesThumbs: null,
    scrollBar: null,		
    scrollBead: null,		
	artSel: null,
    arrTitles: new Array(),
	arrThumbs: new Array(),
	
    SCROLLBAR_SIZE: 498,
    titleIdx: 0,
    TITLE_MAX_NUM: 12,			
    THUMB_MAX_NUM: 6,			
    dataObj: null
}

UIList.create = function() {
    this.listArea = document.getElementById("UIList");
    this.previewTitle = document.getElementById("UIListPreviewTitle");
    this.previewDescription = document.getElementById("UIListPreviewDescription");
	this.issuesThumbs = document.getElementById("UIListThumbnails");
    this.errorMessage = document.getElementById("UIListErrorMessage");
    this.scrollBar = document.getElementById("UIListScrollBar");
    this.scrollBead = document.getElementById("UIListScrollBead");
	this.artSel = document.getElementById("UIListArtSelect");
	this.listTitles = document.getElementById("UIListTitles");
    
    for (var i=0; i < this.TITLE_MAX_NUM; i++) {
        this.arrTitles[i] = document.getElementById("UIListTitle"+i);
    }
    for (var i=0; i < this.THUMB_MAX_NUM; i++) {
        this.arrThumbs[i] = document.getElementById("UIListThumb"+i);
    }
}

UIList.highlightThumb = function(){
    var newTop = 0;
	if (NewsController.currentCategoryID < 5){
		newTop = 12 + ((NewsController.currentCategoryID)*85);
	} else {
		newTop = 12 + ((NewsController.currentCategoryID-5)*85);
	}	
    document.getElementById("blask_image").style.top = newTop;
}

UIList.show = function() {
    this.listArea.style.display = "block";
	this.listTitles.style.display = "block";
    this.fillPreviewTitle();
    this.fillPreviewDescription();
	this.fillThumbnails();	
    this.fillTitles();
    this.highlightTitle(this.titleIdx);
	this.highlightThumb();
    KeyHandler.focusToList();
}

UIList.refresh = function() {
    this.clearContents();
    this.fillPreviewTitle();
    this.fillPreviewDescription();
	this.fillThumbnails();
    this.fillTitles();
	this.highlightThumb();
    this.highlightTitle(this.titleIdx);
}

UIList.setTitleIdx = function (titleIdx) {
    this.titleIdx = titleIdx;
}

UIList.clearContents = function() {
    this.previewTitle.innerHTML = "";
    this.previewDescription.innerHTML = "";
    for (var i=0; i < this.TITLE_MAX_NUM; i++) {
        this.arrTitles[i].innerHTML = "";
    }
}

UIList.hide = function() {
    //this.listArea.style.display = "none";
	this.listTitles.style.display = "none";
    //this.blurTitle(this.titleIdx);	
    KeyHandler.focusToKeyBlocker();		// block remote controller key event
}

UIList.setDataObj = function(pDataObj) {
    this.dataObj = pDataObj;
}

UIList.fillPreviewTitle = function() {
    this.previewTitle.innerHTML = '<span>'+ISSUE_NAME+'</span><h4>'+this.dataObj.categoryTitle+'</h4>';
}

UIList.fillPreviewDescription = function() {

	var description = "<div style='width:345px;height:502px;overflow:hidden;'>";

	if( this.dataObj.arrArticles[this.titleIdx] ){
		description += "<img style='width:345px;height:502px;border:0;' src='http://edgp.gazetaprawna.pl/index.php?act=img&amp;id="+this.dataObj.arrArticles[this.titleIdx].page+"&amp;sub=main_thumb&amp;type=500'>";
		this.artSel.style.position = 'absolute';
		this.artSel.style.left = this.dataObj.arrArticles[this.titleIdx].area.x+'px';
		this.artSel.style.top = this.dataObj.arrArticles[this.titleIdx].area.y+'px';
		this.artSel.style.width = this.dataObj.arrArticles[this.titleIdx].area.width+'px';
		this.artSel.style.height = this.dataObj.arrArticles[this.titleIdx].area.height+'px';
	} else {
		description += "<img style='width:345px;height:502px;border:0;' src='http://edgp.gazetaprawna.pl/index.php?act=img&amp;id="+DataMgr.arrIssues[DataMgr.arrIssueMatrix[2]].subIssues[NewsController.currentCategoryID].cover+"&amp;sub=main_thumb&amp;type=500'>";
	}
	description += "</div>";
	this.previewDescription.innerHTML = description;
}

UIList.fillTitles = function() 
{
    var article = null;
	if (this.dataObj.arrArticles.length == 0) {
		this.artSel.style.display = 'none';
		this.arrTitles[0].innerHTML = Util.wrapInTable("Dodatek nie zawiera artykułów", "", "UIListTitle_style");
	} else {
		this.artSel.style.display = 'block';
		for (var i=0; i < this.TITLE_MAX_NUM; i++) {
			article = this.dataObj.arrArticles[i];
			if (article) {
				this.arrTitles[i].innerHTML = Util.wrapInTable(article.title, "", "UIListTitle_style");
				this.arrTitles[i].style.left="0px";
			}
			else {
				this.arrTitles[i].innerHTML = "";
			}
		}
	}
}

UIList.fillThumbnails = function() 
{
	var issues = DataMgr.getCategories();
	var cover = null;
	var j = 0;
	//issues.length
    for (var i=0; i < this.THUMB_MAX_NUM; i++) {
		if (NewsController.currentCategoryID < 5){
			j = i
		} else {
			j = 5 + i;
		}
		if (j < issues.length){
			cover = issues[j].cover;
		} else {
			cover = null;
		}
		if (cover){
			this.arrThumbs[i].innerHTML = "<img style='width:50px;height:70px;border:0;' src='http://edgp.gazetaprawna.pl/index.php?act=img&amp;id="+cover+"&amp;sub=main_thumb&amp;type=70'>";
		} else {
			this.arrThumbs[i].innerHTML = "";		
		}
    }
}


UIList.highlightTitle = function(pIndex) {
	if (this.dataObj.arrArticles.length > 0) {
		this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.backgroundImage = "url(Resource/image/title_h_bg.png)";
		this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.backgroundPosition="center";
		//this.arrTitles[pIndex].innerHTML = Util.wrapInTable(article.title, "", "UIListTitle_style_h");
		//this.arrTitles[pIndex].style.backgroundPosition="center";
		this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.height = "34px";
		this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.width = "480px";
		this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.left = "0px";
		//this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.top= "5px";
	}
}

UIList.blurTitle = function(pIndex) {
    this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.backgroundImage = "url(none)";
    this.arrTitles[pIndex!=null?pIndex:this.titleIdx].style.left = "0px";
}


UIList.articleUp = function() {
    this.blurTitle(this.titleIdx);
    if (--this.titleIdx < 0) {						
        NewsController.request(NewsController.LIST_FIRST_TITLE_UP);	
    }
    else {
        this.highlightTitle(this.titleIdx);
        this.fillPreviewDescription();
    }
}

UIList.articleDown = function() {
    this.blurTitle(this.titleIdx);
    if (++this.titleIdx > this.TITLE_MAX_NUM -1 || this.titleIdx > this.dataObj.arrArticles.length-1) {
        NewsController.request(NewsController.LIST_LAST_TITLE_DOWN);
    }
    else {
        this.highlightTitle(this.titleIdx);
        this.fillPreviewDescription();
    }
}

UIList.getLastTitleIdx = function() {
    var retValue = this.dataObj.arrArticles.length - 1;
    return retValue;
}

UIList.getTitleIdx = function() {
    var retValue = this.titleIdx;
//    alert("UIList.getTitleIdx() returns " + retValue);
    return retValue;
}

UIList.adjustScrollBar = function(articleIdx, totalArticle) {
    var curPage = Math.floor(articleIdx/this.TITLE_MAX_NUM);
    var totalPage = Math.floor((totalArticle-1)/this.TITLE_MAX_NUM);
    if (totalPage <= 0) {		
        this.hideScrollBar();	
    }
    else {
        var position = Math.round((curPage*this.SCROLLBAR_SIZE)/totalPage);
        this.scrollBead.style.top = position + "px";
        this.showScrollBar();
    }
}

UIList.showScrollBar = function() {
    this.scrollBar.style.display = "block";
}

UIList.hideScrollBar = function() {
    this.scrollBar.style.display = "none";
}

UIList.showInfo = function() {
    var elem = document.getElementById("info_image");
    if (elem.style.display =="none") {
        elem.style.display = "block";
    } else {
        elem.style.display = "none";
    }
//    document.getElementById("info_image").style.display = "block";
    //this.scrollBar.style.display = "none";
}
