﻿
var stickyImg = '';

$(document).ready(function() {
	var ie6 = false;
	var ie7 = false;
	$.each($.browser, function(i, val) {
		if (i == 'msie' && $.browser.version.substr(0, 2) == '6.')
			ie6 = true;
		if (i == 'msie' && $.browser.version.substr(0, 2) == '7.')
			ie7 = true;
	});

	// bind module admin context menu
	$('.contextMenu').each(
		function(intIndex) {
			var elem = $(this);
			var menuId = elem.attr('id');
			var targetEl = elem.attr('target');
			$('#' + targetEl).contextMenu({
				menu: menuId
			},
			function(action, el, pos) {
				window.location.href = action;
			});
		});

	// case study list images expansion
	overThumbnailZoom = 1.05;
	$('.cs-item').hover(function() {
		var el = $(this).find('.cs-img');
		el.css({'top':'0px', 'left':'0px'});
		el.height(Math.ceil(el.height() * overThumbnailZoom));
		el.width(Math.ceil(el.width() * overThumbnailZoom));
		el = $(this).find('.cs-img-shadow');
		el.css({'top':'0px', 'left':'0px'});
		el.height(Math.ceil(el.height() * overThumbnailZoom));
		el.width(Math.ceil(el.width() * overThumbnailZoom));
	}, function() {
		var el = $(this).find('.cs-img');
		el.css({'top':'3px', 'left':'3px'});
		el.height(Math.floor(el.height() / overThumbnailZoom));
		el.width(Math.floor(el.width() / overThumbnailZoom));
		el = $(this).find('.cs-img-shadow');
		el.css({'top':'3px', 'left':'3px'});
		el.height(Math.floor(el.height() / overThumbnailZoom));
		el.width(Math.floor(el.width() / overThumbnailZoom));
		});
	
	$('#list-by-years a').each(
		function(intIndex) {
			$(this).hover(
				function() {
					$('#list-by-months div').each(
						function(intIndex) {
							$(this).slideUp("fast");
						}
					);
					$('#' + $(this).attr('id') + '-months').slideDown("slow");
				},
				function() {
				}
			);
		}
	);
	
	/* highlight news item on hover */
	$('.news-item').hover(function() {
		$(this).find('h2').addClass('broadstreet-dark-blue');
	}, function() {
		$(this).find('h2').removeClass('broadstreet-dark-blue');
		});
	
	// leadership hover fade
	$('.leader-item').each(
		function(intIndex) {
			$(this).hover(
				function() {
					$('.leader-item').not(this).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-item');
							$(this).fadeTo(0, 0.5);
						}
					);
					
					$('.leader-image').not($('#' + $(this).attr('id') + '-img')).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-image');
							$(this).fadeTo(0, 0.5);
						}
					);
				},
				function() {
					$('.leader-item').not(this).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-item', 1, function(){
								$(this).fadeTo(0, 1);
							});
						}
					);
					
					$('.leader-image').not($('#' + $(this).attr('id') + '-img')).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-image', 1, function(){
								$(this).fadeTo(0, 1);
							});
						}
					);
				}
			);
		}
	);
	
	$('.leader-image').each(
		function(intIndex) {
			$(this).hover(
				function() {
					$('.leader-item').not($('#' + $(this).attr('id').replace('-img',''))).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-item');
							$(this).fadeTo(0, 0.5);
						}
					);
					
					$('.leader-image').not(this).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-image');
							$(this).fadeTo(0, 0.5);
						}
					);
				},
				function() {
					$('.leader-item').not($('#' + $(this).attr('id').replace('-img',''))).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-item', 1, function(){
								$(this).fadeTo(0, 1);
							});
						}
					);
					
					$('.leader-image').not(this).each(
						function(intIndex) {
							$(this).doTimeout('backtolife-image', 1, function(){
								$(this).fadeTo(0, 1);
							});
						}
					);
				}
			);
		}
	);


	/* attach print */
//	var stylesheets = [];
	$('#print').click(function() {
//		$('link').each(
//			function(intIndex) {
//				// store original stylesheet link
//				stylesheets[intIndex] = $(this).attr('href');
//				// clear stylesheet link
//				$(this).attr('href', '/Data/style/empty.css');
//			}		
//		);
//		
//		$('head').append('<link type=\'text/css\' rel=\'stylesheet\' href=\'/Data/style/print.css\' />');
		window.print();
		return false;
	});
	
	// bind link modal
	$('#cs-linkbar #link').colorbox({width:'600px', inline:true, href:'#cs-link-modal', title:''});
	$().bind('cbox_complete', function(){ selectLinkText(); });
	// bind map modal
	$('.ext_modal').colorbox({ width: '97%', height: '90%', iframe: true });
	// bind help
	//$('.lightwindow').colorbox({ width: '750px', height: '600px', iframe: true });
	
	$("#ok-msg").slideDown("slow", function() { window.setTimeout(function() { $("#ok-msg").slideUp("slow", function() { $("#ok-msg").remove(); }); return false; }, 2500); });
});

// anti spam emails

var nye = 'NewYork';
var nyd = 'broadstreet.com';
var nyt = '+1 212 780 5700';
var uke = 'London';
var ukd = 'broadstreet.eu.com';
var ukt = '+44 (0)20 7298 0470';
var at = String.fromCharCode(64);
var str = '';

function contactDetails() {
	str = NewYorkEmailLink() + '&nbsp;&nbsp;&nbsp;&nbsp;' + nyt;
	str += '&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#ffffff">|</span>&nbsp;&nbsp;&nbsp;&nbsp;';
	str += LondonEmailLink() + '&nbsp;&nbsp;&nbsp;&nbsp;' + ukt;
	return str;
}

function askUsAQuestionLink() {
	str = '<a href="mailto:' + nye + at + nyd + '; ' + uke + at + ukd + '" id="ask-us-a-question" title="ask us a question"></a>';
	return str;
}

function NewYorkEmailLink()
{
	return '<a href="mailto:' + nye + at + nyd + '"><span class="eml-prefix">' + nye + '</span>' + at + nyd + '</a>';
}

function LondonEmailLink()
{
	return '<a href="mailto:' + uke + at + ukd + '"><span class="eml-prefix">' + uke + '</span>' + at + ukd + '</a>';
}

/* general */

/* trims text by returning the number words defined in length */
function trimtext(text, length) {
	var t = text.replace(/\s/g, ' ');
	var words = t.split(' ');
	if (words.length <= length) return text;
	var ret = '';
	for (var i = 0; i < length; i++) {
		ret += words[i] + ' ';
	}
	return ret;
}
