//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('normal', function () {
							dialog.container.show('normal', function () {
								dialog.data.slideDown('normal'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('normal', function () {
						  dialog.container.hide('normal', function () {
							dialog.overlay.fadeOut('normal', 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); 
}



//sIFR
var sifrFont = {
  src: '/domains/nhec.nl/generate/title.swf'
  ,ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
};
sIFR.useStyleCheck = true;
sIFR.activate(sifrFont);
//sIFR.debug.ratios({ src: '/domains/nhec.nl/generate/title.swf', selector: 'h1' });
sIFR.replace(sifrFont, {
	selector: '#middle .quarter h2,#middle .quarter h1',
	css: ['.sIFR-root {color:#ffffff;text-align:center;}','a,a:hover {color:#ffffff;text-decoration:none;line-height:normal;}'],
	transparent: true
});
sIFR.replace(sifrFont, {
	selector: '#middle .article h2,#middle .article h1',
	css: ['.sIFR-root {color:#2b388f;}','a,a:hover {color:#2b388f;text-decoration:none;line-height:normal;}'],
	transparent: true
});
sIFR.replace(sifrFont, {
	selector: '.headline_module ul li.current_articleID',
	css: ['.sIFR-root {color:#2b388f;font-size:14px;}','a,a:hover {color:#2b388f;text-decoration:none;line-height:normal;}'],
	transparent: true
});
sIFR.replace(sifrFont, {
	selector: '#middle .article h3,.headline_module ul li',
	css: ['.sIFR-root {color:#ffffff;font-size:14px;}','a,a:hover {color:#ffffff;text-decoration:none;line-height:normal;}'],
	transparent: true
});
sIFR.replace(sifrFont, {
	selector: '#right .article h1.title,#right .article h2.title',
	css: ['.sIFR-root {color:#ffffff;font-size:21px}','a,a:hover {color:#ffffff;text-decoration:none;line-height:normal;}'],
	transparent: true
});



var cacheButtonImage = function(o,u,h){
	var obj = o;
	var noHover = h;
	var img = new Image();
	$(img).css('border',0).addClass('buttonImg').load(function(){
		obj.html($(this));
		if(obj.hasClass('current_buttonID')){
			s = obj.children('.buttonImg:eq(0)').attr('src');
			obj.children('.buttonImg:eq(0)').attr('src',s + '&over=1');
			
			obj.parents('li').children('a').each(function(){
				s = $(this).children('.buttonImg').attr('src');
				$(this).children('.buttonImg').attr('src',s + '&over=1');
			});
		}
		if(!noHover){
			obj.hover(
				function(){
					if(!$(this).children('.buttonImg').attr('origSrc'))
						$(this).children('.buttonImg').attr('origSrc',$(this).children('.buttonImg:eq(0)').attr('src'));
		
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s + '&over=1');
				},
				function(){
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s);
				}
			);
		}
	}).attr('src',u);
}


//init
$(document).ready(function(){
	
	if($("div.last_news_article_module").length && $(".headline_module ul li a:eq(0)").length){
		self.location.href = $(".headline_module ul li a:eq(0)").attr('href');
	}
	
	//change home
	if($("#container.home").length){
		$("#left .article:eq(0)").css('visibility','hidden');
	}
	
	//change to images
	$(".sIIR").each(function(){
		t = $(this).text();
		if(t){
			cacheButtonImage($(this),'/domains/nhec.nl/generate/button.php?text=' + escape(t),true);
		}
	});
	$("#footer .breadcrumbs a").each(function(){
		t = $(this).text();
		if(t){
			cacheButtonImage($(this),'/domains/nhec.nl/generate/button.php?text=' + escape(t.toUpperCase()));
		}
	});
	$("#topmenu > ul > li,#menu > ul > li,#bottommenu > ul > li").each(function(){
		//change main
		t = $(this).children('a').text();
		if(t){
			cacheButtonImage($(this).children('a'),'/domains/nhec.nl/generate/button.php?text=' + escape(t.toUpperCase()));
		}
		//change subs
		if($(this).find('ul a').length){
			$(this).find('ul a').each(function(){
				t = $(this).text();
				if(t){
					cacheButtonImage($(this),'/domains/nhec.nl/generate/button.php?type=sub&text=' + escape(t.toUpperCase()));
				}
			});
		}
	});
	$("#topmenu ul ul,#menu ul ul").fadeTo(0,0.3);
	$("#topmenu > ul > li,#menu > ul > li").hover(
		function(){$(this).children('ul').fadeTo('fast',1);},
		function(){$(this).children('ul').fadeTo('fast',0.3);}
	);
	
	//kill empty titles and articles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed').length){
			$(this).remove();
		}
	});
	
	//hide blank br
	$(".module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').css('display','none');
		}
	});
	
	
	//change layout middle blocks.
	var sh = $("#menu").height() - 25;
	if($("#middle > div.article.default").length == 1 && $("#middle > div.article.default").height() < ((sh + 20) * 2) - 20){
		$("#middle > div.article.default").height(((sh + 20) * 2) - 20);
	}else{
		$("#middle > div.article.default").each(function(){
			if($(this).height() < sh){
				$(this).height(sh);
			}else if($(this).height() < (sh * 2) + 20){
				$(this).height((sh * 2) + 20);
			}
		});
	}
	
	//change quarter place
	if($("#middle div.article.quarter").length && $("#left div.article.dubble_width").length){
		$("#middle div.article.quarter").css('float','right');		
	}

	//position alignment top menu items	
	var u = 0;
	$("#topmenu > ul > li").each(function(k,v){
		l = 0;
		switch(k){
			case 1:
				l = (u + 192) - $(this).width();
				break;
			case 2:
				l = u + 192;
				break;
			case 3:
				l = (u + 478) - $(this).width();
				break;
			case 4:
				l = u + 478;
				break;
			case 5:
				l = (u + 767) - $(this).width();
				break;
			case 6:
				l = u + 767;
				break;
		}
		$(this).css('position','absolute');
		if(l){
			$(this).css('left',l);
		}
	});

	//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/nhec.nl/video/video.php?file=" + u + "',320,260);");		
		}
	});
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
	
	//icons
	var files = new Array('ai','avi','bmp','cs','dll','doc','exe','fla','js','mdb','mp3','pdf','ppt','rdp','swf','swt','txt','vsd','xls','xml','zip');
	$(".article a[href]").each(function(){
		p = $(this).attr("href").split('.');
    	e = p[p.length - 1].toLowerCase();
		for(i in files)
			if(files[i] === e)
				$(this).addClass('inline_icon icon_' + e);
	});	
});