var currentNewsIndex = -1;
var maxNewsIndex = 1;
var Max_Car_News_First_Page = 300;
var tabNews = new Array();

function LoadNews()
{
	var ws = new BackEndWS(abswebservice);
	ws.GetNewsNumPage(Culture, OnGetNumber, false);
}

function OnGetNumber(res)
{

	if (res != null)
	{
		//Background preload
		var img = new Image(); img.src = "/img/bg_news.png"; allImgs.push(img);
	
		var num = Number(res);
		maxNewsIndex = num;
		
		var div = document.getElementById("Place_Count_News");
		
		var ref = document.createElement("a"); 
		ref.href = "javascript:void(0)";
		ref.innerHTML = "<img  id='Page_Less' border=0 src='../img/s.gif' height=19 width=17>";
	
		ref.className = "Place_Count_News_Less";
		ref.onclick = function(e)
		{
			Show_Anim_Rond("if (currentNewsIndex > 1) GetNewsForPage(currentNewsIndex - 1,'L');",e);
		}
		
		div.appendChild(ref);
		
		var span = document.createElement("span"); 
		span.id = "Place_For_Current_Page";
		span.innerHTML = "Page <font color=white>1</font> | " + maxNewsIndex;
		span.className = "Text_Num_Page";
		div.appendChild(span);

		var ref = document.createElement("a"); 
		ref.href = "javascript:void(0)";
		ref.innerHTML = "<img id='Page_More' border=0 src='../img/s.gif' height=19 width=17>";
		ref.onclick = function(e)
		{
			Show_Anim_Rond("if (currentNewsIndex < maxNewsIndex) GetNewsForPage(currentNewsIndex + 1, 'M');",e);
		}
		ref.className = "Place_Count_News_More";
		div.appendChild(ref);
		
		
		if (currentNewsIndex == -1)
		{
			GetNewsForPage(1);
		}

	}
}

function CreatePageNum(index)
{
	var ref = document.createElement("a"); ref.href = "javascript:void(0)";
	ref.className = "link_count"; ref.innerHTML = index;
	ref.onclick = function () { GetNewsForPage(index); }
	return ref;
}

function GetNewsForPage(i,sens)
{
	if (currentNewsIndex != i)
	{
		currentNewsIndex = i;
	
		var ws = new BackEndWS(abswebservice);
		ws.GetNews(Culture, i, OnGetNews, false);
		
		var span = document.getElementById("Place_For_Current_Page"); 
		span.innerHTML = "Page <font color=white>" + currentNewsIndex + "</font> | " + maxNewsIndex;
		span.className = "Text_Num_Page";
		
		if (currentNewsIndex==1) {document.getElementById("Page_Less").style.display = "none";} else  {document.getElementById("Page_Less").style.display = "";}
		if (currentNewsIndex==maxNewsIndex) {document.getElementById("Page_More").style.display = "none";} else  {document.getElementById("Page_More").style.display = "";}
	}

}

function SelectIndex(index)
{
	var div = document.getElementById("Place_Count_News");
	
	//set correct style
	for (var i = 0; i < div.childNodes.length; i++)
	{
		if (i == index) div.childNodes[i].className = "link_count_active";
		else div.childNodes[i].className = "link_count";
	}
}

