//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//make same height
var make_same_height = function(selector){
	var max_height = 0;
	$(selector).each(function(){
		if($(this).height() > max_height){
			max_height = $(this).height();
		}
	});
	$(selector).height(max_height);	
}

//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); 
}




$(document).ready(function(){


	/////////////////////////////////////////////////////////  Slide menu
	
	$("#menu > ul > li").hover(
		function(){
			$(this).children().addClass('open');
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden")) {
				i.slideDown(400);
			}
		},
		function(){
			$(this).children().removeClass('open');
			i = $(this).children('ul:eq(0)');
			if(i.is(":visible"))
				i.slideUp(400);
		}
    );
	
	/////////////////////////////////////////////////////////  Popup

	$('body').append('<div id="itemPhotoAlbumPopup"><div class="photo"><div class="left nav"></div><div class="right nav"></div></div><div class="description"></div></div>');
	$('#itemPhotoAlbumPopup').hide();

	var img_index = 0;

	$('#itemPhotoAlbumPopup div.left').click(function(){
		img_index--;
		if(img_index < 0)
			img_index = $(".photoalbum_thumbs .photoalbum_thumb a").length - 1;
		$(".photoalbum_thumbs .photoalbum_thumb a:eq("+ img_index +")").trigger('click');
	});
	
	$('#itemPhotoAlbumPopup div.right').click(function(){
		img_index++;
		
		if(img_index >= $(".photoalbum_thumbs .photoalbum_thumb a").length)
			img_index = 0;
		$(".photoalbum_thumbs .photoalbum_thumb a:eq("+ img_index +")").trigger('click');
	});
	
	$('#itemPhotoAlbumPopup div.photo').hover(
		function(){$('#itemPhotoAlbumPopup div.nav').show();},
		function(){$('#itemPhotoAlbumPopup div.nav').hide();}
	);

	$(".photoalbum_thumbs .photoalbum_thumb a").click(function(e){
		e.preventDefault();
		
		img_index = $(this).parent().prevAll().length;
		
		var obj = $(this).find('img:eq(0)').attr('src');
		var id = 'popwinlink_' + uid();
		var u = obj.replace(/\/*\/AlbumThumb\./,'/AlbumPreview.');
		img[id] = new Image();
		img[id].onload = function(){
			img[id].onload = null;
			w = img[id].width;
			h = img[id].height;

			$('#itemPhotoAlbumPopup div.photo').css('background-image','url(' + u + ')');
			t = $(".photoalbum_thumbs .photoalbum_thumb a:eq("+ img_index +") img").attr('alt');
			if(t){
				$('#itemPhotoAlbumPopup div.description').text(t).width(w -6).show();
			}else{
				$('#itemPhotoAlbumPopup div.description').hide();
			}
			

			if(!$('#itemPhotoAlbumPopup').is(':visible')){
				img_index = $(".photoalbum_thumbs .photoalbum_thumb a").index(obj);

				$('#itemPhotoAlbumPopup div.nav,#itemPhotoAlbumPopup div.photo').height(h);
				
				popWindow('',w,h,$('#itemPhotoAlbumPopup'));
			}else{
				$('#itemPhotoAlbumPopup,#itemPhotoAlbumPopup div.photo').find('div.nav').andSelf().height(h);
				$('#itemPhotoAlbumPopup').width(w);
				$('#modalContainer').animate({height:h,width:w,marginLeft:((w / 2) * -1),marginTop:((h / 2) * -1)},'fast');
			}
		}
		img[id].src = u;
		
		return false;
	});


	/////////////////////////////////////////////////////////  Popup
	
	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);");		
		}
	});


});
