$(document).ready(function()
{

	$('#navcontainer ul li').hover(
		
		function () {
			$(this).addClass('sfhover');			
		}, 
		function () {
			$(this).removeClass('sfhover');
		}
    );

});


$(function(){

	/** Clickables */
	$('.clickable:has(a[href])').click(function()
    {
        var location = $('a[href]', this).eq(0).attr('href');

        if (location !== undefined)
        {
        	window.location = location;
                window.status = location;
        }

    }).bind('mouseenter mouseleave', function()
    {
        $(this).toggleClass('rollover');
    });
	
	// handling search box
	var searchq = 'Search CleanModules';

	$('div#searchbar input[type="text"]').focus(function(){
		if ( $(this).val() === searchq) {
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() === '') {
			$(this).val(searchq);
		}
	}).val(searchq);

	/** Gallery box */
	$('div.gallery-box ul.gallery li a').fancybox(
		{
			hideOnContentClick: false,			
			overlayOpacity: 0.8,
			overlayColor: '#fff'
		}
	);

	function getbox(node){
		var box = $(node).closest('div.gallery-box');
		var images = $('ul.gallery', box);
		return {
			self: box,
			images: images,
			active: $('li.active', images)
		}
	}

	function render_controls(active, control, box){
		$('div.controls *', box.self).show();

		if(active.hasClass((control === 'next' ? 'last' : 'first'))){
			$('div.gallery-'+control, box.self).hide();
		}
	}

	$('div.gallery-box div.gallery-prev a').click(function(){
		var box = getbox(this);		
		var prev = box.active.prev();
		if (prev.length)
		{
			box.active.fadeOut(function(){
				$(this).removeClass('active');
				prev.fadeIn().addClass('active');
			});

		}

		render_controls(prev, 'prev', box);

		return false;
	});

	$('div.gallery-box div.gallery-next a').click(function(){
		var box = getbox(this);
		var next = box.active.next();
		if (next.length)
		{
			box.active.fadeOut(function(){
				$(this).removeClass('active');
				next.fadeIn().addClass('active');
			});
		}

		render_controls(next, 'next', box);
		
		return false;
	});		
});
