$(document).ready(function()
{														 
	
	
	// REPLACE TAB LINKS WITH IDS TO TARGET CONTENT SWITCHER
	//$(".tab-1 a").attr("href", "#tab-1");
	$(".tab-2 a").attr("href", "#tab-2");
	$(".tab-3 a").attr("href", "#tab-3");
	$(".tab-4 a").attr("href", "#tab-4");
	$(".tab-5 a").attr("href", "#tab-5");
	$(".tab-6 a").attr("href", "#tab-6");
	$(".tab-7 a").attr("href", "#tab-7");
	
	// REMOVE THE no-js CLASS TO INDICATE JAVASCRIPT ENABLED
	$('body').removeClass('no-js');
	
	// JQUERY UI TABS WITH FADE EFFECT
	$("#tabs").tabs({ 
		fx: { opacity: 'toggle' },
		show: function(event, ui) {
			if ($('video.video').length) {
				// video element
				$('video.video').each(function() {
					this.pause();
				})
				// we don't want to auto play on mobile devices (most refuse this anyway)
				//$(ui.panel).find('video.video').get(0).play();
			} else {
				// flash
				$f('*').each(function() {
					this.pause();
				});			
				$(ui.panel).find('.video-player .video').flowplayer(0).play();
			}
		}
	});
	
	// LOAD SERVICES VIDEO ON CLICK
	$('.vid-link').click( function ()
	{
		$id = $(this).parent('li').attr('id');
                  
		$('#view-' + $id).overlay({
		
			// custom top position
			top: 260,
		
			// some mask tweaks suitable for facebox-looking dialogs
			mask: {
		
				// you might also consider a "transparent" color for the mask
				color: '#fff',
		
				// load mask a little faster
				loadSpeed: 100,
		
				// very transparent
				opacity: 0
			},
		
			// disable this for modal dialog-type of overlays
			closeOnClick: true,
		
			// load it immediately after the construction
			load: true
		
		});


	});
	
	
	// Turn all client images grey on the page load
	$('.grey').each( function (number){
		
		$imgObj = $('.grey')[number];
		
		if($.browser.msie){
			grayscaleImageIE($imgObj);
		} else {
			$imgObj.src = grayscaleImage($imgObj);
		} 
	});
	
	// Animate client images from black and white to colour
	$(".clients li .grey").mouseover(function()
	{ 
		$(this).animate({ opacity: 0 });
	})
	   
	$(".clients li .grey").mouseout(function()
	{
		$(this).animate({ opacity: 1 });
	});
	
	//
	$('#view_case_studies').click( function (){
			
			if( $('#case-studies-list ul').css('height') == '170px')
			{
				$('#case-studies-list ul').animate({ height: '100%' });
				$(this).html('<span>-</span> View Less');

				$('#case-studies-list ul .hide').slideDown(1000);

			}
			else
			{
				$('#case-studies-list ul').animate({ height: '170px' });
				$(this).html('<span>+</span> View All');
				$('#case-studies-list ul .hide').slideUp(1000);
			}
	});
	
	
	// this shouldn't be needed with flowplayer api
    // if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
    // {
    //  $('.iphone-video').css('display', 'block');
    //  $('.standard-video').css('display', 'none');
    // }

	
	
});

// Submit contact form
function submit_form()
{
	$('#contact-form .msg').slideUp();
	$('#contact-form .submit').css('display', 'none');
	$('#contact-form .ajax-loader').css('display', 'block');
	
	$name = $('#form-name').val();
	$email = $('#form-email').val();
	$company = $('#form-company').val();
	$telephone = $('#form-telephone').val();
	$service = $('#form-service').val();
	$requirements = $('#form-requirements').val();
	
	// Check postcode with ajax
	$.post("/includes/ajax.php?q=submit_form", {name : $name, email : $email, company : $company, telephone : $telephone, service : $service, requirements : $requirements}, function(data) 
	{
		if (data)
		{
			if (data == 1)
			{
				$msg = '<div class="success">Thank you for getting in touch - we will get back to you as soon as possible.</div>';
				$('#contact-form .submit').css('display', 'none');
				$('#form-name').val('');
				$('#form-email').val('');
				$('#form-company').val('');
				$('#form-telephone').val('');
				$('#form-service').val('');
				$('#form-requirements').val('');
			}
			else
			{
				$msg = data;	
			}
			
			$('#contact-form .ajax-loader').css('display', 'none');
			if (data != 1) $('#contact-form .submit').css('display', 'block');
			$('#contact-form .msg').html($msg);
			$('#contact-form .msg').slideDown();
		}
		
	}, "json");
	
	
	
	return;
}

function grayscaleImageIE(imgObj)
{
	imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
}

function grayscaleImage(imgObj)
{
	var canvas = document.createElement('canvas');
	var canvasContext = canvas.getContext('2d');
	
	var imgW = imgObj.width;
	var imgH = imgObj.height;
	canvas.width = imgW;
	canvas.height = imgH;
	
	canvasContext.drawImage(imgObj, 0, 0);
	var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
	
	for(var y = 0; y < imgPixels.height; y++){
		for(var x = 0; x < imgPixels.width; x++){
			var i = (y * 4) * imgPixels.width + x * 4;
			var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
			imgPixels.data[i] = avg; 
			imgPixels.data[i + 1] = avg; 
			imgPixels.data[i + 2] = avg;
		}
	}
	
	canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}

// Append a value in jquery
 $.fn.appendVal = function (newPart) {
   return this.each(function(){ $(this).val( $(this).val() + newPart); });
 };



// PRELOAD IMAGES FUNCTION (Was used for loading colour versions of greyscales)
/*function preload(arrayOfImages) {
	$(arrayOfImages).each(function(){
		//$('<img/>')[0].src = this;
		// Alternatively you could use:
		 (new Image()).src = this;
	});
}*/

