$(function(){
	
	var byName = $.getUrlVar('c');
	if (byName == 'stage'){
		byName = 'fund'
	}
	
	$('#'+ byName +'_header').addClass('header_active');
	
	if (!$('#page table tr th').hasClass('header_active')) {
	    $('#company_header').addClass('header_active');
	}
	
	
	$('#map').hide();
	$('#google-map-switch').toggle(function() {
	  $('#map').fadeIn();
	  $('#flash-map').hide();
	  $('#google-map-switch').text('interactive map');
	  initialize();
	}, function() {
	  $('#flash-map').fadeIn();
	  $('#map').hide();
	  $('#google-map-switch').text('google map');
	  initialize();
	});
	
	$('video').bind('ended', function(){
		this.play();
	});
	
	$('.company').each(function(){
		var matchToHeight = $(this).children('.left').innerHeight() - 5;
		$(this).children('.right').height(matchToHeight);
	});
		
	$('#fancybox-close').hide();
	
	$('.portfolio .quote').hide();
	var randomNum = Math.ceil(Math.random()*5);
	var randomNumTwo = Math.ceil(Math.random()*5);
	if (randomNum == randomNumTwo && randomNum <= 4){
		randomNumTwo = randomNum + 1;
	}
	if (randomNum == randomNumTwo && randomNum == 5){
		randomNumTwo = randomNum - 1;
	}
	$('.portfolio .quote:nth-child('+ randomNum +')').show();
	$('.portfolio .quote:nth-child('+ randomNumTwo +')').show();
	
		
	$('#profiles').css({
		'position': 'absolute',
		'right': '0',
		'top': '0'
	});

	$(window).scroll(function(){
		var pageTop = $(window).scrollTop();
		$('.profile').animate({
			'top': pageTop
		}, 50);
	});
	
	$('.profile').css({
		'display': 'none',
		'position': 'absolute',
		'right': '-5px',
		'top': '0'
	});
	
	$('.ie8 .profile').css({
		'display': 'none',
		'position': 'absolute',
		'right': '-9px',
		'top': '0'
	});
	
	$('.close').css({
		'display': 'block'
	});
	
	$('.close').click(function () { 
	  $('.person').fadeTo("slow", 1);
      $('.ie7 .person img, .ie8 .person img').fadeTo("slow", 1);
      $('.ie7 .person img, .ie8 .person img').css({
      		'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
      });
      $('.ie7 .person h2, .ie8 .person h2').fadeTo("slow", 1);
      var title = $(this).parent().parent().parent().attr("id");
      $('#' + title).hide();
      showRollover();
      	$('.person').click(function () {
	      	  hideRollover();
		      $('.person').fadeTo("slow", 0.33);
		      $('.ie7 .person img, .ie8 .person img').fadeTo("slow", 0.33);
		      $('.ie7 .person h2, .ie8 .person h2').fadeTo("slow", 0.33);
		      var title = $(this).attr('id').split('_');
		      $('#' + title).fadeIn();
		      $('.person').unbind('click');
	    });
    });
	
	$('.person').click(function () {
		  hideRollover();
	      $('.person').fadeTo("slow", 0.33);
	      $('.ie7 .person img, .ie8 .person img').fadeTo("slow", 0.33);
	      $('.ie7 .person h2, .ie8 .person h2').fadeTo("slow", 0.33);
	      var title = $(this).attr('id').split('_');
	      $('#' + title).fadeIn();
	      $('.person').unbind('click');
      });
    
    $('.ie7 .person img, .ie8 .person img').hover(
	  function () {
	    $(this).css({
	    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)'
	    });
	  },
	  function () {
	    $(this).css({
	    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
	    });
	  }
	);
    		
	$('table tr').after('<tr class="portfolioInfo"><td colspan="4"></td></tr>');	
	
	$('table tr.portfolioRow').each(function() {
		$(this).find('a').click(function(e) {
			e.preventDefault();
		});
		$thisIndex = $(this).find('a').attr('href').split('#')[1];
		$(this).next('tr.portfolioInfo').find('td').append($('#'+$thisIndex));
	});
	
	$('table tr.portfolioRow:even td').css({
		'background': '#dbdbdb'
	});
	
	$('table tr.portfolioRow:odd td').css({
		'background': '#ececec'
	});
	
	$("table tr.portfolioRow").hover(
	  function () {
	    $(this).children('td').addClass('on');
	  }, 
	  function () {
	    $(this).children('td').removeClass('on');
	  }
	);
	
	$('.company').css({'margin': '0'}).hide();
	
	$('tr.portfolioRow').click(function() {
		
		$('div.company').slideUp(500, function(){
			$(this).parent().parent().prev('tr').children('td').removeClass('open');
		});
		
		if(!$(this).children('td').hasClass('open')){
			$(this).children('td').addClass('open');
			$(this).next('tr').find('div.company').slideDown();
		}
		 
	 });
	 
	 
	
	$('.pod.cases:not(.active)').hide();
	$('.dropdown .dropinfo').hide();
	$('.dropdown.active .dropinfo').show();
	
	$('.dropdown h2').click(function() {
		
		$('.dropdown.active .dropinfo').slideUp(500,function(){
			$('.dropdown').removeClass('active');
			$('.pod.cases').hide();
 			$('#quotes').fadeIn();
 			$('.closed').show();
 		});
		
		if(!$(this).parent().parent().hasClass('active')){
				
			$(this).parent().parent().children('.dropinfo').slideDown(500, function(){
				$(this).parent().addClass('active');
				var righthand = $(this).parent().attr('id');
	 			$('.pod.cases, #quotes').hide();
	 			$('.pod.'+ righthand).fadeIn();
			});
		
		}
		 
	 });
	 
	$('.left col .highlight, .right col .highlight').sameHeights();
	
	$('.info-content').hide();
	
	$('.info h3').click(function() {
		$('.info').removeClass('active');
		$('.enclosure').fadeOut();
	 	$('.info .info-content').slideUp();
		if($(this).parent().children('.info-content').is(':hidden') == true) {
			$(this).parent().addClass('active');
			$(this).parent().children('.info-content').slideDown(500, function(){
				$('.enclosure').fadeIn();
			});
		 } 
	 });

});

