//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); 
}

//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);	
}


$(document).ready(function(){

	if($('#content #info_blocks .block .article .module_contents form.form_nieuwsbrief').length){
		//obj = $('#content #info_blocks .block .article .module_contents form.form_nieuwsbrief table.form');
		//obj.find('td').attr('colspan','2');
		////obj.find('td.label,td.spacer').remove();//.children('span.required_label').remove();
		////obj.find('tr.trField_submit td').attr('colspan','').attr('width','120');
		////obj.find('tr.trField_submit td').before('<td width="50" class="archief"/>');
		////obj.find('td.archief').html('<a href="/nieuwsbrieven"><img src="/domains/vandermark.com/images/bt_form_archief.png" width="120" height="12"></a>');
		//obj.addClass('altered');
	}

	$('#content #info_blocks .directcontact').click(function(){
		u = $('#content #info_blocks .directcontact .article .content table tr td').find('a').attr('href');
		self.location.href = u;
	});

	//TEAM PATCH
	if($('#middle .module_contents .team_module').length){

		$('.left_menu .submenu_module a').click(function(e) {
			e.preventDefault();
			$('.opengrowblock.open').trigger('click');
			a = $(this).attr('href').match(/#(.*)$/);
			$('div.user:not(.' + a[1] + ')').removeClass('high').fadeTo('normal',0.2);
			$('div.user.' + a[1]).addClass('high').fadeTo('normal',1);
			return false;
		});

		$('#middle .module_contents .team_module div.user .growblock').click( function (e) {
			$('.opengrowblock.open').trigger('click');
		  	o = $(this).next('.opengrowblock');
			if(!o.hasClass('open')){
				$(this).parents('.user').fadeTo('normal',1);
				if($(this).parent().hasClass('last') && o.parent().hasClass('bottom')) {
					o.css({top:$(this).offset().top,left:$(this).offset().left}).show().addClass('open');//.addClass('opengrowblock').prepend(o);
					o.animate({width:327,height:241,top:$(this).offset().top - 126,left:$(this).offset().left - 169},'fast');			
					$(this).css('zIndex',9);
				}else if(o.parent().hasClass('last')) {
					o.css({top:$(this).offset().top,left:$(this).offset().left}).show().addClass('open');//.addClass('opengrowblock').prepend(o);
					o.animate({width:327,height:241,left:$(this).offset().left - 169},'fast');
					$(this).css('zIndex',9);
				} else if(o.parent().hasClass('bottom')) {
					o.css({top:$(this).offset().top,left:$(this).offset().left}).show().addClass('open');//.addClass('opengrowblock').prepend(o);
					o.animate({width:327,height:241,top:$(this).offset().top - 126},'fast');
					$(this).css('zIndex',9);
				} else {
					o.css({top:$(this).offset().top,left:$(this).offset().left}).show().addClass('open');//.addClass('opengrowblock').prepend(o);
					o.animate({width:327,height:241},'fast');
				}
			}
		});
		 
		$('.opengrowblock').click(function(){
			if($(this).parent().hasClass('last') && o.parent().hasClass('bottom')) {
				$(this).animate({width:158,height:115,left:$(this).offset().left + 169,top:$(this).offset().top + 126},'fast',function(){
					$(this).removeClass('open').hide();
					$(this).prev().css('zIndex',2);
				});	
			}else if($(this).parent().hasClass('last')) {
				$(this).animate({width:158,height:115,left:$(this).offset().left + 169},'fast',function(){
					$(this).removeClass('open').hide();
					$(this).prev().css('zIndex',2);
				});	
			} else if(o.parent().hasClass('bottom')) {
				$(this).animate({width:158,height:115,top:$(this).offset().top + 126},'fast',function(){
					$(this).removeClass('open').hide();
					$(this).prev().css('zIndex',2);
				});					
			}else{
				$(this).animate({width:158,height:115},'fast',function(){
					$(this).removeClass('open').hide();
				});
			}
			p = $(this).parents('.user');
			if(!p.hasClass('high')){
				p.fadeTo('normal',0.2);
			}
		});

	}

	//HOME PATCH
	if($(".home").length){

		make_same_height('.module_contents .news_module .listing .article');

	}

	$('.form_nieuwsbrief input.submit').attr('value','');

	//change layout for with_line articles and click function
	$('.article.with_line').each(function(){
		$(this).children('.content').find('img:eq(0)').wrap('<div class="image"></div>');
		$(this).hover(
		  function() {
			$(this).css('background','#f2f2f2');
		  },
		  function() {
			$(this).css('background','#ffffff');
		  }
		).click(function(){
			var u = '';
			$(this).find('a').each(function(){
				a = $(this).text().match(/lees (meer|verder).*/i);
				if(a && a.length){
					self.location.href = $(this).attr('href');
				}
			});
		});
	});
	$('.article.with_line').each(function(){
		t = $(this).children('div.title');
		t.prependTo($(this).children('div.content'));
		$(this).find('div.image').after(t);
	});


	$('.article').not('with_line').each(function(){
		//give images some margin
		//$("img[align='left']").css('margin-right','5px');
		//$("img[align='right']").css('margin-left','5px');
	});
	
	//hide blank br before modules
	$(".module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
	//kill empty titles and articles
	$("#left .article .title,#middle .article,#middle .title,#middle .content,#right .article .title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed').length){
			$(this).remove();
		}
	});

	//news menu
	$('#nieuwsbrieven_menu > ul > li > ul > li > a').click(function(e){
		e.preventDefault();
		u = $(this).next('ul');
		if(u.length && !u.hasClass('open')){
			$('#nieuwsbrieven_menu ul ul ul.open').slideUp('normal').removeClass('open');
			u.slideDown('normal').addClass('open');
			return false;
		}
		return true;
	});
	//on load status
	$('#nieuwsbrieven_menu ul li ul li').each(function(){
		if($(this).find('.current_buttonID').length){
			$(this).children('ul:eq(0)').addClass('open');
		}
	});
	$('#nieuwsbrieven_menu ul ul ul').not('.open').hide();


	$('.form_nieuwsbrief input.input').focus(function(){
		$(this).not('.filled').val('').addClass('filled');
	});
	$('.form_nieuwsbrief').submit(function(e){
		if($(this).find('.filled').length != 2){
			return false;
		}
	});

});

