/* Author: GrafX Design Division
*/


// RSV: Really Simple Validation - global functions
function myOnComplete() {
	return true;
}
function customErrorDisplay(f, errorInfo) {
	// Disabled all errors by default
	for (var i=0; i<myRules.length; i++) {
		var parts 	= myRules[i].split(",");
		var fieldName 	= parts[1];

		// $('label[for='+fieldName+']').removeClass('errorlabel');
		$('fieldset').removeClass('errorfield');
		$('.error').remove();
	}

	for (var i=0; i<errorInfo.length; i++) {
		var fieldName;

		// Radio button
		if (errorInfo[i][0].type == undefined) {
			fieldName = errorInfo[i][0][0].name;
			fieldset = $('[name='+fieldName+']').parent().parent();
		} else {
			fieldName = errorInfo[i][0].name;
			fieldset = $('[name='+fieldName+']').parent();
		}
		// Display the error
		// fieldset.css('background-color', '#FFEEEE')
		// $('#'+fieldName).css('background-color', '#FFEEEE');
		// $('label[for='+fieldName+']').addClass('errorlabel');
		fieldset.addClass('errorfield');
		$('<span class="error">'+errorInfo[i][1]+'</span>').appendTo($('label[for='+fieldName+']').parent());
	}

	if (errorInfo.length == 0) {
		// Disable form on submission
		// $('form').submit(function(){
			// On submit disable its submit button
			$('input[type=submit]', this).attr('disabled', 'disabled');

			// Now disable other form inputs
			// Deprecated as it interferes with validation
			// $('select', this).attr('disabled', 'disabled');
			// $('input[type=text]', this).attr('readonly', 'readonly');
			// $('textarea', this).attr('readonly', 'readonly');

			// Now show the user the loading message
			// $('#container').css({opacity:0.4});
			// $('#message').html('Please wait while your data is saved.<br /><br />If you are uploading large images or video this may take a few minutes depending on your internet connection.');
			// $('#message').fadeIn(300);
		// });

		return true;
	} else {
		return false;
	}
}


// Hover for suckerfish
$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover(
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


// De-obfuscate email addresses
jQuery.fn.deobfuscate = function() {
	return this.each(function() {
		// Original Usage: <span class="email">user(put an @ here)example.com</span>
		// var email = String($(this).html()).replace(/\s*\(.+\)\s*/, "@");

		// Usage: <span class="email">user[at]example.com</span>
		var email = String($(this).html()).replace('[at]', '@');
		$(this).before('<a href="mailto:' + email + '">' + email + "</a>").remove();
	});
};


$(document).ready(function() {
	// De-obfuscate email addresses
	if ($('.email').length) {
		$('.email').deobfuscate();
	}


	// Navigation dropdown
	$('.nav ul').hide();
	$('.nav > li').hover(function() {
		$(this).children('ul').slideDown('fast');
	},function() {
		$(this).children('ul').hide();
	});
	/*
	if (document.all) {
		$('.nav > li').hoverClass('sfHover');
	}
	*/


	// Help text for search boxes
	$('#keyword').val($('#keyword').attr('title'));
	$('#find-address').val($('#find-address').attr('title'));

	$('#keyword, #find-address').focus(function() {
		var getTitle = $(this).attr('title');
		if (this.value == getTitle) {
			$(this).val('');
		}
	});
	$('#keyword, #find-address').blur(function() {
		if (this.value === '') {
			$(this).val($(this).attr('title'));
		}
	});


	// Tabs behaviour
	$('.tab-container > div').hide();			// Hide all content
	$('ul.tabs li:eq(1)').addClass('active').show();	// Activate second tab
	$('.tab-container > div:eq(1)').show();			// Show second tab content


	// Tabs onclick event
	$('ul.tabs li').click(function() {
		$('ul.tabs li').removeClass('active');		// Remove any "active" class
		$(this).addClass('active');			// Add "active" class to selected tab
		$('.tab-container > div').hide();		// Hide all tab content
		var activeTab = $(this).find('a').attr('href');	// Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn();				// Fade in the active ID content
		return false;
	});


	// Colorbox
	if ($('img[rel$="popup"]').length) {
		$('img[rel$="popup"]').colorbox({
			/* width:'600px',
			height:'450px',
			scrolling:true,
			iframe:true, */
			current:'',
			previous:'',
			next:'',
			href: function(){
				var hrefText = $(this).attr('src');
				return hrefText;
			},
			title: function(){
				// var url = $(this).attr('href');
				// return '<a href="'+url+'" target="_blank">Open In New Window</a>';
				var titleText = $(this).attr('title');
				return titleText;
			}
		});
	}


	if ($('a[rel$="newsletter"]').length) {
		$('a[rel$="newsletter"]').colorbox({
			innerWidth:'80%',
			innerHeight:'80%',
			scrolling:true,
			iframe:true,
			current:'',
			previous:'',
			next:'',
			title: function(){
				// var url = $(this).attr('href');
				// return '<a href="'+url+'" target="_blank">Open In New Window</a>';
				var titleText = $(this).attr('title');
				return titleText;
			}
		});
	}


	// Target _blank
	$('a[rel$="external"]').click(function() {
		this.target = "_blank";
		this.title = (this.title!='') ? this.title+' [Opens in a new window]' : '[Opens in a new window]' ;
	});

});





