/*=============================================================
Type:               Main JS file
Document Author:    Dogwoof devs
Author Email:          
============================================================ */

(function( win, $, undefined ){

	var slideshow = $('.slideshow'),
	    slides = slideshow.find('.slides li'),
	    controls = slideshow.find('.triggers a'),
	    next = 1;
    
	slides.bind('click',function(){
	    location.href = $(this).find('a').eq(0).attr('href');
	    return false;
	});

	controls.bind('click', function(e, dontcancel){
	    var self = $(this),
	        href = self.attr('href'),
	        target = slides.filter(href),
	        others = slides.filter(':not('+href+')');
	    if (dontcancel!==true) clearInterval(change);
	controls.removeClass('current');
	    others.fadeOut(500);
	self.addClass('current');
	    target.fadeIn(500);
	    return false;
	});

	var window_focus = true;
		
	$(window).focus(function(){
			window_focus = true;
	});
	$(window).blur(function(){
			window_focus = false;
	});
	
	var change = setInterval(function(){
		if(window_focus){
			controls.eq(next).trigger('click', true);
			next++;
			if ( next > 2 ) next = 0;
		}
	}, 4000);

   
})( this, jQuery );
