/* 
   Author  : Andrew Webster
   Date    : Nov 2010
   Email   : andrew.webster@gmail.com
   Website : www.awebster.co.za   
*/


$.fn.accordion = function(settings) {
	
	settings = jQuery.extend({
		expander : 'reveal',
		toggler  : 'header',
		speed	 : 250,
		multiple : false
	}, settings);
	
	var $el = this;
	var expander = '.'+settings.expander;
	var toggler = '.'+settings.toggler;	

	// close expanders except any that are specified as open
	$el.find('li > a:not(".open")').each(function(){
		$(this).parent('li').children(expander).hide();
		$(this).toggleClass('closed');
	});
	
	$el.find('li > a'+toggler).click(function(){	
	
		clickedID = $el.find('li > a.open').index(this);
		
		// close open answers	
		if(!settings.multiple){			
			if ( $el.find('li a.open').length > 1 ){
				// close only clicked answers (if open)					
				if( $(this).hasClass('open') ){
					$(this).removeClass('open')
						.addClass('closed')
						.next(expander).slideUp(settings.speed);
						return;	
				}				
			} else {			
				// close all open answers		
				$el.children('li').each(function(){									
					$(this).children('a.open')
						.removeClass('open')
						.addClass('closed')
					$(this).children(expander).slideUp(settings.speed);				
				});
			}
		} else {
			// close only clicked answers (if open)					
			if( $(this).hasClass('open') ){
				$(this).removeClass('open')
					.addClass('closed')
					.next(expander).slideUp(settings.speed);
					return;	
			}			
		}
		
		// open clicked answer (unless it was open to begin with)		
		if(clickedID < 0){			
			$(this).nextAll(expander).slideDown(settings.speed);
			$(this).removeClass('closed')
				.addClass('open');
		}			
		return false;	
		
	});
	
	// enable expander controls
	$el.prev('.expander_controls').show();
	
	// show all
	$el.prev('.expander_controls').find('a.showall').click(function(){		
		$(this).parent('.expander_controls').next('ul.expander').children('li').each(function(){			
			$(this).children(expander).slideDown(settings.speed);
			$(this).children('a'+toggler).removeClass('closed')
				.addClass('open');
		});
		return false;
	});	
	
	// close all
	$el.prev('.expander_controls').find('a.closeall').click(function(){		
		$(this).parent('.expander_controls').next('ul.expander').children('li').each(function(){			
			$(this).children(expander).slideUp(settings.speed);
			$(this).children('a'+toggler).removeClass('open')
				.addClass('closed');
		});		
		return false;		
	});
	
	
}

