$(document).ready( function() {
	
	var google_plus_1_url;
	$('.sharing').each( function(i) {
		$(this).find('li').each( function(i) {
			if ( $(this).text() == '+1' ) {
				google_plus_1_url = $(this).parent('ul').parent('div').parent('div').parent('div').children('.for-google-plus-1').html();
				if ( google_plus_1_url ) {
					google_plus_1_url = google_plus_1_url.substring(4, google_plus_1_url.length-3);
					$(this).html('<g:plusone href="'+google_plus_1_url+'"></g:plusone>');
				} else {
					$(this).html('<g:plusone></g:plusone>');
				}
			}
		});
	});
	
	if ( $('#submenu').length > 0 ) {
		vertical_center(50, '#submenu .title');
	}
	if ( $('#what-we-do').length > 0 ) {
		
		$('#what-we-do .curved-box').hide();
		$('#what-we-do .curved-box:first').show();
		$(window).bind("load", function() {
			vertical_center(80, '.curved-box:first span');
		});
		
		$('#what-we-do-menu a').click(function() {
			$('#what-we-do .curved-box').hide();
			var new_content = $(this).attr('href');
			$(new_content).show();
			
			vertical_center(80, '.vertical-centre span');
			
			return false;
		});
	}
	function vertical_center(box_height, element) {
		$(element).each( function(i) {
			text_padding = (box_height - $(this).height()) / 2;
			$(this).css('padding-top', text_padding+'px');
		});
	}
	
	
	
// Text-shadow for IE9 and below.
// -----------------------------------------------------------------------------

	if ($.browser.msie && $.browser.version.substr(0,1) < 10) {
		if ($.browser.version.substr(0,1) == 7) {	// IE7.
			var shadow_h1 = { x: 1, y: -3, radius: 0, color:  "#91c267" }
			var shadow_h2 = { x: 1, y: -2, radius: 0, color:  "#91c267" }
			var shadow_home = { x: 1, y: -1, radius: 0, color:  "#91c267" }
			var shadow_blog = { x: 1, y: -4, radius: 0, color:  "#91c267" }
			var shadow_cal = { x: 1, y: 1, radius: 0, color:  "#91c267" }
			
		} else {	// Every other version of IE up to 9.
			var shadow_h1 = { x: 1, y: 1, radius: 0, color:  "#91c267" }
			var shadow_h2 = shadow_h1
			var shadow_home = shadow_h1
			var shadow_blog = shadow_h1
			var shadow_cal = shadow_h1
		}
		
		$("#main h1").textShadow( shadow_h1 );
		$(".page-id-5 h2").textShadow( shadow_h2 );	// Contact page.
		$(".wide-col .extra-content p").textShadow( shadow_home );	// Contact page.
		$(".home .entry-content p").textShadow( shadow_home );	// Home page.
		$("#primary h3").textShadow( shadow_blog );	// Home page.
		$("#wp-calendar caption").textShadow( shadow_cal );	// Home page.
	}

// -----------------------------------------------------------------------------





// Facebook like integration
// -----------------------------------------------------------------------------

	// if ( (!$.browser.msie) || ($.browser.msie && $.browser.version.substr(0,1) > 6) ) {
	// 	$('head').append('<meta property="og:image" content="'+js_theme_url+'/nav/logo_fb.png" />');
	// 
	// 	$('.facebook-like').each( function(i) {
	// 		$(this).html(
	// 			 '<iframe src="http://www.facebook.com/plugins/like.php?href='+$(this).attr('title')
	// 			+'&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;font=arial'
	// 			+'&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; '
	// 			+'overflow:hidden; width:350px; height:35px;" allowTransparency="true"></iframe>'
	// 		);
	// 	});
	// 	
	// 	$('.facebook-like').css('display', 'block');
	// }

// -----------------------------------------------------------------------------





// Add cellspacing to wp-calendar
// -----------------------------------------------------------------------------

	$('#wp-calendar').attr('cellspacing', '2');

// -----------------------------------------------------------------------------





// Crop testimonial bubble content.
// -----------------------------------------------------------------------------
	
	$('.project-testimonial').each( function(i) {
		if ( $(this).find('.hide').length > 0 ) {
			$(this).find('p').append(' <span class="toggle">...More</span>');
			$(this).find('.hide').hide();
		}
	});
			
	$('.project-testimonial .toggle').click( function() {
		$(this).siblings('.hide').toggle();
		$(this).text( $(this).text() == "...More" ? "...Less" : "...More" );
	});

// -----------------------------------------------------------------------------





// Fix for Firefox rounding.
// -----------------------------------------------------------------------------
	
	fix_firefox_rounding();
});

