if (typeof(window['npo_hide_objects']) == 'undefined') {
	npo_hide_objects = true;
}

if (typeof(window['npo_debug']) == 'undefined') {
	npo_debug = false;
}

function npo_include(script_name) {
    var head_element = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_name);
    head_element.appendChild(js);
    return false;
}

function npo_include_css(url) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var stylesheet = document.createElement('link');
    stylesheet.setAttribute('rel', 'stylesheet');
    stylesheet.setAttribute('href', url);
    stylesheet.setAttribute('type', 'text/css');
    stylesheet.setAttribute('media', 'screen');
    stylesheet.setAttribute('charset', 'utf-8');
    html_doc.appendChild(stylesheet);
    return false;
}

// Create all custom functions/vars in a separate object so we don't
// mess with local scripts
var npo = {
	
	cn_state: "closed",
	search_placeholder: "Zoek op Omroep.nl",
	search_old_value: "",
	omroep_data: "",
	has_never_been_opened: true,
	networks_are_shown: false,
	overlay: "",
	navigation: "",
	timer: "",
	host_name: "cn.omroep.nl",
	root: "",
	assets_root: "",
	sitestat: new Array(),
	klo: new Array(),
	
	television: {
		name: "Televisie",
		follow_up: { name: "meer TV", url: "http://www.omroep.nl/tv/"},
		items: {
			nederland_1: 		{ name: "Nederland 1", url: "http://www.nederland1.nl/"},
			nederland_2: 		{ name: "Nederland 2", url: "http://www.nederland2.nl/"},
			nederland_3: 		{ name: "Nederland 3", url: "http://www.nederland3.nl/"},
			nederland_24: 		{ name: "Nederland 24", url: "http://www.nederland24.nl/"},
			zapp: 		 		{ name: "Z@PP", url: "http://www.zapp.nl/"}, 
			zappelin: 			{ name: "Z@ppelin", url: "http://www.zappelin.nl/"},
			uitzending_gemist: 	{ name: "Uitzending Gemist", url: "http://www.uitzendinggemist.nl/"}
		}
	},
	
	radio: {
		name: "Radio",
		follow_up: { name: "meer radio", url: "http://www.omroep.nl/radio/"},
		items: {
			radio_1: 	{ name: "Radio 1", url: "http://www.radio1.nl/"},
			radio_2: 	{ name: "Radio 2", url: "http://www.radio2.nl/"},
			radio_3:	{ name: "3FM", url: "http://www.3fm.nl/"},
			radio_4: 	{ name: "Radio 4", url: "http://www.radio4.nl/"},
			radio_5: 	{ name: "Radio 5", url: "http://www.radio5.nl/"},
			radio_6: 	{ name: "Radio 6", url: "http://www.radio6.nl/"},
			funx: 		{ name: "FunX", url: "http://www.funx.nl/"}, 
			radiocast:  { name: "Radiocast", url: "http://www.radiocast.nl/"}
		}
	},

	network: {
		items: {
			avro: 			{ name: "AVRO", url: "http://www.avro.nl/" },
			bnn: 			{ name: "BNN", url: "http://www.bnn.nl/" },
			bos: 			{ name: "BOS", url: "http://www.buddhistmedia.com/" },
			eo: 			{ name: "EO", url: "http://www.eo.nl/" },
			human: 			{ name: "HUMAN", url: "http://www.human.nl/" },
			ikon: 			{ name: "IKON", url: "http://www.ikonrtv.nl/" },
			joodse_omroep: 	{ name: "Joodse Omroep", url: "http://www.joodseomroep.nl/" },
			kro: 			{ name: "KRO", url: "http://www.kro.nl/" },
			llink: 			{ name: "LLiNK", url: "http://www.llink.nl/" },
			max: 			{ name: "MAX", url: "http://www.omroepmax.nl/" },
			ncrv: 			{ name: "NCRV", url: "http://www.ncrv.nl/" },
			nio: 			{ name: "NIO", url: "http://www.nioweb.nl/" },
			nmo: 			{ name: "NMO", url: "http://www.nmo.nl/" },
			nos: 			{ name: "NOS", url: "http://www.nos.nl/" },
			nps: 			{ name: "NPS", url: "http://www.omroep.nl/nps/" },
			ohm: 			{ name: "OHM", url: "http://www.ohmnet.nl/" },		
			rkk: 			{ name: "RKK", url: "http://www.katholieknederland.nl/" },
			rvu: 			{ name: "RVU", url: "http://www.rvu.nl/" },
			teleac: 		{ name: "Teleac", url: "http://www.teleac.nl/" },
			tros: 			{ name: "TROS", url: "http://www.tros.nl/" },
			vara: 			{ name: "VARA", url: "http://www.vara.nl/" },
			vpro: 			{ name: "VPRO", url: "http://www.vpro.nl/" },
			zvk: 			{ name: "ZvK", url: "http://www.zvk.nl/" }
		}
	},
	
	fixImageForIE: function(img) {
		if (npo.isIE6()) {
			var src = img.src;
			var div = document.createElement("div");
			div.id = img.id;
			div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')";
			div.style.width = img.width + "px";
			div.style.height = img.height + "px";
			img.replaceNode(div);
		}
	},

	isIE6: function() {
	  return($npo.browser.msie && Number($npo.browser.version) >= 6 &&  Number($npo.browser.version) < 7);
	 },
	
	includeIE6CSS: function() {
		if (npo.isIE6()) {
			npo_include_css(npo.assets_root + '/stylesheets/ie6.css');
		}
	},
	
	// Initialize DOM components, called only once
	initComponents: function() {
		this.overlay.css('opacity', '0');
		this.navigation.css('display', 'none');

		$npo("body").prepend(this.overlay);
		$npo("body").prepend(this.navigation);

		// Add mouse handler to overlay. Must be done here because the overlay
		// is not available earlier.
		$npo("#npo_overlay").click(function() {
			npo.toggleNavigation();
		});
		
		// Set global timeout for ajax requests
		$npo.ajaxSetup({timeout: 15000});
		
		this.writeHeader();
		this.writeContentFrame();
		this.writeTelevisionAndRadioInformation();
		this.writeUgColumn();
		this.writeGidsColumn();
		this.writeOmroepColumn();

		// For some reason IE6 only groks this when using a timeout
		setTimeout("npo.fixImageForIE(document.getElementById('npo_navigation_background'));", 1);
		
		this.writeSitestat();
		
		this.loadRss();		
	},
	
	writeSitestat: function() {
		$npo("div#npo_navigation_header").append("" +
		"<!-- Begin Sitestat4 code -->" +
		"<script language=\"JavaScript1.1\" type=\"text/javascript\">" +
		"<!--" +
		"function sitestat(ns_l) {" +
			"ns_l+=\"&amp;ns__t=\"+new Date().getTime();" +
			"ns_pixelUrl=ns_l;" +
			"ns_0=document.referrer;" +
			"ns_0=(ns_0.lastIndexOf(\"/\")==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf(\"/\"),0):ns_0;" +
			"if(ns_0.length>0)ns_l+=\"&amp;ns_referrer=\"+escape(ns_0);" +
			"if(document.images){ns_1=new Image();ns_1.src=ns_l;}else" +
			"document.write(\"<img src=\"+ns_l+\" width='1' height='1' alt='Sitestat'/>\");" +
		"}" +
		"sitestat(\"http://nl.sitestat.com/klo/omroep-nl/s?centraalnav.main&amp;category=centraalnav\");" +
		"//-->" +
		"</script>" +
		"<noscript>" +
		"<img src=\"http://nl.sitestat.com/klo/omroep-nl/s?centraalnav.main&amp;category=centraalnav\" width='1' height='1' alt=\"Sitestat\" />" +
		"</noscript>" +
		"<!-- End Sitestat4 code -->" +
		"");
	},
	
	writeHeader: function() {
		$npo("div#npo_navigation").prepend(
			$npo('<div id="npo_navigation_header"/>').append(
				$npo('<div id="npo_navigation_header_sluit_link"/>').append(
					$npo('<a href="#"/>').append(
						"Sluit"
					).append(
						$npo('<img src="' + npo.assets_root + '/images/sluit.png" alt="Sluit navigatie"/>')
					)
				)
			).append(
				$npo('<div id="npo_navigation_header_omroepen_link"/>').append(
					$npo('<a href="#"/>').append(
						"Toon overzicht omroepen"
					)
				)
			)
		);
		
		$npo("div#npo_navigation").prepend(
			$npo('<img src="' + npo.assets_root + '/images/uitklap.png" alt="Navigatie" id="npo_navigation_background"/>')
			);
			
		$npo("div#npo_navigation_header_omroepen_link a").click(function() {
			npo.toggleNetworks();
			return false;
		});	

		$npo("div#npo_navigation_header_sluit_link a").click(function() {
			npo.toggleNavigation();
			return false;
		});	
	},
	
	writeContentFrame: function() {
		$npo("div#npo_navigation").append($npo('<div id="npo_navigation_content"/>'));
	},
	
	writeTelevisionAndRadioInformation: function() {
		var televisionColumn = $npo('<div id="npo_column_televisie" class="npo_column"/>');
		var radioColumn = $npo('<div id="npo_column_radio" class="npo_column"/>');
		
		npo.prepareColumn(televisionColumn, this.television);
		npo.prepareColumn(radioColumn, this.radio);
		
		// last items get a special class, because they're "special"
		$npo('div#npo_column_televisie > ul > li:last-child').addClass("npo_column_last_item");
		$npo('div#npo_column_radio > ul > li:last-child').addClass("npo_column_last_item");

	},

	writeUgColumn: function() {
		$npo("div#npo_navigation_content").append(
			$npo('<div id="npo_column_ug" class="npo_column"/>').append(
				$npo('<a href="http://www.uitzendinggemist.nl/"/>').append(
					$npo('<h1/>').append(
						"Uitzending Gemist"
					)
				)
			).append(
				$npo('<div class="npo_loader"/>').append(
					$npo('<img src="'+this.assets_root+'/images/loading.gif" alt="Data wordt ingeladen"/>')
				)
			).append(
				$npo('<ul class="npo_want_to_know_more"/>').append(
					$npo('<li/>').append(
						$npo('<a href="http://www.uitzendinggemist.nl/" class="npo_with_left_arrow"/>').append(
							"naar Uitzending Gemist"
						)
					)
				)
			)
		);
	},

	writeGidsColumn: function() {
		$npo("div#npo_navigation_content").append(
			$npo('<div id="npo_column_gids" class="npo_column"/>').append(
				$npo('<a href="http://gids.omroep.nl/"/>').append(
					$npo('<h1/>').append(
						"Gids"
					)
				)
			).append(
				$npo('<div class="npo_loader"/>').append(
					$npo('<img src="'+this.assets_root+'/images/loading.gif" alt="Data wordt ingeladen"/>')
				)
			).append(
				$npo('<ul class="npo_want_to_know_more"/>').append(
					$npo('<li/>').append(
						$npo('<a href="http://gids.omroep.nl/" class="npo_with_left_arrow"/>').append(
							"naar tv-gids"
						)
					)
				).append(
					$npo('<li class="npo_with_dotted_top"/>').append(
						$npo('<a href="http://gids.omroep.nl/?medium=Radio" class="npo_with_left_arrow"/>').append(
							"naar radio gids"
						)
					)
				)
			)
		);
	},

	writeOmroepColumn: function() {
		$npo("div#npo_navigation_content").append(
			$npo('<div id="npo_column_omroep" class="npo_column"/>').append(
				$npo('<a href="http://www.omroep.nl/"/>').append(
					$npo('<h1/>').append(
						"Omroep"
					)
				)
			).append(
				$npo('<div id="npo_search"/>').append(
					$npo('<form method="POST" action="'+this.root+'/searches/"/>').append(
						$npo('<input type="text" id="npo_search_field" name="query" value="' + this.search_placeholder + '"/>')
					).append(
						$npo('<input type="submit" id="npo_search_submit" value=""/>')
					)
				)
			).append(
				$npo('<div class="npo_loader"/>').append(
					$npo('<img src="'+this.assets_root+'/images/loading.gif" alt="Data wordt ingeladen"/>')
				)
			).append(
				$npo('<ul class="npo_want_to_know_more"/>').append(
					$npo('<li/>').append(
						$npo('<a href="http://www.omroep.nl/" class="npo_with_left_arrow"/>').append(
							"naar Omroep.nl"
						)
					)
				)
			)
		);
		
		// Attach handlers to search field
		var theField = $npo('#npo_search_field');
		theField.blur(function() 	{npo.searchFieldBlurred();});
		theField.keyup(function(e) 	{npo.searchFieldKeyUp(e);});
		theField.focus(function() 	{npo.searchFieldGotFocus();});
	},

	searchFieldBlurred: function() {
		var theField = $npo("#npo_search_field");
		if (theField.val() == "") {
			theField.val(this.search_placeholder);
			$npo("#npo_omroep_data").html(this.omroep_data);
			// and hide the suggestions box
		}
	},
	
	searchFieldKeyUp: function(e) {
		clearTimeout(this.timer);
		var theField = $npo("#npo_search_field");
		if (theField.val().length > 0 && theField.val() != this.search_placeholder) {
			// Trigger search after 350ms of inactivity to prevent overloading the server
			this.timer = setTimeout("npo.triggerSearch();", 350);
		} else {
			$npo("#npo_omroep_data").html(this.omroep_data);			
		}
	},

	searchFieldGotFocus: function() {
		var theField = $npo("#npo_search_field");
		if (theField.val() == this.search_placeholder) {
			theField.val("");
		}		
	},
	
	triggerSearch: function() {
		var theField = $npo("#npo_search_field");
		// Only search if the value has changed and is at least 2 (non-WS) characters long
		if (theField.val() != this.search_old_value && npo.trim(theField.val()).length > 1) {
			this.search_old_value = theField.val();
			var query = npo.normalizeQuery(theField.val());
			$npo.ajax({
				type: "GET",
				cache: true,
				dataType: "jsonp",
				error: npo.failedSearch,
				success: npo.showSearchSuggestions,
				url: npo.root + "/searches/" + query
				});
		}
	},
	
	showSearchSuggestions: function(data, textStatus) {
		var theDiv = $npo("#npo_omroep_data");
		theDiv.html("");
		theDiv.append("<h2>Zoeksuggesties</h2>");

		if (data.length == 0) {
			theDiv.append("<i>Geen suggesties gevonden.</i>");
		} else {
			for (var item=0; item < data.length; item++) {
				var query = npo.normalizeQuery(data[item].search.query);
				var theValue = data[item].search.query;
				var fieldValue = $npo("#npo_search_field").val();
				
				var strpos = theValue.toLowerCase().indexOf(fieldValue.toLowerCase());
				
				if (strpos > -1) {
					var queryStart = theValue.substr(0, strpos);
					var queryMiddle = theValue.substr(strpos, fieldValue.length);
					var queryEnd = theValue.substr(strpos+fieldValue.length);
					
					theValue = queryStart + "<b>" + queryMiddle + "</b>" + queryEnd;
				}
				
				theDiv.append('<a href="' + npo.root + '/searches/' +
								query +
								'" class="npo_with_left_arrow" onclick="npo.customSubmit(this); return false;">' +
								theValue +
								'</a>');
			}
		}
	},
	
	// Custom submit function to submit search suggestion, taken from Rails template.
	// Wrap a form around the hyperlink on click
	// TODO: replace with jquery code
	customSubmit: function(node) {
		var f = document.createElement('form');
		f.style.display = 'none';
		node.parentNode.appendChild(f);
		f.method = 'POST';
		f.action = node.href;
		var m = document.createElement('input');
		m.setAttribute('type', 'hidden');
		m.setAttribute('name', '_method');
		m.setAttribute('value', 'put');
		f.appendChild(m);
		f.submit();
	},
	
	normalizeQuery: function(someString) {
		return encodeURIComponent(npo.trim(someString));
	},
	
	// Utility function to insert content into television/radio columns
	prepareColumn: function(column, data) {
		column.append('<a href="' + data.follow_up.url + '"><h1>' + data.name + '</h1></a>');
		var theList = $npo('<ul/>');
		for (var item in data.items) {
			theList.append($npo("<li/>").append(
							$npo('<span/>').append(
							$npo('<img src="'+this.assets_root+'/images/logo/' + item + '.png" alt="' + data.items[item].name + '"/>')).append(
							$npo('<a class="black" href="' + data.items[item].url + '"/>').append(
								data.items[item].name
							))));				
		}
		
		column.append(theList);
		
		column.append(
			$npo('<ul class="npo_want_to_know_more"/>').append(
				$npo('<li/>').append(
					$npo('<a href="' + data.follow_up.url + '" class="npo_with_left_arrow"/>').append(
						data.follow_up.name
					)
				)
			)
		);
		$npo('div#npo_navigation_content').append(column);		
	},
	
	// Toggle the list of networks
	toggleNetworks: function() {
		if (this.networks_are_shown) {
			$npo("div#npo_navigation_header_omroepen_link a").text("Toon overzicht omroepen");
			// Fade in other columns
			$npo("#npo_column_televisie").fadeIn("normal");
			$npo("#npo_column_radio").fadeIn("normal");
			$npo("#npo_column_ug").fadeIn("normal");
			$npo("#npo_column_gids").fadeIn("normal");
			$npo("#npo_omroep_column").fadeOut("normal");
			this.networks_are_shown = false;
		} else {
			if ($npo("#npo_omroep_column").length == 0) {
				$npo("#npo_navigation_content").append($npo("<div id='npo_omroep_column'/>").css('display', 'none'));
				npo.writeNetworks();
			}
			$npo("div#npo_navigation_header_omroepen_link a").text("Verberg overzicht omroepen");
			// Fade out other columns
			$npo("#npo_column_televisie").fadeOut("normal");
			$npo("#npo_column_radio").fadeOut("normal");
			$npo("#npo_column_ug").fadeOut("normal");
			$npo("#npo_column_gids").fadeOut("normal");
			$npo("#npo_omroep_column").fadeIn("normal");
			this.networks_are_shown = true;			
		}
	},
	
	// Append list of networks to div
	// TODO: Insert real logos here
	writeNetworks: function() {
		$npo("#npo_omroep_column").append(
			$npo("<h1/>").append("Omroepen")
		);
		
		var theList = $npo('<ul/>');
		var counter = 1;
		var column = 0;
		for (var item in this.network.items) {
			theList.append($npo("<li/>").append(
				$npo('<img src="'+this.assets_root+'/images/logo/'+ item +'.png" alt="' + this.network.items[item].name + '"/>')
			).append(
				$npo('<a class="black" href="' + this.network.items[item].url + '"/>').append(this.network.items[item].name)
			));
			if ((counter % 7) == 0) {
				$npo("#npo_omroep_column").append(theList);
				column++;
				theList = $npo('<ul/>');
				theList.css('left', column*151 + "px");
			}
			counter++;
		}
		if (theList.length > 0) {
			$npo("#npo_omroep_column").append(theList);
		}
		
	},
	
	// Reposition the dropdown
	updateNavigationLocationAndSize: function() {
		$npo('div#npo_navigation').css(
			'top',
			$npo("div#npo_button_container").offset().top + $npo("div#npo_button_container").height() + 10 + "px"
			);
		$npo('div#npo_navigation').css(
			'left',  
			$npo("div#npo_button_container").offset().left + $npo("div#npo_button_container").width() - 772 + "px"
			);
		
		var $htmlHeight = $npo('html').get(0).clientHeight;
		var $htmlScrollHeight = $npo('html').get(0).scrollHeight;
		var $bodyScrollHeight = $npo('body').get(0).scrollHeight;
		
		var $htmlWidth = $npo('html').get(0).clientWidth;
		var $htmlScrollWidth = $npo('html').get(0).scrollWidth;
		
		$newHeight = Math.max($htmlHeight, Math.max($htmlScrollHeight, $bodyScrollHeight));
		$npo('div#npo_overlay').css("height", $newHeight + "px");
		$htmlScrollWidth > $htmlWidth 		? $npo('div#npo_overlay').css("width", $htmlScrollWidth + "px") : $npo('div#npo_overlay').css("width", $htmlWidth + "px"); 
		$npo('div#npo_overlay').css("top", "0px");
	},

	// Switch navigation on/off
	toggleNavigation: function() {
		if (this.cn_state == "closed") {
			if (this.has_never_been_opened) {
				npo.initComponents();
				this.has_never_been_opened = false;
			}
			npo.updateNavigationLocationAndSize();
			$npo("div#npo_overlay").show();
			$npo("div#npo_overlay").fadeTo("normal", 0.65);
			$npo(window).bind('resize.npo', function() {npo.updateNavigationLocationAndSize();});
			
			npo.toggleProblemObjectsVisibility(false);
			$npo("div#npo_navigation").fadeIn("normal");
			this.cn_state = "open";
		} else {
			$npo("div#npo_overlay").fadeTo("normal", 0, function() {
				$npo(window).unbind('resize.npo');
				$npo("div#npo_overlay").hide();			
			});

			$npo("div#npo_navigation").fadeOut("normal", function() {npo.toggleProblemObjectsVisibility(true);});
			this.cn_state = "closed";
		}
	},

	toggleProblemObjectsVisibility: function(visible) {
	  //toont of verbergt problematische objecten (flash, selectfields)
		//ook voor alle iframes
	  var vis = (visible === false && npo_hide_objects)?"hidden":"visible";
	  $npo("select,object,embed").css("visibility", vis);
	  $npo("iframe").css("visibility", vis);
	 },
	
	// Utility function to replace the loader placeholder with actual content
	replaceWith: function(oldNode, newNodeHtml, content) {
		$npo(oldNode).replaceWith($npo(newNodeHtml).append(content));
	},
	
	// Load RSS/JSON data and modify the DOM
	loadRss: function() {
		$npo.ajax({
			type: "GET",
			cache: true,
			dataType: "jsonp",
			error: npo.failedUgData,
			success: npo.importUgData,
			url: this.root+"/feeds/uitzending-gemist-tips"
			});

		$npo.ajax({
			type: "GET",
			cache: true,
			dataType: "jsonp",
			error: npo.failedTipsData,
			success: npo.importTipsData,
			url: this.root+"/feeds/omroepnl-etalage"
			});

		$npo.ajax({
			type: "GET",
			cache: true,
			dataType: "jsonp",
			error: npo.failedGidsData,
			success: npo.importGidsData,
			url: this.root+"/feeds/tv-tips"
			});
	},

	failedUgData: function(request, textStatus, errorThrown) {
		npo.failAjax("ug");
	},

	failedTipsData: function(request, textStatus, errorThrown) {
		npo.failAjax("tips");
	},

	failedGidsData: function(request, textStatus, errorThrown) {
		npo.failAjax("gids");
	},
	
	failedSearch: function(request, textStatus, errorThrown) {
		npo.failAjax("search");
	},
	
	failedSurvey: function(request, textStatus, errorThrown) {
		npo.failAjax("survey");
	},
	
	failAjax: function(item) {
		switch(item) {
			case "ug":
				npo.replaceWith(
					'div#npo_column_ug > div.npo_loader',
					"<div id='npo_ug_data' class='npo_column_data'/>",
					"<i>Geen tips van Uitzending Gemist beschikbaar.</i>"
					);
				break;
			case "tips":
				npo.replaceWith(
					'div#npo_column_omroep > div.npo_loader',
					"<div id='npo_omroep_data' class='npo_column_data'/>",
					"<i>Geen webtips van Omroep.nl beschikbaar.</i>"
					);
				npo.omroep_data = $npo("#npo_omroep_data").html();
				break;
			case "gids":
				npo.replaceWith(
					'div#npo_column_gids > div.npo_loader',
					"<div id='npo_gids_data' class='npo_column_data'/>",
					"<i>Geen gidstips beschikbaar.</i>"
					);
				break;
			case "search":
				npo.showSearchSuggestions("","");
				break
		}
	},
	
	// Import Uitzending Gemist data
	importUgData: function(data, textStatus) {
		if (data != "") {
			var title = data[0].feed_item.title;
			var description = data[0].feed_item.description;
			var link = data[0].feed_item.link;
			var image_uri = data[0].feed_item.enclosure_location;
			var image_html = '';
			if (image_uri != '') {
				image_html = "<img src='" + image_uri + "' alt='' class='npo_tip'/>";
			} else {
				image_html = '';
			}

			npo.replaceWith(
				'div#npo_column_ug > div.npo_loader',
				"<div id='npo_ug_data' class='npo_column_data'/>",
				"<strong>TIP</strong>" + "<h2><a class='black' href='" + link + "'>" + title + "</a></h2>" + image_html + npo.truncate(description,165)
				);				
		} else {
			npo.failAjax("ug");
		}
	},
	
	// Import Gids data
	importGidsData: function(data, textStatus) {
		if (data != "") {
			var title = data[0].feed_item.title;
			var description = data[0].feed_item.description;
			var link = data[0].feed_item.link;
	
			npo.replaceWith(
				'div#npo_column_gids > div.npo_loader',
				"<div id='npo_gids_data' class='npo_column_data'/>",
				"<strong>TIP</strong>" + "<h2><a class='black' href='" + link + "'>" + title + "</a></h2>" + npo.truncate(description,165)
				);
		} else {
			npo.failAjax("gids");
		}
	},
	
	// Import Tips data
	importTipsData: function(data, textStatus) {
		if (data != "") {
			var item = data[0];
			newHTML = "<h2><a class=\"npo_with_left_arrow black\" href=\"" + item.feed_item.link + "\">" + item.feed_item.title + "</a></h2>";
			newHTML += item.feed_item.description;
	
			npo.replaceWith(
				'div#npo_column_omroep > div.npo_loader',
				"<div id='npo_omroep_data' class='npo_column_data'/>",
				newHTML
				);
			npo.omroep_data = $npo("#npo_omroep_data").html();
		} else {
			npo.failAjax("tips");
		}
	},

	getSitestat: function() {
		if (typeof(window['ns_1']) !== 'undefined') {
			var slug = /nl\.sitestat\.com\/klo\/.*/.exec(ns_1.src).toString().replace(/&amp;/g, '&');
			var slugArray = slug.split("&");
			npo.sitestat['omroep'] = /nl\.sitestat\.com\/klo\/([\w\.\-]+)/.exec(slugArray[0])[1];
			npo.sitestat['categorie'] = "";
			npo.sitestat['thema'] = "";
		
			for (var i=1; i < slugArray.length; i++) {
				var splittedItem = slugArray[i].split('=');
				switch(splittedItem[0]) {
					case "category":
						npo.sitestat['categorie'] = splittedItem[1];
						break;
					case "thema":
						npo.sitestat['thema'] = splittedItem[1];
						break;
				}
			}

			// do JSONP request for survey (thema is ignored)
			$npo.ajax({
				type: "GET",
				cache: true,
				dataType: "jsonp",
				error: npo.failedSurvey,
				success: npo.triggerSurvey,
				url: this.root+"/surveys/" + encodeURIComponent(npo.sitestat['omroep']) +
											"?category=" + encodeURIComponent(npo.sitestat['categorie'])
				});
			}
	},
	
	stripClickHandler: function() {
		// Explicitly remove onclick handlers because there exist some
        // scripts that apply custom handlers
        $npo('div#npo_button_container a').get(0).onclick = '';
		setTimeout("npo.stripClickHandler();", 250);
	},
	
	triggerSurvey: function(data, textStatus) {
		if (data != "") {
			// do something
			npo.klo['is_polite'] 	= data.survey.is_polite;
			npo.klo['appearance'] 	= data.survey.appearance;
			npo.klo['chance'] 		= data.survey.chance;
			npo.klo['name'] 		= data.survey.name;
			npo.klo['identifier']	= data.survey.identifier;
			
			npo_include(npo.assets_root + "/javascripts/legacy-klo.js");
		}
	},
	
	trim: function(someString) {
		return someString.replace(/^\s+|\s+$/g, '');
	},
	
	// Function to truncate strings at a specified length
	truncate: function(someString, maxLength) {
		if (someString.length > maxLength) {
			for (var i = maxLength-1; someString.charAt(i) != ' '; i--) {
				maxLength--;
			}
			return someString.substr(0, maxLength) + "[&hellip;]";
		} else {
			return someString;
		}		
	}
}