$(function(){

	Cufon.replace('#main h1, #main h2.blue, #main h2.primary, #main .title, #content .img_std p.stats, #content .module .digits', { 
			fontFamily: 'AvantGarde'
	});	
	
	

	
	$('#footer li.about').append("<ul><li><a href='about-us/#Our vision'>Our vision</a></li><li><a href='about-us/#Our strategy'>Our strategy</a></li><li><a href='about-us/#Where we operate'>Where we operate</a></li><li><a href='about-us/#Principal offices and contacts'>Principle offices and contacts</a></li></ul>");
	
	$('#footer li.divisions').append("<ul><li><a href='our-divisions/'>Shipbroking</a></li><li><a href='our-divisions/'>Technical</a></li><li><a href='our-divisions/'>Logistics</a></li><li><a href='our-divisions/'>Environmental</a></li></ul>");
	
	$('#quicklinkcontain ul li.home').hide();
	
	
	
	$(".FinDownloadWrap ul li:nth-child(3)").css("border","0");
	$(".FinDownloadWrap ul li:nth-child(4)").css("border","0");
		
	
	
	
	/** Form default value show/hide
	*************************/	
	if( !($('#contact_form').length > 0) ){		
		$(function() {
		  $('input[type="text"]:not(".noblur")').add('textarea')	  
		  .focus(function() {
			var $this = $(this);
			
			if (!$this.data('default')) {
			  $this.data('default', $this.val());
			}
			
			if ($this.val() == $this.data('default')) {
			  $this.val('');
			}
		  })	  
		  .blur(function() {
			var $this = $(this);
			
			if ($this.val() == '') {
			  $(this).val($this.data('default'));
			}
		  })	  
		});
	}
	
	
	/** Nav bleeds
	**************************/	
	function addBleeds(el){
		$el = el;
		$el.parent('td').prev('td').addClass('bleed-l');
		$el.parent('td').next('td').addClass('bleed-r');
	}	
	function removeBleeds(el){
		$el = el;
		if( !($el.hasClass('active')) ){
			$el.parent('td').prev('td').removeClass('bleed-l');
			$el.parent('td').next('td').removeClass('bleed-r');
		}
		$('#nav td a.active').each(function(){
			addBleeds($(this));
		});
		
	}
	$('#nav td a.active').each(function(){
		addBleeds($(this));
	});
	$('#nav td a').hover(function(){
			addBleeds($(this));
	},function(){
			removeBleeds($(this));	
	});		
	
	
	/** Sitemap expand
	**************************/	
	jQuery(function(){
		var $sitemap = jQuery('#footer > .sitemap_row');
		var sitemapHeight;
		var destination;
		
		jQuery('#footer a.sitemap').click(function(){
			
			sitemapHeight = $sitemap.height();
			destination = jQuery('body').height() + sitemapHeight;		
			
			if( $sitemap.is(':hidden') ){				
				$sitemap.slideToggle(250);				
				jQuery('html:not(:animated),body:not(:animated)').animate({	scrollTop: destination	}, 250);				
			} else {			
				$sitemap.slideToggle(250);
			}			
			
			return false;
		});
	});
	
	/** Table row highlight
	**************************/	
	$('#content table.share_docs tr:not(".year") td, #content table.news_announce td').hover(function(){
		$(this).parent('tr').addClass('hilite');
	},function(){
		$(this).parent('tr').removeClass('hilite');	
	});
	
	/** Intro listings highlight
	**************************/	
	$('#content ul.intro_listings li').hover(function(){
		$(this).addClass('hilite');
	},function(){
		$(this).removeClass('hilite');	
	});
	
	
	
	
	
	/** Map overlays
	**************************/	
	if( $('#worldmap').length > 0 ){
		function closeListener(){
			$('#map_lb .map_overlay .close').click(function(){
				$('#map_lb').empty();
			});
			$(document).keyup(function(e){
				if (e.keyCode == 27) $('#map_lb .map_overlay .close').click();
			});		
		}
		
		/* on click */
		$('#worldmap area').click(function(){
			$('#map_lb').empty();	
			$($(this).attr('href')).clone().appendTo('#map_lb');		
			$('#map_lb .map_overlay').hide().fadeIn(250);			
			closeListener();		
			return false;	
		});
		/* */
		
		/* on hover */	
		(function(){
			var holdIt;
			$('#worldmap area').hover(function(){			
				function considerMeHovered(){
					clearTimeout(holdIt);
					if( $('#map_lb > div').length < 1){	
						$('#map_lb').empty();	
						$($(this).attr('href')).clone().appendTo('#map_lb');		
						$('#map_lb .map_overlay').hide().fadeIn(250);			
						closeListener();				
					}
				}
				holdIt = setTimeout(considerMeHovered, 400);			
			},function(){
				clearTimeout(holdIt);
			});
			
			$('#worldmap area').click(function(){
				return false;
			});
		})();
		/* */	
		
		// on hover
		$('#worldmap area').hover(function(){
			$('#map .hilite').empty();
			href = $(this).attr('href');
			if( href == '#fareast' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/fareast-over.gif" />')
					.fadeIn(150);
			} else if( href == '#cis' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/cis-over.gif" />')
					.fadeIn(150);
			} else if( href == '#uk' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/uk-over.gif" />')
					.fadeIn(150);
			} else if( href == '#gulf' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/gulf-over.gif" />')
					.fadeIn(150);
			}
		},function(){
			$('#map .hilite').empty();
		});
	}
	


/** Map overlays
	**************************/	
	if( $('#worldmap1').length > 0 ){
		function closeListener(){
			$('#map_lb .map_overlay .close').click(function(){
				$('#map_lb').empty();
			});
			$(document).keyup(function(e){
				if (e.keyCode == 27) $('#map_lb .map_overlay .close').click();
			});		
		}
		
		/* on click */
		$('#worldmap1 area').click(function(){
			$('#map_lb').empty();	
			$($(this).attr('href')).clone().appendTo('#map_lb');		
			$('#map_lb .map_overlay').hide().fadeIn(250);			
			closeListener();		
			return false;	
		});
		/* */
		
		/* on hover */	
		(function(){
			var holdIt;
			$('#worldmap1 area').hover(function(){			
				function considerMeHovered(){
					clearTimeout(holdIt);
					if( $('#map_lb > div').length < 1){	
						$('#map_lb').empty();	
						$($(this).attr('href')).clone().appendTo('#map_lb');		
						$('#map_lb .map_overlay').hide().fadeIn(250);			
						closeListener();				
					}
				}
				holdIt = setTimeout(considerMeHovered, 400);			
			},function(){
				clearTimeout(holdIt);
			});
			
			$('#worldmap1 area').click(function(){
				return false;
			});
		})();
		/* */	
		
		// on hover
		$('#worldmap1 area').hover(function(){
			$('#map .hilite').empty();
			href = $(this).attr('href');
			if( href == '#fareast' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/fareast-over.gif" />')
					.fadeIn(150);
			} else if( href == '#cis' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/cis-over.gif" />')
					.fadeIn(150);
			} else if( href == '#uk' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/uk-over.gif" />')
					.fadeIn(150);
			} else if( href == '#gulf' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/gulf-over.gif" />')
					.fadeIn(150);
			}
		},function(){
			$('#map .hilite').empty();
		});
	}





/** Map overlays
	**************************/	
	if( $('#worldmap3').length > 0 ){
		function closeListener(){
			$('#map_lb .map_overlay .close').click(function(){
				$('#map_lb').empty();
			});
			$(document).keyup(function(e){
				if (e.keyCode == 27) $('#map_lb .map_overlay .close').click();
			});		
		}
		
		/* on click */
		$('#worldmap3 area').click(function(){
			$('#map_lb').empty();	
			$($(this).attr('href')).clone().appendTo('#map_lb');		
			$('#map_lb .map_overlay').hide().fadeIn(250);			
			closeListener();		
			return false;	
		});
		/* */
		
		/* on hover */	
		(function(){
			var holdIt;
			$('#worldmap3 area').hover(function(){			
				function considerMeHovered(){
					clearTimeout(holdIt);
					if( $('#map_lb > div').length < 1){	
						$('#map_lb').empty();	
						$($(this).attr('href')).clone().appendTo('#map_lb');		
						$('#map_lb .map_overlay').hide().fadeIn(250);			
						closeListener();				
					}
				}
				holdIt = setTimeout(considerMeHovered, 400);			
			},function(){
				clearTimeout(holdIt);
			});
			
			$('#worldmap3 area').click(function(){
				return false;
			});
		})();
		/* */	
		
		// on hover
		$('#worldmap3 area').hover(function(){
			$('#map .hilite').empty();
			href = $(this).attr('href');
			if( href == '#fareast' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/fareast-over.gif" />')
					.fadeIn(150);
			} else if( href == '#cis' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/cis-over.gif" />')
					.fadeIn(150);
			} else if( href == '#uk' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/uk-over.gif" />')
					.fadeIn(150);
			} else if( href == '#gulf' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/gulf-over.gif" />')
					.fadeIn(150);
			}
		},function(){
			$('#map .hilite').empty();
		});
	}
	
	
	
/** Map overlays
	**************************/	
	if( $('#worldmap4').length > 0 ){
		function closeListener(){
			$('#map_lb .map_overlay .close').click(function(){
				$('#map_lb').empty();
			});
			$(document).keyup(function(e){
				if (e.keyCode == 27) $('#map_lb .map_overlay .close').click();
			});		
		}
		
		/* on click */
		$('#worldmap4 area').click(function(){
			$('#map_lb').empty();	
			$($(this).attr('href')).clone().appendTo('#map_lb');		
			$('#map_lb .map_overlay').hide().fadeIn(250);			
			closeListener();		
			return false;	
		});
		/* */
		
		/* on hover */	
		(function(){
			var holdIt;
			$('#worldmap4 area').hover(function(){			
				function considerMeHovered(){
					clearTimeout(holdIt);
					if( $('#map_lb > div').length < 1){	
						$('#map_lb').empty();	
						$($(this).attr('href')).clone().appendTo('#map_lb');		
						$('#map_lb .map_overlay').hide().fadeIn(250);			
						closeListener();				
					}
				}
				holdIt = setTimeout(considerMeHovered, 400);			
			},function(){
				clearTimeout(holdIt);
			});
			
			$('#worldmap4 area').click(function(){
				return false;
			});
		})();
		/* */	
		
		// on hover
		$('#worldmap4 area').hover(function(){
			$('#map .hilite').empty();
			href = $(this).attr('href');
			if( href == '#fareast' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/fareast-over.gif" />')
					.fadeIn(150);
			} else if( href == '#cis' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/cis-over.gif" />')
					.fadeIn(150);
			} else if( href == '#uk' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/uk-over.gif" />')
					.fadeIn(150);
			} else if( href == '#gulf' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/gulf-over.gif" />')
					.fadeIn(150);
			}
		},function(){
			$('#map .hilite').empty();
		});
	}
	
	
/** Map overlays
	**************************/	
	if( $('#worldmap5').length > 0 ){
		function closeListener(){
			$('#map_lb .map_overlay .close').click(function(){
				$('#map_lb').empty();
			});
			$(document).keyup(function(e){
				if (e.keyCode == 27) $('#map_lb .map_overlay .close').click();
			});		
		}
		
		/* on click */
		$('#worldmap5 area').click(function(){
			$('#map_lb').empty();	
			$($(this).attr('href')).clone().appendTo('#map_lb');		
			$('#map_lb .map_overlay').hide().fadeIn(250);			
			closeListener();		
			return false;	
		});
		/* */
		
		/* on hover */	
		(function(){
			var holdIt;
			$('#worldmap5 area').hover(function(){			
				function considerMeHovered(){
					clearTimeout(holdIt);
					if( $('#map_lb > div').length < 1){	
						$('#map_lb').empty();	
						$($(this).attr('href')).clone().appendTo('#map_lb');		
						$('#map_lb .map_overlay').hide().fadeIn(250);			
						closeListener();				
					}
				}
				holdIt = setTimeout(considerMeHovered, 400);			
			},function(){
				clearTimeout(holdIt);
			});
			
			$('#worldmap5 area').click(function(){
				return false;
			});
		})();
		/* */	
		
		// on hover
		$('#worldmap5 area').hover(function(){
			$('#map .hilite').empty();
			href = $(this).attr('href');
			if( href == '#fareast' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/fareast-over.gif" />')
					.fadeIn(150);
			} else if( href == '#cis' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/cis-over.gif" />')
					.fadeIn(150);
			} else if( href == '#uk' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/uk-over.gif" />')
					.fadeIn(150);
			} else if( href == '#gulf' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/gulf-over.gif" />')
					.fadeIn(150);
			}
		},function(){
			$('#map .hilite').empty();
		});
	}
	
	
	
	
	
	
	
	
/** Map overlays
	**************************/	
	if( $('#worldmap2').length > 0 ){
		function closeListener(){
			$('#map_lb .map_overlay .close').click(function(){
				$('#map_lb').empty();
			});
			$(document).keyup(function(e){
				if (e.keyCode == 27) $('#map_lb .map_overlay .close').click();
			});		
		}
		
		/* on click */
		$('#worldmap2 area').click(function(){
			$('#map_lb').empty();	
			$($(this).attr('href')).clone().appendTo('#map_lb');		
			$('#map_lb .map_overlay').hide().fadeIn(250);			
			closeListener();		
			return false;	
		});
		/* */
		
		/* on hover */	
		(function(){
			var holdIt;
			$('#worldmap2 area').hover(function(){			
				function considerMeHovered(){
					clearTimeout(holdIt);
					if( $('#map_lb > div').length < 1){	
						$('#map_lb').empty();	
						$($(this).attr('href')).clone().appendTo('#map_lb');		
						$('#map_lb .map_overlay').hide().fadeIn(250);			
						closeListener();				
					}
				}
				holdIt = setTimeout(considerMeHovered, 400);			
			},function(){
				clearTimeout(holdIt);
			});
			
			$('#worldmap2 area').click(function(){
				return false;
			});
		})();
		/* */	
		
		// on hover
		$('#worldmap2 area').hover(function(){
			$('#map .hilite').empty();
			href = $(this).attr('href');
			if( href == '#fareast' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/fareast-over.gif" />')
					.fadeIn(150);
			} else if( href == '#cis' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/cis-over.gif" />')
					.fadeIn(150);
			} else if( href == '#uk' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/uk-over.gif" />')
					.fadeIn(150);
			} else if( href == '#gulf' ){
				$('#map .hilite')
					.hide()
					.html('<img src="images/content/map/gulf-over.gif" />')
					.fadeIn(150);
			}
		},function(){
			$('#map .hilite').empty();
		});
	}
	
	
	
	
	
	/** Small World Map
	**************************/
	if( $('#smallmap').length > 0 ){		
		
		var $regionlist = $('#content .panels .listings ul.list');
		
		$('#smallmap area').hover(function(){			
			var region = $(this).attr('href');				
			region = region.substring(1, region.length);
			
			if( region == 'fareast' ){				
				$regionlist.children('li.'+region).animate({
					backgroundColor : '#83bf40',
					borderBottomColor: '#83bf40'
				}, 250);				
			} else if( region == 'cis' ){				
				$regionlist.children('li.'+region).animate({
					backgroundColor : '#00a4d6',
					borderBottomColor: '#00a4d6'
				}, 250);				
			} else if( region == 'uk' ){				
				$regionlist.children('li.'+region).animate({
					backgroundColor : '#e55825',
					borderBottomColor: '#e55825'
				}, 250);				
			} else if( region == 'gulf' ){				
				$regionlist.children('li.'+region).animate({
					backgroundColor : '#74277e',
					borderBottomColor: '#74277e'
				}, 250);				
			}			
			
			$regionlist.children('li.'+region).children('a').animate({
				color : '#ffffff'
			}, 250);
						
			
		},function(){
			$regionlist.children('li').each(function(){
				$(this).animate({
					backgroundColor : '#f1f0f1',
					borderBottomColor: '#bbbabb'
				}, 150);
				
				$(this).children('a').animate({
					color: '#265787'
				}, 150);
				
			});
		});		
		
	}
	
	
		
	/** Register form
	**************************/	
	
		/** Multi-select control
		**************************/		
		function insertNode(nodeVal, nodeText, control){				
			// test if node already exists
			var nodeExist = false;			
			$('#' +  + control + ' option').each(function(){
				if( nodeVal == $(this).val() ) nodeExist = true;
			});			
			// if node doesn't exist, add it			
			if ( nodeExist == false ) $('#' + control).append('<option value="'+nodeVal+'">'+nodeText+'</option>');	
		}
		
		function removeNode(nodeVal, nodeText, control){	
			$('form .multi .col_2 select option[value='+nodeVal+']').remove();
		}
		
		// add node options
		$('#putType').click(function(){
			$('#jobtype option:selected').each(function(){
				insertNode($(this).val(), $(this).text() , 'jobselect');			
			});
			return false;
		});
		
		// remove node options
		$('#removeType').click(function(){
			$('#jobtype option:selected').each(function(){
				removeNode($(this).val(), $(this).text(), 'jobselect');			
			});
			return false;
		});
		
			$('#putLocation').click(function(){
				$('#jobtypeLocation option:selected').each(function(){
					insertNode($(this).val(), $(this).text(), 'jobselectlocation');		
				});
				return false;
			});

			// remove node options
			$('#removeLocation').click(function(){
				$('#jobtypeLocation option:selected').each(function(){
					removeNode($(this).val(), $(this).text(), 'jobselectlocation');			
				});
				return false;
			});
		
	
	

});

