// Mister F!
/*@cc_on 
	@if (@_win32)
		try {document.execCommand('BackgroundImageCache', false, true);}catch(e){}
	@end
	@if (@_jscript_version <= 5.6)
		(function pngFix (gif, arr) {
		       var k = arr.length, el, es;
		       while (k--) {
		           if (el=document.getElementById(arr[k])) {
		               es=el.style;es.height=el.height;es.width=el.width;es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='"+el.src+"',sizingMethod='image')";el.src=gif;
		           }
		       }
		   })("/assets/images/blank.gif",['logo-gcp','gcp-map-icon']);
	@end
@*/
var _isIE = /*@cc_on!@*/ false;
var _isLteIE7 = /*@cc_on @*/ /*@if (@_jscript_version <= 5.6) true; /*@end @*/ false;

// $('img.photo',this).imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images

// mit license. paul irish. 2010.

// callback function is passed the last image to load
//   as an argument, and the collection as `this`

/*
 * Special event for image load events
 * Needed because some browsers does not trigger the event on cached images.

 * MIT License
 * Paul Irish     | @paul_irish | www.paulirish.com
 * Andree Hansson | @peolanha   | www.andreehansson.se
 * 2010.
 *
 * Usage:
 * $(images).bind('load', function (e) {
 *   // Do stuff on load
 * });
 * 
 * Note that you can bind the 'error' event on data uri images, this will trigger when
 * data uri images isn't supported.
 * 
 * Tested in:
 * FF2-3.6
 * IE6-8
 * Chromium4 Developer
 * Opera 9-10
 */ 
// (function ($) {
// 
// $.event.special.load = {
// 	setup: function(data, namespaces, hollaback) {
// 		var retVal = false;
// 		
// 		if (this.tagName.toLowerCase() === 'img' && this.src !== "") {
// 			// Image is already complete, fire the hollaback (fixes browser issues were cached
// 			
// 			// images isn't triggering the load event)
// 			if (this.complete || this.readyState === 4) {
// 				$(this).bind('load', data || {}, hollaback).trigger('load');
// 				retVal = true;
// 			}
// 			
// 			// Check if data URI images is supported, fire 'error' event if not
// 			else if (this.readyState === 'uninitialized' && this.src.indexOf('data:') >= 0) {
// 				$(this).trigger('error');
// 				retVal = true;
// 			}
// 		}
// 		
// 		return retVal;
// 	}
// }
// 
// }(jQuery));


