// ============================================== //

		// EASING FUNCTIONS //

// ============================================== //

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});


(function($){
		  
	// ============================================== //
	
			// SET ROUNDED CORNERS ON LINKS //
	
	// ============================================== //
	
	$.fn.setBlockLink = function(options) {
		var defaults = {
			spanBefore: '<span><span><span><span>',
			spanAfter: '</span></span></span></span>'
		};
		// Extend our default options with those provided.
		var opts = $.extend(defaults, options);
		// Our plugin implementation code goes here.
		block_link = function(target){
			var textHold = $(target).text();
			$(target).html(opts.spanBefore+textHold+opts.spanAfter);
			$(target).css('padding','0');
		};
		//		
		
		//
		$('a.block_link').each(function(i){
			block_link(this);
		});
		//
		$('#navigation ul li a.active').each(function(i){
			block_link(this);
		});
		
	};
	
	// ============================================== //

			// IMAGE PRELOAD //
	
	// ============================================== //

	$.fn.preloadImages = function() {
		
		for(var i = 0; i<arguments.length; i++){
			$("<img>").attr("src", arguments[i]);
		};
		
	};
	
	// ============================================== //

			// NEWS //
	
	// ============================================== //

	$.fn.initialiseNews = function (){
		//
		disableLink = function(target){
			$(target).removeAttr('href');
			$(target).unbind('click');
			$(target).addClass('disabled');
		};
		//
		enableLink = function(target){
			if($(target).attr('href') != '#'){
				$(target).attr({ href: '#'});
				//
				var whichNum = 0;
				if (target=='#news_more'){
					whichNum = 1;
				}else{
					whichNum = -1;
				};
				//
				$(target).bind("click", function(){
					advanceNews(whichNum);
					return false;
				});
				//
				$(target).removeClass("disabled");
			};
		};
		//
		advanceNews = function(n){
			increment +=n;
			//
			$('#news').animate({scrollTop: (offSet*increment)}, 800);
			//
			if(increment==total){
				disableLink('#news_more');
			}else{
				enableLink('#news_more');	
			};
			//
			if(increment==1){
				disableLink('#news_back');
			}else{
				enableLink('#news_back');	
			};	
		};
		//
		//// Initialise News
		//
		var newsHead='<div class="news_block"><\/div>';
		$('#news').prepend(newsHead).animate({scrollTop: 0}, 0.1);
		//
		var newsArr = new Array();
		var initInc = 0;
		var newsHold = '';
		$('#news div.news_item').each(function() {
			newsHold+='<div class="news_item">'+$(this).html()+'<\/div>';
			initInc++;
			if(initInc==3){
				newsArr.push(newsHold);
				newsHold='';
				initInc=0;
			};
			
		});
		//
		if(initInc<3){
			newsArr.push(newsHold);
		};
		//
		newsHold='';
		var total = newsArr.length;
		for(i=0; i<=total-1;i++){
			newsHold+='<div class="news_block">'+newsArr[i]+'<\/div>';
		};
		//
		$('#news').html(newsHead+newsHold);
		//
		var offSet = $('#news div.news_block').eq(0).height();
		var increment = 1;
		//
		$('#news_container').append('<a class="news_more" id="news_more" title="More news">More News</a><a class="news_back disabled" id="news_back" title="Back">Back<\/a>').css('margin-bottom','0');
		//
		enableLink('#news_more');
		//
		$(window).load(function () {
	        $('#news div.news_block').css('margin','0');
			$('#news').animate({scrollTop: offSet}, 800);
   		}); 
	};
	
	// ============================================== //

			// PORTFOLIO SLIDE VIEWER //
	
	// ============================================== //

	$.fn.unload_image = function(attr_a, attr_b){
		
		$(this).find('ul li:not(:first-child) img').each(function(i){
			$(this).attr(attr_b, $(this).attr(attr_a));
			$(this).removeAttr(attr_a);
		});
		
	};
	//
	$.fn.slideViewer = function(options) {
		var defaults = {
			pictWidth: 375,
			pictHeight: 210,
			animSpeed: 850
		};
		// Extend our default options with those provided.
		var opts = $.extend(defaults, options);
		var j=0;
		//
		return this.each(function(){
			var container = $(this);
			// number of li's in list
			var pictEls = container.find("li").size();
			// full width of ul
			var stripViewerWidth = opts.pictWidth*pictEls;
			//
			container.find("ul").css("width" , stripViewerWidth);
			//
			container.each(function(i) {
				//
				$(this).after('<div class="slideNav" id="slideNav' + j + '"><ul><\/ul><\/div>');
				//
				var whichList = $('div#slideNav' + j + ' ul');
				var linkList = '';
				$(this).find('li').each(function(n) {
					// For each UL add a number button
					linkList += '<li><a title="' + $(this).find('img').attr('alt') + '" href="#">'+(n+1)+'<\/a><\/li>';										
				});
				whichList.append(linkList);
				//
				$('div#slideNav' + j + ' a').each(function(z) {
					$(this).bind('click', function(){
						$(this).addClass('current').parent().parent().find('a').not($(this)).removeClass('current'); // wow!
						var cnt = - (opts.pictWidth*z);
						$(this).parent().parent().parent().prev().find('ul').data('currentState', z).animate({left: cnt}, opts.animSpeed, "easeInOutExpo");
						return false;
					});
				});
				jQuery('div#slideNav' + j + ' a:eq(0)').addClass('current');
				// make image clickable   
				$(this).find('ul').hover(function() {
					$(this).addClass('slide_hover');
				}, function() {
					$(this).removeClass('slide_hover');
				});
				//
				$(this).find('ul').data('currentState', 0).data('liTotal', pictEls).bind('click', function(){
					var z = $(this).data('currentState')+1;
					//
					if(z>$(this).data('liTotal')-1){
						z=0;
					};
					//
					var cnt = - (opts.pictWidth*z);
					//
					$(this)
						.animate({left: cnt}, opts.animSpeed, "easeInOutExpo")
						.data('currentState', z)
						.parent()
						.next()
						.find('a.current')
						.removeClass('current')
						.parent().parent()
						.find('a:eq('+z+')')
						.addClass('current');
				});
				j++;
			});
		});				 
	};
	
	$.fn.initialiseSlideViewer = function(){
		
			var target = '#content div.portfolio_item div.portfolio_slides';
			$(target).unload_image('src','original');
			//
			$(target).slideViewer();
			//
			$(window).load(function () {
				$('#content div.portfolio_item div.portfolio_slides').unload_image('original','src');
			});
		
	};

	// ============================================== //

			// MOVIE VIEWER //
	
	// ============================================== //

	$.fn.initialiseMovieViewer = function(){
		
		_getPageSize = function() {var xScroll, yScroll;if (window.innerHeight && window.scrollMaxY) {xScroll = window.innerWidth + window.scrollMaxX;yScroll = window.innerHeight + window.scrollMaxY;} else if (document.body.scrollHeight > document.body.offsetHeight){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;};var windowWidth, windowHeight;if (self.innerHeight) {if(document.documentElement.clientWidth){windowWidth = document.documentElement.clientWidth;} else {windowWidth = self.innerWidth;};windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;};if(yScroll < windowHeight){pageHeight = windowHeight;} else { pageHeight = yScroll;};if(xScroll < windowWidth){	pageWidth = xScroll;		} else {pageWidth = windowWidth;};arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;};

		_getPageScroll = function() {var xScroll, yScroll;if (self.pageYOffset) {yScroll = self.pageYOffset;xScroll = self.pageXOffset;} else if (document.documentElement && document.documentElement.scrollTop) {yScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;} else if (document.body) {yScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;};arrayPageScroll = new Array(xScroll,yScroll);return arrayPageScroll;};
		
		///
		isQTInstalled = function() {
			var qtInstalled = false;
			qtObj = false;
			if (navigator.plugins && navigator.plugins.length) {
				for (var i=0; i < navigator.plugins.length; i++ ) {
				 var plugin = navigator.plugins[i];
				 if (plugin.name.indexOf("QuickTime") > -1) {
					qtInstalled = true;
				 };
			  };
			} else {
				execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
				qtInstalled = qtObj;
			};
			return qtInstalled;
		};
		//
		qtWriteHtml = function(mov_path) {
			var qtHTML = "";
			qtHTML = QT_GenerateOBJECTText_XHTML(mov_path , '480', '285', '');
			return qtHTML;
		};
		//
		loadFlash = function(flv_path, mov_path){
			$('#lightbox-container-content').show();
			//
			if(isQTInstalled()) {
				$('#lightbox-flash').append(qtWriteHtml(mov_path));
			} else {
				var flashvars = {
					moviePath: flv_path
				};
				swfobject.embedSWF("http://www.emberinteractive.co.uk/includes/flash/ember_movie_player.swf", "lightbox-flash", "492", "298", "8.0.0", "expressInstall.swf", flashvars);
			};	
		};
		//
		positionMovie = function(){
			// Get page sizes
				var arrPageSizes = _getPageSize();
				// Style overlay and show it
				$('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = _getPageScroll();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				$('#jquery-lightbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 5),
					left:	arrPageScroll[0]
				});
		};		
		//
		setMoviePlayer = function(target){
			
			var linkPath = target.getAttribute('href',2);
			var query_string = linkPath.substring(0, linkPath.indexOf('.mov'));

			//
			var flv_path = query_string+'.flv';
			var mov_path = query_string+'.mov';
			var mov_title = target.getAttribute('title');
			
			//
			$('body').append('<div id="jquery-overlay"><\/div><div id="jquery-lightbox"><div id="lightbox-container"><div id="lightbox-container-content"><h2>'+mov_title+'<\/h2><a href=# title="Close window"><\/a><div id="lightbox-flash"><\/div><\/div><\/div><\/div>');
			//
			// Get page sizes
			var arrPageSizes = _getPageSize();
			//
			$('#jquery-overlay').css({
				backgroundColor:	'#040406',
				opacity:			0,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).animate({opacity: 0.9}, 250);
			//
			// Get page scroll
			var arrPageScroll = _getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 5),
				left:	arrPageScroll[0]
			}).show();
			//
			// Assigning click events in elements to close overlay
			$('#jquery-overlay,#jquery-lightbox,#lightbox-container-content a,').click(function() {
				clearTimeout(t);
				closeMoviePlayer();
				return false;	
			});
			
			$('#lightbox-container').click(function() {
				return false;						
			});
			
			$(window).resize(function() {
				positionMovie();
			});
			var t;
			$(window).scroll(function () { 
				clearTimeout(t);
				t = setTimeout(function() {positionMovie()}, 300); 
			});
			//
			positionMovie();
			$('#lightbox-container').animate({ width: 492},function() {loadFlash(flv_path, mov_path); });
			//
		};
		
		closeMoviePlayer = function() {
			$('#jquery-lightbox').remove();
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
			//
			$(window).resize(function() {
			});
		};
		
		this.unbind('click').click(function() {
			setMoviePlayer(this);
			return false;
		});
	
	};	
	
	
