// JavaScript Document

var $ = jQuery;

$(document).ready(function() {
// entire block clickable function
	$(".clickable").click(function(){
    	window.location=$(this).find("a").attr("href");return false;
	});
});


// GENERAL //

// cufon font replacement

Cufon.replace("h1,h2,h4,h5,nav,p.client,p.title,h3.client,h4.title", { fontFamily: "tradegothic", hover: true });


// remove a outline
$(document).ready(function() {
	$(".send").focus(function() {
  		$(this).blur();
	});
});


// z-index fix

$(document).ready(function() {
	$(function() {
		var zIndexNumber = 100;
		$('ul#work li,ul#casestudy li').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 1;
		}); 
	});
});


// HOME //

// add li classes 'work'

$(document).ready(function() {
	$("#work").find("#image").each(function(i) {
	$(this).addClass( "item" + i );
	});
});


// home rollover fade

$(document).ready(function() {

jQuery.each(jQuery.browser, function(i) {
   var lowEndBrowser = $.browser.msie && parseInt($.browser.version) < 9;
   if(lowEndBrowser){
   
	// if IE do this   
	$("ul#work li,ul#casestudy li").each(function() {
		$("ul#work li,ul#casestudy li").hover(function(){
			$(this).find("#tag,#tag_corner").stop().fadeTo(100,1);
			$(this).find("#image").stop().fadeTo(100,.5);
		},function(){
			$(this).find("#tag,#tag_corner").stop().fadeTo(100,0);
			$(this).find("#image").stop().fadeTo(100,1);
		});
	})
      
   }else{

	// not IE do this
	$("#work,#casestudy").hover(
		function() {}, 
		function() {        
		$("#work>li,#casestudy>li").stop().fadeTo(100,1);
		}
	);

	$("#work>li,#casestudy>li").hoverIntent(
		function(){
			$(this).attr("class", "current"); // Add class .current
			$(this).siblings().stop().fadeTo(100,.4); // Fade other items
			$(this).find("#tag,#tag_corner").stop().fadeTo(100,1); // Fade tag
			$(this).stop().fadeTo(100,1); // Fade current to 100%
		},
		function(){            
			$(this).removeClass("current"); // Remove class .current
			$(this).stop().fadeTo(100,1); // This should set the other's opacity back to 100% on mouseout
			$(this).find("#tag,#tag_corner").stop().fadeTo(100,0); // Fade tag 
		});
   
   }
 
 });
 
});


// news

// hover fade

$(document).ready(function() {
	$("article.post").each(function() {
		$("article.post").hover(function(){
			$(this).find("#overlay").stop().fadeTo(200,1);
			$(this).find("#image").stop().fadeTo(200,.5);
		},function(){
			$(this).find("#overlay").stop().fadeTo(200,0);
			$(this).find("#image").stop().fadeTo(200,1);
		});
	})
});


// product slideshow

// remove image link

$(document).ready(function(){
	$('#slideshow ul li.image a').removeAttr('href');
});


// slideshow

$(document).ready(function(){
	// Find out how many images are in our slideshow...
	$(function() {
		var slides = $('#slideshow1').children('li').length;
		if (slides > 1) {
			$('#slideshow1').cycle({
				fx:			'scrollHorz',
				speed:		700,
				timeout:		0,
				easing:		'easeinout',
				next:		'#next',
				prev:		'#prev',
				before:		onBefore,
				after:		onAfter
			});
		} else { // There's 0 or 1 image(s).
			$("#next").fadeOut(100);
			$("#prev").fadeOut(100);
		}
	});

	// onAfter
	function onAfter(curr,next,opts) {
	    	var caption = (opts.currSlide + 1) + '/' + opts.slideCount;
	    	$('#counter1').html(caption);

	    	var alt = $(next).find('img').attr('alt');
	    	$('#description span.caption').html(alt);

	    	$('#description span.caption').stop().fadeTo(100,1);
	}
	// end of onAfter

	// onBefore
	function onBefore() {
	    	$('#description span.caption').stop().fadeTo(100,0);
	}
	// end of onBefore
});



//slideshow for news

$(document).ready(function(){
	// Find out how many images are in our slideshow...
	$(function() {
		var slides = $('#slideshowNews1').children('li').length;
		if (slides > 1) {
			$('#slideshowNews1').cycle({
				fx:			'fade',
				speed:		700,
				timeout:	8000,
				pager:  '#sliderNav',
				pagerAnchorBuilder: function(idx, slide) { 
       			 	// return selector string for existing anchor 
        			return '#sliderNav li:eq(' + idx + ') a'; 
    			} 

			});
		} else { // There's 0 or 1 image(s).

		}
	});
});	
	






// work multitext


$(document).ready(function(){

	$(function() {
		$('#description1').cycle({
			fx:			'scrollVert',
			speed:		700,
			timeout:	0,
			easing:		'easeinout',
			next:		'#next.multi',
			prev:		'#prev.multi'
    });
});
	
});



// news masonry filter

