var newsObj=$("contents_news_block");


var newsCookie="category";
var newsPrefix="category_";

var newsClassBlockOpen="ig_news_open";
var newsClassBlockClosed="ig_news_closed";

var newsClosedDefault=0;

var elem;
var newsInterval=null;
var newsTimeDelay=1;
var newsHeightIncPercent=5;

var objBlock;
var objBlockH;
var objBlockHeight;

newsSetContainerHeight();

newsSetTree(newsObj);

newsSetWindowOpen(newsObj);


function newsSetContainerHeight()
{
var newsBlockHeight=parseInt($("contents_news_block").scrollHeight);
$("contents_news_block").style.height=newsBlockHeight+"px";
}


function newsSetWindowOpen(newsObj)
{
var re=/\/item\//;
	for(var child = newsObj.firstChild; child; child = child.nextSibling)
	{
		if(child.tagName && child.tagName.toLowerCase()=="a" && child.href.search(re)!=-1)
		{
		child.onclick=newsWindowOpen;
		child.href='javascript:void(0);';
		}
	newsSetWindowOpen(child);
	}
}


function newsWindowOpen(event)
{
event = event || window.event
var elem = event.target || event.srcElement;
var winURL=elem.getAttribute("url");
controllerWindow(0, "ig_window", winURL, 400, 300, "scrollbars=1, resizable");
}


function newsSetTree(newsObj)
{
var objID;
	for(var child = newsObj.firstChild; child; child = child.nextSibling)
	{
		if(child.tagName && child.tagName.toLowerCase()=="img" && child.id)
		{
		objID=parseInt(child.id.replace(newsPrefix, ""));
			if(newsFindInCookie(objID))
			{
			newsClosedDefault?newsShow(child):newsHide(child);
			}
			else
			{
			newsClosedDefault?newsHide(child):newsShow(child);
			}
		}
	newsSetTree(child);
	}
}

function newsShow(elem)
{
var objContents=elem.parentNode.nextSibling;
elem.className=newsClassBlockOpen;
elem.onclick=newsCollapse;
objContents.className=newsClassBlockOpen;
}


function newsBlockIncreaseHeight()
{
var newsHeightInc=objBlockHeight*newsHeightIncPercent/100;
	if(objBlockH<objBlockHeight)
	{
	objBlockH+=newsHeightInc;
	objBlock.style.height=objBlockH+"px";
	}
	else
	{
	objBlock.style.height="";
	clearInterval(newsInterval);
	}
}


function newsHide(elem)
{
var objContents=elem.parentNode.nextSibling;
elem.className=newsClassBlockClosed;
elem.onclick=newsExpand;
objContents.className=newsClassBlockClosed;
}


function newsBlockDecreaseHeight()
{
var newsHeightInc=objBlockHeight*newsHeightIncPercent/100;
	if(objBlockH>0)
	{
	objBlockH-=newsHeightInc;
	objBlockH=(objBlockH>0)?objBlockH:0;
	objBlock.style.height=objBlockH+"px";
	}
	else
	{
	objBlock.style.height=0;
	newsHide(elem);
	clearInterval(newsInterval);
	}
}


function newsCollapse(event)
{
event = event || window.event;
elem = event.target || event.srcElement;
var objContents=elem.parentNode.nextSibling;
var objID=parseInt(elem.id.replace(newsPrefix, ""));
newsWriteCookie(objID);
objBlock=objContents;
objBlockHeight=parseInt(objBlock.scrollHeight);
objBlockH=objBlockHeight;
	if(newsInterval)
	{
	clearInterval(newsInterval);
	}
newsInterval=setInterval(newsBlockDecreaseHeight, newsTimeDelay);
return true;
}


function newsExpand(event)
{
event = event || window.event
elem = event.target || event.srcElement
var objContents=elem.parentNode.nextSibling;
var objID=parseInt(elem.id.replace(newsPrefix, ""));
newsWriteCookie(objID);
newsShow(elem);
clearInterval(newsInterval);
objBlock=objContents;
objBlockHeight=parseInt(objBlock.scrollHeight);
objBlock.style.height=0;
objBlockH=0;
	if(newsInterval)
	{
	clearInterval(newsInterval);
	}
newsInterval=setInterval(newsBlockIncreaseHeight, newsTimeDelay);
return true;
}


function newsWriteCookie(objID)
{
var dm=location.host;
dm="."+dm.replace(/www./, "");
var dt=new Date();
var tc=dt.getTime();
dt.setTime(tc+86400*1000*30);
var c=newsReadCookie();
if(!c) c="";
var str="["+objID+"]";
var s=c.indexOf(str);
	if(s==-1)
	{
	c=c+str;
	}
	else
	{
	c=c.replace(str, "");
	}
document.cookie=newsCookie + "=" + c + "; expires=" + dt.toGMTString() + "; path=/; domain=" + dm;
}


function newsFindInCookie(objID)
{
var c=newsReadCookie();
var str="["+objID+"]";
var s=c.indexOf(str);
if(s==-1) return 0;
return 1;
}


function newsReadCookie()
{
var c=document.cookie;
var s=c.indexOf(newsCookie+"=");
if(s==-1) return "";
s=c.indexOf("=",s)+1;
var e=c.indexOf(";",s);
if(e==-1) e=c.length;
return c.substring(s,e);
}
