if (typeof jQuery != 'undefined') {
	// Add a new expression to find hard typed external links
	jQuery.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname);
	};
}

$(document).ready(function(){
	// Set defaults for jQuery
	if (jQuery.fn.datepicker) {
		$.datepicker.setDefaults($.datepicker.regional[$('body').data('language')]);
	}
	
	// Set every "data-rel" attributes to "rel" attribute
	// This is to maintain HTML5 compliance
	$('a[data-rel]').each(function(){
		$(this).attr('rel', $(this).data('rel'));
	});
	
	// Set every external link that do not already have "rel" or "data-rel" to "external"
	$('a:external:not([rel]):not([data-rel])').each(function(){
		$(this).attr('rel', 'external');
	});
	
	if (jQuery.fn.colorbox) {	
		$('.jquery.colorbox').each(function(){
			$data = $(this).data();
			$(this).find('a').colorbox($data);		
		});		
	}
	
	if (jQuery.fn.flash) {
		$('.jquery.flash').each(function(){
			$(this).flash($(this).data());
		});
	}

	// Open external link in a new window
	$('a[rel="external"]').click(function(clickEvent){
		clickEvent.preventDefault();

		try {
			var pageTracker = _gat._getTrackerByName();			
			pageTracker._trackEvent('External Links', this.href);
		} catch(err){}

		window.open(this.href);
	});

	// Basic behaviors for forms
	$('form').each(function(){
		if (jQuery.fn.ckeditor) {
			$('.jquery.rte.basic').each(function(){
				$(this).ckeditor({"docType":"<!DOCTYPE html>","entities_latin":false,"resize_dir":"vertical","resize_minHeight":300,"toolbar":[["Source"],["Cut","Copy","Paste","PasteText","PasteFromWord"],["Bold","Italic","Underline","Strike"],["Subscript","Superscript"],["NumberedList","BulletedList"],["Link","Unlink","Anchor"],["Maximize"],["About"]],"toolbarCanCollapse":false,"language":$('body').data('language')});   
			});
		}
		if (jQuery.fn.datepicker) {
			$('input.jquery.dp').each(function(){
				$(this).datepicker($(this).data());
			});
		}
	});
	
	$('form').each(function() {

		// Display sample in forms
		className = 'hasExample';
		
		$(this).find('input[type="text"][title],textarea[title]').each(function(){
			$(this).focus(function(eventFocus){
				if($(this).hasClass(className)) {					
					$(this).val('');
					$(this).removeClass(className);
				}
			});
				
			$(this).blur(function(eventBlur){				
				if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			});
			
			$(this).change(function(eventChange){
				if ($(this).val() != '' && $(this).val() != $(this).attr('title')) {
					$(this).removeClass(className);
				}
			});
					
			if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				if(!$(this).hasClass(className)) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			}
		});

		// "a" html entity to trigger form submission
		$(this).find('div.buttons a:not([rel="external"])').click(function(e){		
			e.preventDefault();
						
			// Set the 'rel' of the link to the hidden 'input[action]'
			$(this).parents('form').first().find('input[name="action"]').attr('value', $(this).attr('rel'));
			
			// Submit the parent form
			$(this).parents('form').trigger('submit');
		});
	
		$(this).submit(function(e){	
			// Remove any sample values for the form
			$('.' + className, this).val('');
		});
	});
});