// ============================================== //

			// BANNER VIEWER //
	
// ============================================== //

	$.fn.initBannerViewer = function(){
		var target = this;
		//add unique Id to each element for flash embed
		$(this).find('dd').find('div.banner_flash_area').each(function(index){$(this).attr("id", "banner_flash_" + index);});
		//
		target.find('dt').find('a').bind('click', function(){
			openElement($(this));
			return false;							   
		});
		 //
		openElement = function(thisTarget){
			closeElement(target.find('a.banner_active'));
			thisTarget.unbind('click').html('close').bind('click', function(){
				closeElement($(this));
				return false;							   
			}).addClass('banner_active').parent().parent().next().slideDown('slow', function(){
				//var swfHeight = 
				//var swfWidth = ;
				var swfUrl = $(this).prev().find('a').attr('rel');
				var swfWidth = $(this).find('div.banner_flash_content').width();
				var swfHeight = $(this).find('div.banner_flash_content').height();
				var swfId = $(this).find('div.banner_flash_area').attr('id');
				//
				var flashvars = {};
				var params = {wmode: 'transparent',};
				var attributes = {};
				//
				swfobject.embedSWF(swfUrl, swfId, swfWidth, swfHeight, '8.0.0','', flashvars, params, attributes);
			});
			
		};
		//
		closeElement = function(thisTarget){
			if(thisTarget.attr('href')!=undefined){
				var swfId = thisTarget.parent().parent().next().find('object').attr('id');
				var thisHtml = '<div id="'+swfId+'" class="banner_flash_area"></div>';
				//
				thisTarget.unbind('click').html('view').bind('click', function(){
					openElement($(this));
					return false;							   
				}).removeClass('banner_active').parent().parent().next().find('div.banner_flash_content').html(thisHtml).parent().slideUp('slow');
			};
		};
	};
	
	
})(jQuery);
//
$(document).ready(function(){
	$('body').setBlockLink();
	
	$('body').preloadImages('images/bg_header.gif', 'images/bg_main.gif', 'images/bg_footer.gif', 'images/ember_image_loader.gif');
	// News initialise
	if ($('#news_container').length) {
		$('#news').initialiseNews()
	};
	// Portfolio slide viewer initialise
	if ($('#content div.portfolio_item').eq(0).length) {
		$('body').initialiseSlideViewer();
	};
	// Movie viewer initialise
	if ($('#content a.open_video_player').eq(0).length) {
		$('a.open_video_player').initialiseMovieViewer();
	};
	// portfolio banner viewer initialise
	if ($('dl#banner_viewer').length) {
		$('dl#banner_viewer').initBannerViewer();
	};
	// scroll to functions
	if(document.location.search){
		var query_string=document.location.search;
		query_string=query_string.split('?');
		var target=$('#'+query_string[1]);
		//
		var targetOffset = $(target).offset().top;
		//
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		return false;
	};
	
});

$(window).load(function () {
	/* set colour at end to prevent white flash on load */
	$('body').css("background-color","#E6ECF1");
});