function OnGetNews(res)
{
	if (res != null)
	{
		var div = document.getElementById("Place_Content_News");
		div.innerHTML = "";

		for (var i = 0; i < res.length; i++)
		{
			var txtElement = document.createElement("font"); 
			txtElement.className = "news_content_Text";

			var shortText = res[i].Text;
			if (shortText.length > Max_Car_News_First_Page)
			{
				var txt = res[i].Text;
				shortText = shortText.slice(0, Max_Car_News_First_Page-3);
				txtElement.innerHTML = shortText + " ...";
			}
			else
			{
				txtElement.innerHTML = shortText;
			}

			var table = document.createElement("table"); 
			table.cellSpacing= 0; 
			table.cellPadding= 0; 
			table.width	= "100%"; 
			table.align	= "top";
			table.height= '110px';
			table.border= 0;
			table.style.background = "#32353A url('/img/Bg.gif')";
						var tr = table.insertRow(-1);
			var td	= tr.insertCell(-1); 
			td.align	= "center"; 
			td.innerHTML	= "<img src='/img/bgl.gif'>";

			var td	= tr.insertCell(-1); 
			td.align	= "center"; 
			td.width	= "66"; 
			td.vAlign	= "middle";
			td.style.padding	='5px';

			var img = new Image(); img.src = res[i].ImgPath;
			tabNews.push(img);
			td.innerHTML	= "<a href=\"" + res[i].Link + "\" target=\"_blank\"><img src=\"" + res[i].ImgPath + "\" class=\"news_Icon\" border=0></a><br><span class=\"date_news\">" + res[i].Date + "</span>";

			var td = tr.insertCell(-1); 
			td.width = "100%"; 
			td.vAlign = "top"; 
			td.style.paddingLeft = "3px";
			td.style.paddingTop  = "3px";

				var font = document.createElement("font"); 
				font.className = "news_title_Text"; 
				font.innerHTML = res[i].Title + "&nbsp;...&nbsp;&nbsp;&nbsp;";
				td.appendChild(font);
				
				var subFont = document.createElement("font");
				subFont.style.cursor = "pointer";
				subFont.className = "news_title_Text";
				subFont.style.color = "#FFFFFF";
				subFont.style.fontWeight = "normal";
				subFont.style.textDecoration = "underline";
				subFont.innerHTML = "Ampliar info";
				subFont.title = 'Click to read more ...';
				
				subFont.onmouseover = function (e) {this.style.color='#FF6600';};
				subFont.onmouseout = function (e) {this.style.color='#FFFFFF';};
				
				Set_On_onclick_Next(subFont, res[i].Title, res[i].Text,res[i].ImgPath, res[i].Date, res[i].Link );

				
				td.appendChild(subFont);
				var br = document.createElement("br"); 
				br.style.lineHeight = "3px";
				td.appendChild(br);
				td.appendChild(txtElement);

			var td	= tr.insertCell(-1); 
			td.align= "center"; 
			td.innerHTML= "<img src='/img/bgr.gif'>";
			
			div.appendChild(table);

			if (i != res.length - 1)
			{
				var hr = document.createElement("br");
				hr.style.lineHeight = "10px";
				div.appendChild(hr);
			}

		}
	}
	else
	{
		alert("Error while loading news");
	}
	newsLoaded = true;
}


function Set_On_onclick_Next(obj,P1,P2,P3,P4,P5)
{ 

	obj.onclick = function(e) 
	{
	 	ShowNewsCOntent(obj, P1, P2,P3,P4,P5)
	}
}

function Set_On_onmouseover_Next(obj,P1,P2,P3,P4,P5)
{ 
	obj.onmouseover = function(e) 
	{
	 	ShowNewsCOntent(obj, P1, P2,P3,P4,P5)
	}
}

