jQuery.fn.delay = function(time,callback) {
  return this.animate({opacity:1},time,callback);
};

/**
 * Returnerer startelementet for hendelsen
 * 
 * @version	20061011
 * @param	e		event
 */
$.getEventTarget = function(e) {
  	e = e || window.event;
  	return e.target || e.srcElement;
};

var menu = {
	init:function(){
		menu.cleanUp();
		$('div.menu').click(menu.handleClick);
		menu.setActive($('div.menu .active'));
	},
	
	cleanUp:function(){
		$('div.menu a p').each(function(){
			var html = $(this).html();
			$(this).parent().html(html);
		});
	},
	
	handleClick:function(e){
		var target = $($.getEventTarget(e));
		
		if(target.attr('href').length == 0){
			e.preventDefault();
			menu.showMenu(target);
		}
	},
	
	hasSubmenu:function(anchor){
		return (anchor.attr('rel').search('folder') != -1);
	},
	
	showMenu:function(anchor){
		var paneClass = anchor.attr('rel').match(/two|three|four/g)[0];
		
		anchor
			.parents('div.level')
				.nextAll('div.level')
					.children('div.border')
						.children('div.wrap')
							.html('');
							
		var pane = menu.setPane(anchor);
							
		
		if(anchor.parents('div.level').hasClass('one') && paneClass == 'two'){
			menu.hidePane($('div.three'));
		}else if(anchor.parents('div.level').hasClass('one') && paneClass == 'four'){
			menu.hidePane($('div.two'));
			menu.hidePane($('div.three'));
		}else if(anchor.parents('div.level').hasClass('two') && paneClass == 'four'){
			menu.hidePane($('div.three'));
		}	
		
		if(pane.parents('div.level').hasClass('inactive')){
			pane.parents('div.level')
				.css({opacity:'0'})
				.removeClass('inactive')
				.animate({opacity:'1'},'fast');
		}
		
		pane.children('ul').animate({left:'0'});
		anchor.parents('ul').children('li').addClass('inactive');
		anchor.parents('div.level').removeClass('inactive');
		anchor.parents('li').siblings('li').removeClass('active');
		anchor.parents('li').addClass('active');
	},
	
	setPane:function(anchor){
		var length = anchor.next('ul').children('li').length;
		var className = ((length > 1)?' class="top"':'');
		var content = '<ul'+className+'>'+anchor.next('ul').html()+'</ul>';
		var paneClass = anchor.attr('rel').match(/two|three|four/g)[0];
		var pane = $('div.'+paneClass).children('div.border').children('div.wrap');
		pane.html(content);
		
		return pane;
	},
	
	hidePane:function(pane){
		$(pane).animate(
			{opacity:'0'},
			'fast',
			function(){
				$(this).addClass('inactive');
			}
		);
	},
	
	setActive:function(element){
		element.addClass('active');
		var anchor = element.parent().prev();
		
		if(anchor.length > 0){
			var pane = menu.setPane(anchor);
			pane.parents('div.level').removeClass('inactive');
			pane.children('ul').css({left:'0'});
			menu.setActive(anchor.parents('li'));
		}
	}
};

var images = {
	occupied:false,
	play:false,
	init:function(){
		$('div.images div.thumbs li a').click(images.handleClick);
		images.startShow();
	},
	
	handleClick:function(e){
		e.preventDefault();
		if(!images.occupied){
			images.occupied = true;
			var url = $(this).attr('rel');
			var image = $("<img height='330' width='488' src='"+url+"' />");
			$('div.images div.carousel img:first').css({zIndex:'1'});
			$('div.images div.carousel').append(image);
			$('div.images div.carousel img:first').fadeOut(
				'slow',
				function(){
					$(this).remove();
					images.occupied = false;
				}
			);
		}
	},
	
	startShow:function(){
		if($('.carousel img').length > 1){
			images.play = true;
			images.change();
		}
	},
	
	endShow:function(){
		images.play = false;
	},
	
	change:function(){
		$('.carousel img:last').delay(2500,function(){
			$(this).fadeOut(1500,function(){
				$(this)
					.clone()
						.show()
						.prependTo('.carousel');
				$(this).remove();
				if(images.play){
					images.change();
				}
			});
		});
	}
};

var contacts = {
	googlemap_visible:true,
	init:function(){
		//console.log('inserting google map');
		$('table.employees tr.vcard').click(function(){
			var img = $($(this).find('td.portrait').html());
			$('div.contacts :first').css({zIndex:'1'});
			$('div#googlemap').append(img);
			if(contacts.googlemap_visible){
				$('div#googlemap').css({
					background: "#fff"
				})
					.children('div')
						.fadeOut(
							'slow',
							function(){
								$(this).remove();
							}
						);
				
				contacts.googlemap_visible = false;
			}
			else{
				$('div#googlemap img.portrait:first').fadeOut(
					'slow',
					function(){
						$(this).remove();
					}
				);
			}
			$('table.employees tr.vcard').removeClass('active');
			$(this).addClass('active');
		});
	}
};

/*var contacts = {
	occupied:false,
	init:function(){
		$('table.employees tr.vcard').click(
			function(){
				$('table.employees tr.vcard').removeClass('active');
				$(this).addClass('active');
				if(!contacts.occupied){
					contacts.occupied = true;
					var img = $($(this).find('td.portrait').html());
					$('div.contacts img.portrait:first').css({zIndex:'1'});
					$('div.contacts').append(img);
					$('div.contacts img.portrait:first').fadeOut(
						'slow',
						function(){
							$(this).remove();
							contacts.occupied = false;
						}
					);
				}
			}
		);
	}
};*/

$(document)
	.ready(menu.init)
	.ready(images.init)
	.ready(contacts.init)
	.ready(function(){
		$("a[rel='external']").attr({
			target: "_blank"
		});
	});
