(function($){
	$(document).ready(function() {
		$('input[type=text]').not('.no-clear').focus(function(){
			var currentVal = $(this).val();										// Read initial field value
			$(this).val('');														// Clear initial value

			$(this).blur(function(){
				var getNewVal = $(this).val();										// Read new input value
				if ( getNewVal == '' || getNewVal == ' ' ) {
					$(this).val(currentVal);										// Switch to initial value
				}
			});
		});

		// fix PNG for IE6
		if ( $.browser.msie && $.browser.version == '6.0' )
			DD_belatedPNG.fix('div, a, img, li, a em');

		// Cufon
		Cufon.replace('h2.pageSection', { fontFamily: 'light' });
		Cufon.replace('h2.pageSection span', { fontFamily: 'ultralight' });

		// skinable selects
		if ( $("select.skinable").length )
			$("select.skinable").selectbox({animationSpeed: 200});

		// fix sidebar height
//		if ( $('.sidebar').length ){
//		 	$('.sidebar').not('.fixed').css({height: ($('.roundedWhiteBox').height()-26)});
//		 	$('.sidebar').not('.fixed').css({height: ($('.roundedWideWhiteBox').height()-26)});
//		}

		// round corners
//		$('.roundedWhiteBox, .roundedWideWhiteBox').corner("14px");
//		$('.roundedWhiteBox, .roundedWideWhiteBox').corner("cc:#b6a8c8");
//		$('.sidebar').corner("left 14px");

		// remove any borders on last LI element
		$("ul").not('#directions ul, .boxes, .sidebar ul, ul.research, #testimonials, div.tabs ul, ul.expandable, #logos ul').each(function(){
			 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
		});
		
		// handle tooltips in map
		if ( $('#map').length ){
			$('a.pin').each(function(){
				$(this).append('<em>'+$(this).children('img').attr('alt')+'</em>');
				$(this).children('em').css({ right: ($(this).children('em').width()+16) });
			});

			$('a.pin').hover(
				function(){
					$('a.pin em').hide();
					$(this).children('em').fadeIn('slow');
				},
				function(){
					$(this).children('em').fadeOut('fast');
				}
			);
		}

		// add delegates
		$('a#triggerAddDelegates').click(function(){
			var counter = $('#moreDelegates fieldset').length + 1;
			var $delegate = '<fieldset><h4 class="delegate"><a onclick="$(this).parents(\'fieldset\').remove(); return false;" href="#" class="removeDelegate floatRight"><img src="images/icons/close-button.gif" width="13" height="13" alt="Remove delegate"/></a>Delegate '+counter+'</h4><p class="row"><label for="delegate'+counter+'FirstName">First Name</label><input type="text" class="i-text" name="delegate'+counter+'FirstName" id="delegate'+counter+'FirstName" value="" /></p><p class="row"><label for="delegate'+counter+'LastName">Last Name</label><input type="text" class="i-text" name="delegate'+counter+'LastName" id="delegate'+counter+'LastName" value="" /></p><p class="row"><label for="delegate'+counter+'Email">Email</label><input type="text" class="i-text" name="delegate'+counter+'Email" id="delegate'+counter+'Email" value="" /></p></fieldset>';

			$('#moreDelegates').append($delegate);
			return false;
		});

		// sidebar menu
		$(".sidebar ul a.sub").each(function(){
			if ( $(this).siblings('ul').hide() ){
				$(this).click(function(){
					$(this).siblings('ul').slideToggle('medium');
					return false;
				});
			}
		});

		// lavalamp
		$("#navigator").lavaLamp({
			fx: "backout",
			speed: 500,
			click: function(event, menuItem) {
				return true;
			}
		});

		// image preloader
		// jQuery.preLoadImages("images/center/01.jpg", "images/center/02.jpg", "images/center/03.jpg");
		var cache = [];
		// Arguments are image paths relative to the current page.
		$.preLoadImages = function() {
			var args_len = arguments.length;
			for (var i = args_len; i--;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}		 			

		// dialogs
		if ( typeof $.fn.jqm != 'undefined') {
			$('.dialog').jqm({
				modal: false,
				trigger: 'ul.research li a.smallButton, li.emailUpdates a, li.report a, li.email a, a.recommend',
				closeClass:"close",
				overlay: 60,
				ajax:'@href'
			});

			$('#terms').jqm({
				modal: false,
				trigger: 'a.tac',
				closeClass:"close",
				overlay: 60,
				ajax:'@href'
			});
		}

		// makes scrollers for index page
		if ( $("#scrollme").length ) {

			var index = $("#scrollme ul li").length;
			$("#logos a#backLogo").fadeOut();
			$('#scrollme').serialScroll({
				items:'li',
				prev:'#logos a#backLogo',
				next:'#logos a#nextLogo',
				axis:'x',
				start:0,
				duration:300,
				force:true,
				cycle:false,
				exclude:'3',
				onBefore:function(button, e, elem, $pane, $items, pos){
					if ( $items == 0 ) {
						$("#logos a#backLogo").fadeOut();
					}

					if ( $items == index-4 ) {
						$("#logos a#nextLogo").fadeOut();
					}
				}
			});

			$("#logos a#backLogo").click(function(){
				$("#logos a#nextLogo:hidden").fadeIn();
				$('#scrollme').trigger('a#backLogo');
			});
			$("#logos a#nextLogo").click(function(){
				$("#logos a#backLogo:hidden").fadeIn();
				$('#scrollme').trigger('a#nextLogo');
			});
		}


		// testimonials
		$("ul#testimonials li:nth-child(5n+1)").addClass('first');

		// tabs
		$(".tabs").each(function(){
			var c = $("ul.tabs-content",this)[0];
			$("ul.tabs a:not(.no-tab)",this).click(function(){
				var trg = $(this).attr("class");
				var self = this;
				$(this).parent().siblings().removeClass("on");
				$(this).parent().addClass("on");
				if ( $(c).children("li:visible").length )
					$(c).children("li:visible").not("li."+trg).fadeOut('fast',function(){
						$(c).children("li."+trg).fadeIn('fast');
					});
				else
					$(c).children("li."+trg).fadeIn('fast');
				return false;
			});
			$("ul.tabs li.on a").click();
		});

		// expandables
		$("ul.expandable > li:not(.no-tab) > a").click(function(){
			$(this).siblings("ul").slideToggle('medium');
			$(this).parent().toggleClass('on');
			return false;
		});
		$("ul.expandable li.on ul").show();

		// form a.submit behaviour
		$("form a.submit").click(function(){
			$(this).parents("form").submit();
			return false;
		});
	});
})(jQuery);
	
jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};


