/* Cached 22-2-2012 16:51:23 */ 
var _uriBase = "http://avro.nl";
var _isAuthenticated = false;
var jQueryScriptOutputted = false;
var jQueryUIScriptOutputted = false;
var head = document.getElementsByTagName('head')[0];
var uriBase = (typeof (_uriBase) == "undefined") ? "http://avro.nl" : _uriBase;
var uriLogin = "/User/Login";
var staticExtern = "http://staticextern.avro.nl";
var searchInterval;
var avroKids = false;
var avroUsername = "";

window.innerShiv = function () { function h(c, e, b) { return /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i.test(b) ? c : e + "></" + b + ">" } var c, e = document, j, g = "abbr article aside audio canvas datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video".split(" "); return function (d, i) { if (!c && (c = e.createElement("div"), c.innerHTML = "<nav></nav>", j = c.childNodes.length !== 1)) { for (var b = e.createDocumentFragment(), f = g.length; f--; ) b.createElement(g[f]); b.appendChild(c) } d = d.replace(/^\s\s*/, "").replace(/\s\s*$/, "").replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "").replace(/(<([\w:]+)[^>]*?)\/>/g, h); c.innerHTML = (b = d.match(/^<(tbody|tr|td|col|colgroup|thead|tfoot)/i)) ? "<table>" + d + "</table>" : d; b = b ? c.getElementsByTagName(b[1])[0].parentNode : c; if (i === !1) return b.childNodes; for (var f = e.createDocumentFragment(), k = b.childNodes.length; k--; ) f.appendChild(b.firstChild); return f } } ();

