/*
	DESC:
	
	This will initiate all of the date pickers on each page. The code for controlling
	the lookup popup windows is in here as well.
	
	The intented use of this file is to be placed at the bottom of each page for best performance.
*/
	
	
	$(function(){
		$('.date-pick').datePicker();
	});
	
	
	

/*
	DESC:
	
	This will initiate all of the expanding textareas which are like the facebook textboxes which
	expand in height when the content nearly reaches the height of the textarea.
*/

	$(document).ready (function() {
		
		$('textarea.expanding').autogrow({
			maxHeight: 150,
			minHeight: 50,
			lineHeight: 16
		});



/*
	DESC:
	
	This will create the lookup fields like the data pickers. These open new windows when the icons
	are clicked.
*/

		
		var windowsOpen = new Array();
			
		$('.lookup').each(function(){
				var r = $.trim($(this).attr('rel')).toLowerCase();
				var n = $.trim($(this).attr('name'));
				
				var h = function(){
						lnk = $(this).attr('rel');
						lnk = lnk.replace('{module}', param_module);
						lnk = lnk.replace('{option}', param_option);
						lnk = lnk.replace('{template}', param_template);
						lnk = 'index.php?field=' + n + '&' + lnk;
						
						
						var winW = 700;
						var winH = 500;
						
							for(var i=0; i < windowsOpen.length; i++){
								windowsOpen[i].close();
							}
						
						var winID = $(this).attr('id') + new Date().getTime();
						var w = window.open(lnk, winID, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + winW + ',height=' + winH);
						
						windowsOpen[windowsOpen.length] = w;
						
						w.moveTo((screen.width / 2) - (winW / 2), (screen.height / 2) - (winH / 2));
						
						this.blur();
						return false;
					}
				
				var a = $('<a href="#" class="lookup" rel="' + r + '"><span>Lookup</span></a>').click(h);
				$(this).after(a);
				$(this).blur(function(){
					if($.trim($(this).val()) == '' || $(this).val() == ''){
						var n = $.trim($(this).attr('name'));
						$('#' + n + '_value').attr('value', '');
					}
				});
			});
			
		$(window).unload(function(){
			for(var i=0; i < windowsOpen.length; i++){
				windowsOpen[i].close();
			}
		});

	});