jQuery(document).ready(function() {

	if(jQuery('body').hasClass('page-template-page-home-php')){
	
		/* homepage hero */
		jQuery('#heroes').cycle({
			fx: 'scrollfadeHorz', 
			delay: 9000,
			timeout: 13000,
			speed: 900,
			//random: 1,
			//prev: 'a#cycleprev',
			next: 'a#cyclenext',
			pager: '.hero-nav',
			pagerAnchorBuilder: function(idx, slide) {
				return '<a href="#" class="slider-pager" title="View this slide"></a>';
			}
		});
		
		
		/* Project Carousel - Homepage */
		
		jQuery('#carousel').cycle({
			fx: 'scrollfadeHorz',
			delay: 8000,
			timeout: 16000,
			speed: 900,
			next: '#carousel-next',  // selector for element to use as click trigger for next slide 
			prev: '#carousel-prev'
		});
		
		jQuery('#heroes h2').hide();
		
	}
	
	/* Screenshot Carousel - Single project */
	if(jQuery('body').hasClass('single-post')){
		jQuery('#screenshot-slider').cycle({
			fx: 'fade',
			delay: 3000,
			timeout: 6000,
			speed: 900,
			pager: '.screenshot-nav',
			pagerAnchorBuilder: function(idx, slide) {
				idx++;
		    	return '<a href="#" class="screenshot-pager" title="View this slide">'+idx+'</a>';
			}	
		});
		
	}
		
	if(jQuery('body').hasClass('single-post') || jQuery('body').hasClass('page')){
		jQuery('.hentry h3:first').addClass('intro');
	}
	
	/* Remove border from last li on sidebar */

	jQuery('.sidebar ul li:last-child').addClass('last');
	//alert(jQuery.browser);
	
	if(jQuery('body').hasClass('category-work')){
		stdprojectFilter();
		allProjects();
		//twocolSlide('p.breadcrumb','.category-filter a.taglink','.project');
	} else {
		//alert('This is IE');
	}
	
	jQuery('.close-open-filter').click(function(e){
		jQuery('.category-filter ul.clearfix').slideToggle();
		if(jQuery(this).text() == 'Close filters'){
			jQuery(this).text('Open filters');
		} else {
			jQuery(this).text('Close filters');
		}
		jQuery.get('http://'+document.domain+'/interfacesession.php?filteropen=yes');
	});

	
	jQuery('.service').click(function(e){
		e.preventDefault();
		var href = jQuery('h3 a',this).attr('href');
		//alert(href);
		window.location = href;
	});
	
	jQuery('#cta-get-in-touch').scrollFollow({
	    //speed: 1000,
	    offset: -42
	    //killSwitch: 'exampleLink',
	    //onText: 'Disable Follow',
	    //offText: 'Enable Follow'
    });
    
});


// Standard project filter 

function stdprojectFilter(){
	var filterids = new Array();
	/*jQuery('.category-filter li').click(function(e){
		var href = jQuery('a',this).attr('href');
		//window.location = href;
	}*/
	jQuery('.category-filter a.taglink').click(function(e){
		var filterid = jQuery(this).attr('id');
		var filtername = jQuery(this).text();
		var breadhtml = jQuery('p.breadcrumb').html();
		jQuery('.project').animate({
			width: '0px',
			height: '0px',
			paddingLeft: '0px',
			paddingRight: '0px',
			marginLeft: '0px',
			marginRight: '0px',
			opacity: 0
		});
		//alert(jQuery('p.breadcrumb').html());
		if(!inArray(filterid, filterids)){
			filterids.push(filterid);
			jQuery(this).addClass('currentcat');
			var newbread = breadhtml.replace('<span>All projects</span>' ,'');
			jQuery('p.breadcrumb').html(newbread);
			jQuery('p.breadcrumb').append('<span class="'+filterid+'" style="display: none">'+filtername+'</span>');
			jQuery('span.'+filterid).delay(200).fadeIn();
		} else {
			if(filterids.length == '1'){
				jQuery('p.breadcrumb').html('<span class="showing">Showing: </span><span>All projects</span>');
			} else {
				//alert('This should remove: '+filterid+'\nOld bread: '+breadhtml+'\nNew bread: '+newbread);
				//newbread = breadhtml.replace(filterid ,'');
				jQuery('span.'+filterid).fadeOut();
			}
			removeByElement(filterids,filterid);
			jQuery(this).removeClass('currentcat');
		}
		
		jQuery('.project').each(function(){
			var infilter = true;
			var projectname = jQuery('h3 a', this).text();
			for(var i in filterids){
				//alert('This is filter class: '+filterids[i]);
				if(!jQuery(this).hasClass(filterids[i])){
					var infilter = false;
				}
				//alert(filterids[i]);
				var numelements = jQuery('.project').size();
				//confirm('This is the number of elements before we hide the filtered ones: '+numelements);
				//alert(filterid);
			}
			//alert(projectname+' is in the filter? '+infilter)
			if(infilter == false){
				jQuery(this).addClass('filtered');
				jQuery(this).fadeOut();
			} else {
				//jQuery(this).slideDown();
				jQuery(this).delay(200).fadeIn('slow');
				jQuery(this).animate({
					width: '414px',
					height: '250px',
					paddingLeft: '15px',
					paddingRight: '15px',
					marginLeft: '7px',
					marginRight: '7px',
					opacity: 1
				});
				jQuery(this).removeClass('filtered');
			}
		});
		
		if(filterids.length < '1'){
			// Clear the filters
			jQuery('.project').show();
			jQuery('a#tagclass-all').addClass('currentcat');
			jQuery('.project').removeClass('filtered');
			jQuery('p.breadcrumb').html('<span class="showing">Showing: </span><span>All projects</span>');
		}
		var filtelements = jQuery('.filtered').size();
		//alert('This is how many elements are filtered '+filtelements);
		var numelements = filtelements - numelements;
		//alert('This is the number of elements after we hide the filtered ones: '+numelements);
		/*jQuery('.project').each(function(e){
		});*/
		jQuery('a#tagclass-all').removeClass('currentcat');
		e.preventDefault();
	});
	
	
}

