//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); 
}

//animation
var start_project_photo_scroll = function(){
	x = $(".projects_module .details div.Photos a:eq(0)").width();
	$(".projects_module .details div.Photos div.PhotosPane").animate({'left':(x * -1) + 'px'}, Math.round(x * 20),"linear",function(){
		$(".projects_module .details div.Photos div.PhotosPane a:eq(0)").appendTo(".projects_module .details div.Photos div.PhotosPane");
		$(".projects_module .details div.Photos div.PhotosPane").css('left','0px');
		start_project_photo_scroll();
	});
}

$(document).ready(function(){

	$("#menu").css('margin-right',($("#container_menu").width() / 2 - $("#menu").width() / 2) + 'px');
    $("#menu ul li").hover(
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden"))
				i.slideDown('normal');
		},
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":visible"))
				i.slideUp('normal');
		}
    );

	$("#cms_middle_column .article.half:even").addClass('first');

    $(".rightmenu ul li").mouseover(
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden")){
				$("#menu ul ul.open").removeClass('open').slideUp('normal')
				i.addClass('open').slideDown('normal');
			}
		}
	);
	if($(".rightmenu ul ul li.current_buttonID").length){
		$(".rightmenu ul ul li.current_buttonID").parent().addClass('open').show();
	}
	
	if($(".projects_module").length){
		$("form.filter select").change(function(){
			$(this).parents('form').trigger('submit');
		});
		
		$(".projects_module .project").hover(
			function(){$(this).addClass('hover');},
			function(){$(this).removeClass('hover');}
		).click(function(){
			self.location.href = $(this).find('a').attr('href');
		});
		
		//if($(".projects_module .details .Photos a").length){
		//	start_project_photo_scroll();
		//}
		
		var index = 0;
		$(".projects_module .details .Photos a").click(function(e){
			e.preventDefault();

//			console.debug(e.target);

			var obj = this;
			var id = 'popwinlink_' + uid();
			var u = $(this).attr('href');
			img[id] = new Image();
			img[id].onload = function(){
				img[id].onload = null;
				w = img[id].width;
				h = img[id].height;

				$('#projectPhotoAlbumPopup div.photo').css('background-image','url(' + u + ')');
				t = $(".projects_module .details .Photos a:eq("+ index +") img").attr('alt');
				if(t){
					$('#projectPhotoAlbumPopup div.description').text(t).show();
				}else{
					$('#projectPhotoAlbumPopup div.description').hide();
				}
				

				if(!$('#projectPhotoAlbumPopup').is(':visible')){
					index = $(".projects_module .details .Photos a").index(obj);

					$('#projectPhotoAlbumPopup div.nav,#projectPhotoAlbumPopup div.photo').height(h);
					$('#projectPhotoAlbumPopup div.left').click(function(){
						index--;
						if(index < 0)
							index = $(".projects_module .details .Photos a").length - 1;
						$(".projects_module .details .Photos a:eq("+ index +")").trigger('click');
					});
					
					$('#projectPhotoAlbumPopup div.right').click(function(){
						index++;
						if(index >= $(".projects_module .details .Photos a").length)
							index = 0;
						$(".projects_module .details .Photos 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;
			
			return false;
		});
		
		
	}

	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
	
	
	//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/mulderobdam.com/video.php?file=" + u + "',320,260);");		
		}
	});

	//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);
	});
	
});