$(window).resize(function() {
	fix_firefox_rounding();
});

function fix_firefox_rounding() {
	if ( $.browser.mozilla ) {
		if ( $(window).width() % 2 == 0 ) {
			$('#searchform #s').css('left', '7px');
		} else {
			$('#searchform #s').css('left', '6px');
		}
	}
}

// -----------------------------------------------------------------------------





// Portfolio image swapping.
// -----------------------------------------------------------------------------
function image_swap() {
	
	// Load all large images.
	$('#project-image-menu').after('<div id="temp-swap" style="display: none"></div>');
	$('#project-image-menu ul li a').each( function() {
		$('#temp-swap').append( '<img src="'+$(this).attr('href')+'" />' );
	});
	
	// When a thumbnail is clicked.
	$('#project-image-menu a').click( function() {
		if ( $('#image-fade .second').length <= 0 ) {
			
			image_size = $(this).attr('class').split(' ');
			clickedthumb = $(this);
			// margin_left = ( 468 - image_size[0].substr(1) ) / 2; // (col width - image width) / 2
			margin_left = ( 448 - image_size[0].substr(1) ) / 2; // (col width - image width) / 2
			caption_width = parseInt( image_size[0].substr(1) );
		
			// Duplicate image.
			$('#image-fade img').after(
				'<img src="'+clickedthumb.attr('href')+'" style="'+$('#image-fade img:first').attr('style')
				+'" alt="'+clickedthumb.children('img').attr('alt')+'" class="second" />'
			);
			
			$('#image-fade img').animate({
				width: image_size[0].substr(1),
				height: image_size[1].substr(1),
				marginLeft: margin_left
			}, 300, function() {
			});
			
			$('#image-fade img:first').animate({
				opacity: 0
			}, 300, function() {
				$('#image-fade .second').removeClass('second');
				$('#image-fade img:first').remove();
					
				// Caption.
				$('#image-fade .caption').text( clickedthumb.children('img').attr('alt') );
			});
		
		
			$('#large-image-caption').animate({
				width: caption_width
			}, 300, function() {
			});
		}
		
		return false;
	});
}

// -----------------------------------------------------------------------------





// Animate menu
// -----------------------------------------------------------------------------

function animate_menu(env) {
	if (env == 'computer') {
		
		// Initialise.
		$('#menu').removeClass('submenu');
		$('#submenu-inner').hide();
		
		// if ( $.browser.msie ) {
		if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
			$('#entry-content').append('<div id="content-fade"></div>');
		}
		
		// When parent button is hovered.
		$('#parent-button, #see-more-menu').hover( function() {
			$('#menu').addClass('usejs');
			
			// if ( $.browser.msie ) {
			if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
				$('#content-fade').css({'width': '100%', 'height': $('#entry-content').height()+'px', 'opacity': '0.7'});
				$('#submenu-inner').show();
				
			} else {
				$('#entry-content').animate({
					opacity: '0.3'
				});
				$('#submenu-inner').fadeIn();
			}
		});
		
		// When menu div is blurred.
		$('#menu').hover( function() {
		}, function() {
			// if ( $.browser.msie ) {
			if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
				$('#submenu-inner').hide();
				$('#menu').removeClass('usejs');
				$('#content-fade').css({'width': '0px', 'height': '0px', 'opacity': '0'});
				
			} else {
				$('#submenu-inner').fadeOut( function() {
					$('#menu').removeClass('usejs');
				});
				$('#entry-content').animate({
					opacity: '1'
				});
			}
		});
	}
}



