//config sIFR
var russel = {
	src: '/domains/versterre.nl_2/generate/RusselSquare.swf'
//	,ratios: [8, 1.41, 9, 1.33, 10, 1.35, 15, 1.31, 19, 1.28, 20, 1.27, 28, 1.26, 29, 1.24, 30, 1.25, 40, 1.24, 43, 1.23, 44, 1.24, 64, 1.23, 72, 1.22, 73, 1.23, 115, 1.22, 116, 1.21, 119, 1.22, 121, 1.21, 1.22]
};
sIFR.useStyleCheck = true;
sIFR.activate(russel);

/* == Converts color to HEX == */
var hex = function(N) {
	if(N==null) return "00";
	N = parseInt(N);
	if(N==0 || isNaN(N)) return "00";
	N = Math.max(0, N);
	N = Math.min(N, 255);
	N = Math.round(N);
	return "0123456789ABCDEF".charAt((N - N%16) / 16) + "0123456789ABCDEF".charAt(N%16);
};
/* == Converts colors to HEX == */
var hexed = function(color) {
	if(!color) { return false; }
	if(color.search('rgb') > -1) {
		color = color.substr(4,color.length-5).split(', ');
		color = hex(color[0]) + hex(color[1]) + hex(color[2]);
	}
	color = color.replace('#','');
	if(color.length < 6) {
		color = color.substr(0, 1) + color.substr(0, 1) + color.substr(1, 1) + color.substr(1, 1) + color.substr(2, 1) + color.substr(2, 1);
	}
	return '#' + color;
};

//my own routine to do a calculated sifr replacement.
var jquery_sifr_replacement = function(obj,wm){
	$title = obj;
	sIFR.replace(russel, {
							elements: $title
							,wmode: wm ? '' : 'transparent'
							,css: [
							  '.sIFR-root {display: block; background-color: ' + hexed($title.css('backgroundColor')) + '; font-size: ' + (parseInt($title.css('fontSize'))) + '; color: ' + hexed($title.css('color')) + '; text-align: ' + $title.css('text-align') + '}'
							  ,'a { text-decoration: none; color: ' + hexed($title.css('color')) + '; }'
							  ,'a:hover { color: ' + hexed($title.css('color:hover')) + '; }'
							]
						});
}

