var currentSection;
var currentBkgImage = 0;

var images = [
	'images/image1.jpg',
	'images/image2.jpg',
	'images/image3.jpg',
	'images/image4.jpg',
	'images/image5.jpg',
	'images/image6.jpg',
	'images/image7.jpg',
	'images/image8.jpg',
	'images/image9.jpg'
];
var imgcnt = 0;
var image;

var sections = [
	'who-we-are',
	'what-we-do',
	'strategies',
	'our-heritage',
	'what-sets-us-apart',
	'investing-with-us',
	'contacts',
	'principals',
	'terms-of-use',
	'privacy-policy'
];

var sectionIndex = function(section) {
	var i = 0;
	while ( section != sections[i] && i < 8) {
		i++;
	};
	return i;
}



function isiPad(){
    return (navigator.platform.indexOf("iPad") != -1);
}
function isiPhone(){
    return (
        //Detect iPhone
        (navigator.platform.indexOf("iPhone") != -1) ||
        //Detect iPod
        (navigator.platform.indexOf("iPod") != -1)
    );
}
function isDroid(){
    if( navigator.userAgent.match(/Android/i)   ) {
        return true;
    }
}
function isBlackberry(){
    if( navigator.userAgent.match(/BlackBerry/i)   ) {
        return true;
    }
}


$(window).load(function() {
	if ( isBlackberry() ) {		
		$('body').addClass("blackberry");

		/* Pages tweaks */
		var endCurrentSection = function() {
			if ( currentSection ) {
				$('div#' + currentSection).append($('#content').children());
			}					
		}

		var showSection = function(section) {
			if ( section == currentSection ) return;

			endCurrentSection();

			$('#content').html(
				$('div#'+section).children()
			);

			currentSection = section;

			var nextImage;
			var idx = sectionIndex(section);
			if ( imgcnt >= idx ) {
				nextImage = idx;
				//while ((nextImage = Math.round(imgcnt*Math.random())) == currentBkgImage);
			} else {
				nextImage = currentBkgImage;
			}

			currentBkgImage = nextImage;
		}

		$('.menu dd a').each( function(idx, el) {
			$(el).click(function(e) {
				e.preventDefault();
				var id = $(this).attr('class');
				showSection(id);
				$('#scroller')[0].scrollTop = 0;
				$('.menu dd').each( function(idx, elem) {
					$(elem).removeClass('active');
				});
				$(this).parent().addClass('active');
			});
		});

		if ( window.location.search && window.location.search != '' ) {
			var reqSection = window.location.search.split('=')[1];
			$('.menu dd:nth-child(' + reqSection + ') a').each( function(idx, el) {
				$(el).click();
			});
		} else {
			$('.menu dd:first a').each( function(idx, el) {
				$(el).click();
			});
		}

		$('.login-button').click(function(e) {
			e.preventDefault();
			showSection('login-box');
			$('.menu dd').removeClass('active');
		});

		$('a.terms-of-use, a.privacy-policy').each( function(idx, el) {
			$(el).click(function(e) {
				e.preventDefault();
				showSection($(el).attr('class'));
				$('.menu dd').each( function(idx, elem) {
					$(elem).removeClass('active');
				});
			});
		});

		$('.sc-content-principalsRegular1En p a, .sc-content-principalsRegular1Jp p a, .sc-content-principalsRegular1De p a').each( function(idx, el) {
			$(el).click(function(e) {
				var href = $(el).attr('href');
                // THIS NEVER WORKED
				if ( href.substring(0, 1) == "#" ) { 
					e.preventDefault();
					var id = href.substring(1);
					var target = $('#' + id);
					var targetY = target.offset().top;
                    $(document).scrollTop(targetY+240);
					//$('#scroller')[0].scrollTop = targetY - 240;
				}
			});
		});		
		
	} 
});

