$( function()
{
	var config =
	{
		// list of elements that will expand functionality afetr add class js
		jsdepend: '#featuredLinks, ul.tabs-nav',
		// list of inputs/textareas with default text
		formtips: '#search input[name="search"]',
		// list of elements containing drop down menu
		dropdown: 'ul#support-nav li',
		// speed of drop down sliding
		dropdown_scroll: 500,
		// slider scrolling speed
		slider_scroll: 500,
		// lists of 'a' elements containing image, using ibox (lightbox) gallery
		ibox: 'ul.portfolio > li > a'
	};
	
	var cache =
	{
		featuredLinks:
		{
			odd: false,
			width: 0
		},
		
		tweets:
		{
			width: 0
		},
		
		page_tabs:
		{
			current: null
		},
		
		slider:
		{
			items: 0,
			width: 0,
			total_width: 0
		}
	};

	$(config.jsdepend).addClass('js');
	
	// TOOLTIPS BEGIN
	
	$(config.tooltips).css('z-index', 99999);
	
	$(config.tooltips).hover( function(event)
	{
		$tooltip = $(this).children('div.tooltip');
		$tooltip.show();
	}, function()
	{
		$tooltip = $(this).children('div.tooltip');
		$tooltip.hide();
	});
	
	$(config.tooltips).mousemove( function(event)
	{
		$this = $(this);
		$tooltip = $(this).children('div.tooltip');
		
		$tooltip.css
		({
			'top': event.pageY - $this.offset().top + 20,
			'left': event.pageX - $this.offset().left - $tooltip.width() + 10
		});
	});
	
	//TOOLTIPS END
	
	// FORMTIPS BEGIN
	
	$(config.formtips).each( function()
	{
		$(this).data('tip', $(this).val());
	});

	$(config.formtips).focus( function()
	{
		if ($(this).val() == $(this).data('tip'))
		{
			$(this).val('');
		}
	});
		
	$(config.formtips).blur( function()
	{
		if ($(this).val() == '')
		{
			$(this).val($(this).data('tip'));
		}
	});
	
	// FORMTIPS END
	
	// FROM VALIDATION BEGIN
	
	function is_valid_email(email)
	{
		return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
	}

	$('#name,#message').blur( function()
	{
		$(this).removeClass('valid');
		
		if ($(this).val() == '')
		{
			$(this).addClass('error');
		} else
		{
			$(this).removeClass('error').addClass('valid');
		}
	});
	
	$('#email').blur( function()
	{
		$(this).removeClass('valid');
		
		if ( ! is_valid_email($(this).val()))
		{
			$(this).addClass('error');
		} else
		{
			$(this).removeClass('error').addClass('valid');
		}
	});
	
	$('#comment #submit').click( function()
	{
		$('#name,#message,#email').blur();

		if ($('#comment input.error').length > 0)
		{
			return false;
		}
	});
	
	// FROM VALIDATION END
	
	// DROPDOWN MENU
	
	$(config.dropdown).hover( function()
	{
		$(this).children('ul').not(':animated').slideDown(config.dropdown_slide);
	}, function()
	{
		$(this).children('ul').slideUp(config.dropdown_slide);
	});
	
	// DROPDOWN MENU
	
	// BLOG / STORE SLIDER CONTROLS
	
	$srv = $('div#featuredLinks ul.items');
	$srv_items = $srv.children('li');
	
	$srv_nav_prev = $('<li />').
		addClass('prev').
		append($('<a />').
	text('Previous'));
	
	$srv_nav_next = $('<li />').
		addClass('next').
		append($('<a />').
	text('Next'));
	
	$srv_nav = $('<ul />').
		addClass('nav').
		css('margin-top', $srv.height()).
		append($srv_nav_prev).
	append($srv_nav_next);
	
	$srv.after($srv_nav);
	
	$srv.
		css('position', 'absolute').
		css('left', 0).
		parent().
	css('position', 'relative');
	
	cache.featuredLinks.width = ($srv_items.length - 2) * $srv_items.outerWidth(true);
	cache.featuredLinks.odd = false;
	
	$srv_nav_prev.click( function()
	{
		var scroll = $srv.parent().width();

		if ($srv.position().left < 0)
		{
			cache.featuredLinks.odd = ! cache.featuredLinks.odd;
			
			$srv.not(':animated').animate
			({
				'left': '+=' + scroll
			}, config.featuredLinks_scroll);
		}
		
		return false;
	});
	
	$srv_nav_next.click( function()
	{
		var scroll = $srv.parent().width();
		
		if ($srv.position().left > -cache.featuredLinks.width)
		{
			cache.featuredLinks.odd = ! cache.featuredLinks.odd;
			
			$srv.not(':animated').animate
			({
				'left': '-=' + scroll
			}, config.featuredLinks_scroll);
		}
		
		return false;
	});
	
	
});

