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


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

$(document).ready(function(){

	//my own alert...
	alert = function(str){
		popWindow('',300,100,$('<div id="alert_popup clear"><span class="string">' + str + '</span></div>'));
	}
	
	//change some css layout here so we are unobstructive.
	$('body').css('overflow','hidden');
	$('#container.home #left div.default:eq(0),#submenu ul li:eq(0)').addClass('first');
	$('#right .article:eq(0)').wrap('<div class="headerArticle"></div>');
	
	//fix ie6 png...
	$(document).pngFix(); 
	
	//rescale content container
	var doRescale = function(){
		$('#container').height($(window).height() - 20).css('margin-top','10px');
		$('#container_scroll,#shadow_scroll').height($('#container').height());
	}
	$(window).resize(doRescale);
	doRescale();
	
	//show all found alerts
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}
	
	//kill empty titles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('.module_contents').length){
			$(this).remove();
		}
	});
	
	//hide blank br
	$(".article .content .module_contents,#left > .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});

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

	//vrijwilliger db
	if($('.vrijwilligers_module').length){
		$('#middle').remove();
		$('#left').width($('#page div.page').width());
	}

	//alter news module
	if($('.news_module').length){
		//change more info link
		if($('.news_module div.more_info').prev().is('br')){
			$('.news_module div.more_info').prev().remove();
		}
		$('.home .news_module div.more_info a').each(function(){
			u = $(this).attr('href').replace(/&buttonID=[0-9+]/,'&buttonID=1');
			$(this).attr('href',u);
		});
		//change date location.
		$('.news_module div.content .news_date').each(function(){
			$(this).prependTo($(this).parents('.content').prev('div.title_margin').find('div.title'));
		});
	}

	//link icons
	var files = new Array('ai','avi','flv','bmp','cs','dll','doc','exe','fla','gif','jpg','js','mdb','pdf','png','ppt','rdp','txt','vsd','xls','xml','zip');
	$("#page a[href]").not("a[href$=php],a[href$=html]").each(function(){
		if($.trim($(this).text())){
			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);
		}
	});
	
	//kill MS word stuff!
	$(".MsoNormal,.MsoNormal *").removeAttr('style');

	//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');
	
					h = c.substring(c.indexOf('height=') + 7);
					h = h.substring(0,h.indexOf(','));
	
					w = c.substring(c.indexOf('width=') + 6);
					w = w.substring(0,w.indexOf(','));
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
				}
			}

		//change all movie files
		}else if($(this).attr('href') && $(this).attr('href').indexOf('module=flash_flv_player') > -1){
			//get filename...
			u = $(this).attr('href').match(/(file=).*(.flv)/)[0];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/jasa.nl_2/popup/video.php?" + u + "',300,240);");		
		}
	});
		
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
	
	if($('#container.home').length){
		//l = ($('#container_scroll').width() / 2) + 100;
		//t = ($('#container_scroll').height() / 2) - 50;
		//x = ''+l+'px '+t+'px';
		//$('#container_scroll').animate({backgroundPosition:x},7000);

		var animMoonMan = function(){
			$('#container_scroll').animate({backgroundPosition:(($('#container_scroll').width() / 2) + 50) + "px"},14000,'swing');			
		}

		var animMoonShadow = function(){
			$('#shadow_scroll').animate({backgroundPosition:"-300px center"},60000,'linear',function(){
				$('#shadow_scroll').css({backgroundPosition:($('#container_scroll').width() - 300) + "px center"});
				animMoonShadow();
			});			
		}
		$('#shadow_scroll').css({backgroundPosition:($('#container_scroll').width() - 300) + "px center"});
		animMoonShadow();
		animMoonMan();
	}
	
	//moon applet
	$('.moonphases_module,.moonphases_module .moon_info').click(function(){
		if(!$('.moonphases_module .moon_info').hasClass('visual')){
			$('.moonphases_module .moon_image').fadeTo('normal',0.50);
			$('.moonphases_module .moon_info').slideDown('normal',function(){$(this).addClass('visual');});
		}else{
			$('.moonphases_module .moon_image').fadeTo('normal',1);
			$('.moonphases_module .moon_info').slideUp('normal',function(){$(this).removeClass('visual');});
		}
	});
});