var arAttached = new Array();

function getElementForRestyling(e)
{
	var e = e || event;
	var sender = e.target || e.srcElement;
	
	var par = sender;
	var i = 0;
	while (true)
	{
		i++;
		//alert(par.className);
		if (par.className.indexOf('restyle') != -1 || par == null || i >10)
			break;
		par = par.parentNode || par.parentElement;
	}
	return par;
}

function addstyles(e)
{
	//m_divDebug.innerHTML += "add <br />";
	var obj = getElementForRestyling(e);

	// getting children
	var childDivs = obj.getElementsByTagName('DIV');
	
	for (var nDiv = 0; nDiv < childDivs.length; nDiv++)
	{
		if(!childDivs[nDiv].className)
			continue;
		if (childDivs[nDiv].className == 'boxtop')
			childDivs[nDiv].className = 'boxtop boxtophover';
		if (childDivs[nDiv].className == 'boxmid')
			childDivs[nDiv].className = 'boxmid boxmidhover';
		if (childDivs[nDiv].className == 'boxbottom')
			childDivs[nDiv].className = 'boxbottom boxbottomhover';
	}
}


function redirectpage(evt,link)
{
	var e = evt || event;
	var sender = e.target || e.srcElement;

	//alert('checking sender ' + sender.id + ', clickable divs:' + m_arrClickableDivs.length);
	while(sender != null)
	{
		for(var nDiv = 0; nDiv <m_arrClickableDivs.length ;nDiv++)
		{
			//alert('sender ' + sender.id + ' clickable ' + m_arrClickableDivs[nDiv].id);
			if(sender.id == m_arrClickableDivs[nDiv].id)
			{
				//alert('clicked div' + m_Link[nDiv]);
				window.location = m_Link[nDiv];
				return;
			}
		}
		//alert('moving to parent ' + sender.parentNode);
		sender = sender.parentNode;
	}
}

function removestyles(e)
{

	//m_divDebug.innerHTML += "remove <br />";
	var obj = getElementForRestyling(e);
	// getting children
	var childDivs = obj.getElementsByTagName('DIV');
	
	for (var nDiv = 0; nDiv < childDivs.length; nDiv++)
	{
		if(!childDivs[nDiv].className)
			continue;
		if (childDivs[nDiv].className == 'boxtop boxtophover')
			childDivs[nDiv].className = 'boxtop';
		if (childDivs[nDiv].className == 'boxmid boxmidhover')
			childDivs[nDiv].className = 'boxmid';
		if (childDivs[nDiv].className == 'boxbottom boxbottomhover')
			childDivs[nDiv].className = 'boxbottom';
	}
}

var m_arrClickableDivs;
var m_divDebug;
var m_Link;

function window_load()
{
	// getting all divs to set links
	/*
	m_divDebug = document.createElement('DIV');
	document.getElementsByTagName('body')[0].appendChild(m_divDebug);
	m_divDebug.style.position = 'absolute';
	m_divDebug.style.top = '400px';
	m_divDebug.style.left = '0';
	*/
	
	var curdiv;
	m_arrClickableDivs = new Array();
	m_Link = new Array();
	var nCorrectDiv = 0;
	var alldivs = document.getElementsByTagName('DIV');
	//alert(alldivs);
	for (var nDiv = 0; nDiv < alldivs.length; nDiv++)
	{
		if (alldivs[nDiv].className.indexOf('setlink') != -1)
		{
			m_arrClickableDivs[nCorrectDiv] = alldivs[nDiv];
			nCorrectDiv++;
			curdiv = alldivs[nDiv];			
			
			if(curdiv.id == '')
				curdiv.id = 'div_' + nCorrectDiv;

			var allAs = curdiv.getElementsByTagName('A');
			for (var nA = 0; nA < allAs.length; nA++)
			{
				if (allAs[nA].className.indexOf('getlink') != -1)
				{
					var strHref = new String(allAs[nA].href);
					m_Link[nCorrectDiv-1] = strHref;
					//alert('adding redirect too : ' + strHref  + 'num: ' + nCorrectDiv);
					AddEvent("click", curdiv, function(evt){redirectpage(evt,strHref);});
					if (curdiv.className.indexOf('restyle') != -1)
					{
						//alert('aa');
						AddEvent('mouseover',curdiv,function(){addstyles();});
						AddEvent('mouseout',curdiv,function(){removestyles();});
					}
				}
			}
		}
	}
}

function AddEvent(strEvent, objSrc, objHandler)
{	
	if (window.addEventListener)
	{	
		objSrc.addEventListener(strEvent, objHandler, false);
	}
	else
	{		
		objSrc.attachEvent('on' + strEvent, objHandler);
	}
}

AddEvent("load",window,window_load);