/*
** Linker 
** written by Marek Robak, Bonnier Business (Polska)/Arunas Janeliunas, Verslo Zinios
*/

linker = function(){}

linker.activate;
linker.rinkos;
linker.spekuliantai;
linker.archyvas;
linker.creditinfo;

linker.pop_up;

linker.urlRinkos       = 'http://rinkos.vz.lt/stock_details.html?stock_id=%ID%&lang=lit';
linker.urlSpekuliantai = 'http://www.spekuliantai.lt/forums.php?m=posts&q=%ID%&n=last#bottom';
linker.urlArchyvas     = 'http://archyvas.vz.lt/index.php?ss=3&sa=1&sn=1&sp=1&s=%ID%&st=2';
linker.urlCreditInfo   = 'http://parduotuve.vz.lt/reitingai/creditinfo/';

linker.mouse_over = function(e)
{
     if(e)
     {
          e.style.textDecoration = 'underline';
     }
}

linker.mouse_out = function(e)
{
     if(e)
     {
          e.style.textDecoration = 'none';
     }
}

linker.show_pop_up = function(e)
{
	e = e || window.event;
	
    this.pop_up.style.left = (Event.pointerX(e)-20)+'px';
    this.pop_up.style.top  = (Event.pointerY(e)+5)+'px';

	var tg = (window.event) ? e.srcElement : e.target;

	if (this.creditinfo[tg.id]) {
		$('linker_creditinfo').show();			   
		$('linker_creditinfo').href = 'http://vz.lt/App_Modules/Redirect/redirectUrl.aspx?keyword=' + Url.base64encode('to_creditinfo') + '&link=' + Url.base64encode( this.urlCreditInfo+[tg.id]+"/"+Url.base64encode([tg.id]+'|'+this.creditinfo[tg.id]) );			   
	}
	else {
		$('linker_creditinfo').hide();			   
	}

	if (this.rinkos[tg.id]) {
		$('linker_rinkos').show();			   
		$('linker_rinkos').href = 'http://vz.lt/App_Modules/Redirect/redirectUrl.aspx?keyword=' + Url.base64encode('to_investor') + '&link=' + Url.base64encode(this.urlRinkos.replace('%ID%',this.rinkos[tg.id]));			   
	}
	else {
		$('linker_rinkos').hide();			   
	}

	/*
	if (this.spekuliantai[tg.id]) {
		$('linker_spekuliantai').show();			   
		$('linker_spekuliantai').href = 'http://vz.lt/App_Modules/Redirect/redirectUrl.aspx?keyword=' + Url.base64encode('to_spekuliantai') + '&link=' + Url.base64encode(this.urlSpekuliantai.replace('%ID%',this.spekuliantai[tg.id]));			   
	}
	else {
		$('linker_spekuliantai').hide();			   
	}
	*/

	if (this.archyvas[tg.id]) {
		$('linker_archyvas').show();			   
		$('linker_archyvas').href = 'http://vz.lt/App_Modules/Redirect/redirectUrl.aspx?keyword=' + Url.base64encode('to_archyvas') + '&link=' + Url.base64encode(this.urlArchyvas.replace('%ID%',this.archyvas[tg.id]));			   
	}
	else {
		$('linker_archyvas').hide();			   
	}

    Effect.BlindDown(this.pop_up, {duration:0.4});
}

linker.hide_pop_up = function()
{
    Effect.BlindUp(this.pop_up, {duration:0.2});
}

linker.simple_replace = function(nodes)
{
     for(var i = 0; i < nodes.length; i++)
     {
          if (nodes[i]) {
          	nodes[i].innerHTML = linker.parser(nodes[i].innerHTML); 
          }	
     }
}

linker.parser = function(txt)
{
     if(txt != null)
     {
          for(var i in linker.activate)
          {
               var searchstring = linker.activate[i].replace(/,/g,'|');
			   
			   //if(searchstring=='Gala') alert('Gala');
			   
               txt = txt.replace(new RegExp('(' + searchstring + ')([ .,;?!&])' + '(.*)'), '&nbsp;<span class="linker_activate" id="' + i + '" onMouseOver="linker.mouse_over(this);" OnMouseOut="linker.mouse_out(this);">$1</span>$2$3');
          }
     }
     return txt;
}