// Setup correct roots
npo.root = "http://" + npo.host_name;
npo.assets_root = "http://assets." + npo.host_name;

// Load jQuery
npo_include(npo.assets_root + "/javascripts/jquery-npo-1.3.2.min.js");
npo_include_css(npo.assets_root + '/stylesheets/default.css');

// Trigger init function
npo_init();

function npo_init() {
	
	// Add a document ready handler to the button to call our init function
	// Prefix jQuery to '$npo' instead of '$' so we don't get in the way
	// of other scripts. 
	if (typeof(window['$npo']) == 'undefined') {
		setTimeout("npo_init();", 100);
		return;
	}
	
	//$npo.noConflict();
	
	npo.overlay = $npo("<div id='npo_overlay'/>");
	npo.navigation = $npo("<div id='npo_navigation'/>");

	// wait for button_container to become available
	if ($npo("#npo_button_container").length == 0) {
		setTimeout("npo_init();", 50);
		return;		
	}

	var fix = $npo("div#npo_button_container a"); //fixt bug die bij klik op links npo menu opent (via VARA)
	$npo("div#npo_button_container a").bind("click.npo", function() {
  	npo.toggleNavigation();
    return false;
  });

	npo.includeIE6CSS();
	npo.stripClickHandler();
	npo.getSitestat();
}
