
var ticker_width = 700;
var ticker_height = 18;
var ticker_speed = 1;
var ticker_pause_onhover = 1;

var ticker_interval_id;
var ticker_rss;

var ticker_speed = (document.all) ? ticker_speed : Math.max(1, ticker_speed-1);
var copyspeed = ticker_speed;
var pausespeed = (ticker_pause_onhover == 0) ? copyspeed : 0;

var ticker_switch = {0: 'TICKER AAN', 1: 'TICKER UIT'};
var unreal_rss = '<nobr style="font-weight:bold">Ga voor het LAATSTE NIEUWS naar <a class="ticker_news" style="color:white" href="http://www.telegraaf.nl/" target="_blank">www.telegraaf.nl</a></nobr>';
document.write('<span id="temp" style="visibility:hidden;position:absolute;top:-200px;left:-100000px"></span>');


var actualwidth='';
var rh_ticker_iedom, rh_ticker_ns;




function processRSS(responseHTML, real) {
	var $t = $('temp');
	$t.set('html', responseHTML);
	actualwidth = $t.offsetWidth;
	if (real) ticker_rss = responseHTML;

	rh_ticker_iedom.set('html', responseHTML);
	//$t.dispose();
}

function setStaticTicker() {
	processRSS(unreal_rss);
	rh_ticker_iedom.style.left = ((ticker_width - actualwidth)/2)+"px";
}




function populate(start) {

	rh_ticker_iedom.style.left = (ticker_width + 8) +"px";

	if (ticker_rss)
		processRSS(ticker_rss, true);
	else
		new Request.HTML({
			headers: {'Accept': 'text/html'}, url:'/rss.pl', /*update: rh_ticker_iedom,*/ 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				processRSS(responseHTML, true);
				if (!start) setStaticTicker();
			}}).get();


	if (start)
		ticker_interval_id = setInterval("rh_ticker_scroll()",20);
	else
		setStaticTicker();

}


function rh_ticker_scroll() {
	var left = parseInt(rh_ticker_iedom.style.left, 10);
	if (left > (actualwidth*(-1) + 8)) {
		rh_ticker_iedom.style.left = (left - copyspeed) + "px"
	} else {
		rh_ticker_iedom.style.left = (ticker_width + 8)+"px"
	}
}


function flipTicker(cb) {

	if (cb.checked)
	{
		populate(true);
		document.cookie = 'noticker=0; path=/; expires=Tue, 31 Dec 2199 23:00:00 UTC';
	}
	else
	{
		clearInterval(ticker_interval_id);
		setStaticTicker();
		document.cookie = 'noticker=1; path=/; expires=Tue, 31 Dec 2199 23:00:00 UTC';
	}
	document.getElementById('ticker_info').innerHTML = ticker_switch[cb.checked?1:0];
	//'TICKER AAN';
}



function createTicker() {
	var $ticker = $('ticker');
	if (!$ticker) return;
	ticker_width = $ticker.offsetParent.offsetWidth - 125;
	
	with (document) {
		var noticker = cookie.indexOf('noticker=1') >= 0;
		var ticker_info_text = noticker ? 'TICKER AAN' : 'TICKER UIT';

		var t = '<td colspan="4" style="line-height:1.5;">'+
		'<div style="height:' + ticker_height + 'px;width:98px;float:right;overflow:hidden;">' + 
			'<table cellpadding="0" width="100%" height="18px"><tr><td style="width:80px;font-weight:bold;vertical-align:middle"><nobr id="ticker_info">'+ ticker_info_text + '</nobr></td><td style="vertical-align:middle" aling="right">' +
		    '<input type="checkbox" onclick="flipTicker(this)" ' + (noticker ? '' : 'checked') + ' />' + 
			'</td></tr></table>' +
		'</div>' +
		'<div style="position:relative;width:'+ticker_width+'px;height:'+ticker_height+'px;overflow:hidden">'+
		  '<div style="position:absolute;width:'+ticker_width+'px;height:'+ticker_height+'px;" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=ticker_speed">'+
		    '<div id="ie_rh_ticker" style="position:absolute;left:0px;top:0px"></div>' + 
		'</div></div></td>';
		
		$('ticker').empty().set('html', t);
	}
	rh_ticker_iedom = $('ie_rh_ticker'); 
	populate(!noticker);
}