/********************
* Lazy loading
********************/
var equalHeightColumns = {
	init : function(){
		var content = $('#content');
		var articleHeight = $('article', content).outerHeight();
		var asideHeight = $('aside', content).outerHeight();
		var highestCol = Math.max(articleHeight,asideHeight);
		$('article', content).height(highestCol);
	}
}
/********************
* Map
********************/
var Map = {
	init : function() {
		var latlng = new google.maps.LatLng(51.5113901,-0.1480826);
		var options = {
		  zoom: 15,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP,
		  //disableDefaultUI: false,
		  mapTypeControl: false,
		  mapTypeControlOptions: {
		  	style: google.maps.MapTypeControlStyle.DEFAULT},
		  scaleControl: false,
		  navigationControl: true,
		  navigationControlOptions: {
		  	style: google.maps.NavigationControlStyle.SMALL},
		  keyboardShortcuts: false					  
		}; 
		map = new google.maps.Map(document.getElementById('map'), options);
		var marker = new google.maps.Marker({
			clickable: false,
			position: new google.maps.LatLng(51.5113901,-0.1480826),
			map: map,
			icon: '/assets/images/marker.png'
		});
	}
}
/********************
* Fade in images
********************/
var ImageFader = {
	init : function(){
		var holders = $('.holder', '#content').css('display','none');
		holders.each(function(index, el) {
			var d = new Date();
			var md = d.getUTCMilliseconds();
			var t = (d * Math.random());

			var holder = $(el),
			Img = $('img', holder).css('display', 'none');
			oSrc = holders.attr('src')
			$(Img).load(function() {
				$(this).appendTo(holder)
			}).animate({opacity: "show", duration: 500, easing : 'easeOutCirc'});
			
		}); 
	}
}
/********************
* Full screen backgrounds
********************/
var SlideReveal = {
	init : function(){
		var articles = $("article", '#content');
		var headers = $('hgroup.accessibility', '#header');
		articles.each(function(index, el) {
			var article = $(el);
			// stupid IE won't play nicely with mulitiple class names...
			var oClass = article[0].className.split(' ')[0];
			var nClass = oClass + '-hover';
			article.bind({
				toggleState : function(e){
					if (!$('#page-home').length) {
						headers.eq(index).toggle();
					};
				},
				mouseenter : function(e) {
					article.dequeue().stop().animate({height: '250px'}, 300, 'easeOutQuad')
					.addClass(nClass)
					.trigger('toggleState');
				},
				mouseleave : function(e) {
					article.animate({height: '215px'}, 300, 'easeOutSine')
					.removeClass(nClass)
					.trigger('toggleState');
				},
				click : function(e) {
					var link = $('footer a', this)[0];
					window.location.href = link.href;
				}
			});
		});
	}
}
var pressTitle = {
	init : function(){
		var articles = $("article", '#content');
		var pressheader = $('header.press-header');
		articles.each(function(index, el) {
			var article = $(el);
			article.bind({
				toggleState : function(e){
					pressheader.find('.first').toggle();
					pressheader.find('div').eq(index+1).toggle();
				},
				mouseenter : function(e) {
					$(this).addClass('hover').trigger('toggleState');
				},
				mouseleave : function(e) {
					$(this).removeClass('hover').trigger('toggleState');
				}
			});
		});
	}
}
/********************
* Sliding people
********************/
var SlideRevealPeople = {
	init : function(){
		var articles = $("article", '#content');
		articles.each(function(index, elem) {
			var article = $(elem);
			article.bind({
				mouseenter : function(e) {
					var el = $('footer', this);
					el.dequeue().stop().animate({
						top: '198px'
					}, 500, 'easeOutQuad');
					$(this).addClass('hover');
				},
				mouseleave : function(e) {
					var el = $('footer', this);
					el.animate({
						top: '235px'
					}, 500, 'easeOutSine');
					$(this).removeClass('hover');
				},
				click : function(e) {
					var link = $('footer a', this)[0];
					window.location.href = link.href;
				}
			});			
		});
	}
}
/********************
* Main nav rollover animation
********************/
var navRollover = {
	init : function(){
		navItems = $("#main-nav a").not('.active');
		var oColour = '#4C3D2A',
		nColour = '#000000',
		bgOpos = '50% -889px',
		bgNpos = '50% -879px';
		navItems.each(function(index, el) {
			var navItem = $(el);			
			navItem.bind({
				mouseenter : function(e) {
					navItem.dequeue().stop().animate({
						backgroundPosition:bgNpos},
						{duration:150}
					)
					.css({color : nColour})
				},
				mouseleave : function() {
					navItem.animate({
						backgroundPosition:bgOpos},
						{duration:500}
					)
					.css({color : oColour})
				}				
			});
		});
	}
}
/********************
* Full screen backgrounds
********************/
var backgroundAdjust = {
	init : function(){
		var bgDiv = $('<div id="fs-background"><div id="fs-background-holder"/></div>').prependTo('body');
		/*@cc_on
		   /*@if (@_jscript_version > 5.6)
				$('#fs-background-holder').npFullBgImg(backgroudPath, {fadeInSpeed: 300, center: false});
		   	@elif (@_jscript_version <= 5.6)
				$('#fs-background-holder').npFullBgImg(backgroudPath, {fadeInSpeed: 300, center: false});					
			@else @*/
			if ((screen.width>=800) && (screen.height>=600))
			{
				//alert('Screen size: 1024x768 or larger');
				$('#fs-background-holder').npFullBgImg(backgroudPath, {fadeInSpeed: 300, center: true});
			}
			else
			{
				//$(document.body).css('width','800px');
				$(document.body).css('background','url(' +  backgroudPath + ')');
			}
			
		   /*@end
		@*/		
		
	}
}
var redirectDisclaimer = {
	init : function(){
		var cookie_name = "agreed";
		
		$("input.radio").bind('click',function () { 
			var state = $(this).val();
			//$.cookie(cookie_name, state);
			window.location.href = '/investor-relations/secure-reports/?agree='+ state;
		});
	}
}
/********************
* Load map
********************/
if(document.getElementById('map')){
	if (!_isIE) {
		Map.init()	
	};
};
/********************
* Lazy loading
********************/
$(function() {
	if ($('#page-home, #page-funds.top-level, #page-investor-relations.top-level, #page-404').length) {
		$.ajax({
			type: "GET",
			url: '/assets/javascript/jquery.easing.1.3.js',
			cache: true,
			dataType: "script",
			success: function () {
				//ImageFader.init();
				SlideReveal.init();
			}
		});
	};
	if ($('#page-our-people .index').length) {
		$.ajax({
			type: "GET",
			url: '/assets/javascript/jquery.easing.1.3.js',
			cache: true,
			dataType: "script",
			success: function () {
				//ImageFader.init();
				SlideRevealPeople.init();
			}
		});
	};
	if ($('#page-press').length) {
		pressTitle.init();
	};
	if ($('#tabs').length) {
		$('li', '#tabs').not('.active').bind('mouseenter', function(event) {
			$(this).addClass('active');
		}).bind('mouseleave', function(event) {
			$(this).removeClass('active');
		});
		$('li', '#tabs').bind('click', function(event) {
			var link = $('a', this);
			window.location.href = link.attr('href');
		});
	};	
	if (!_isLteIE7) {
		$.ajax({
	      type: "GET",
	      url: '/assets/javascript/jquery.backgroundPosition.js',
	      cache: true,
	      dataType: "script",
	      success: function () {
	        navRollover.init();
	      }
	    });
	};
	$.ajax({
		type: "GET",
		url: '/assets/javascript/jquery.npFullBGImg.js',
		cache: true,
		dataType: "script",
		success: function () {
			backgroundAdjust.init();
		}
	});
	if($('#content.generic-layout').length){
		equalHeightColumns.init();
	}
	if($('#page-legal-disclaimer').length){
		redirectDisclaimer.init();
	}
});
