/**
 * Collection of utility functions
 * 
 * @author Webstores <info at webstores dot nl>
 *         Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
var WS = {
	toString: function() {
		return 'Webstores Internet totaalbureau utility functions';
	}
};

WS.Util = {
	/**
	 * Open links with rel external in a new window/tab
	 */
	externalLinks: function() {
		$('a[rel="external"]').each(function() {
			this.target = '_blank'
		});
	},
	
	/**
	 * Make table rows clickable
	 * 
	 * @param {Object} options Optional parameters
	 */
	rowClick: function(options) {
		options = $.extend({
			selector: 'table.clickable tr',
			anchorIndex: 0
		}, options || {});
		
		$(options.selector).each(function() {
			var anchor = $('a:eq(' + options.anchorIndex + ')', this);
			
			if(anchor.length) {
				$(this).click(function() {
					switch(anchor.attr('rel')) {
						case 'external':
							window.open(anchor.attr('href'));
							break;
						default:
							window.location.href = anchor.attr('href');
							break;
					}
				});
				
				$(this).attr('title', anchor.attr('title'));
			}
		});
	},
	
	/**
	 * Fix placeholders for unsupported browsers
	 * 
	 * @param {Object} options Optional parameters
	 */
	fixPlaceholders: function(options) {
		options = $.extend({
			selector: '[placeholder]',
			focusClass: 'focus',
			blurClass: 'placeholder'
		}, options || {});
		
		if(typeof $('<input>').attr('placeholder') == 'undefined') {
			$(options.selector).focus(function() {
				var el = $(this);
				
				if(el.val() == el.attr('placeholder')) {
					el.val('');
				}
				
				el.removeClass(options.blurClass);
				el.addClass(options.focusClass);
			}).blur(function() {
				var el = $(this);
				
				// The first expression checks for cached form values after soft refresh (e.g. Firefox)
				if(el.val() == el.attr('placeholder') || el.val() == '') {
					el.removeClass(options.focusClass);
					el.addClass(options.blurClass);
					el.val(el.attr('placeholder'));
				}
			}).blur().parents('form').submit(function() {
				$(this).find(options.selector).each(function() {
					var el = $(this);
					
					if(el.val() == el.attr('placeholder')) {
						el.val('');
					}
				});
			});
		}
	},
	
	/**
	 * Fix <element other than anchor>:hover for nested lists in IE6
	 * 
	 * @param {Object} options Optional parameters
	 */
	fixIE6HoverList: function(options) {
		options = $.extend({
			selector: '#primary-nav li',
			activeClass: 'active'
		}, options || {});
		
		$(options.selector).each(function() {
			$(this).hover(function() {
				$('> ul', this).addClass(options.activeClass);
			},
			function() {
				$('> ul', this).removeClass(options.activeClass);
			});
		});
	},
	
	/**
	 * Converts a number of seconds to a string of minutes:seconds (e.g. 4:25)
	 * 
	 * @param {Number} sec Seconds to convert to minutes
	 */
	secondsToMinutes: function(sec) {
		var minutes = Math.floor(sec / 60);
		var seconds = Math.floor(sec % 60);
		
		if(seconds < 10) {
			seconds = '0' + seconds;
		}
		
		return minutes + ':' + seconds;
	}
};

