/**
 * @author TrojanMyth
 */
function belsisNewsPanel(pictureContainer,listContainer,listItemCount,instanceName){
	this.picContainer = document.getElementById(pictureContainer);
	this.listContainer = document.getElementById(listContainer);
	this.imageEngine = "";	
	this.newsPage = "";	
	this.imagePath = "";
	
	this.showItem = 0;
	
	this.ImageWidth = 415;
	this.ImageHeight = 300;
	
	this.ThumbImageWidth = 82;
	this.ThumbImageHeight = 56;
	
	this.newsList = new Array();
	
	this.createdNewsList = new Array();
	
	this.listItemCount = 0;
	this.pictureAreaContainerId;
	this.headerAreaContainerId;
	this.headerTextAreaContainerId;
	this.cursorContainer;
	this.instanceName = instanceName;
	if(listItemCount!=null && listItemCount!=undefined){
		this.listItemCount= 5;		
	}
	else{
		this.listItemCount=  parseInt(listItemCount);
	}
	
	return this;
}

belsisNewsPanel.prototype = {
	addNews:function(headerText,picture,headerId){
		this.newsList.push({headerText:headerText,pic:picture,nid:headerId});
	},
	deleteNews:function(index){
		
	},
	createId:function(len){
		var string = "ABCDEFGHIJKLMNOPRSTUVYZ0123456789";
		randomnumber=Math.floor(Math.random()*string.length);
		var ret = "";
		for ( var i = 0; i < len; i++) {
			randomnumber=Math.floor(Math.random()*string.length);
			ret = ret + "" + string.substring(randomnumber,randomnumber+1);
			
		}
		return ret;
	},
	render:function(){
		if(this.picContainer!=null && this.picContainer!=undefined){
			if(this.listContainer!=null && this.listContainer!=undefined){
				
				this.pictureAreaContainerId = this.createId(10);
				
				
				var pictureArea = document.createElement("div");
				pictureArea.setAttribute("id",this.pictureAreaContainerId);
				pictureArea.className = "newsArea";
				
				this.headerAreaContainerId = this.createId(10);
				var headerArea = document.createElement("div");
				headerArea.setAttribute("id", this.headerAreaContainerId);
				headerArea.className = "newsTextArea";
				
				this.headerTextAreaContainerId = this.createId(10);
				var headerTextArea = document.createElement("div");
				headerTextArea.setAttribute("id", this.headerTextAreaContainerId);
				headerTextArea.className = "newsHead";
				
				headerArea.appendChild(headerTextArea);
				pictureArea.appendChild(headerArea);
				
				this.picContainer.innerHTML = "";
				this.picContainer.appendChild(pictureArea);
				
				
				var listTable = document.createElement("table");
				listTable.style.tableLayout = "auto";
				listTable.cellspacing = "0";
				listTable.cellpadding = "0";
				listTable.setAttribute("cellspacing","2");
				listTable.setAttribute("cellpadding","0");
				
				var listTableBody = document.createElement("tbody");
				
				var cursorRow = document.createElement("tr");
				this.cursorContainer = this.createId(5);
				cursorRow.setAttribute("id", this.cursorContainer);
				
				var listRow = document.createElement("tr");
				
				for ( var i = 0; i < this.listItemCount; i++) {
					if(this.newsList.length > i){
						cursor = document.createElement("td");
						cursor.setAttribute("newsInd", i);
						cursor.className = "newsThumbCursorArea";
						cursorRow.appendChild(cursor);
						listItem = document.createElement("td");
						var imageId = this.createId(11);
						this.createdNewsList.push(imageId);
						listItem.innerHTML = '<img id="'+imageId+'"  src="'+ this.imageEngine +''+ this.imagePath +''+ this.newsList[i].pic +'&w='+ this.ThumbImageWidth +'&h='+ this.ThumbImageHeight +'&f=jpg&zc=1&bg=FFFFFF" width="'+this.ThumbImageWidth +'" height="'+this.ThumbImageHeight +'" class="newsThumb"  onMouseOver="'+this.instanceName+'.changeNews(this)" onClick="document.location.href=\''+ this.newsPage +'&nid='+ this.newsList[i].nid +'\'" newsInd="'+i+'" newsId="3" newsHeader="'+ this.newsList[i].headerText+'" newsPic="'+this.newsList[i].pic+'">';
						listRow.appendChild(listItem);
					}
				}
				listTableBody.appendChild(cursorRow);
				listTableBody.appendChild(listRow);
				listTable.appendChild(listTableBody);
				this.listContainer.innerHTML ="";
				this.listContainer.appendChild(listTable);
				this.showNews(this.showItem);
			}
		}
	},
	changeNews:function(img){
		cursors = document.getElementById(this.cursorContainer);
		for ( var i = 0; i < cursors.childNodes.length; i++) {
			if(cursors.childNodes[i].getAttribute("newsInd") == img.getAttribute("newsInd")){
				cursors.childNodes[i].className = "newsThumbCursorCurrent";
			}
			else{
				cursors.childNodes[i].className = "newsThumbCursorArea";
			}
		}
		
		var headerTextContainer = document.getElementById(this.headerTextAreaContainerId);
		headerTextContainer.innerHTML = img.getAttribute("newsHeader");
		
		var pictureArea = document.getElementById(this.pictureAreaContainerId);
		pictureArea.style.backgroundImage = 'url('+this.imageEngine +''+ this.imagePath +''+ img.getAttribute("newsPic") +'&w='+ this.ImageWidth +'&h='+this.ImageHeight +'&f=jpg&zc=1&bg=FFFFFF)';		
	},
	showNews:function(index){
		showNews = document.getElementById(this.createdNewsList[index]);
		if(showNews!=null && showNews!=undefined){
			this.changeNews(showNews);
		}
	}
}