function ShowNewsCOntent(elt, title, text, image, date, link)
{
	if (document.getElementById("flash"))
	{
		document.getElementById("flash").style.display='none'; 
	}
	
	if (document.getElementById("Content_Table_News_")) 
	{
		document.body.removeChild(document.getElementById("Content_Table_News_"));
	}
	
	var table = document.createElement("table"); 
	table.id = 'Content_Table_News_';
	table.border = 0;
	table.cellSpacing = 0; table.cellPadding = 2; table.width = "100%";
	table.style.height="259px";
	table.style.position = "absolute"; table.style.zIndex = "5000"; table.style.left = 1;
	table.style.width = "474px";
	
	var tr = table.insertRow(-1);
	var td = tr.insertCell(-1); td.align="right"; td.colSpan=2; td.innerHTML = "<img src='/img/s.gif' style='cursor:pointer' width=24 height=22 onclick='Show_Moveable_Popup(false,\"Detail_News\",After_Close_Contact);'>";
	

	var tr = table.insertRow(-1);
	var td = tr.insertCell(-1); 
	td.align = "center"; 
	td.width = "66"; 
	td.vAlign = "top";
	td.style.paddingLeft = "16px";
	td.style.paddingRight = "15px";
	
	td.innerHTML = "<a href=\"" + link + "\" target=\"_blank\"><img src=\"" + image + "\" class=\"news_Icon\" border=0></a>";
	//<br><span class=\"date_news\">" + date + "</span>

	var td = tr.insertCell(-1); td.width = "100%"; td.vAlign = "top"; td.style.paddingLeft = "3px";
	var font = document.createElement("font"); 
	font.className = "news_title_Text";  font.style.color = "#FF6600";
	font.style.fontWeight = "normal";
	font.innerHTML = title;
	td.style.paddingRight = "15px";
	td.appendChild(font);
	
	var tr = table.insertRow(-1);
	var td = tr.insertCell(-1);  td.colSpan = 2;
	td.style.fontSize = "3px"; td.style.borderBottom = "1px solid #8A8A8A";
	td.innerHTML = "&nbsp;";
	
	var tr = table.insertRow(-1);
	
	var td = tr.insertCell(-1); td.vAlign = "top"; td.colSpan = 2; td.height = "100%";
	var txtElement = document.createElement("font"); 
	txtElement.className = "news_content_Text";
	txtElement.innerHTML = "<p class=textnews style='line-height:1.5em;padding-left:15px;padding-right:15px;padding-top:13px;text-align:justify;font-size:11px;color:#CECECE;'>" + text + "</p>" ;
	td.appendChild(txtElement);
	
	table.style.display = "none";
	document.body.appendChild(table);

	Show_Moveable_Popup(true, 'Detail_News', After_Open_ClientArea, 'main', 'POPUP_NEWS', 400, 400, 326,163, 108, 26, 484, 413, 12, '/img/bg_news.png', 'Content_Table_News_');
}

function close_detail()
{

	if (document.getElementById("flash"))
	{
		document.getElementById("flash").style.display='inline'; 
	}

	document.getElementById("PLACE_MIDDLE_CONTENT_1").style.display = '';
	
	if (document.getElementById("Detail_Company")) document.getElementById("Detail_Company").style.display='';
    document.getElementById("Place_Count_News").style.display = '';
    document.getElementById("Middle_Content").removeChild(document.getElementById("Detail_News"));
}


function getAlphaImgStr(path, width, height, handlerFor, cursor, id, onclick, swapImg)
{
	var browserPngSupport = (ie7 || gecko);

	var arrayResult = new Array();
	
	if ((swapImg) && (swapImg != ""))
	{
		arrayResult.push("<div style=\"cursor:pointer;width:" + width + ";height:" + height + ";\" onmouseover=\"changeAlphaImg(this.firstChild, '" + swapImg + "');\" onmouseout=\"changeAlphaImg(this.firstChild, '" + path + "');\"");
		
		if ((onclick) && (onclick != ""))
		{
			arrayResult.push(" onclick=\""); arrayResult.push(onclick); arrayResult.push("\"");
		}
		
		if ((id) && (id != ""))
		{
			arrayResult.push(" id=\""); arrayResult.push(id); arrayResult.push("\"");
		}
		
		arrayResult.push(">");
	}
	
	if (!browserPngSupport)
	{
		arrayResult.push("<div style=\"width:"); arrayResult.push(width);
		arrayResult.push(";height:"); arrayResult.push(height);
		arrayResult.push(";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale enabled=true src='"); arrayResult.push(path); arrayResult.push("');");
		
		if ((cursor) && (cursor != ""))
		{
			arrayResult.push("cursor:"); arrayResult.push(cursor);
		}
		
		arrayResult.push("\"");
	}
	else
	{
		arrayResult.push("<img src=\""); arrayResult.push(path); arrayResult.push("\"");
		arrayResult.push(" style=\"width:"); arrayResult.push(width); arrayResult.push(";height:"); arrayResult.push(height);
		
		if ((cursor) && (cursor != ""))
		{
			arrayResult.push(";cursor:"); arrayResult.push(cursor);
		}
		
		 arrayResult.push(";\"");
	}
	
	if ((handlerFor) && (handlerFor != ""))
	{
		arrayResult.push(" handleFor=\""); arrayResult.push(handlerFor); arrayResult.push("\"");
	}
	
	if (!browserPngSupport)
	{
		arrayResult.push("></div>");
	}
	else
	{
		arrayResult.push(">");
	}
	
	if ((swapImg) && (swapImg != ""))
	{
		arrayResult.push("</div>");
	}
	
	return arrayResult.join("");
}