
loadFlash = function(loadDiv,swfPath) {
	var swfWidth = $(loadDiv).css('width');
	var swfHeight = $(loadDiv).css('height');
	$(loadDiv).flash({swf:swfPath,height: swfHeight,width: swfWidth,wmode: 'transparent'});
}

loadTwitter = function(twitterName) {

	new TWTR.Widget({
		  id: "tweets",
		  version: 2,
		  type: 'profile',
		  rpp: 4,
		  interval: 6000,
		  width: 310,
		  height: 300,
		  theme: {
			shell: {
			  background: '#ffffff',
			  color: '#f50c0c'
			},
			tweets: {
			  background: '#ffffff',
			  color: '#363636',
			  links: '#cc0606'
			}
		  },
		  features: {
			scrollbar: false,
			loop: false,
			live: false,
			hashtags: false,
			timestamp: true,
			avatars: true,
			behavior: 'all'
		  }
		}).render().setUser(twitterName).start();
}

getStyleSheetVal = function(element,id,classname,property) {
	var $el = $("<"+element+" id="+id+" class="+classname+"></"+element+">").hide().appendTo("body");
    var $val = $el.css(property);
    $el.remove();
    return $val;
}

ajaxLoad = function(element,href) {

	if(typeof href != 'undefined') {
		var path = href.replace('#!/','');
		page = path.split('/');
		ajaxPagePreload(page);
		$(element).load('ajax/'+path,'',ajaxPageOnload);
		ajaxPagePostload(page);
	}
	
}

checkAjaxUrl = function() {
	if(typeof window.location.pathname != 'undefined') {
		if(window.location.pathname.substr(0,3) != '#!/' && window.location.pathname != '/') {
			window.location = 'http://' + window.location.host + '/#!' + window.location.pathname;
		}
	}
	
}


setFlashPath = function(page) {
	var swfPath = '/templates/version1/swf/';
	if (page[0] == 'about-us' || page[0] == 'governance') {
		var swf = 'pylons';
	} else if(page[0] == 'our-typical-investments' || page[0] == 'awards') {
		var swf = 'windmill';
	} else if(page[0] == 'the-team' || page[0] == 'investor-login') {
		var swf = 'landing';
	} else {
		var swf = 'road';
	}
	animationPath = swfPath + swf + '.swf';
}

setMetadata = function(page) {

	var meta_title = 'BBIP';
	if(page != '') {
	var meta_title = meta_title + ' | ' + page[0].charAt(0).toUpperCase() + page[0].slice(1);
	}
	document.title = meta_title;
}

ajaxPagePreload = function(page) {

	setMetadata(page);
	setFlashPath(page);
	$('div.main_nav').children('ul').children('li').children('a').removeClass('selected');
	$('#nav_'+page[0]).addClass('selected');
	//var pointerMargin = getStyleSheetVal('img','pointer','set_'+page[0],'marginLeft').replace('px','');
}

ajaxPagePostload = function(page) {

	//$('div#content_area').animate({opacity: 1}, {queue: false, duration: 'slow'});
	//$("div#content_holder").animate({marginTop: '0'});
}

loadAjaxPage = function() {
	
	if (window.location.hash != "") {
		ajaxLoad("div#content_area",window.location.hash);
	} else {
		page = new Array();
		page[0] = 'about-us';
		ajaxPagePreload(page); 
		ajaxPagePostload(page);
	}
}

var current_hash = window.location.hash;

check_hash = function () {
    if ( window.location.hash != current_hash) {
        current_hash = window.location.hash;
        ajaxLoad("div#content_area",current_hash);
    }
}

hashCheck = function() {
	setInterval( "check_hash()", 50 );
}

setCufon = function() {
	Cufon.replace('a.cufon',{hover: 'true'})('h1.cufon',{hover: 'true'});
}

setBulletSlides = function() {
	$(".click_slide").click(function(){
		var clickDiv = $(this).attr('id');
		var slideDiv = '#'+clickDiv+'_slide';
		if($(slideDiv).css('display') == 'none') {
			$(slideDiv).slideDown();
		} else {
			$(slideDiv).slideUp();
		}
	});
}

ajaxPageOnload = function() {
	setGallery();
	//if (page == 'news') { loadTwitter('m8Capital');}
	
	setCufon();
	loadFeature();
	if (page == 'our-typical-investments') { setBulletSlides();} else {loadFlash('#main_swf',animationPath);}
	
	$(window).resize(function() {
		$('.body').css('height','100%');
	});
	hashCheck();
	moreLinks();
	ajaxifyLinks();
}

ajaxifyLinks = function() {

	$('a.ajax').each(function() {
		if($(this).attr('href').indexOf('#!/') == -1) {
			domainStrippedUrl = $(this).attr('href').replace('http://'+location.hostname,'');
			$(this).attr('href','#!'+domainStrippedUrl);
		}
	});
	
	$('a.ajax').click(function(){
		ajaxLoad($(this).attr('href'));
	});
}

moreLinks = function() {
	$("a.more").click(function(){
		$(this).parent().children('.more_text').slideDown();
		$(this).html('less &uarr;');
		$(this).click(function(){
			$(this).parent().children('.more_text').slideUp();
			$(this).html('more &darr;');
		});
		
		return false;
	});
}

