/*-----------------------------------------------------------------------------------

 	Custom JS - All front-end jQuery
 
-----------------------------------------------------------------------------------*/
 


/*-----------------------------------------------------------------------------------*/
/*	Let's dance
/*-----------------------------------------------------------------------------------*/


jQuery(document).ready(function() {


/*-----------------------------------------------------------------------------------*/
/*	Superfish Settings - http://users.tpg.com.au/j_birch/plugins/superfish/
/*-----------------------------------------------------------------------------------*/


	jQuery('#primary-menu ul').superfish({
		delay: 200,
		animation: {opacity:'show', height:'show'},
		speed: 'fast',
		autoArrows: false,
		dropShadows: false
	}); 


/*-----------------------------------------------------------------------------------*/
/*	Slides.js Settings - http://slidesjs.com/
/*-----------------------------------------------------------------------------------*/
	
	function dt_sliderInit() {
		
		if(jQuery().slides) {
			
			$slides = jQuery('#slides');
			$controls = jQuery('.next, .prev');
			
			$slides.slides({
				effect: 'fade',
				crossfade: true,
				generatePagination: false,
				preload: true,
				autoHeight: true,
				preloadImage: $slides.attr('data-img'),
				play:  parseInt($slides.attr('data-auto'))
			});
			
			$controls.css({ opacity: 0.9});
			
			$controls.hover( function() {
				jQuery(this).css({ opacity: 1});
			}, function() {
				jQuery(this).css({ opacity: 0.9});
			});
			
			$slides = jQuery('#teleport-slides');
			
			$slides.slides({
				effect: 'fade',
				crossfade: false,
				generatePagination: false,
				autoHeight: true,
				bigTarget: true,
				play:  parseInt($slides.attr('data-auto'))
			});


		}
		
	}
	
	dt_sliderInit();


/*-----------------------------------------------------------------------------------*/
/*	Overlay Settings
/*-----------------------------------------------------------------------------------*/
	
	
	$image = jQuery('.featured-image a');
	
	$image.hover( function() {
	
		jQuery(this).find('.overlay').css({
			opacity: 0,
			display: 'block'
		}).stop().animate({
			opacity: 1
		}, 160);
		
	}, function() {
	
		jQuery(this).find('.overlay').stop().animate({
			opacity: 0
		}, 160);
	
	});
	
	$image = jQuery('.effect a');
	
	$image.hover( function() {
	
		jQuery(this).stop().animate({
			opacity: 0.8,
		}, 160);
				
	}, function() {
	
		jQuery(this).stop().animate({
			opacity: 1,
		}, 160);
	
	});
	
	
	$image = jQuery('.post');
	
	$image.hover( function() {
	
		jQuery(this).find('.post-overlay').css({
			opacity: 0,
			display: 'block'
		}).stop().animate({
			opacity: 1
		}, 160);
		
	}, function() {
	
		jQuery(this).find('.post-overlay').stop().animate({
			opacity: 0
		}, 160);
	
	});


/*-----------------------------------------------------------------------------------*/
/*	Portfolio Filtering
/*-----------------------------------------------------------------------------------*/	
	
	
	$container = jQuery('#items');
	
	if(jQuery().isotope) {
	
		$container.isotope({
		    // options
		    itemSelector : '.post',
		    animationEngine: 'best-available'
		});
		 
		isOptionLinkClicked = false;
		
		// filter items when filter link is clicked
		jQuery('#filter a').click(function(){
			
			jQuery('#filter a').removeClass('active');
			jQuery(this).addClass('active');
			
			var selector = jQuery(this).attr('data-filter');
			
			$container.isotope({ filter: selector });
			
	        return false;
			
		});
	
	}
	
	
/*-----------------------------------------------------------------------------------*/
/*	Portfolio AJAX
/*-----------------------------------------------------------------------------------*/


	$post = jQuery('.post.on');
	$id = $post.attr('id');
	$teleport = jQuery('#teleport');
	$loader = jQuery('#loader');
	
	
	function dt_closeTeleport() {
		
		if($teleport.height() != 0 ) {
			
			$teleport.stop().animate({
				opacity: 0
			}, 200);
			
			$teleport.stop().animate({
				height: 0
			}, 600, 'easeOutQuart');
			
			jQuery('#filter').animate({
				marginTop: -20
			}, 100);
		}
		
	}
	
	function dt_openTeleport() {
		
		$teleport.animate({
			height: $teleport.find('.hentry').outerHeight()
		}, 800, 'easeOutQuart', function () {
			
			$teleport.css({
				height: 'auto'
			});
	
		});
		
		jQuery('#filter').animate({
			marginTop: 0
		}, 100);
		
		dt_close();
	}
	
	function dt_getPortfolio(postId) {
		
		$loader.fadeIn(200);
		
		jQuery.scrollTo('#content', 500);
		dt_closeTeleport();
		
		jQuery.ajax({
			url: jQuery('#teleport').attr('data-load'),
			context: document.body,
			data: "id="+$id,
			success: function(html){
			
				$teleport.html(html);
				dt_sliderInit();
				
				$teleport.find('img').batchImageLoad( {
					loadingCompleteCallback: function () { 
						dt_openTeleport();
						$loader.fadeOut(200);
					}
				});

	
			}
			
		});			
		
	}
	
	
	function dt_close() {
	
		jQuery('.close a').click( function() {
			
			$post.removeClass('active');
			dt_closeTeleport();
			return false;
		
		});
	
	}
	
	$post.click( function () {
		
		$this = jQuery(this);

		$id = $this.attr('id');
		$id = $id.split('post-')[1];
		
		if(!$this.hasClass('active')) {
			dt_getPortfolio($id);
			$post.removeClass('active');
			$this.addClass('active');
		}
		
		return false;
		
	});


/*-----------------------------------------------------------------------------------*/
/*	Plugins
/*-----------------------------------------------------------------------------------*/	

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = Math.ceil((ph-ah) / 2);
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

jQuery('.post-overlay-inner').vAlign();
	
/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/**
 *  Plugin which is applied on a list of img objects and calls
 *  the specified callback function, only when all of them are loaded (or errored).
 *  @author:  H. Yankov (hristo.yankov at gmail dot com)
 *  @version: 1.0.0 (Feb/22/2010)
 *	http://yankov.us
 */

(function($) {
$.fn.batchImageLoad = function(options) {
	var images = $(this);
	var originalTotalImagesCount = images.size();
	var totalImagesCount = originalTotalImagesCount;
	var elementsLoaded = 0;

	// Init
	$.fn.batchImageLoad.defaults = {
		loadingCompleteCallback: null, 
		imageLoadedCallback: null
	}
    var opts = $.extend({}, $.fn.batchImageLoad.defaults, options);
		
	// Start
	images.each(function() {
		// The image has already been loaded (cached)
		if ($(this)[0].complete) {
			totalImagesCount--;
			if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
		// The image is loading, so attach the listener
		} else {
			$(this).load(function() {
				elementsLoaded++;
				
				if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);

				// An image has been loaded
				if (elementsLoaded >= totalImagesCount)
					if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
			});
			$(this).error(function() {
				elementsLoaded++;
				
				if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
					
				// The image has errored
				if (elementsLoaded >= totalImagesCount)
					if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
			});
		}
	});

	// There are no unloaded images
	if (totalImagesCount <= 0)
		if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
};
})(jQuery);


/*-----------------------------------------------------------------------------------*/
/*	We've finished dancing!
/*-----------------------------------------------------------------------------------*/


});
