/**
 * Form text box hints.
 * 
 * This plug-in will allow you to set a 'hint' on a text box or
 * textarea.  The hint will only display when there is no value
 * that the user has typed in, or that is default in the form.
 * 
 * You can define the hint value, either as an option passed to
 * the plug-in or by altering the default values.  You can also
 * set the hint class name in the same way.
 * 
 * Examples of use:
 * 
 *     $('form *').textboxhint();
 * 
 *     $('.date').textboxhint({
 *         hint: 'YYYY-MM-DD'
 *     });
 * 
 *     $.fn.textboxhint.defaults.hint = 'Enter some text';
 *     $('textarea').textboxhint({ classname: 'blurred' });
 *
 * @copyright Copyright (c) 2009, 
 *            Andrew Collington, andy@amnuts.com
 * @license New BSD License
 */
(function($) {
    $.fn.textboxhint = function(userOptions) {
        var options = $.extend({}, $.fn.textboxhint.defaults, userOptions);
        return $(this).filter(':text,textarea').each(function(){
        	var el = $(this);
        	el.attr('changed', 0);
            if (el.val() == '') {
            	el.attr('typedValue', el.val());
            }
            el.focus(function(){
                if (el.attr('typedValue') == '') {
                	el.removeClass(options.classname).val('');
                }
            }).blur(function(){
            	el.attr('typedValue', el.val());
                if (el.val() == '') {
                	el.addClass(options.classname).val(options.hint);
                }
            }).keydown(function() {
            	el.attr('changed', 1);
            }).blur().closest('form').submit(function(){
            	if (el.attr('typedValue') == '' && el.attr('changed') != 1) {
                	el.removeClass(options.classname).val('');
                }
            });
        });
    };
 
    $.fn.textboxhint.defaults = {
        hint: 'Please enter a value',
        classname: 'hint'
    };
})(jQuery);

$(document).ready(function(){
	// Add and remove input box values
	$('input.clearOnFocusQuick').textboxhint({ hint: "Quick search" });
	$('input.clearOnFocusKeyword').textboxhint({ hint: "Keyword search" });
	$('input.clearOnFocusDate').textboxhint({ hint: "dd/mm/yyyy" });
	$('input.clearOnFocusDay1, input.clearOnFocusDay2').textboxhint({ hint: "dd" });
	$('input.clearOnFocusMonth1, input.clearOnFocusMonth2').textboxhint({ hint: "mm" });
	$('input.clearOnFocusYear1, input.clearOnFocusYear2').textboxhint({ hint: "yyyy" });
	
	// Search show / hide
	$("#trigger").click(function() {
		if ( !$("#search-advanced-outer").is(':animated') ) { $("#search-advanced-outer").slideToggle(); }
	});   
	$("#search-advanced-outer a").click(function() {
		if ( !$("#advanced-search").is(':animated') ) { $("#advanced-search").slideUp(); }
	});
	
	// Reset Font Size
	var originalFontSize = $('html').css('font-size');
	$(".resetFont").click(function(){
		$('html').css('font-size', originalFontSize);
	});
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		$('html').css('font-size', newFontSize);
		return false;
	});
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		$('html').css('font-size', newFontSize);
		return false;
	});
  
	// Find last tag in tag list and add class 'last' to it.
	$('ul.tags').each(function() {
		$(this).find('a:last').addClass('last');
	}); 

	// comments form validation
	$("#commentForm").validate();

	// Info box
	
	// This is bad, but less bad than Google displaying the bloody comment T&Cs in its search results.
	var boxHTML = '\
	<div id="infoBox" style="display: none"> \
		<div class="wrapper"> \
			<div class="content"> \
				<h2>Comments: Terms and conditions</h2> \
				<ul> \
					<li>We reserve the right to edit comments. A comment may be edited if the contribution is too long, strays partly off topic or contains inappropriate language. The decision to edit a comment is at the discretion of Axis of Eco.</li> \
					<li>Comments we consider to be abusive, defamatory, discriminatory or off-topic, or otherwise unlawful, will not be accepted. Messages that are factually wrong and misleading may also be deleted.</li> \
					<li>Preference will be given to contributions which are no more than 200 words long but we reserve the right to publish longer contributions if, in the view of the editors, a comment offers a particular insight.</li> \
					<li>Comments that seek to endorse commercial products or activities or solicit business will not be accepted.</li> \
				</ul> \
			</div> \
			<div class="footer"> \
				<a class="close"> \
					<img src="http://www.axisofeco.com/images/closelabel.gif" title="close" /> \
				</a> \
				<div class="clear"></div> \
			</div> \
		</div> \
	</div>';
	
	$('a[rel*=infobox]').click(function() {										
		var boxId = ''+$(this).attr('href')+'';		
		if ( !$('#infoBox').hasClass('showing') ) {
			$('body').append(boxHTML);
			leftValue = $(window).width() / 2 - 225;
			topValue = $(window).scrollTop() + 50;
			$('#infoBox').css({"left" : leftValue, "top" : topValue}).fadeIn('fast').addClass('showing');
		}
	});
	
	$('body').click(function(){
		$('#infoBox').removeClass('showing').fadeOut('fast', function() {
			$('#infoBox').remove();
		});
	});	
	$('#infoBox, a[rel*=infobox]').click(function(event){
		event.stopPropagation();
	});

});