// All projects filter 
function allProjects(){
	jQuery('#tagclass-all').click(function(event){
		event.preventDefault();
		jQuery('p.breadcrumb').html('<span class="showing">Showing: </span><span>All projects</span>');
		jQuery('.project').animate({
			width: '414px',
			height: '250px',
			paddingLeft: '15px',
			paddingRight: '15px',
			marginLeft: '7px',
			marginRight: '7px',
			opacity: 1
			//overflow: 'visible'
		});
		jQuery('.project').fadeIn();
		jQuery('.project').removeClass('filtered');
		jQuery(this).addClass('currentcat');
		jQuery('a.taglink').removeClass('currentcat');
		filterids.length = 0;
	});
}

// Custom transition 'scrollfadeHorz'
	
jQuery.fn.cycle.transitions.scrollfadeHorz = function($cont,$slides,opts){
	$cont.css("overflow","hidden").width();
	opts.before.push(function(curr,next,opts,fwd){
		jQuery.fn.cycle.scrollfadeReset(curr,next,opts);
		opts.cssBefore.left=fwd?(next.cycleW-1):(1-next.cycleW);opts.animOut.left=fwd?-curr.cycleW:curr.cycleW;
	});
	opts.cssFirst={left:0};
	opts.cssBefore={top:0};
	opts.animIn={opacity:1,left:0};
	opts.animOut={opacity:0,top:0};
};

// Custom reset

jQuery.fn.cycle.scrollfadeReset=function(curr,next,opts,w,h,rev){
	jQuery(opts.elements).not(curr).hide();
	opts.cssBefore.opacity=0;
	opts.cssBefore.display="block";
	if(w!==false&&next.cycleW>0){
		opts.cssBefore.width=next.cycleW;
	}
	if(h!==false&&next.cycleH>0){
		opts.cssBefore.height=next.cycleH;
	}
	opts.cssAfter=opts.cssAfter||{};
	opts.cssAfter.opacity=0;
	opts.cssAfter.display="none";
	jQuery(curr).css("zIndex",opts.slideCount+(rev===true?1:0));
	jQuery(next).css("zIndex",opts.slideCount+(rev===true?0:1));
};

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function removeByElement(arrayName,arrayElement){
	for(var i=0; i<arrayName.length;i++ ){ 
		if(arrayName[i]==arrayElement)
	    	arrayName.splice(i,1); 
	} 
}

//Custom project transition

function twocolSlide(breadcrumb,trigger,target){
	var filterids = new Array();
	var projids = new Array();
	jQuery(trigger).click(function(e){
		var filterid = jQuery(this).attr('id');
		var filtername = jQuery(this).text();
		var breadhtml = jQuery(breadcrumb).html();
		//alert(jQuery('p.breadcrumb').html());

		// Get the positions of each element
		jQuery(target).each(function(){
			var position = jQuery(this).position();
			var projid = 'projid'+jQuery(this).index();
			projids.push(projid);
			//alert(projid);
			eval(projid+ ' = {top: '+position.top+', left: '+position.left+'}'); 	
			jQuery(this).attr('id', projid);
		});
		
		// Dsiplay breadcrumbs and on states
		if(!inArray(filterid, filterids)){
			filterids.push(filterid);
			jQuery(this).addClass('currentcat');
			var newbread = breadhtml.replace('<span>All projects</span>' ,'');
			jQuery(breadcrumb).html(newbread);
			jQuery(breadcrumb).append('<span class="'+filterid+'" style="display: none">'+filtername+'</span>');
			jQuery('span.'+filterid).delay(200).fadeIn();
		} else {
			if(filterids.length == '1'){
				jQuery(breadcrumb).html('<span class="showing">Showing: </span><span>All projects</span>');
			} else {
				//alert('This should remove: '+filterid+'\nOld bread: '+breadhtml+'\nNew bread: '+newbread);
				//newbread = breadhtml.replace(filterid ,'');
				jQuery('span.'+filterid).fadeOut();
			}
			removeByElement(filterids,filterid);
			jQuery(this).removeClass('currentcat');
		}
		
		jQuery(target).each(function(){
			var infilter = true;
			var projectname = jQuery('h3 a', this).text();
			for(var i in filterids){
				//alert('This is filter class: '+filterids[i]);
				if(!jQuery(this).hasClass(filterids[i])){
					var infilter = false;
				}
				//alert(filterids[i]);
				var numelements = jQuery('.project').size();
				//confirm('This is the number of elements before we hide the filtered ones: '+numelements);
				//alert(filterid);
			}
			//alert(projectname+' is in the filter? '+infilter)
			if(infilter == false){
				jQuery(this).slideUp();
				jQuery(this).addClass('filtered');
			} else {
				var newprojid = 'projid'+jQuery(this).index();
				var thisprojid = jQuery(this).attr('id');
				jQuery(this).css(thisprojid);
				//jQuery(this).removeClass('filtered');
			}
		});

		
		
	});
}