scrollMore = function(wrapperDiv,wrapperDivHeight,scrollDiv,scrollDivHeight){
		$(wrapperDiv).css('height',wrapperDivHeight);
		$(scrollDiv).css('overflow','hidden');
		$(scrollDiv).html('<div class="scroll_arr" style="display:none;"><a href="javascript:void();" class="uarr" style="opacity: 0;"><img src="/templates/version1/css/images/spacer.gif" title="Scroll Up"/></a></div><div class="scroller"><div class="scroll_content">'+$(scrollDiv).html()+'</div></div><div class="scroll_arr" style="display:none;"><a href="javascript:void();" class="darr"><img src="/templates/version1/css/images/spacer.gif" title="Scroll Up"/></a></div>');
		$(scrollDiv).children('.scroller').css('height',scrollDivHeight);
		scrollDivOverflow = parseInt(scrollDivHeight) - parseInt($(scrollDiv).children('.scroller').children('.scroll_content').css('height'));
		if (scrollDivOverflow < 0) {$(".scroll_arr").css("display","inline");}
		$(scrollDiv).children('.scroller').css('overflow','hidden');
		var sd = $(scrollDiv).children('.scroller').children('.scroll_content');
		$(scrollDiv).children('.scroll_arr').children('.uarr').mousedown(function(){
				if(parseInt(sd.css('marginTop')) < 0) {
					sd.everyTime ( 10, function (){
							sd.animate({marginTop:'+=50'},{
								step: function(now, fx) {
									if(parseInt(sd.css('marginTop'))> 0) {
										$(".uarr").css("opacity","0");
										sd .stop (true) .stopTime ();
									}
								},
								duration: '200',
								easing:'linear'
							});
					});
				}
		});
		$(scrollDiv).children('.scroll_arr').children('.uarr').mouseup(function(){
			sd .stop (true) .stopTime ();
		});
		$(scrollDiv).children('.scroll_arr').children('.darr').mousedown(function(){
				if(parseInt(sd.css('marginTop')) > scrollDivOverflow) {
				$(".uarr").css("opacity","1");
					sd.everyTime ( 10, function (){
							sd.animate({marginTop:'-=50'},{
								step: function(now, fx) {
									if(parseInt(sd.css('marginTop')) < scrollDivOverflow) {
									$(".uarr").css("opacity","1");
										sd .stop (true) .stopTime ();
									}
								},
								duration: '200',
								easing:'linear'
							});
					});
				}
		});
		$(scrollDiv).children('.scroll_arr').children('.darr').mouseup(function(){
			sd .stop (true) .stopTime ();
		});
		
		
		return false;
}

loadFeature = function() {
			if($('#main_box_content').length > 0) {
 			scrollMore('#main_box','378px','#main_box_content','190px');
			Cufon.refresh();
			window.scroll(0,0);
			}
		}

loadMainImage = function(imageIndex) {
		
		
		}

function setGallery() {
	
	nextImgIndex = 1;
	
	setImage = function(imageIndex) {
			
			var nthImageIndex = parseInt(imageIndex)+1;
			
			if($('.gallery_thumbs a:nth-child('+nthImageIndex+')').next().length == 0) {
			nextImgIndex = 0;
			} else {
			nextImgIndex = nextImgIndex+1;
			}
			var largePath = $('.gallery_thumbs a:nth-child('+nthImageIndex+')').attr('href');
			var title = $('.gallery_thumbs a:nth-child('+nthImageIndex+')').children('img').attr('title');
			
			
			$('#image_label').html(title);
			
			currentImgIndex = imageIndex;
			
			var img = '<img src="'+largePath+'"/>';
			$('#image_box').html('');
			$('#image_box').append(img);
	}
	
	$('.gallery_thumbs a').click(function(){
			setImage($('.gallery_thumbs a').index($(this)));
			return false;
		});
	
	$(' a.gallery_next').click(function(){
			setImage(nextImgIndex);
			return false;
		});
	
	$(' a.gallery_next').mouseover(function(){
			$(this).animate({ opacity: 1 });
		});
	$(' a.gallery_next').mouseout(function(){
			$(this).animate({ opacity: 0 });
		});
		
	setImage(0);
}

function setBioLinks() {
	
	$('.bio_links li a').click(function(){
				var bio = $(this).attr('href');
				loadBio(bio);
				return false;
			});
}

function validateLogin() {
	var err = '';
	if (Pass.value == "") {
		err = 'Please enter your password';
	}
	if (User.value == "") {
		err = 'Please enter your username';
	}
	if (!err == '') {
		$("#LoginError").text(err);
		return false;
	}
	return true;
	
}

function validateSignUp() {
	var err = '';
	if (ConfirmPass.value != Pass.value) {
		err = 'Password and Retyped password do not match please confirm and re-enter';
	}
	if (ConfirmPass.value == "") {
		err = 'Please retype your password to confirm';
	}
	if (Pass.value == "") {
		err = 'Please enter your password';
	}
	if (ConfirmEmail.value != Email.value) {
		err = 'Password and Retyped password do not match please confirm and re-enter';
	}
	if (ConfirmEmail.value == "") {
		err = 'Please retype your email to confirm';
	}
	if (Email.value == "") {
		err = 'Please enter your email';
	}
	if (Surname.value == "") {
		err = 'Please enter your surname';
	}
	if (FirstName.value == "") {
		err = 'Please enter your first name';
	}
	if (!err == '') {
		$("#SignupError").text(err);
		return false;
	}
	return true;
	
}

function log(data) {
	console.log(data);
}

function removePX(str) {
	return str.substr(0, str.length - 2);
}

$(document).ready(
	function() {
		loadAjaxPage();
		ajaxPageOnload();
	
    whipitup();

});
