;(function($) {
  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);
    }
  }
})(jQuery);




var do_submit_form = false;

$(document).ready(
	function(){

		// add event handlers
		// be sure to return false so link is not followed
		$('.tal_contact').focus( function (event) { return doClearInput(event); } );
		$('.tal_contact').blur( function (event) { return doRefillInput(event); } );
		
		
		$('#tal_contact_request').submit( function (event) { return doCheckContactRequestForm(); } );
		
		// chat popup
		$('a.chat_popup').click( function () { window.open( $(this).attr('href'), 'mychatwindow', 'height=500,width=300,status=1,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1'); return false; } );
		
		// add open in new window attribute to new_window class links
		$('a.new_window').each( function () { $(this).attr('target', '_blank'); } );

		// add mouse over
		//$('img.rollover').preload(  { find:/\.(gif|jpg|png)/, replace:'_over.$1' }  );
		addHover('img.rollover');
		

		// screen size
		try {
			var i_w = window.innerWidth;
			var i_h = window.innerHeight;
			if (i_w == null) {
				i_w = document.documentElement.clientWidth;
				i_h = document.documentElement.clientHeight;
			}
			$.post('/biz/ajax/s', {iw : i_w, ih : i_h, sw : screen.width, sh : screen.height});
		}
		catch (err) { }
	}
);


function doCheckContactRequestForm() {
	var do_submit_form = 0;
	
	do_submit_form = true;
	
	$('.tal_contact').each( 
		function (i) {
			var item = $(this);
			if ('firstname' == item.attr('name') 
					|| 'lastname' == item.attr('name') 
					|| 'email' == item.attr('name') 
					|| 'phone' == item.attr('name') 
					|| 'university' == item.attr('name')) {
				if (!item.val() 
						|| 'First Name*' == item.val() 
						|| 'Last Name*' == item.val() 
						|| 'Email*' == item.val() 
						|| 'Phone*' == item.val() 
						|| 'Library or School Name*' == item.val()) {
					do_submit_form = false;
				}
			}
		}
	);
	if (!do_submit_form) {
		alert("Please fill out at least your Name, Library, Email, and Phone number");
		return false;
	}
	return do_submit_form;
}

function doClearInput(event) {
	var i = $(event.target);
	
	if ('First Name*' == i.val()
			|| 'Last Name*' == i.val()
			|| 'Library or School Name*' == i.val()
			|| 'Title' == i.val()
			|| 'Email*' == i.val()
			|| 'Phone*' == i.val()
			|| 'Notes' == i.val()
		 ) 
	{
		// reset the value of the field
		i.val('');
	}
//	else
//	{
//		i.val('"' + i.attr('name') + '"');
//	}
}



function doRefillInput(event) {
	var i = $(event.target);
	
	if ('' == i.val()) {
		if ('firstname' == i.attr('name')) {
			i.val('First Name*');
		}
		else if ('lastname' == i.attr('name')) {
			i.val('Last Name*');
		}
		else if ('university' == i.attr('name')) {
			i.val('Library or School Name*');
		}
		else if ('title' == i.attr('name')) {
			i.val('Title');
		}
		else if ('email' == i.attr('name')) {
			i.val('Email*');
		}
		else if ('phone' == i.attr('name')) {
			i.val('Phone*');
		}
		else if ('notes' == i.attr('name')) {
			i.val('Notes');
		}
	}
}

function addHover(css_selector) {
	$(css_selector).hover(
		function () { 
			var img = $(this).attr('src'); 
			img = img.replace('.gif', '_over.gif');
			img = img.replace('.jpg', '_over.jpg');
			img = img.replace('.png', '_over.png');
			
			jQuery.preLoadImages(img);
			
			$(this).attr('src', img);
		},
		
		function () { 
			var img = $(this).attr('src'); 
			img = img.replace('_over.gif', '.gif');
			img = img.replace('_over.jpg', '.jpg');
			img = img.replace('_over.png', '.png');
			$(this).attr('src', img);
		}
	);
}