linker.putIcons = function(item)
{
	var node = item.firstChild;

    while (node) {
        if (node.nodeType === 3) {
			if ( node.nodeValue.indexOf('(MOKAMA)') > 0 ) {
				node.nodeValue = node.nodeValue.replace( /\(MOKAMA\)/gi, '' );
				item.insert( {'bottom' : '<img alt="(MOKAMA)" title="(MOKAMA)" width="16" height="9" src="/gfx/icon_key.gif" style="margin-right:3px;">'} ); 
			}	
			if ( node.nodeValue.indexOf('(NUOTRAUKOS)') > 0 ) {
				node.nodeValue = node.nodeValue.replace( /\(NUOTRAUKOS\)/gi, '' );
				item.insert( {'bottom' : '<img alt="(NUOTRAUKOS)" title="(NUOTRAUKOS)" width="11" height="9" src="/gfx/icon_photo.gif" style="margin-right:3px;">'} ); 
			}	
			if ( node.nodeValue.indexOf('(VIDEO)') > 0 ) {
				node.nodeValue = node.nodeValue.replace( /\(VIDEO\)/gi, '' );
				item.insert( {'bottom' : '<img alt="(VIDEO)" title="(VIDEO)" width="11" height="9" src="/gfx/icon_video.gif" style="margin-right:3px;">'} ); 
			}	
			if ( node.nodeValue.indexOf('(NUOTRAUKOS, VIDEO)') > 0 ) {
				node.nodeValue = node.nodeValue.replace( /\(NUOTRAUKOS, VIDEO\)/gi, '' );
				item.insert( {'bottom' : '<img alt="(NUOTRAUKOS)" title="(NUOTRAUKOS)" width="11" height="9" src="/gfx/icon_photo.gif" style="margin-right:3px;">'} ); 
				item.insert( {'bottom' : '<img alt="(VIDEO)" title="(VIDEO)" width="11" height="9" src="/gfx/icon_video.gif" style="margin-right:3px;">'} ); 
			}	
		}	
        node = node.nextSibling;
    }
}	

linker.go = function()
{
	 if ( $('col2') ) {
		//$('col2a').innerHTML = $('col2a').innerHTML.replace( /{key}/gi, '<img alt="[Registruotiems]" title="[Registruotiems]" width="17" height="8" src="/gfx/icon_key.gif">' );
		$$('#col2 td.aview_headline').each( function(item){linker.putIcons(item);} );
		$$('#col2 a h1 b').each( function(item){linker.putIcons(item);} );
		$$('#col2 a').each( function(item){linker.putIcons(item);} );
	 }	
	 if ( $('col3') ) {
		$$('#col3 .inner2 a').each( function(item){linker.putIcons(item);} );
	 }	
	 if ( $('col4') ) { 
		//$('col4').innerHTML = $('col4').innerHTML.replace( /{key}/gi, '<img alt="[Registruotiems]" title="[Registruotiems]" width="17" height="8" src="/gfx/icon_key.gif">' );
		$$('#col4 .inner p a').each( function(item){linker.putIcons(item);} );
		$$('#col4 .inner p a b').each( function(item){linker.putIcons(item);} );
		$$('#col4 .inner .header b').each( function(item){linker.putIcons(item);} );
	 }	
	 this.pop_up = $('linker_pop_up');
	 
	 strRequest = '/App_Modules/RelatedInfo/GetLinkedTags.aspx?context='+Url.base64encode(window.location.href);
	 new Ajax.Request( strRequest, 
							{ method: 'post',
							  onSuccess: function(res) 
							             {
							               eval(res.responseText);
	 									   
						               	   linker.activate     = linker_activate;
	 									   linker.rinkos       = linker_rinkos;
	 									   linker.archyvas     = linker_archyvas;
	 									   linker.creditinfo   = linker_creditinfo;
	 									   
							               linker.simple_replace([$('articleintro'),$('articleinternals')]);
										   
									   	   // show TextAds then
										   if ($('copyrigntMessagePR')==null) {
												nsix_params.run();
										   }
							             } 
							});

}