function initJQuery() {
	//if the jQuery object isn't available
	if (typeof (jQuery) == 'undefined') {
		if (!jQueryScriptOutputted) {
			//only output the script once..
			jQueryScriptOutputted = true;
			var js = document.createElement('script');
			js.setAttribute('language', 'javascript');
			js.setAttribute('type', 'text/javascript');
			js.setAttribute('src', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js');
			head.appendChild(js);
		}
		setTimeout("initJQuery()", 50);
	} else {
		//load jQueryUI if not yet available
		if (typeof (jQuery.ui) == 'undefined') {
			if (!jQueryUIScriptOutputted) {
				//jQuery.getScript("http://staticextern.avro.nl/Scripts/Plugins/jquery-ui-1.8.16.custom.min.js");
				jQuery.getScript("https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js");
				jQueryUIScriptOutputted = true;
			}
			setTimeout("initJQuery()", 50);
		} else
			onJqLoaded();
	}
}

/* Partial JS for HTML goes here */
/* Default navigation bar */
function onJqLoaded() {
	var activeItem = "";
	var external = false;
	var avrodomain = false;
	var initialClass = "";

	if (document.domain.indexOf("avro.nl") > -1 || document.domain == 'localhost' || document.domain == 'wwwhost')
		avrodomain = true;

	//retrieve settings from uri?
	//var jsUri = jQuery("#nbScript").attr("src");
	var jsUri = jQuery("script[src*='/avrobalk.ashx']").attr("src");
	if (jsUri && jsUri.indexOf('?') > -1) {
		//try parsing the querystring, a=activeItem, ext=external site
		var querystring = jsUri.split('?')[1].split('&');
		for (var i = 0; i < querystring.length; i++) {
			var keyvalue = querystring[i].split('=');
			switch (keyvalue[0]) {
				case "active":
				case "a":
				case "item":
				case "selected":
					activeItem = keyvalue[1];
					break;
				case "avrokids":
					avroKids = (keyvalue[1] == "1");
					break;
				case "ext":
					external = true;
					break;
				case "class":
					initialClass = " " + keyvalue[1];
					break;
			}
		}
	}

	if (jQuery("link[type='text/css'][href*='/css/Grid/avro_import.css']").length == 0)
		external = true;
	//start by loading CSS
	if (external) {
		var noIECache = (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) ? "?" + new Date().getMilliseconds() : "";

		jQuery(document.createElement('link'))
			.attr({ type: 'text/css', href: uriBase /* staticExtern */ + '/css/Components/bovenbalk.css' + noIECache, rel: 'stylesheet', media: 'screen' })
			.appendTo(head);

		jQuery(document.createElement('link'))
			.attr({ type: 'text/css', href: uriBase + '/css/Handlers/fonts.ashx' + noIECache, rel: 'stylesheet', media: 'screen' })
			.appendTo(head);
	}

	//start building the html
	var aItems = new Array(5);
	aItems[0] = Array("programmas", "PROGRAMMA'S", uriBase + "/programmas/");
	aItems[1] = Array("kunstcultuur", "KUNST & CULTUUR", "http://cultuur.avro.nl/");
	aItems[2] = Array("opiniedebat", "OPINIE & DEBAT", "http://debat.avro.nl/");
	aItems[3] = Array("leden", "LEDEN", uriBase + "/leden/");
	aItems[4] = Array("avro", "AVRO", uriBase + "/avro/");

	var navigationBar = "<section id=\"AVROBar\" class=\"AVROBar" + initialClass + "\">";
	navigationBar += "<aside class=\"MyBlock\"><iframe width=\"100%\" height=\"320\" id=\"iframe_popup\" src=\"\" frameborder=\"0\" scrolling=\"auto\" style=\"display: block; \"></iframe><a id=\"CloseMyBlock\" href=\"#\" title=\"sluiten\" class=\"CloseMyBlock\">x</a><span class=\"Indicator\">indicator</span></aside>";
	navigationBar += "<aside class=\"MainBar\"><nav><h1><a href=\"" + uriBase + "\">AVRO</a></h1><ul role=\"Primary Navigation\" class=\"MainNav\">";
	for (iItem = 0; iItem < aItems.length; iItem++) {
		var oItem = aItems[iItem];
		if (oItem[0] == activeItem)
			sActive = "class=\"Active\"";
		else
			sActive = "";

		navigationBar += "<li " + sActive + "><a href=\"" + oItem[2] + "\">" + oItem[1] + "</a></li>";
	}
	navigationBar += "</ul><ul class=\"OptionNav\"><li id=\"navSearch\" class=\"Search\"><input value=\"\" type=\"text\" id=\"navigationbarSearch\" name=\"q\" placeholder=\"Zoek op trefwoord\" class=\"Round\"><div id=\"navigationBarSearchResults\" class=\"SearchResults\"></div><a href=\"#\" id=\"searchButton\">Zoeken</a></li>";
	if (avrodomain)
		navigationBar += "<li id=\"inlogButton\"><a href=\"" + uriBase + "/User/Login\">Inloggen</a></li>";

	if (!avroKids)
		navigationBar += "<li id=\"npoButton\"><div id=\"npo_button_container\" class=\"npo_button_lol\"><a>Ontdek Omroep.nl</a></div></li></ul></nav></aside></section>";
	else
		navigationBar += "<li id=\"npoButton\"><div id=\"zapp_button_container\" class=\"zapp_button_dol\"><a href=\"http://www.zapp.nl\">Ontdek Zapp.nl</a></div></li></ul></nav></aside></section>";

	jQuery("body").prepend(innerShiv(navigationBar, false));

	var scriptEl = document.createElement('script');
	jQuery(scriptEl).attr({
		type: 'text/javascript',
		src: (avroKids) ? "http://balk.omroep.nl/zapp/zapp.js?theme=dark" : "http://assets.cn.omroep.nl/javascripts/npo-explore.js",
		id: (avroKids) ? "pb-zapp-bar" : "pb-nbo-bar"
	});

	if(!avroKids)
		head.appendChild(scriptEl);

	loadFunctions(avrodomain);
}
/* End partial JS */

function loadFunctions(avrodomain) {
	var autocomplete = jQuery('#navigationbarSearch').autocomplete({
		source: uriBase + "/Ajax/SearchBar?type=keyword&callback=?",
		minLength: 2,
		delay: 50,
		appendTo: "#navigationBarSearchResults",
		select: function (event, ui) {
			switch (ui.item.facet) {
				case "programinfo":
					document.location = uriBase + "/" + ui.item.programid + "/";
					break;
				case "results":
					search(ui.item.description);
					break;
				default:
					search(ui.item.value);
					break;
			}
			return false;
		}
	}).data("autocomplete");

	if (typeof (autocomplete) != "undefined")
		autocomplete._renderItem = function (ul, item) {
			var li = "<li></li>";
			var innerText = item.value;
			if (item.facet == "programinfo") {
				li = "<li class=\"ProgramTitle\"></li>";
				innerText = "<h4>" + item.value + "</h4><p>" + item.description + "</p>";
			}
			if (item.facet == "results") {
				li = "<li class=\"MoreResults\"></li>";
				innerText = "bekijk alle <strong>" + item.value + "</strong> resultaten";
			}

			return jQuery(li).data("item.autocomplete", item)
					.append("<a>" + innerText + "</a>")
					.appendTo(ul);
		};

	jQuery("#navigationbarSearch").focus(function () {
		jQuery("#navSearch").addClass("Active");
		clearInterval(searchInterval);

		jQuery("#navSearch").bind("mouseover", function () {
			clearInterval(searchInterval);
		}).bind("mouseout", function () {
			// [MSc] 2011-09-27: Don't hide search when input has focus
			if (jQuery("#navigationbarSearch:focus").length)
				return;
			searchInterval = setInterval(function () {
				jQuery("#navSearch").removeClass("Active");
				jQuery("#navSearch").unbind("mouseover").unbind("mouseout");
				jQuery('#navigationbarSearch').blur();
			}, 2000);
		});
	});
	// [MSc] 2011-09-27: Hide search on input blur
	jQuery("#navigationbarSearch").blur(function () {
		searchInterval = setInterval(function () {
			jQuery("#navSearch").removeClass("Active");
			jQuery("#navSearch").unbind("mouseover").unbind("mouseout");
		}, 500);
	});

	// Handle the search button click
	jQuery("#searchButton").live("click", function (e) {
		e.preventDefault();
		var val = jQuery("#navigationbarSearch").val();
		if (val.length > 0)
			search(val);
	});
	// Handle the enter on the search field
	jQuery("#navigationbarSearch").bind('keydown', function (e) {
		if (e.keyCode == 13) {
			e.preventDefault();
			var val = jQuery(this).val();
			if (val.length > 0) {
				jQuery("#navigationBarSearchResults").hide();
				search(val);
			}
		}
	});

	jQuery('#inlogButton a, #CloseMyBlock').click(function () {
		openAvroLogin();
		return false;
	});

	if (avrodomain)
		jQuery.getScript(uriBase + "/User/JavascriptUser");

	if (window.location.href.indexOf("login=1") > 0) {
		jQuery('#AVROBar').addClass('active');
		loadIframe();
		setTimeout(function () {
			jQuery('#AVROBar').removeClass('active');
		}, 2000);
	}
}
function search(val) {
	// [MSc] 2011-09-27: Add random number to url when we're in "zoeken" to make sure an actual search is being performed
	if (document.location.href.indexOf("/zoeken/") < 0)
		document.location.href = uriBase + "/zoeken/#search:" + val;
	else
		document.location.href = uriBase + "/zoeken/?" + (Math.floor(Math.random() * 999999)).toString() + "#search:" + val;
}

function loadIframe() {
	var iframe = jQuery('#iframe_popup');
	if ((jQuery(iframe).attr("src") == null) || (iframe == "") || (jQuery(iframe).attr("src") == "")) {
		jQuery(iframe).attr("src", uriBase + uriLogin + "?" + new Date().getMilliseconds());
	}
}

function loadUser(username) {
	if (username != "") {
		jQuery("#inlogButton a").text("profiel");
		avroUsername = username;
	}
}

function openAvroLogin(scrollToTop) {
	if (!jQuery.browser.msie) {
		jQuery('#AVROBar').toggleClass('active');
	} else {
		//if (jQuery('.AVROBar').attr('class') == 'AVROBar') {
		if (!jQuery('.AVROBar').hasClass('pseudo') ) {
			jQuery('.AVROBar').animate({ height: "420px" }, 100).addClass('pseudo');
		} else {
			jQuery('.AVROBar').animate({ height: "90px" }, 100).removeClass('pseudo');
		}
	}

	if (scrollToTop)
		scrollTo(0, 0);

	loadIframe();
}
//set everything in motion
initJQuery();
