//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); 
}

var skipNextPSframe = function(){
	i = $('#photo_slideshow .menu a').index($('#photo_slideshow .menu a.active'));
	if(i == $('#photo_slideshow .menu a').length - 1){
		i = 0;
	}else{
		i++;
	}
	$('#photo_slideshow').fadeTo(5000,1,function(){
		$('#photo_slideshow .menu a:eq(' + i + ')').trigger('click');
	});
}

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(){

/*	-------------------------------------------
	HIDE BR BEFORE MODULES / KILL EMPTY ARTICLES
	----------------------------------------- */
	$(".module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed,form,input,frame,.sIFR-replaced').length){
			$(this).remove();
		}
	});

/*	-------------------------------------------
	LAYOUT MANUPULATIONS
	----------------------------------------- */
	$(".module_contents .calendar_shortlist_module").addClass('clear');
	$("table.calendar td.month_title").find('br').remove();
	$('table.calendar td.month_title').attr('colspan', '7');
	$('table.calendar td.city').hide();
	$('table.calendar td.country').hide();

/*	-------------------------------------------
	TEAM PAGE
	----------------------------------------- */
	$('.left_menu .submenu_module a:not(.all)').click(function(e) {
		e.preventDefault();
		$('.left_menu .submenu_module a').removeClass('selected');
		$('.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);
		$('.left_menu .submenu_module a.' + a[1]).addClass('selected');
		return false;
	});
	$('.left_menu .submenu_module a.all').click(function(e) {
		$('.left_menu .submenu_module a').removeClass('selected');
		$(this).addClass('selected');
		$('div.user').addClass('high').fadeTo('normal',1);
	});
	//CHANGE USER
	$('.team_module .listing .user').each(function(){
		$(this).hover(
		  function() {
			$(this).css('background','#666666');
			$(this).find('.employee_corner_green').addClass('hover');
		  },
		  function() {
			$(this).css('background','#c2da4c');
			$(this).find('.employee_corner_green').removeClass('hover');
		  }
		).click(function(){
			$(this).find('.link').each(function(){
				a = $(this).children('a').attr('href');
				if(a && a.length){
					self.location.href = a;
				}
			});
		});
	});

/*	-------------------------------------------
	DYNAMIC MENU
	----------------------------------------- */
	$('#menu ul li:first ul').css('margin-left', '5px');
	$("#menu > ul > li").mouseover(
		function(){
			if(!$(this).hasClass('current_buttonID') && !$(this).parents('li:last').hasClass('current_buttonID')){
				i = $(this).children('ul:eq(0)');
				if(i.is(":hidden")){
					i.show();
				}
				$('#menu > ul > li.current_buttonID').addClass('off');
				$('#menu ul li.current_buttonID ul').hide();
			}
		}
	);
	$("#menu > ul > li").mouseout(
		function(){
			if(!$(this).hasClass('current_buttonID') && !$(this).parents('li:last').hasClass('current_buttonID')){
				i = $(this).children('ul:eq(0)');
				if(i.is(":visible")){
					i.hide();
				}
				$('#menu > ul > li.current_buttonID').removeClass('off');
				$('#menu ul li.current_buttonID ul').show();
			}
		}
	);
	$('#menu ul ul li.current_buttonID').parents('li').addClass('current_buttonID').find('a:first').addClass('current_buttonID');
	$('#menu ul li.current_buttonID ul').show();

/*	-------------------------------------------
	ROOSTER
	----------------------------------------- */
	$('.rooster_module .enableTipTip').tipTip();
	$('.rooster_module').each(function(){
		$(this).parents('.content').eq(0).css('padding-bottom','0px');
		$(this).parents('.article').eq(0).css('padding-bottom','0px');
	});

/*	-------------------------------------------
	PHOTO SLIDESHOW
	----------------------------------------- */
	if($('#photo_slideshow').length){
		w = $('#photo_slideshow .menu').width();
		$('#photo_slideshow .menu').css('margin-left',590-w);
		$('#photo_slideshow .menu').css('display','block');
		$('#photo_slideshow .menu a:eq(0)').addClass('active');
		$('#photo_slideshow .menu a').click(function(){
			i = $('#photo_slideshow .menu a').index(this);
			x = (($('#photo_slideshow ul li:eq(' + i + ')').width()) * i) * -1;
			$('#photo_slideshow ul').animate({marginLeft:x},1100);
			$('#photo_slideshow .menu a.active').removeClass('active');
			$(this).addClass('active');
			skipNextPSframe();
		});
	
		$('#photo_slideshow').fadeTo(1000,1,function(){
			$('#photo_slideshow .menu a:eq(' + i + ')').trigger('click');
		});
	}

/*	-------------------------------------------
	IMAGE MARGINS
	----------------------------------------- */
	$("img[align='left']").css('margin-right','20px');
	$("img[align='right']").css('margin-left','20px');


/*	-------------------------------------------
	CHANGE 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');
	
					h = c.substring(c.indexOf('height=') + 7);
					h = h.substring(0,h.indexOf(','));
	
					w = c.substring(c.indexOf('width=') + 6);
					w = w.substring(0,w.indexOf(','));
	
					$(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);");		
		}
	});

/*	-------------------------------------------
	MAKE SAME HEIGHT
	----------------------------------------- */
	make_same_height("#left .article.colour .content");

});

