$(function() {

	$('a.target_blank').each(function() {
		$(this).click(function(){window.open(this.href);return false;});
	});
	
	// hide previous news
	$('a#news-2008').click(function(){
		if ($('div#news-2008-items').css('display')=='block') {
			$('div#news-2008-items').slideUp("fast");
			$('a#news-2008').text('show');
		} else {
			$('div#news-2008-items').slideDown("fast");
			$('a#news-2008').text('hide');
		}
		return false;
	});
	$('a#news-2007').click(function(){
		if ($('div#news-2007-items').css('display')=='block') {
			$('div#news-2007-items').slideUp("fast");
			$('a#news-2007').text('show');
		} else {
			$('div#news-2007-items').slideDown("fast");
			$('a#news-2007').text('hide');
		}
		return false;
	});
	$('a#news-2008').text('show');
	$('a#news-2007').text('show');

	// case study popups
	$(document).resize(function() { alert(1);
		$("#blanket").height($(window).height());
		$("#blanket").width($(window).width());
	});
	$('a.case-visit').click(function() {
		$("#nav").css("z-index","0");
		$("#blanket").fadeTo("fast",0.1, function() {
			$(this).show();$(this).fadeTo("slow",0.8, function() {
				$("#case-popup").show();
			});
		});
		return false;
	});
	function clearPopup() {
		$("#nav").css("z-index","3");
		$("#case-popup").hide(1, function() {
			$("#blanket").fadeOut("fast");
		});
		return false;
	}
	function changeScreen(context) {	
		var bigger = $("img", context).attr("src");
		bigger = bigger.substr(0,bigger.length-4);
		bigger = bigger + "_lg.jpg";
		$('a.screen-popup').removeClass('screen-selected');
		$(context).addClass('screen-selected');
		if ($.browser.msie && $.browser.version < 7) {
			$("img", "#image-popup").attr("src",(bigger));
			$("a", $(context.parent())).css("border-bottom","0");
			$(context).css("border-bottom","4px solid #000");
		} else {
			$("img", "#image-popup").fadeTo(100,0, function() {
				$("img", "#image-popup").attr("src",(bigger));
				$("img", "#image-popup").fadeTo(400,1);
			});

			$('a:not(.screen-selected)','#case-popup ul').fadeTo(20,0.2);
			$(context).fadeTo(200,1);
		}
	}
	$('#close-popup').click(function() {
		clearPopup();
	});
	if ($.browser.msie && $.browser.version < 7) {
		$('a.screen-selected').css("border-bottom","4px solid #000");
	} else {
		$('a.screen-popup').fadeTo(20,0.2);
		$('a.screen-selected').fadeTo(200,1);
	}
	$('a.screen-popup').each(function() {
		$(this).click(function(){
			changeScreen(this);
			return false;
		});
		$(this).hover(
			function() {
				if (!$(this).hasClass('screen-selected'))
					$(this).fadeTo(200,1);
			}, function() {
				if (!$(this).hasClass('screen-selected'))
					$(this).fadeTo(20,0.2);
			}
		);
	});

	$('#next-popup').click(function() {
		var selected = $('a.screen-selected');
		var li = $('a.screen-selected').parent();
		var next = li.next();
		if ($('#case-popup li').index(li[0])+1 < $('li', '#case-popup ul').length) {
			$('a.screen-popup', next).addClass('screen-selected');
		} else {
			$('a.screen-popup', '#case-popup ul li:first').addClass('screen-selected');
		}
		selected.removeClass('screen-selected');
		changeScreen($('a.screen-selected'));
	});
	$('#prev-popup').click(function() { 
		var selected = $('a.screen-selected');
		var li = $('a.screen-selected').parent();
		if ($('#case-popup li').index(li[0])==0) {
			$('a.screen-popup', '#case-popup ul li:last').addClass('screen-selected');
		} else {
			$('a.screen-popup', li.prev()).addClass('screen-selected');
		}
		selected.removeClass('screen-selected');
		changeScreen($('a.screen-selected'));
	});

	// Clear the default Text #name #email #phone #message
	function defaultText(field) {
		if ($('#'+field).val() == 'Enter your '+field) { $('#'+field).css('color', '#aaa'); }
		$('#'+field).focus(function() { $(this).css('background-color','#ff9'); if ($(this).val() == 'Enter your '+field) { $(this).val(''); $(this).css('color', '#000'); } });
		$('#'+field).blur(function() { $(this).css('background-color','#fff'); if ($(this).val() =='') { $(this).val('Enter your '+field); $(this).css('color', '#aaa'); } });
	}
	var fields = Array('name', 'email', 'phone', 'message', 'field', 'institution', 'url', 'specialty', 'position');
	for (var i=0; i<fields.length; i++)
		defaultText(fields[i]);

	// make the drop down menus work in ie6
	if ($.browser.msie && $.browser.version < 7) {
		$('#nav ul.top-level li').hover(
			function() { $('ul', this).css('left', '0'); $('ul', this).css('bottom', '33px'); },
			function() { $('ul', this).css('left', '-999em'); }
		);
		$('#menu-main-1').hover(
			function() { $('a.base-link', this).css('background', 'url(nav.gif) 0 -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(nav.gif) 0 -36px no-repeat'); }
		);
		$('#menu-main-2').hover(
			function() { $('a.base-link', this).css('background', 'url(nav.gif) -82px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(nav.gif) -82px -36px no-repeat'); }
		);
		$('#menu-main-3').hover( 
			function() { $('a.base-link', this).css('background', 'url(nav.gif) -179px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(nav.gif) -179px -36px no-repeat'); }
		);
		$('#menu-main-4').hover( 
			function() { $('a.base-link', this).css('background', 'url(nav.gif) -276px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(nav.gif) -276px -36px no-repeat'); }
		);
		$('#menu-main-5').hover( 
			function() { $('a.base-link', this).css('background', 'url(nav.gif) -381px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(nav.gif) -381px -36px no-repeat'); }
		);
		$('#menu-main-6').hover( 
			function() { $('a.base-link', this).css('background', 'url(nav.gif) -476px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(nav.gif) -476px -36px no-repeat'); }
		);
	}

});