/*
 * Every function in this file is (c) Jeroen Serpieters <jeroen(at)politics.be>
 */

function cartoon(id)
{
	var windowname = 'cartoons';
	var width = 600;
	var height = 600;

	var screenX = (screen.width / 2 - width / 2);
	var screenY = (screen.height / 2 - height / 2);
	var features= 'width=' + width + ',height=' + height;
	features += ',screenX=' + screenX + ',left=' + screenX;
	features += ',screenY=' + screenY  +',top=' + screenY;
	features += ',scrollbars = yes, resizable = yes';
	var mywin = window.open('/cartoons/popup/'+id+'/', windowname, features);
											
	if( mywin ) 
		mywin.focus();
}

function mailto(user, host, domain)
{
	location.href = 'mailto:' + user + '@' + host + '.' + domain;
}

function externalLinks()
{
	var a = document.getElementsByTagName('a'), i = a.length, href;

	while (i--)
	{
		href = a.item(i).getAttribute('href');
		
		if (href && !internalUrl(href))
		{
			a.item(i).target = '_blank';
		}
	}
}

function internalUrl(url)
{
	var re = /http:\/\//;

	if (re.test(url))
	{
		url = url.replace(re, '');
		
		var i = url.indexOf('/');
		if (i != -1)
		{
			url = url.substr(0, i);
		}

		var parts = url.split('.');
		if (parts[parts.length - 1] == 'be' && parts[parts.length - 2] == 'politics')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return true;
	}
}

function unserialize(s)
{	
	s = s.substring(s.indexOf('{') + 1, s.indexOf('}'));
	pk = s.split(';');
	s = new Array();

	for (i = 0; i < pk.length - 1; i+=2)
	{
		dp = pk[i].split(':');
		s.push(dp[dp.length-1]);
		dp = pk[i+1].split(':');
		s.push(dp[dp.length-1]);
	}

	return s;
}

function qInit()
{
	qc = document.getElementById('qc');
	if (qc) 
	{
		addEvent(qc, 'click', qCheck);
		addEvent(qc, 'keypress', qCheck);
	}
}

function qCheck()
{
	var a = unserialize(document.getElementById('qid').value), correct = false, atl = 0;
	
	for (var i = 0; i < a.length - 1; i += 2)
	{	
		antw = document.getElementsByName('antwoord_' + a[i]);
		
		for (var j= 0; j < antw.length; j++)
		{
			if (antw[j].value == a[i+1])
			{
				qMarkAsCorrect('antwoord_' + a[i] + '_' + a[i+1]);

				if (antw[j].checked)
				{
					atl++;
				}
			}
		}
	}

	with (document.getElementById('analyse'))
	{
		innerHTML = '<p>Je hebt <strong>' + atl + '</strong> van de ' + a.length / 2
												 + ' vragen correct beantwoord.</p>';
		style.border = '1px solid #D1CEA3';
		style.margin = '25px';
		style.padding = '25px 25px 10px 25px';
	}

	scrollTo(0, 0);
}

function qMarkAsCorrect(id)
{
	document.getElementById(id).className = "correct";
}

function qMarkAsWrong(id)
{
	document.getElementById(id).className = "wrong";
}

function qHideRadioButtons()
{
	var a = document.getElementsByTagName('input'), i = a.length, name;
	var re = /antwoord_[0-9]*/;

	while (i--)
	{
		name = a.item(i).getAttribute('name');

		if (name && re.test(name))
		{
			a.item(i).className = 'hidden';
		}
	}
}

function qDisableRadioButtons()
{ 
	var a = document.getElementsByTagName('input'), i = a.length, name;
	var re = /antwoord_[0-9]*/;

	while (i--)
	{
		name = a.item(i).getAttribute('name');

		if (name && re.test(name))
		{
			a.item(i).disabled = 1;
		}
	}
}

function sendtofriend(module, arg)
{
	var windowname = 'sendtofriend';
	var width = 260;
	var height = 260;

	var screenX = (screen.width / 2 - width / 2);
	var screenY = (screen.height / 2 - height / 2);
	var features= 'width=' + width + ',height=' + height;
	features += ',screenX=' + screenX + ',left=' + screenX;
	features += ',screenY=' + screenY  +',top=' + screenY;
	features += ',scrollbars = no, resizable = no';
	var mywin = window.open('/stuur-naar-een-vriend/'+module+'/'+arg, windowname, features);
											
	if( mywin ) 
		mywin.focus();	
}

function webtv(video, title)
{
	  var LeftPosition = (screen.width) ? (screen.width-665)/2 : 0;
	  var TopPosition = (screen.height) ? (screen.height-430)/2 : 0;
	  //var url = 'http://www.zoomin.tv/videoplayer/index.cfm?id='+video+'&mode=normal&quality='+ quality +'&pid='+ pid;
	  var popwin = window.open('/webtv/popup/' + video + '-' + escape(title) + '/','MTV6_window','toolbar=no,width=665,height=430,top='+TopPosition+',left='+LeftPosition+',directories=no,status=no,statusbar=0,scrollbars=no,resizable=no,menubar=no');
      popwin.focus();
}

/*
 * getQueryVariable()
 * 	(c) http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/JavaScript/Get_Query_String_variables_in_JavaScript
*/
function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split('&');

	for (var i = 0; i < vars.length; i++)
	{
    	var pair = vars[i].split("=");
	    if (pair[0] == variable) 
		{
			return pair[1];
		}
	} 

	return null;
}

function verkiezingsprofiel(id)
{
	var windowname = 'Verkiezingen';
	var width = 600;
	var height = 600;

	var screenX = (screen.width / 2 - width / 2);
	var screenY = (screen.height / 2 - height / 2);
	var features= 'width=' + width + ',height=' + height;
	features += ',screenX=' + screenX + ',left=' + screenX;
	features += ',screenY=' + screenY  +',top=' + screenY;
	features += ',scrollbars = yes, resizable = yes';
	var mywin = window.open('/verkiezingen/popup/'+id+'/', windowname, features);
											
	if( mywin ) 
		mywin.focus();
}

