// Variables
var jsLib = {
	jqueryPlugins 	: '/includes/js/plugins',
	validatePath	: '/includes/js/plugins/validate'
};
jsLib = $.extend(jsLib, {
	fastConfirm		: jsLib.jqueryPlugins + '/fastConfirm/jquery.fastconfirm.min.js', // 2.1.1 - http://blog.pierrejeanparra.com/jquery-plugins/fast-confirm/
	fastConfirmCSS	: jsLib.jqueryPlugins + '/fastConfirm/jquery.fastconfirm.css',
	jNotify			: jsLib.jqueryPlugins + '/jNotify/jquery.jnotify.min.js', // 1.1.00 - http://www.givainc.com/labs/jnotify_jquery_plugin.htm
	jNotifyCSS		: jsLib.jqueryPlugins + '/jNotify/css/jquery.jnotify.css',
	query 			: jsLib.jqueryPlugins + '/query/jquery.query.js', // 2.1.7 - http://plugins.jquery.com/project/query-object
	metadata		: jsLib.jqueryPlugins + '/metadata/jquery.metadata.min.js',// 2.1 - http://docs.jquery.com/Plugins/Metadata
	uniForm			: jsLib.jqueryPlugins + '/uniForm/uni-form.jquery.min.js', //http://sprawsm.com/uni-form/
	validate		: jsLib.jqueryPlugins + '/validate/jquery.validate.min.js' // 1.8.1 - http://bassistance.de/jquery-plugins/jquery-plugin-validation/
});


// Plugin: Message Box Notice
var applyMessageBox = function(){
	if(!messageBoxArray.length) return;
	
	yepnope({
		load	: [jsLib.jNotify,
				   jsLib.jNotifyCSS],
		complete: function(){		
			$.each(messageBoxArray, function(i,v){
				var $this = $.extend({message:'', type:'notice'}, v);
				$.jnotify($this.message, {
					type	: $this.type, 
					delay	: 5000,
					sticky	: (/error|warning/i).test($this.type)? true : false
				});
			});
		}
	});
}();


// Plugin: Server Side Column Sorting
var applyColumnSort = function(){
	var $this = $('table.results');
	if(!$this.length) return;
	
	yepnope({
		load	: jsLib.query,
		complete: function(){
			var c = ($.query.get('c') != 'undefined' && $.query.get('c').length)? $.query.get('c') : $('th:[alt]:first', $this).attr('alt');
			var d = (Boolean($.query.get('d')))? 1 : 0;

			if ($.query.get('c').length == 0 && typeof colOrderDefault != 'undefined') c = colOrderDefault;	
			if ($.query.get('d').length == 0 && typeof colOrderDesc != 'undefined') d = Boolean(colOrderDesc);
			
			$('.results:first th[alt >= ]')
			.addClass('header')
			.click(function(){
				d = (d)?0:1;
				if(c != $(this).attr('alt')) d=0;	
				c = $(this).attr('alt');										
				if(typeof colOrderSubmit != 'undefined') colOrderSubmit(c, d); 
				else window.location = $.query.set('c', c).set('d', d);
			})
			.append($('<span class="icon carat-2-n-s" style="float:right;"></span>'));
			
			$('.results:first th:[alt='+c+']').addClass((d)? 'headerSortUp' : 'headerSortDown');	
			$('.headerSortUp span:first').removeClass('carat-2-n-s').addClass('carat-1-n');
			$('.headerSortDown span:first').removeClass('carat-2-n-s').addClass('carat-1-s');
		}
	});
}();

// Plugin: validate
var loadValidatorOptions = {};
var applyValidate = function(){
	var $this = $('form');
	if(!$this.length) return;

	yepnope({
		load	: [jsLib.metadata,
			 	   jsLib.validate,
				   jsLib.validatePath + '/validationLibrary.js',
				   jsLib.uniForm], 
		complete: function(){													   
			if(typeof loadValidators !== 'undefined') loadValidators();
			
			$.validator.setDefaults({ 
				errorClass: 'errorField', 
				errorElement: 'p', 
				errorPlacement: function(error, element) { 
					error.prependTo(element.parents('div.ctrlHolder'))
				},
				highlight: function(element,errorClass) {
					$(element).parents('div.ctrlHolder').addClass('error');
				},
				unhighlight: function(element,errorClass) {
					$(element).parents('div.ctrlHolder').removeClass('error');
				}
			});
			
			$this.each(function(){
				var validatorOptions = $.extend({ignore: '.ignore,button'}, loadValidatorOptions);
				$(this).uniform(); 
				$(this).validate(validatorOptions);				
			});	
			
			$('input.focus:first').focus();
		}
	});
}();


// Plugin: Fast Confirm
var applyFastConfirm = function(){
	var $this = $('.fastConfirm');
	if(!$this.length) return;
		
	yepnope({
		load	: [jsLib.fastConfirm,
			       jsLib.metadata,
		           jsLib.fastConfirmCSS],
		complete: function(){
			$this.click(function(e){
				var $this = $(this);
				e.preventDefault();
				e.stopPropagation();
				var metadata = {
					position : ($this.parents('table:first').hasClass('results'))? 'right' : 'top',
					onProceed: function(trigger){ if(!$(trigger).is('[href]')) return; window.location = $(trigger).attr('href'); }
				};
				$this.fastConfirm($.extend(metadata, $this.metadata()));
				return false;
			});
		}
	});
};
applyFastConfirm();

// Buttons as links
$('[href]').not('a,.fastConfirm').click(function(e){
	var $this = $(this);
	if($this.is('[target]')) window.open($this.attr('href'), $this.attr('target'));
	else window.location = $this.attr('href');
});


$(document).ready(function(){
	$('.field, textarea').focus(function() {
        if(this.title==this.value) {
            this.value = '';
        }
    }).blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
    });
});	
