jQuery.preloadImages = function() { for(var i = 0; i<arguments.length; i++) jQuery("<img>").attr("src", arguments[i]); }

$(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(i) {
		$('a', '#case-popup ul').removeClass('screen-selected');
		$('a#nav-'+i).addClass('screen-selected');
		if ($.browser.msie && $.browser.version < 7) {
			$('img.screen-selected', '#image-popup').hide();
			$('img', '#image-popup').removeClass('screen-selected');

			$('img#image-'+i).addClass('screen-selected');
			$('img.screen-selected', '#image-popup').show();	
		} else {
			$('img.screen-selected', '#image-popup').fadeTo(200,0, function() {
				$('img', '#image-popup').removeClass('screen-selected');

				$('img#image-'+i).addClass('screen-selected');
				$('img.screen-selected', '#image-popup').fadeTo(200,1);
			});

			$('a:not(.screen-selected)','#case-popup ul').fadeTo(20,0.2);
			$('a.screen-selected','#case-popup ul').fadeTo(20,1);
		}
		$('#case-curr').text(i);
	}
	$('#close-popup').click(function() {
		clearPopup();
	});
	if (!($.browser.msie && $.browser.version < 7)) {
		$('a.screen-popup').fadeTo(20,0.2);
		$('a.screen-selected').fadeTo(200,1);
	}
	$('a.screen-popup').each(function() {
		$(this).click(function(){
			changeScreen($(this).attr('id').substr(4,1));
			return false;
		});
		if (!($.browser.msie && $.browser.version < 7)) {
			$(this).hover(
				function() {
					if (!$(this).hasClass('screen-selected'))
						$(this).fadeTo(200,1);
				}, function() {
					if (!$(this).hasClass('screen-selected'))
						$(this).fadeTo(20,0.2);
				}
			);
		}
	});
	$('div.case-holder').hover(function() {
		$(this).addClass('case-over');
	}, function() {
		$(this).removeClass('case-over');
	});	
	$('#next-popup').click(function() {
		var i = parseInt($('a.screen-selected').attr('id').substr(4,1));
		if (i < $('li', '#case-popup ul').length) {
			changeScreen(parseInt(i)+1);
		} else {
			changeScreen(1);
		}
	});
	$('#prev-popup').click(function() { 
		var i = parseInt($('a.screen-selected').attr('id').substr(4,1));
		if (i==1) {
			changeScreen($('li', '#case-popup ul').length);
		} else {
			changeScreen(i-1);
		}
	});

	// 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(/images/nav.gif) 0 -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(/images/nav.gif) 0 -36px no-repeat'); }
		);
		$('#menu-main-2').hover(
			function() { $('a.base-link', this).css('background', 'url(/images/nav.gif) -82px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(/images/nav.gif) -82px -36px no-repeat'); }
		);
		$('#menu-main-3').hover( 
			function() { $('a.base-link', this).css('background', 'url(/images/nav.gif) -179px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(/images/nav.gif) -179px -36px no-repeat'); }
		);
		$('#menu-main-4').hover( 
			function() { $('a.base-link', this).css('background', 'url(/images/nav.gif) -276px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(/images/nav.gif) -276px -36px no-repeat'); }
		);
		$('#menu-main-5').hover( 
			function() { $('a.base-link', this).css('background', 'url(/images/nav.gif) -381px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(/images/nav.gif) -381px -36px no-repeat'); }
		);
		$('#menu-main-6').hover( 
			function() { $('a.base-link', this).css('background', 'url(/images/nav.gif) -476px -36px no-repeat'); },
			function() { $('a.base-link', this).css('background', 'none'); $('a.selected', this).css('background', 'url(/images/nav.gif) -476px -36px no-repeat'); }
		);
	}

});