function SmoothScroll() {
	$('.scroll').click(function() {
	  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	  && location.hostname == this.hostname) {
		var $target = $(this.hash);
		$target = $target.length && $target
		|| $('[name=' + this.hash.slice(1) +']');
		if ($target.length) {
		  var targetOffset = $target.offset().top;
		  $('html,body')
		  .animate({scrollTop: targetOffset}, 1000);
		 return false;
		}
	  }
	});
}

function Accordian() {
	var togglers = $('.deepLinks h2');
	var elements = ".deepLinks .nav";
	$(elements).hide();
	togglers.click(
	function() {
		var checkElement = $(this).next();
			if((checkElement.is(elements)) && (checkElement.is(':visible'))) {
			checkElement.slideUp(500);
			return false;
		}
		if((checkElement.is(elements)) && (!checkElement.is(':visible'))) {
			$(elements + ':visible').slideUp(500);
			checkElement.slideDown(500);
			return false;
		}
	});
}

$(document).ready(function() {

	$(document).pngFix();
	SmoothScroll();
	Accordian();

	$("a[rel*='external']").attr({ target: "_blank" });

	var volatile = $(".field input")
	volatile.focus(function(event) {
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
		}
	}, volatile.blur(function(event) {
		if ($(this).val() == "") {
			$(this).val($(this).attr("title"));
		}
	}
    ));

	// Now get the font changed: http://cufon.shoqolate.com/generate/
	Cufon.replace('h1, .topNav, .deepLinks h2, #rightCol .section .more, #content .featured h2, #content .category h2, #content .articleList h2, #content .contact h2, .caseLink, #keyPersonnel h2 span, h2, highlightedBlock h3, .monoBasic, .monoLeft, .button, .viewMore, .monoRight', { fontFamily: 'SunSansLight', hover: true });

	Cufon.replace('#rightCol h2, .articleSection h2', { fontFamily: 'SignPainter', hover: true });
	Cufon.replace('.titles span', { fontFamily: 'SignPainter', hover: true });


	//Custom selectboxes
	$('#content .contact .select select').selectmenu({ style: 'dropdown', width: '299px', maxHeight: 200 });
	$('#content .contact .dateSelect select').selectmenu({ style: 'dropdown', width: '93px', maxHeight: 200 });
	$('#rightCol .filter select').selectmenu({ style: 'dropdown', width: '300px', maxHeight: 200 });
	$('#content .manage select').selectmenu({ style: 'dropdown', width: '160px', maxHeight: 200 });

	//Custom Checkboxes
	$('input').customInput();

	// Clients Filter

	$('.hidden').hide();
	$('#rightCol .filter').show();

	$('.filter .custom-checkbox input').click(function() {
		if ($('.filter .custom-checkbox input').is(':checked') == true) {
			var newValue = $(this).val();
			$('.filter .hidden input').val(newValue);
			$(".filter").trigger('change');
		}
	});

	hideAllSubFilters()
	function hideAllSubFilters() {
		$('.filter .businessObjectives').hide();
		$('.filter .verticalMarket').hide();
		$('.filter .audience').hide();
		$('.filter .tactic').hide();
	}

	$('#filterCategory').change(function() {
		var newValue = 'all';

		if ($('#filterCategory').val() == "all") {
			$('.filter .businessObjectives').show();
			$('.businessObjectives label').removeClass('checked');
			$('.all label').addClass('checked');
			$('.filter .hidden input').val(newValue);
			$(".filter").trigger('change');
		}

		if ($('#filterCategory').val() == "businessObjectives") {
			$('.filter .businessObjectives').show();
			$('.businessObjectives label').removeClass('checked');
			$('.all label').addClass('checked');
			$('.filter .hidden input').val(newValue);
			$(".filter").trigger('change');
		} else {
			$('.filter .businessObjectives').hide();
		}

		if ($('#filterCategory').val() == "verticalMarket") {
			$('.filter .verticalMarket').show();
			$('.verticalMarket label').removeClass('checked');
			$('.all label').addClass('checked');
			$('.filter .hidden input').val(newValue);
			$(".filter").trigger('change');
		} else {
			$('.filter .verticalMarket').hide();
		}

		if ($('#filterCategory').val() == "audience") {
			$('.filter .audience').show();
			$('.audience label').removeClass('checked');
			$('.all label').addClass('checked');
			$('.filter .hidden input').val(newValue);
			$(".filter").trigger('change');
		} else {
			$('.filter .audience').hide();
		}

		if ($('#filterCategory').val() == "tactic") {
			$('.filter .tactic').show();
			$('.tactic label').removeClass('checked');
			$('.all label').addClass('checked');
			$('.filter .hidden input').val(newValue);
			$(".filter").trigger('change');
		} else {
			$('.filter .tactic').hide();
		}

	});

	//Twitter Pod
	if ($('#twitter_div').length > 0) {
		var script = document.createElement('script');
		script.src = 'http://twitter.com/javascripts/blogger.js';
		script.type = 'text/javascript';

		var script2 = document.createElement('script');
		script2.src = 'http://twitter.com/statuses/user_timeline/' + $('#twitter_div').attr('rel') + '.json?callback=twitterCallback2&count=2';
		script2.type = 'text/javascript';

		document.getElementsByTagName('head')[0].appendChild(script);
		document.getElementsByTagName('head')[0].appendChild(script2);
	}

	$('#aboutUs').empty();
	$('#aboutUs').flash({
		src: '/_images/flash/about-us.swf',
		width: 960,
		height: 355
	});
	$('#caseStudyVideoHolder').empty();
	$('#caseStudyVideoHolder').flash({
		src: '/_images/flash/video-player.swf',
		width: 640,
		height: 345,
		flashvars: { imagePath: "/_images/flash/blank.gif", videoPath: "/_videos/disney.flv" }
	});

	// CarouFredSel

	var slideCounter = $('#bannerSlider li').size();
	$("#bannerSlider li:not(:first) a").attr("tabIndex", "-1");
	$("#bannerSlider").carouFredSel({
		items: 1,
		scroll: {
			pauseOnHover: true,
			easing: "easeInOutExpo",
			duration: 1000,
			onBefore: function(oldItems, newItems) {
				oldItems.children('a').attr("tabIndex", "-1");
				newItems.children('a').removeAttr("tabIndex");
			}
		},
		auto: 5000,
		pagination: {
			container: ".carouselNav ul",
			anchorBuilder: function(nr) {
				var str = '<li><a href="#">';
				str += 'Slide' + ' ' + nr + ' ' + 'of' + ' ' + slideCounter;
				str += '</a></li>';
				return str;
			}
		}
	});

	function VideoIsPlaying(boolean) {
		// IF video is playing Pause
		if (_videoIsPlaying == true) {
			$("#bannerSlider").trigger("pause");
		} else {
			$("#bannerSlider").trigger("play");
		}
	}

	//Lightbox
	$(".lightbox").each(function() {
		$(this).ariaLightbox({
			pos: "auto",
			em: false,
			zIndex: "9999",
			titleText: getLightboxTitle(this),
			makeHover: false,
			disableWidth: 1,
			disableHeight: 1,
			descText: function() {
				return $(this).find("img").attr("rel");
			}
		});
	});

	function getLightboxTitle(link) {
		var img = $(link).find("img");
		return (img.attr("alt")) ? img.attr("alt") : 'Image';
	}


});
