/**
 * RIMA Design JavaScript
 *
 * @author Rob Simpkins - http://blocblue.com
 * @copyright RIMA Design
 */

jQuery(function($) {
	// Add class to last menu item - IE 6/7/8 do not support :last-child selector
	$('ul#menu-main li:last-child').addClass('last-child');
	
	// Initialise hidden state on 3rd level navigation items
	$('ul#menu-main-1 ul li:not(.current-menu-item):not(.current-menu-ancestor) ul').hide();
	
	// Initialise click event to show/hide on 3rd level navigation items
	$('ul#menu-main-1 a[href="#"]').click(function() {
		$(this).parent().children('ul').slideToggle();
		return false;
	});
	
	// Initialise feature banner cycle
	if($('#banner .banner').length > 1) {
		$('#banner').after('<ul id="banner-menu">').cycle({ 
		    fx: 'fade', 
		    speed: 500, 
		    timeout: 8000, 
			pause: true,
			cleartypeNoBg: true,
		    pager: '#banner-menu',
			activePagerClass: 'current-banner',
			pagerAnchorBuilder: function(index, slide) { 
				return '<li><a href="#">' + pad(index + 1, 2) + '</a></li>'; 
			}
		});
	}
	
	// Initialise twitter feed cycle
	if($('#current-tweet .tweet').length > 1) {
		$('#current-tweet').cycle({ 
		    fx: 'scrollUp', 
		    speed: 500, 
		    timeout: 8000, 
			pause: true,
			cleartypeNoBg: true,
			height: 125,
			width: 240
		});
	}
	
	// Initialise client image hover effect in IE6 and IE7
	if($('#ie6, #ie7').length == 1) {
		$('a.client').hover(function() {
			$(this).children('img').css('top', '-130px');
		}, function() {
			$(this).children('img').css('top', '0');			
		});
	}
	
	// Implement HTML5 placeholder support for older browsers
	if(! has_placeholder_support()) {
		$('input').each(function() {
			if($(this).attr('placeholder') && $(this).val() == '')
				$(this).val($(this).attr('placeholder'));
		});

		$('input').focus(function() {
			if($(this).val() == $(this).attr('placeholder'))
				$(this).val('');
		}).blur(function() {
			if($(this).val() == '')
				$(this).val($(this).attr('placeholder'));
		});
	}
	
	/**
	 * Pad specified number with zeroes until required length is reached.
	 *
	 * @author Greg Jorgensen - http://typicalprogrammer.com/
	 * @param integer number
	 * @param integer length
	 * @return string
	 */
	function pad(number, length) {
	    string = number.toString();
	    if (string.length < length)
	        string = ('000' + string).slice(-length);
	    return string;
	}
	
	/**
	 * Check whether current browser has placeholder attribute support.
	 *
	 * @return boolean
	 */
	function has_placeholder_support()
	{
		var input = document.createElement('input');
		return 'placeholder' in input;
	}
});