$.fn.sameHeights = function() {

	$(this).each(function(){
		var tallest = 0;
		$(this).each(function(i){
			if (tallest < $(this).height()) { tallest = $(this).height(); }
		});
		$(this).css({'height': tallest});
	});
	return this;
};

function hideRollover(){
	 	$('html:not(.ie7, .ie8, .ie9) #people-images .block img').hover(function(){
			$(this).grayscale();
		},function(){
			$(this).grayscale();
	  	});
	  	
	  	$('.ie7 .person img, .ie8 .person img').hover(function() {
		    $(this).css({
		    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)',
		    	'opacity': '0.33'
		    });
		  },
		  function () {
		    $(this).css({
		    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)',
		    	'opacity': '0.33'
		    });
		 });
		
		$('.ie9 .person img').hover(function() {
		    $(this).css({
		    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
		    });
		}, function (){
		    $(this).css({
		    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
		    });
		});
	
}

function showRollover(){
      $('html:not(.ie7, .ie8, .ie9) #people-images .block img').hover(function(){
		$(this).attr('src', $(this).attr('alt'));
			},function(){
		$(this).grayscale();
	  });
	  $('.ie7 .person img, .ie8 .person img, .ie9 .person img').hover(
		  function () {
		    $(this).css({
		    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)'
		    });
		  },
		  function () {
		    $(this).css({
		    	'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
		    });
		  }
	);
}