$(function(){
	
	if ( !isBlackberry()  && !nojs) {
		$('body').addClass("not-blackberry");
		
		/* Homepage tweaks */
		Cufon.replace('#languages-not a', {
			hover: true
				      });
		
		$("#big-logo").fadeTo(600, 1.0);
		
		setTimeout(function() {
			       $('#home-menu').fadeTo(600, 1.0);
			   }, 200);
		
		setTimeout(function() {
			       $('.footnote').fadeTo(600, 1.0);
			   }, 400);
		
		setTimeout(function() {
			       $('#languages').fadeTo(600, 1.0);
			   }, 600);
		
		$('#home-menu dd a').bind('click', function(e) {
					      var self = this;
					      $('#big-logo, #home-menu, .footnote, #languages').fadeTo(300, 0.0);
					      setTimeout(function() { document.location = $(self).attr('href'); }, 350);
					      e.preventDefault();
					  });
		
		$('body').supersleight({shim: '/images/x.gif'});


		/* Pages tweaks */
		var wh = $(window).height();
		if ( wh < 760 ) wh = 760;
	
		$('#bgr-image div img')
			.hide()
			.css('height', wh);
	
		function correctPositions() {
			var wh = $(window).height();
			var bh = $('body').height();
			var hh = $('html').height();
		
			var ww = $(window).width();
		
			// alert("body height " + bh + ", window height " + wh + ", html height " + hh);
		
			if ( wh < 760 ) wh = 760;					
            if(isiPhone() || isiPad() || isDroid()){
                var ch = $("#content").outerHeight()+64;
	    		$('#bgr-image div img').css('height', ch);
    			$('#background, #background #panel, #background .shadow').css('height', ch);
            }
            else{
	    		$('#bgr-image div img').css('height', wh);
    			$('#background').css('height', wh);
            }
		
			// console.log('ww is ' + ww);
		}
	
		$(window).bind('resize', function() {
			setTimeout(function() {
				correctPositions();
			}, 1);
		});
		correctPositions();
	
	
	
		var endCurrentSection = function() {
			if ( currentSection ) {
				$('div#' + currentSection).append($('#content').children());
			}					
		}
	
		var showSection = function(section) {
			if ( section == currentSection ) return;
		
			endCurrentSection();
		
			$('div#'+section).find('> *, img, li').css('opacity', '0');
		
			$('#content').append(
				$('div#'+section).children()
			);
			$('#content').find('> *, img, li').animate({opacity:1.0}, 400, 'swing', function() {
				if(!$.support.opacity)
					$(this).get(0).style.removeAttribute('filter');
			});
		
			currentSection = section;
		
			var nextImage;
			var idx = sectionIndex(section);
			if ( imgcnt >= idx ) {
				nextImage = idx;
				//while ((nextImage = Math.round(imgcnt*Math.random())) == currentBkgImage);
			} else {
				nextImage = currentBkgImage;
			}

			$('#b' + currentBkgImage + ' img').show();
			$('#i' + currentBkgImage + ' img').hide();
			$('#i' + nextImage + ' img').show();
		
			if(!$.support.opacity)
				$('#b' + currentBkgImage + ' img').css('filter', 'alpha(opacity=100)');
			$('#b' + currentBkgImage + ' img').fadeOut(250);
		
			currentBkgImage = nextImage;
		
		
			// IE fix for homepage margin set
		  	if ($.browser.msie && $.browser.version < 7) {
				$('body').supersleight({shim: '/images/x.gif'});
			
				setTimeout(function() {
					correctPositions();
				},500);
			}else if(isiPhone() || isiPad() || isDroid()){
				setTimeout(function() {
					correctPositions();
				},500);
            }
            $(document).scrollTop(0);
		}
	
		$('.menu dd a').click(function(e) {
			e.preventDefault();
			var id = $(this).attr('class');
			showSection(id);
			$('#scroller')[0].scrollTop = 0;
			$('.menu dd').removeClass('active');
			$(this).parent().addClass('active');
		});
	
		if ( window.location.search && window.location.search != '' ) {
			var reqSection = window.location.search.split('=')[1];
			$('.menu dd:nth-child(' + reqSection + ') a').click();
		} else {
			$('.menu dd:first a').click();
		}

		$('.login-button').click(function(e) {
			e.preventDefault();
			showSection('login-box');
			$('.menu dd').removeClass('active');
		});
	
		$('a.terms-of-use, a.privacy-policy').click(function(e) {
			e.preventDefault();
			showSection($(this).attr('class'));
			$('.menu dd').removeClass('active');
		});
	
		$(window).load(function() {
			$('#bgr-image div').css('visibility', 'visible');
			imgcnt = 9;
		});
	
		$(document).ready(function() {		
			correctPositions();
		
			// IE fix for homepage margin set
		  	if ($.browser.msie && $.browser.version < 7) {
				setTimeout(function() {
					correctPositions();
					$('body').supersleight({shim: '/images/x.gif'});
				},500);
			}
		});

		$('.sc-content-principalsRegular1En p a, .sc-content-principalsRegular1Jp p a, .sc-content-principalsRegular1De p a').click(function(e) {
			var href = $(this).attr('href');

			if( (start = href.indexOf( '#' )) >= 0 ) {
				//e.preventDefault();
				var id = href.substr( (start + 1), 2 ); /* id is 2 characters long */
				var target = $('#' + id);
				var targetY = target.offset().top;
				//$(document).scrollTop(targetY - 240);
			}
		});
	}
});