/* PNG fix for IE6
*/

function pngfix()
{
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);

  if (version >= 5.5 && version < 7)
  {
    for(var i=0; i<document.images.length; i++)
    {
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
      {
        var imgID = (img.id) ? "id='" + img.id + "' " : "";
        var imgClass = (img.className) ? "class='pngfixed " + img.className + "' " : "";
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
        var imgStyle = "display:inline-block;" + img.style.cssText + ";visibility:visible;";
        if (img.align == "left") imgStyle = "float:left;" + imgStyle;
        if (img.align == "right") imgStyle = "float:right;" + imgStyle;
        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
        var strNewHTML = "<div " + imgID + imgClass + imgTitle
        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
        + "(src=\'" + img.src + "\', sizingMethod='scale');\"></div>";
        img.outerHTML = strNewHTML;
        i = i-1;
      }
    }
  }
}


var search;

function openSearch() {
	//$('iframe').load('/static/onlinelinks.html');
	//new Request.HTML({headers: {'Accept': 'text/html'}, update: $('pageframe')}).get('/static/telegraafi.html');
	$('pageframe').src = '/tmg/search.pl';
}

function checkInner() {
	var m = document.location.href.match(/\Wif=([^&;]+)/);
	if (m) {
		var url = m[1];
		if (url.match(/:\/\//)) return;

		var pf = document.getElementById('pageframe');	
		if (pf) {
			pf.src = m[1];
		}
		else if (url.match(/vrouw/)) {
			document.location.href = '../vrouw/?if=' + url;
		}
	}
}

function go2(btn, url) {
	var pg = url.replace(/[^\d]/g, '');
	pg = parseInt(pg, 10);
	var fwd = btn.innerHTML.match(/arrows\/right/);
	if (fwd) {
		
	}
}

function checkThumbNavigator() {
	$$("#navbuttons a.button").each(function(n){ 
		var onclk = n.getProperty("onclick");
		if (onclk) {
			//var newStrFnc = onclk.replace(/\'\)/, "/index.htm')");
			var newStrFnc = onclk.replace(/go\(/, "go2(");
			var fnc = new Function(newStrFnc);
			var url = newStrFnc.match(/go2\((.*?)\)/);
			n.onclick = function() { go2(n, url[1]); return false; }
//			n.onclick = function() { (new Function(newStrFnc))() };
		}
	});	
}

var doOnce = true;
window.addEvent('domready', function() {
	if (!doOnce) return;
	doOnce = false;
	var url = document.location.href;
	var match = url.match(/(\d{4}\/\d{2}\/\d{2})/);
	var selID = "";
	if (match) {
		selID = '#archief-' + match[1].replace(/\//g, '-');
	}

	$$('#searchmenu_src_parent').addEvent('click', function() { openSearch() });

	new Request.HTML({headers: {'Accept': 'text/html'}, update: 'archieflinks', url:'/static/archief.html', onSuccess: function() {if (selID) $$(selID).addClass('selected')} }).get();
	new Request.HTML({headers: {'Accept': 'text/html'}, update: 'onlinelinks', url:'/static/onlinelinks.html'}).get();
	new Request.HTML({headers: {'Accept': 'text/html'}, update: 'telegraafiblok', url: '/static/telegraafi.html'}).get();
	new Request.HTML({headers: {'Accept': 'text/html'}, update: 'tijdschriften', url: '/static/digiedition.html'}).get();

	pngfix();
	checkInner();
	createTicker();
	//checkThumbNavigator();
});