function grayscaleImages(){
	$('.person img').grayscale();
	
	var swap = function() {
      var left = 'src';
      var right = 'original-src';
      var temp = $(this).attr(left);
      $(this)
        .attr(left, $(this).attr(right))
        .attr(right, temp);
    }
	
	$('html:not(.ie7, .ie8) #people-images .block img').hover(function(){
		$(this).attr('src', $(this).attr('alt'));
	},function(){
		$(this).grayscale();
	});
	
	$('html:not(.ie7, .ie8) .senior-show').hover(function() {
		$('html:not(.ie7, .ie8) .person.senior img').each(function(){
			$(this).attr('src', $(this).attr('alt'));
		});  	  
	}, function() {
		$('html:not(.ie7, .ie8) .person.senior img').each(function(){
			$(this).grayscale();
		});
	});
	
	$('.ie7 .senior-show, .ie8 .senior-show').hover(function() {
		$('.person.senior img').each(function(){
			$(this).css({
	    		'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)'
		    });
		});  	  
	}, function() {
		$('.person.senior img').css({
    		'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
	    });
	});
	
}

function openNews(){
	var theHash = document.location.hash;
	var substr = theHash.split('_');
	var valHash = substr[1];
	$('#' + valHash).parent().slideDown(500, function(){
			$('#' + valHash + ' .info .info-content').slideDown(500, function(){
				$('.enclosure').fadeIn();
			});
	});
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

function initialize() {
    var myOptions = {
      zoom: 15,
      center: new google.maps.LatLng(51.5099053, -0.1454383),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      styles: [{
			    featureType: "water",
			    stylers: [
			      { hue: "#000000" },
			      { saturation: -100 },
				  { lightness: -20 },
				  { gamma: 0 }
			    ]},
			    {
			    featureType: "road.arterial",
			    stylers: [
			      { hue: "#688AB8" },
			      { saturation: -55 },
				  { lightness: -20 },
				  { gamma: 0 }
			    ]},
			    {
			    featureType: "road.highway",
			    stylers: [
			      { hue: "#688AB8" },
			    ]},
			    {
			    featureType: "landscape.man_made",
			    stylers: [
			      { hue: "#FFFFFF" },
			      { saturation: -100 },
				  { lightness: -20 },
				  { gamma: 0 }
			    ]},
			    {
			    featureType: "poi.attraction",
			    stylers: [
			      { visibility: "on" },
			      { hue: "#688AB8" },
			      { saturation: -55 },
				  { lightness: -10 },
				  { gamma: 0 }
			    ] },
			    {
			    featureType: "poi.business",
			    stylers: [
			      { visibility: "off" }
			    ]},
			    {
			    featureType: "poi.government",
			    stylers: [
			      { visibility: "off" }
			    ]},
			    {
			    featureType: "poi.park",
			    stylers: [
			      { hue: "#688AB8" },
			      { saturation: 0 },
				  { lightness: -50 },
				  { gamma: 0 }
			    ]}
			  ]
    }
    
    var contentString = 
    '<div id="bubbleContent">'+
    '<img src="assets/images/content/exterior.jpg" style="float:right;" alt="Graphite " /><p>Graphite Capital Management LLP<br/>' +
    'Berkeley Square House <br/>'+
    'Berkeley Square <br/>'+
    'London W1J 6BQ <br/>'+
    'T: 020 7825 5300<br/>F: 020 7825 5399</p>' +
    '</div>';

	var infowindow = new google.maps.InfoWindow({
	    content: contentString
	});
    
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
 
    var image = 'assets/images/construct/mapicon.png';
    var shadow = new google.maps.MarkerImage("assets/images/construct/shadow-mapicon.png",
        new google.maps.Size(51.0, 43.0),
        new google.maps.Point(0, 0),
        new google.maps.Point(14.0, 40)
    );
    var myLatLng = new google.maps.LatLng(51.5099053, -0.1454383);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shadow: shadow
    });
    
    infowindow.open(map,marker);
}



