﻿/// <reference path="/javascript/$-1.4.1.min.js" />
/// <reference path="/javascript/$-ui-1.8.1.custom.min.js" />

// js/nojs
$("html").removeClass("nojs").addClass("js");

// AddThis
var addthis_config = { services_exclude: 'print', ui_cobrand: "Zorghart", data_use_flash: false };

// escapeHTML
function escapeHTML(str) {
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
}

// http://www.carronmedia.com/extend-google-analytics-with-jquery/
function trackGoogleAnalytics(googleAnalyticsKey) {

	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

	// use jQuery to call the Google Analytics JavaScript
	$.getScript(gaJsHost + "google-analytics.com/ga.js", function () {

		//tell Analytics about the current page load using standard _trackPageview method
		try {
			var pageTracker = _gat._getTracker(googleAnalyticsKey);
			pageTracker._trackPageview();
		} catch (err) { }

		//loop though each anchor element
		$('a[href]').each(function () {

			var href = $(this).attr('href');
			var name = $(this).attr('name');
			var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;

			//check for links starting with http or https, making sure that links to our own domain are excluded
			if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
				$(this).click(function () {
					var extLink = href.replace(/^https?\:\/\//i, '');
					pageTracker._trackEvent('ClickOut', extLink, name);
				});
			}
			//check for links starting with mailto:
			else if (href.match(/^mailto\:/i)) {
				$(this).click(function () {
					var mailLink = href.replace(/^mailto\:/i, '');
					pageTracker._trackEvent('Email', 'Click', mailLink);
				});
			}
			//check for links with file extension that match the filetypes regular expression:
			else if (href.match(filetypes)) {
				$(this).click(function () {
					var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
					var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
					pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
				});
			}
		});
	});
}

// init
$(function () {
	init();
});

function init() {

	// datepicker

	$.datepicker.setDefaults($.datepicker.regional['nl']);

	$('input.datepicker').datepicker({
		changeMonth: true,
		changeYear: true,
		numberOfMonths: 2,
		showButtonPanel: true
	});

	$('input.datepicker-geboortedatum').datepicker({
		changeMonth: true,
		changeYear: true,
		minDate: new Date(new Date().getYear - 66, 1, 1),
		maxDate: new Date(),
		yearRange: '-66:+0'
	});

	// sollicitatiedialog
	$('#btnOnlineSolliciteren').click(function () {
		// AsyncFileUpload - size
		$('input:file').attr('size', 44);
		// Show sollicitatie
		$('#sollicitatiedialog').slideDown(2000);
		return false;
	});

	// download
	$('a.download').click(function () {
		$(this).after('<iframe style="display:none;" src="' + this.href + '" target="_blank">');
		return false;
	});

	// hideonclick
	$('body').click(function () {
		$('.hideonclick').fadeOut('7000');
	});

	// collapsed/expanded
	$('div.collapsed')
	.children().not('span.title').hide().end().end()
	.live('click', function () {
		$(this)
		.removeClass('collapsed')
		.addClass('expanded')
		.children().not('span.title').fadeIn();
	});
	$('div.expanded')
	.live('click', function () {
		$(this)
		.removeClass('expanded')
		.addClass('collapsed')
		.children().not('span.title').fadeOut();
	});

	// heading opened/closed
	$('div.formfieldrow label.heading')
	.live('click', function () {
		// chnage value of hidden field for postback
		var hidden = $(this).next('input:hidden');
		hidden.val(hidden.val() == 'opened' ? 'closed' : 'opened');
		// change appearance for immediate response
		$(this)
		.parent()
		.toggleClass('closed')
		.toggleClass('opened');
	});

	// accordionmenu
	$('ul.accordionmenu li.hashinmenu > a')
	.live('click', function () {

		var up = $(this).parents('ul').find('li');
		var down = $(this).parent('li:not(.selectedpath)');

		up.find('ul').slideUp();
		down.find('ul').slideDown();

		up.removeClass('selectedpath');
		down.addClass('selectedpath');

		return false;
	});

	/* AsyncPostBackMessage and AsyncPostBackProgress */
	
	var isInAsyncPostBackProgress = false;

	$(window).unload(function () {
		//alert('window unloading');
		hideAsyncPostBackMessage();
	});

	$('#aspnetform').submit(function () {
		//alert('window submitting');
		hideAsyncPostBackMessage();
	});

	Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(function (sender, e) {

		isInAsyncPostBackProgress = true;
		setTimeout(function () {
			if (isInAsyncPostBackProgress) {
				$('#divAsyncPostBackProgressMessage').show();
				$('#divAsyncPostBackProgressBackground').fadeTo(1000, 0.1);
				isInAsyncPostBackProgress = true;
			}
		}, 300);

		hideAsyncPostBackMessage();
	});

	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {

		if (isInAsyncPostBackProgress) {
			$('#divAsyncPostBackProgressBackground').stop().fadeOut(200).hide();
			$('#divAsyncPostBackProgressMessage').stop().hide();
		}
		isInAsyncPostBackProgress = false;

		if (e.get_error()) {
			e.set_errorHandled(true);
			if (e.get_response().get_timedOut()) {
				showAsyncPostBackMessage("De bewerking is afgebroken omdat deze te lang duurt. Probeer het later nogmaals.");
			}
			else if (e.get_response().get_statusCode() != 200) {
				showAsyncPostBackMessage("Er is een onbekende fout opgetreden. Probeer het opnieuw alstublieft of neem contact op met de beheerder.<br/>" + e.get_error().message);
			}
			else {
				showAsyncPostBackMessage(e.get_error().message);
			}
		}
		else if (e.get_response().get_aborted()) {
			showAsyncPostBackMessage("De bewerking is beeindigd. Probeer het later nogmaals.");
		}
//		else {
//			showAsyncPostBackMessage('Async postback ok.');
//		}
	});

	function hideAsyncPostBackMessage(message) {
		$('#divAsyncPostBackMessage').html('').hide();
	}

	function showAsyncPostBackMessage(message) {
		$('#divAsyncPostBackMessage').html(message).show();
	}

};