$(document).ready(function(){

	// Set the speed for all transitions within.
	var speed = 700;
	
	$('#primary').masonry({ // Apply to all elements within the #primary div.
	    columnWidth: 320, 
	    // only apply masonry layout to visible elements
	    itemSelector: '.post:not(.invis)',
	    animate: true,
	    animationOptions: {
	        duration: speed,
	        easing: 'easeinout',
	        queue: false
	    }
	});
	
	$('#filtering-nav a').click(function(){ // Give each link in the in the filtering navbar a click function.
	
	    var colorClass = '.' + $(this).attr('class');
	    
	    if(colorClass=='.all') {
	        // show all hidden boxes
	        $('#primary').children('.invis')
	        	.toggleClass('invis').stop().animate({opacity: 1},{ duration: speed });
	            //.toggleClass('invis').stop().fadeTo(700, 1.0);
	    } else {
	        // hide visible boxes
	        $('#primary').children().not(colorClass).not('.invis')
	            .toggleClass('invis').stop().animate({opacity: 0},{ duration: speed });
	            //.toggleClass('invis').stop().fadeTo(700, 0.0);
	        // show hidden boxes
	        $('#primary').children(colorClass+'.invis')
	            .toggleClass('invis').stop().animate({opacity: 1},{ duration: speed });
	            //.toggleClass('invis').stop().fadeTo(700, 1.0);
	    }
	    
	    $('#primary').masonry();
	    return false;
	});

});

// contact form toggle values

$(document).ready(function(){

	swapValue = [];

	$(".text,.message").each(function(i){
		swapValue[i] = $(this).val();
		
	$(this).focus(function(){
		if ($(this).val() == swapValue[i]) {
		$(this).val("");
	}
	
	$(this).addClass("focus");
		}).blur(function(){
		if ($.trim($(this).val()) == "") {
		$(this).val(swapValue[i]);
		
	 $(this).removeClass("focus");
	 
	}
      
	});
   
});

});


// Staff page

$(document).ready(function(){
	// Find all the staff profiles.
	var staff = $("#staffGrid article");

	var nameTag = staff.children('div');
	nameTag.fadeTo(100, 0);
	
	var newImage = staff.children('img.active');
	newImage.fadeTo(100, 0);

	// Functions for each individual profile article:
	
	// Mouse Over
	var hoverOver = function() { /* Instructions for when the staff article is mouseovered. */
	
		var profile = $(this);
		
		// Increase the size of the article element to accomodate all the razzle dazzle.
		profile.css({
			marginTop: '-32px',
			marginRight: '-25px',
			marginBottom: '-8px',
			marginLeft: '-14px',
			height: '200px',
			width: '199px',
			zIndex: '20'
		});
		
		// Expand the replacement image into view.
		var newImage = profile.children("img.active");
		newImage.stop(true, false).animate({
			width: '180px',
	        	height: '178px',
	        	opacity: '1',
	        	top: '22px',
	        	left: '4px'
		}, 200, 'easeout');
		
		// Snap the old image back into the (new) centre of the article element.
		var oldImage = profile.children('img.inactive');
		oldImage.css({
			left: '14px',
			top: '32px'
		});
		
		// Fade the nametag out.
		var nameTag = profile.children('div');
		nameTag.stop(true, false).delay(200).fadeTo(150, 1.0); // Deactiavate the staff
		
		// Browser-selective effects...
		if (($.browser.msie) && ($.browser.version == "6.0" || $.browser.version == "7.0" || $.browser.version == "8.0")) {
			// IE6 is excluded from page-wide transitions (IE6's AlphaImageLoader DX Extension goes haywire with overlapping instances).
			// Overcome IE6's double padding issues.
			
		} else {
			// The following applies to all browsers except IE6.
			profile.siblings().stop(true, true).fadeTo(100, .4);
			profile.stop(false, false).fadeTo(100,1.0);
		}
		
	}
	
	// A function to deactivate a profile triggered after the nametag disappears.
	staff.active = function(state, profile) { /* True/false variable for 'state' determines the whether the staff article is active or deactive and the profile is holds the variable jQuery (DOM) element. */
		if (state!==true) {

			// Resize the profile article.
			//profile.stop(true, true);
			profile.css({
				marginTop: '0px',
				marginRight: '0px',
				marginBottom: '0px',
				marginLeft: '0px',
				height: '160px',
				width: '160px',
				zIndex: '10'
	        });

			// Move the original image back to its original position.	        
	        var oldImage = profile.children('img.inactive');
	        //oldImage.stop(true, false);
	        oldImage.css({
	        	left: '0px',
	        	top: '0px'
        	});
		}
	}
	
	// Mouse Out
	var hoverOut = function() { /* Instructions for when the staff article is abandoned. */

		var profile = $(this);
        	
		// Fade the nametag out.
		var nameTag = profile.children('div');
		nameTag.stop(true, false).fadeTo(150, 0.0, function () {staff.active(false, profile);}); // Deactiavate the staff

		// Fade the 'mouseover' image out and move it back to its old position.
		var newImage = profile.children('img.active');
		newImage.stop(true, true);
		newImage.animate({
			top: '32px',
			left: '14px',
			width: '160px',
			height: '160px',
			opacity: '0'
		}, 100, 'easeout');
	};

	staff.hover(hoverOver, hoverOut); // Set the profile animations to the extensive ones above.

	if (($.browser.msie) && ($.browser.version == "6.0" || $.browser.version == "7.0" || $.browser.version == "8.0")) {
		// Nothing to see here.
		var nameTags = staff.children('div');

		nameTags.css({
			'padding': '1px 1px 5px 1px'
		});
	} else {
		// Applies to the HTML5 section which houses the mass of profiles.
		var profileSpace = $("#staffGrid");
		profileSpace.hover(null, function() {
			$(this).children("article").stop(true, false).fadeTo(350, 1);
		});
	}

});