//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
		else
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	}
	popEl.modal({
			onOpen: function (dialog) {
						//dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('fast', function () {
							dialog.container.show('fast', function () {
								dialog.data.slideDown('fast'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('fast', function () {
						  dialog.container.hide('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
							  $.modal.close(); // must call this to have SimpleModal
											   // re-insert the data correctly and
											   // clean up the dialog elements
							});
						  });
						});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}

var SGtmr;
var scrollGraduates = function(){
	x = $('.graduates_module.swap div:eq(0)').height() * -1;
	$('.graduates_module.swap div:eq(0)').animate({marginTop:x + 'px'},'slow',function(){
		$('.graduates_module.swap div:eq(0)').css('margin-top','0px').appendTo($('.graduates_module.swap'));
		$('.graduates_module.swap div:eq(0)').css('margin-top','0px');
		if(SGtmr)
			clearTimeout(SGtmr);
		SGtmr = setTimeout(scrollGraduates,5000);
	});
}


//catch all alerts
var foundOnloadAlerts = new Array();
var alert = function(str){
	foundOnloadAlerts[foundOnloadAlerts.length] = str;
}

var menuSkipper = function(way){
	if(way == 'next'){
		u = $('#menu li.current_buttonID').next('li');
	}else{
		u = $('#menu li.current_buttonID').prev('li');
	}
	if(!u.length){
		if(way == 'next'){
			u = $('#menu li.current_buttonID').parents('li').next('li');
		}else{
			u = $('#menu li.current_buttonID').parents('li').prev('li');
		}
	}
	if(!u.length){
		u = $('#menu li:eq(0)');
	}
	self.location.href = u.find('a:eq(0)').attr('href');
}


//start
$(document).ready(function(){

	//my own alert...
	alert = function(str){
		popWindow('',300,100,$('<div id="alert_popup" class="clear alert"><span class="string">' + str + '</span></div>'));
	}
	
	if($('#splash_popup').length){
		popWindow('',300,360,$('#splash_popup'));
	}
	
	if($('.loginform').length && $('.loginform').height() < 128){
		$('.loginform').height(128);
	}
	
	$(".leftmenu ul li:first a").addClass('first');
	$(".leftmenu").parents('.article').find('div.title').remove();
		
	//Menu stuff.
	$('.menu').find('ul li:eq(0)').addClass('first');
	//highlight main button
	if($("ul ul li.current_buttonID").length){
		$("ul ul li.current_buttonID").parents('li').addClass('current_mainID hover').find('a:eq(0)').addClass('hover');
	}else{
		$("ul li.current_buttonID").addClass('hover');
	}
	//change to images
	$("#menu > ul > li > a").each(function(){
		t = $(this).text();
		$(this).html('<img src="/domains/versterre.nl_2/generate/button.php?text=' + escape(t) + ($(this).parent().hasClass('hover')  ? '&over=1' : '') + '" alt="' + t + '" border="0" />');
	});
	//do mouse over
	$("#menu > ul > li").hover(
		function(){
			if(!$(this).hasClass('current_mainID')){
				s = $(this).find('img').attr('src');
				$(this).addClass('hover').find('img').attr('src',s + '&over=1');
			}
		},
		function(){
			if(!$(this).hasClass('current_mainID')){
				s = $(this).find('img').attr('src');
				$(this).removeClass('hover').find('img').attr('src',s.replace('&over=1','&over=0'));
			}
		}
	);
	//submenus
	var zIndex = 2;
	$("#menu ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden')){
				  $(this).children('ul:eq(0)').css('z-index',zIndex++).slideDown('normal');
			 }
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible'))
				  $(this).children('ul:eq(0)').slideUp('normal');
			 }
	);
	
	//kill empty titles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('.module_contents').length && !$(this).find('img').length){
			$(this).remove();
		}
	});
	
	//hide blank br
	$(".article .content .module_contents,#left .content .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});

	//change infut fields
	$('input.input').each(function(){
		$(this).wrap('<span class="' + $(this).attr('class') + '"><span></span></span>');
	});
	
	//change margins of every image
	$(".article div.content img[align]").each(function(){
		if(!$(this).next().is("img[align=" + $(this).attr('align') + "]") && $(this).attr('hspace')){
			$(this).css('margin-' + ($(this).attr('align') == 'left' ? 'right' : 'left'), $(this).attr('hspace') + 'px').removeAttr('hspace');
		}
	});
	

	if($('.graduates_module.swap').length){
		SGtmr = setTimeout(scrollGraduates,5000);
		$('.graduates_module.swap').click(function(){self.location.href='/geslaagdenfotos-en-multivlaai/wie-kan-je-feliciteren';});
	}

	//check theorie form
	$(".register_module select.les").change(function(){
        var selIndx = this.selectedIndex;

		i = parseInt($(this).attr('class').split(' ')[1].split('_')[1]);
		$(".register_module select.les:gt(" + i + ")").each(function(){
			if(this.selectedIndex < selIndx)
				this.selectedIndex = selIndx;
			$(this).children("option").show();
			$(this).children("option:lt(" + selIndx + ")").hide();
		});

		$(".register_module select.examen")[0].selectedIndex = selIndx;
		$(".register_module .examen option").show();
		$(".register_module .examen option:lt(" + selIndx + ")").hide();
	});
	$(".register_module div.tabs span.tab").click(function(){
		var n = $(this);
		if(!$(".register_module div.blad:visible").hasClass(n.find(':checkbox').attr('class'))){
			$(".register_module div.blad:visible").slideUp('normal',function(){
				$(".register_module div.tabs span.tab.on").removeClass('on');
				n.addClass('on');
				n.find(':checkbox').attr('checked','checked');
				$(".register_module div.blad." + n.find(':checkbox').attr('class')).slideDown('normal');

				if(n.find(':checkbox').is(':checked')){
					$(".register_module div.blad." + n.find(':checkbox').attr('class')).find(':input').removeAttr('disabled');
				}else{
					$(".register_module div.blad." + n.find(':checkbox').attr('class')).find(':input').attr('disabled','disabled');
				}
			});
		}
	});
	$(".register_module div.tabs span.tab input").click(function(){
		var n = $(this);
		if(n.is(':checked')){
			$(".register_module div.blad." + n.attr('class')).find(':input').removeAttr('disabled');
		}else{
			$(".register_module div.blad." + n.attr('class')).find(':input').attr('disabled','disabled');
		}
	});
	$(".register_module div.tabs span.tab input").each(function(){
		var n = $(this);
		if(n.is(':checked')){
			$(".register_module div.blad." + n.attr('class')).find(':input').removeAttr('disabled');
		}else{
			$(".register_module div.blad." + n.attr('class')).find(':input').attr('disabled','disabled');
		}
	});
	$(".register_module .getrouwd input,.register_module .aanhef input").click(function(){
		if($(".register_module .getrouwd label.ja input:checked").length && $(".register_module .aanhef label.female input:checked").length){
			$('.register_module div.row.meisjesnaam').slideDown('normal');
		}else{
			$('.register_module div.row.meisjesnaam').slideUp('normal');
		}
	});
	$(".register_module .eerdere_lessen input").click(function(){
		if($(".register_module .eerdere_lessen label.ja input:checked").length){
			$('.register_module .rijlessen .vorige_rijschool_block').slideDown('normal');
		}else{
			$('.register_module .rijlessen .vorige_rijschool_block').slideUp('normal');
		}
	});


	if($("#middle .googlemultimaps_module").length){
		$("#map-side-bar").prependTo($('#right'));
	}

	var make_same_height = function(selector){
		var max_height = 0;
		$(selector).each(function(){
			if($(this).height() > max_height){
				max_height = $(this).height();
			}
		});
		$(selector).height(max_height);	
	}

	//change all halfs
	$("#middle div.article.half:odd").addClass('noline');
/*
	$("#middle div.article.half:even").each(function(){
		t = $(this);
		p = $(this).next().next();
		if(p.height() < t.height()){
			p.children('div.title').height(t.children('div.title').height() + 'px');
			p.children('div.content').height(t.children('div.content').height() + 'px');
			t.children('div.title').height(t.children('div.title').height() + 'px');
			t.children('div.content').height(t.children('div.content').height() + 'px');
			
			p.height(t.height() + 'px');
			t.height(t.height() + 'px');
		}else{
			t.children('div.title').height(p.children('div.title').height() + 'px');
			t.children('div.content').height(p.children('div.content').height() + 'px');
			p.children('div.title').height(p.children('div.title').height() + 'px');
			p.children('div.content').height(p.children('div.content').height() + 'px');

			t.height(p.height() + 'px');
			p.height(p.height() + 'px');
		}
	});
*/
	make_same_height("#middle div.article.half div.title");
	make_same_height("#middle div.article.half div.content");
	

	$('#right div.article').after('<hr />');

	//sifr now
	$('#content h1,font .title').each(function(){
		jquery_sifr_replacement($(this));
	});
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

	//kill MS word stuff!
	$(".MsoNormal,.MsoNormal *").removeAttr('style');

	//show all found alerts
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}
	
	
	
	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		i = 0;

		//change all onclick popups
		if($(this).attr('onclick')){
			i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");

			if(i){
				w = 640;
				h = 480;
				
				u = $(this).attr('href');
				e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e == 'png' || e == 'gif' || e == 'jpg'){
					var id = 'popwinlink_' + uid();
					$(this).attr('id',id);

					img[id] = new Image();
					img[id].onload = function(){
						img[id].onload = null;
						w = img[id].width;
						h = img[id].height;
	
						$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick').toString();

					h = parseInt(c.match(/height=([0-9]+)/)[1]) + 25;
					w = parseInt(c.match(/width=([0-9]+)/)[1]) + 25;
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
				}
			}

		//change all movie files
		}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
			//get filename...
			u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/versterre.nl_2/video.php?file=" + u + "',320,260);");		
		}
	});
	
});

		
