//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(){
	
	//kill empty titles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text())){
			$(this).remove();
		}
	});
	
	//hide blank br
	$(".article .content .module_contents,#middle > .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
	//change program buttons
	$("#program_categories a").each(function(){
		t = $(this).text();
		$(this).html('<img src="/domains/waerdsetempel.nl/generate/button.php?text=' + escape(t) + '" alt="' + t + '" border="0" />');
	}).hover(
		function(){
			if(!$(this).parent().hasClass('selected_category') && !$(this).parent().hasClass('current_buttonID')){
				s = $(this).children('img').attr('src');
				$(this).children('img').attr('src',s + '&over=1');
			}
		},
		function(){
			if(!$(this).parent().hasClass('selected_category') && !$(this).parent().hasClass('current_buttonID')){
				s = $(this).children('img').attr('src');
				$(this).children('img').attr('src',s.replace('&over=1','') + '&over=0');
			}
		}
	).parents("#program_categories").css('display','block');

/* //doesn't work, they got a breakout script running...
	//order ticket in popup.
	$("a.external_order").click(function(e){
		e.preventDefault();
		popWindow($(this).attr('href'),800,600);
		return false;
	});
*/
	$('img[align=left]').css('margin-right','10px');
	$('img[align=right]').css('margin-left','10px');

	//header
	randomHeader = function(i,s){
		//var i = 0;
		//while(i == n){
		//	i = Math.floor(Math.random()*6);
		//}
		if(s == 'B'){
			$('#header .background_B').css('background-image','url(/domains/waerdsetempel.nl/images/backgrounds/' + background_header_image[i] + ')').fadeIn('normal',function(){
				headerTmr(i,'A');
			});
		}else{
			$('#header .background_A').css('background-image','url(/domains/waerdsetempel.nl/images/backgrounds/' + background_header_image[i] + ')');
			$('#header .background_B').fadeOut('normal',function(){
				headerTmr(i,'B');
			});
		}
	}
	headerTmr = function(i,s){
		if(htmr)
			clearTimeout(htmr);
		i++;
		if(i == background_header_image.length)
			i = 0;
		htmr = setTimeout("randomHeader("+i+",'"+s+"');",6000);
	}
	var htmr;
	var i = 0;//Math.floor(Math.random()*6);
	$('#header .background_A').css('background-image','url(/domains/waerdsetempel.nl/images/backgrounds/' + background_header_image[i] + ')');
	headerTmr(i,'B');

	//flicker popup UI
	$(".flickralbum_module ul.thumbs li a").click(function(e){
		var currImgIndx = $(this).parents('ul.thumbs').find('a').index(this);
		
		o = $('<div id="flickr_popup_ui"><div class="image"><div class="back"></div><div class="forward"></div></div></div>');
		o.find('.image').css({backgroundImage:'url(' + $(this).attr('href') + ')'}).hover(
			function(){$(this).find('div').fadeIn('normal');},
			function(){$(this).find('div').fadeOut('normal');}
		);

		o.find('.forward').click(function(){
			currImgIndx++;
			u = $(".flickralbum_module ul.thumbs li a:eq(" + currImgIndx + ")").attr('href');
			$(this).parents('.image').css({backgroundImage:'url(' + u + ')'});
		});
		o.find('.back').click(function(){
			currImgIndx--;
			u = $(".flickralbum_module ul.thumbs li a:eq(" + currImgIndx + ")").attr('href');
			$(this).parents('.image').css({backgroundImage:'url(' + u + ')'});
		});
		
		popWindow('',504,350,o);
		
		return false;
	});

	//add margin to news articles
	$(".news_article img").each(function(){
		$(this).addClass($(this).attr('align') + '_margin');
	});
	
	//content stuff
	$("#middle").height($("#page").height());

	//my own alert...
	alert = function(str){
		popWindow('',300,100,$('<div id="alert clear"><span class="string">' + str + '</span></div>'));
	}
	//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);");		
		}
	});

});