/// <reference path="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/>

$(function () {

	//sort out buttons
	$('.btn').each(function () {
		$(this).html('<i></i><span><i></i><span></span>' + $(this).text() + '</span>');
	})

	//home page buttons
	$('#home .articleSplash').carousel('.featureItem', '.headButtons a');
	//about us page buttons
	$('#about .articleSplash').carousel('.featureItem', '.headButtons a');
	//clients page feature
	$('#clientFeatureArea').carousel('.featureItem', '.featurePagination a');
	//features page
	$('#featuresFeatureArea').navigateItems('.featureItem', '#ffaNav a');
	// Begin search field clear
	$('input').resetDefaultValue(); // for all input elements
	$('input.className').resetDefaultValue(); // for some elements
	$('#q').resetDefaultValue(); // for a especific element
	$(':text').resetDefaultValue(); // avoid button/reset/submit buttons
	$('textarea').resetDefaultValue(); // work with textarea too

	$('.filterList li').click(function () {
		if ($(this).hasClass('selected')) {
			$('.clientList').scrollTop(0);
			$(this).removeClass('selected');
		}
		else {
			$(this).addClass('selected').siblings('.selected').removeClass('selected');
			$('.clientList').scrollTop($('.clientList').scrollTop() + $($('a', this).attr('href')).position().top);
		}
		return false;
	});
});

//fix the filterList (for ie7) -- not working yet
$(window).load(function () {
	$('.filterList').each(function () {
		var fl = $(this);
		var offset = fl.css({ visibility: 'hidden', display: 'block' }).offset(); //it has to have layout so we can find the offset
		fl.parent().hover(function () { fl.show(); }, function () { fl.hide(); }); //attach a js hover
		$('body').append(fl.css({ visibility: '', display: '', left: offset.left, top: offset.top })); //replace at end of doc for zIndex fix
	});
});


(function ($) {
	$.fn.carousel = function (features, links) {
		return this.each(function () {
			var container = $(this); //keep ref
			features = container.find(features);
			links = container.find(links);
			var nav = links.filter('.previous a,.next a'); //find prev/next links
			links = links.not(nav); //remove these from set of links
			links.click(function () { //bind to each link
				if ($(this).attr('href').length > 1) { //ignore empty links (#) (prev/next)
					features.not($($(this).attr('href')).show()).hide(); //hide all others, show the selected one
					$(this).parent().addClass('selected').siblings().removeClass('selected'); //add classes to parent
				}
				return false;
			}).first().triggerHandler('click'); //make the first item selected by default
			nav.filter('.previous a').click(function() { //bind the prev nav link
				var proposed = links.filter('.selected a').parent().prev().find('a')
				if(links.filter(proposed).length == 0)
					proposed = links.last(); //default to looping to end
				proposed.triggerHandler('click');
				return false;
			});
			nav.filter('.next a').click(function() { //bind the next nav link
				var proposed = links.filter('.selected a').parent().next().find('a')
				if(links.filter(proposed).length == 0)
					proposed = links.first(); //default to looping to beginning
				proposed.triggerHandler('click');
				return false;
			});
		});
	};
})(jQuery);

(function ($) {
	$.fn.navigateItems = function (features, links) {
		return this.each(function () {
			features = $(this).find(features);
			links = $(this).find(links);
			links.filter('a.prev').click(function() { //bind the prev nav link
				var proposed = features.filter('.selected').prev();
				if(features.filter(proposed).length == 0)
					proposed = features.last();
				features.filter('.selected').removeClass('selected').fadeOut('fast', function() {proposed.addClass('selected').fadeIn('fast');});
				return false;
			});
			links.filter('a.next').click(function() { //bind the next nav link
				var proposed = features.filter('.selected').next();
				if(features.filter(proposed).length == 0)
					proposed = features.first();
				features.filter('.selected').removeClass('selected').fadeOut('fast', function() {proposed.addClass('selected').fadeIn('fast');});
				return false;
			});
			features.filter(':visible').addClass('selected');
		});
	};
})(jQuery);

// Clear search fields on focus
jQuery.fn.resetDefaultValue = function() {
	function _clearDefaultValue() {
		var _$ = $(this);
		if ( _$.val() == this.defaultValue ) { _$.val(''); }
	};
	function _resetDefaultValue() {
		var _$ = $(this);
		if ( _$.val() == '' ) { _$.val(this.defaultValue); }
	};
	return this.click(_clearDefaultValue).focus(_clearDefaultValue).blur(_resetDefaultValue);
}

