jQuery(document).ready(function() {

	jQuery("p:empty").remove();
	
	/* jQuery(".client").equalHeights(); */

	if(jQuery('body').hasClass('home')){
		/* Homepage hero */
		
		jQuery('#hero-container').cycle({
			fx: 'fade',
			delay: 3000,
			timeout: 6000,
			random: 1,
			speed: 900
		});
		
		jQuery('#what-we-do').show();
		
		jQuery('#what-we-do-slider').cycle({
			fx: 'fade',
			delay: 6000,
			timeout: 6000,
			speed: 900,
			before: makeLink,
			after: makeLink,
			pager: '#what-we-do-nav',
			pagerAnchorBuilder: function(idx, slide) {
	        var text = jQuery('a', slide).eq(0).attr('title');
	        var href = jQuery('a', slide).eq(0).attr('href');
	        return '<li><a class="left" href="#" title="' + text + '">' + text + '</a><a class="right" href="' + href + '" title="' + text + '"></a></li>';
			}
		});
		
		jQuery('li.activeSlide a.right').click(function(event){
			var href = jQuery(this).attr('href');
			window.location = href;
		});
	}
	
	/* Revmove pipe on last li - Footer */
	jQuery('.footer li:last-child').addClass('last');
	//jQuery('.worked-with li:last-child').addClass('last');
	
	jQuery('.image-gallery').cycle({
			fx: 'fade',
			pager: '.gallery-nav',
			timeout: 0,			
			pagerAnchorBuilder: function(idx, slide) {
	        //var src = jQuery(slide).eq(0).attr('id');
	        var src = jQuery(slide).attr('rel');
	        //return '<a href="#" title="' + src + '"></a>';
	        
	        return '<a href="#" class="gallery-thumb" title="View this image"><img width="43px" height="31px" src="'+src+'" alt="Gallery thumbnail image" /></a>';
			}
		        		
	});
	jQuery('#foot-slide').cycle({ 
	    fx:     'scrollHorz', 
	    timeout: 0, 
	    next:   '#next', 
	    prev:   '#prev' 
	});
	
	/*jQuery.each(jQuery.browser, function(i, val) {
   		if(i=="msie"){
   			if(jQuery.browser.version.substr(0,3)>"7"){
				jQuery('body').parallax({
			      'elements': [
			        {
			          'selector': 'div.clouds_mid',
			          'properties': {
			            'x': {
			              'background-position-x': {
			                'initial': 50,
			                'multiplier': 0.04,
			                'unit': '%'
			              }
			          	},
			            'y': {
			              'background-position-y': {
			                'initial': 294,
			                'multiplier': 0.00
			              }
			          	}
			          }
			        },
			        {
			          'selector': 'div.clouds_far',
			          'properties': {
			            'x': {
			              'background-position-x': {
			                'initial': 50,
			                'multiplier': 0.02,
			                'unit': '%'
			              }
			            },
			            'y': {
			              'background-position-y': {
			                'initial': 275,
			                'multiplier': 0.00
			              }
			          	}
			          }
			        },
			        {
			          'selector': 'div.clouds_close',
			          'properties': {
			            'x': {
			              'background-position-x': {
			                'initial': 50,
			                'multiplier': 0.01,
			                'unit': '%'
			              }
			            },
			            'y': {
			              'background-position-y': {
			                'initial': 279,
			                'multiplier': 0.00
			              }
			          	}
			          }
			        }
			      ]
			    });
			}
		} else {
		
			jQuery('body').parallax({
				'elements': [
				{
				  'selector': 'div.clouds_mid',
				  'properties': {
				    'x': {
				      'background-position-x': {
				        'initial': 50,
				        'multiplier': 0.04,
				        'unit': '%'
				      }
				  	},
				    'y': {
				      'background-position-y': {
				        'initial': 294,
				        'multiplier': 0.00
				      }
				  	}
				  }
				},
				{
				  'selector': 'div.clouds_far',
				  'properties': {
				    'x': {
				      'background-position-x': {
				        'initial': 50,
				        'multiplier': 0.02,
				        'unit': '%'
				      }
				    },
				    'y': {
				      'background-position-y': {
				        'initial': 275,
				        'multiplier': 0.00
				      }
				  	}
				  }
				},
				{
				  'selector': 'div.clouds_close',
				  'properties': {
				    'x': {
				      'background-position-x': {
				        'initial': 50,
				        'multiplier': 0.01,
				        'unit': '%'
				      }
				    },
				    'y': {
				      'background-position-y': {
				        'initial': 279,
				        'multiplier': 0.00
				      }
				  	}
				  }
				}
				]
			});
		
		}
	});*/
	
});

// 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); 
	} 
}

function makeLink(){
	jQuery('li.activeSlide a.right').click(function(event){
		var href = jQuery(this).attr('href');
		window.location = href;
	});
	//alert('Mitch\'s glitch');
}

//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');
			}
		});
		
	});
}