(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}

	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
		
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
		
			start = toArray(start);
		
			fx.start = [start[0],start[2]];
		
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
		
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

// -----------------------------------------------------------------------------


/* Created by Martin Hintzmann 2008 martin [a] hintzmann.dk
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 *
 * Version: 0.2
 * Requires: jQuery 1.2+
 * http://plugins.jquery.com/project/textshadow
 *
 */
(function($) {
	$.fn.textShadow = function(option) {
		if (!$.browser.msie) return;
		var IE6 = $.browser.version < 7;
		return this.each(function() {
			var el = $(this);
			var shadow = el.textShadowParse(this.currentStyle["text-shadow"]);
			shadow = $.extend(shadow, option);

			el.textShadowRemove();

			if (shadow.x == 0 && shadow.y == 0 && shadow.radius == 0) return;

			if (el.css("position")=="static") {
				el.css({position:"relative"});
			}
			el.css({zIndex:"0"});
			if (IE6) {
				el.css({zoom:"1"});
			}
			
			var span=document.createElement("span");
			$(span).addClass("jQueryTextShadow");
			$(span).html(el.html());
			$(span).css({
				padding:		this.currentStyle["padding"],	
				width:		el.width(),
				position:	"absolute",
				zIndex:		"-1",
				color:		shadow.color!=null?shadow.color:el.css("color"),
				left:			(-parseInt(shadow.radius)+parseInt(shadow.x))+"px",
				top:			(-parseInt(shadow.radius)+parseInt(shadow.y))+"px"
			});
			
			if (shadow.radius != 0) {
				if (shadow.opacity != null) {
					$(span).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelradius="+parseInt(shadow.radius)+", enabled='true', makeShadow='true', ShadowOpacity="+shadow.opacity+")");
				} else {
					$(span).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelradius="+parseInt(shadow.radius)+", enabled='true')");
				}
			}	
			el.append(span);
		
	  });
	};
	
	$.fn.textShadowParse = function(value) 
	{
		value = String(value)
			.replace(/^\s+|\s+$/gi, '')
			.replace(/\s*!\s*important/i, '')
			.replace(/\(\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^\)]+)\s*\)/g, '($1/$2/$3/$4)')
			.replace(/\(\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^\)]+)\s*\)/g, '($1/$2/$3)')
	
		var shadow = {
			x      : 0,
			y      : 0,
			radius : 0,
			color  : null
		};

		if (value.length > 1 || value[0].toLowerCase() != 'none') {
			value = value.replace(/\//g, ',');
			var color;
			if ( value.match(/(\#[0-9a-f]{6}|\#[0-9a-f]{3}|(rgb|hsb)a?\([^\)]*\)|\b[a-z]+\b)/i) && (color = RegExp.$1) ) {
				shadow.color = color.replace(/^\s+/, '');
				value = value.replace(shadow.color, '');
			}

			value = value
				.replace(/^\s+|\s+$/g, '')
				.split(/\s+/)
				.map(function(item) {
						return (item || '').replace(/^0[a-z]*$/, '') ? item : 0 ;
					});

			switch (value.length)
			{
				case 1:
					shadow.x = shadow.y = value[0];
					break;
				case 2:
					shadow.x = value[0];
					shadow.y = value[1];
					break;
				case 3:
					shadow.x = value[0];
					shadow.y = value[1];
					shadow.radius = value[2];
					break;
			}
			if ((!shadow.x && !shadow.y && !shadow.radius) || shadow.color == 'transparent') {
				shadow.x = shadow.y = shadow.radius = 0;
				shadow.color = null;
			}
		}

		return shadow;
	};

	$.fn.textShadowRemove = function() {
		if (!$.browser.msie) return;
		return this.each(function() {
			$(this).children("span.jQueryTextShadow").remove();
		});
	};
})(jQuery);

if(typeof Array.prototype.map == 'undefined') {
	Array.prototype.map = function(fnc) {
		var a = new Array(this.length);
		for (var i = 0; i < this.length; i++) {
			a[i] = fnc(this[i]);
		}
		return a;
	}
}

