//config sIFR
var dinBold = {
	src: '/domains/watermanagement.nu/sifr/sifr.swf'
};
sIFR.activate(dinBold);


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);
};
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;
};

var jquery_sifr_replacement = function(obj,wm){
	$title = obj;
	if($title.length){
		sIFR.replace(dinBold, {
								elements: $title
								,wmode: wm ? '' : 'transparent'
								,css: [
								  '.sIFR-root {background-color: ' + hexed($title.css('backgroundColor')) + '; font-size: ' + (parseInt($title.css('fontSize'))) + '; color: ' + hexed($title.css('color')) + '; }'
								  ,'a { text-decoration: none; color: ' + hexed($title.css('color')) + '; }'
								  ,'a:hover { color: ' + hexed($title.css('color:hover')) + '; }'
								]
							});
	}
}

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


$(document).ready(function(){

	var buttonNr = new Array();
	var ulNr = 0;
	$("#menu ul").each(function(){
		buttonNr[ulNr] = 0;
		$(this).children('li').each(function(){
			$(this).addClass('color_' + buttonNr[ulNr]);
			buttonNr[ulNr]++;
			if(buttonNr[ulNr] > 3)
				buttonNr[ulNr] = 0;
		});
		ulNr++;
	});

    $("#menu ul li").hover(
		function(){
			if($(this).children('ul:eq(0)').is(':hidden'))
				$(this).children('ul:eq(0)').slideDown();
		},
		function(){
			if($(this).children('ul:eq(0)').is(':visible'))
				$(this).children('ul:eq(0)').slideUp();
		}
    );


	//build icons
	var files = new Array('asf','avi','flv','mov','mpg','mpeg','mp4','qt','smil','swf','wmv','3g2','3gp');
	$("#page a[href]").not("a[href$=php],a[href$=html]").each(function(){
		if($.trim($(this).text())){
			u = $(this).attr("href");
			p = u.split('.');
			e = p[p.length - 1].toLowerCase();
			for(i in files)
				if(files[i] === e || $(this).hasClass('media')){
					$(this).attr('href',"javascript:popWindow('/domains/watermanagement.nu/media.php?media=" + escape(u) + "',320,245)");
				}
		}
	});
	
	
	$('.article h1').each(function(){
		jquery_sifr_replacement($(this));
	});	
});
