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



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


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


//sIFR
var sifrFont = {
  src: '/domains/captainbob.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.replace(sifrFont, {
	selector: 'h1.title',
	css: ['.sIFR-root {color:#ffffff;font-size:16px;}','a,a:hover {color:#ffffff;text-decoration:none;line-height:normal;}'],
	transparent: true
});



//load a photo
var index = 0;
var img = new Array();

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

	//change to images
	$(".sIIR").each(function(){
		t = $(this).text();
		if(t){
			cacheButtonImage($(this),'/domains/captainbob.nl/generate/button.php?text=' + escape(t),true);
		}
	});
	
	//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'));
	}
	
	//submenus
	var zIndex = 2;
	$(".leftmenu 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');
			 }
		 }
	);
	$('.leftmenu ul li a.current_buttonID').parents('li').find('a:first').addClass('current_buttonID');
	$(".leftmenu ul li").each(function(){
		//change main
		t = $(this).children('a').text();
		if(t){
			cacheButtonImage($(this).children('a'),'/domains/captainbob.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/captainbob.nl/generate/button.php?type=sub&text=' + escape(t.toUpperCase()));
				}
			});
		}
	});
	
	//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").each(function(){
		if($(this).find('.leftmenu').length && !$(this).find('.leftmenu ul li').length){
			$(this).parents('.article').remove();
		}else 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');
		}
	});
	
	//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);
		});
	}

	if($(".photoalbum_module").length){
		//addpopup container
		if(!$('#projectPhotoAlbumPopup').length){
			$('body').append("<div id=\"projectPhotoAlbumPopup\"><div class=\"photo\"><div class=\"left nav\"></div><div class=\"right nav\"></div></div><div class=\"description\"></div></div>");
		}

		//thumb mouseover and click
		$(".photoalbum_thumbs a").click(function(e){
			h = $(this).attr('href');
			photoID = h.match(/photoID=([0-9]+)/);
			albumID = h.match(/albumID=([0-9]+)/);
			setID = h.match(/setID=([0-9]+)/);

			var obj = $(this);
			$.getJSON('/modules/photoalbums/index.php',{photoID:photoID,albumID:albumID,setID:setID,json_photo_data:1},function(data){
		
				var id = 'popwinlink_' + uid();
				var u = '/' + data['photo']['path'];
				img[id] = new Image();
				img[id].onload = function(){
					img[id].onload = null;
					w = img[id].width;
					h = img[id].height;
		
					index = $(".photoalbum_thumbs a").index(obj);
		
					$('#projectPhotoAlbumPopup div.photo').css('background-image','url(' + u + ')');
					t = $(".photoalbum_thumbs a:eq("+ index +") img").attr('alt');
					if(t){
						$('#projectPhotoAlbumPopup div.description').text(t).show();
					}else{
						$('#projectPhotoAlbumPopup div.description').hide();
					}
					
		
					if(!$('#projectPhotoAlbumPopup').is(':visible')){
		
						$('#projectPhotoAlbumPopup div.nav,#projectPhotoAlbumPopup div.photo').height(h);
						$('#projectPhotoAlbumPopup div.left').click(function(){
							index--;
							if(index < 0)
								index = $(".photoalbum_thumbs a").length - 1;
							$(".photoalbum_thumbs a:eq("+ index +")").trigger('click');
						});
						
						$('#projectPhotoAlbumPopup div.right').click(function(){
							index++;
							if(index >= $(".photoalbum_thumbs a").length)
								index = 0;
							$(".photoalbum_thumbs a:eq("+ index +")").trigger('click');
						});
						
						$('#projectPhotoAlbumPopup div.photo').hover(
							function(){$('#projectPhotoAlbumPopup div.nav').show();},
							function(){$('#projectPhotoAlbumPopup div.nav').hide();}
						);
						
						popWindow('',w,h,$('#projectPhotoAlbumPopup'));
					}else{
						$('#projectPhotoAlbumPopup,#projectPhotoAlbumPopup div.photo').find('div.nav').andSelf().height(h);
						$('#projectPhotoAlbumPopup').width(w);
						$('#modalContainer').animate({height:h,width:w,marginLeft:((w / 2) * -1),marginTop:((h / 2) * -1)},'fast');
					}
				}
				img[id].src = u;
				
				
		
				//load reaction
				var photoID = data['ID'];
				$(".photoalbum_module .reactions").load('/modules/photoalbums/reactions.php?standalone=1&photoID=' + photoID,function(){
					a = self.location.href;
					a = a.indexOf('photoID') > -1 ? a.replace(/photoID=([0-9]+)/[1],photoID) : a + '&photoID=' + photoID;
					$(".photoalbum_module .reactions .loginform form").attr('action',a);
	
					$("input[value=registreer]").attr('onclick','').click(function(){
						self.location.href = '/index.php?module=register';
					});
					
					$(".photoalbum_module .reactions a").each(function(){
						$(this).attr('href',$(this).attr('href').replace(/modules\/photoalbums\/reactions\.php/,'index.php'));
					})
				});
			});
			return false;
		}).hover(
			function(){
				$(this).addClass('thumbHover').fadeTo('slow',0.5);
			},
			function(){
				$(this).removeClass('thumbHover').fadeTo('slow',1.0);
			}
		);
	}
	
	//change all popups...
	$(".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/captainbob.nl/video.php?file=" + u + "',320,260);");		
		}
	});
	
});

		
