jQuery(document).ready(function() {
	
	// Get path to JS files
	
	myscriptsrc = document.getElementById('myscript').getAttribute('src');
    jspath = myscriptsrc.substr(0, myscriptsrc.length - 9);    
	
	// Main menu
	
	$('.sf-menu').superfish({ 
        delay: 0,
        speed: 'slow',
        autoArrows: false,
        dropShadows: false,
        onBeforeShow: function() {
        	$('#menu').stop().animate({height: 85}, 200);
        },
        onHide: function() {
        	if ($('.sf-menu li.sfHover').length == 0) $('#menu').stop().animate({height: 50}, 200);
        }
    });    
   
    // Galleria
    
    Galleria.loadTheme(jspath + 'galleria.classic.js');
	$("#galleria").galleria({
	    transition: 'fade'
	});
	
	// Cycle	
	
	left = (960 - 16 * $('#cycle > img').size()) / 2;
	$('#cycle').before('<div id="cycle-dir"><div id="cycle-prev"><a href="#"><span>Prev</span></a></div><div id="cycle-next"><a href="#"><span>Next</span></a></div></div>').after('<div id="cycle-nav">').cycle({
		fx: 'fade',
		prev: '#cycle-prev',
        next: '#cycle-next',
    	pager: '#cycle-nav',
    	pagerAnchorBuilder: function(idx, slide) { 
	        return '<a href="#"><span>' + idx + '</span></a>'; 
	    }
	});
	$('#cycle-nav').css('padding-left', left);
	
	$('.widget-featured').cycle();
	
	// jCarousel
	
	$('#carousel ul').jcarousel({
        wrap: 'circular',
        scroll: 1,
        auto: 3
    });
	
	// Image grid Masonry
	
	var $container = $('#puzzle');
	$container.imagesLoaded(function(){
		$container.masonry({
			itemSelector : '.piece'
		});
	});
	
	// Image grid Isotope (filtering)
    
    $('#filter a').click(function(){
		var selector = $(this).attr('data-filter');
		$('#puzzle').isotope({ filter: selector });
		$('#filter li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	
	// Add image hover
	
	$(".piece a img, .snippet img").fadeTo('slow', .5);
	
	$(".piece a, .snippet").live({		
		mouseenter: function() {
			$(this).find('img').stop().fadeTo('slow', 1);	
		},
        mouseleave: function() {
			$(this).find('img').stop().fadeTo('slow', .5);
		}
	});
    
    // FancyBox for all image links
    
    $('a[href$="jpg"], a[href$="png"], a[href$="gif"]').fancybox({		
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'speedIn': 600, 
		'speedOut': 100,
		'padding': 4,
		'overlayColor': '#000000',
		'overlayOpacity': 0.8
	});
	
	// FancyBox for comment form
	
	$("#postcomment a").fancybox({
		'modal': true,
		'padding': 4
	});
	
	// Back to top link
    
    $('a#backtop').click(function(e) {
		e.preventDefault();
		$.scrollTo($(this).attr('href'), 750);		
	});	
	
	// Misc
    
	$('.photogrid img[title]').removeAttr('title');
	$('#homewidgets div.col-1 div.widget:last-child').css({'background': 'none', 'padding-bottom': '0'});
	
});
