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

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

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

		// HOME FLASH EMBED //

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

function embedFlash(){
	//
	var flashvars = {
	 xmlNavPath: 'http://www.zookinteriors.co.uk/index.php/scripts/zook_home_data'
	 // imageList: 'images/zook_bg_01.jpg,images/zook_bg_02.jpg,images/zook_bg_03.jpg,images/zook_bg_04.jpg,images/zook_bg_05.jpg,images/zook_bg_06.jpg,images/zook_bg_07.jpg,images/zook_bg_08.jpg,images/zook_bg_09.jpg'
	};
	//
	var params = {
		wmode: 'transparent',
		allowScriptAccess: 'always',
		scale: 'noscale',
		salign: 'lt'
	};
	//
	var attributes = {};
	//
	swfobject.embedSWF('../includes/flash/zook_home_anim.swf', 'home_flash_holder', '100%', '100%', '8.0.0','', flashvars, params, attributes);	
	$('#header').css('display', 'block');
};

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

		// CONTACT PAGE //

// ============================================== //
function initContactForm(){
	
	var clearMePrevious = '';

	// clear input on focus
	$('.clearMeFocus').focus(function() {
		if($(this).val()==$(this).attr('title')) {
			clearMePrevious = $(this).val();
			$(this).val('');
		}
	});
	
	// if field is empty afterward, add text again
	$('.clearMeFocus').blur(function(){
		if($(this).val()=='') {
			$(this).val(clearMePrevious);
		}
	});	
};

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

		// FOLIO VIEWER //

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

var contentWidth = 0;
var liMargin = 20;// match this to value in css
var imgArray = [];
var arrayLoadItem = 0;
var target = '';
var totalLi = 0;
var windowWidth = 0;
//
var moveVal = 0;
var winOffSet = 0;
var winOffSetLeft = 0;
var xPos =0;

function initFolioViewer(){
	//
	target= $('#folio_viewer');
	totalLi = target.find('ul').find('li').length-1;
	windowWidth = target.width();
	//
	target.find('ul').find('li').each(function(i) {
		imgArray.push($(this).find('img').attr('src'));
		$(this).find('img').hide();
	});
	//
	checkWidth();
};

function checkWidth(){
	if(target.find('ul').find('li').eq(totalLi).find('img').attr('width')>0){
		initFolioImages();
	}else{
		setTimeout('checkWidth()',100); 
	}
};

function initFolioImages(){
	target.find('ul').find('li').each(function(i) {
		var thisWidth = $(this).find('img').attr('width');
		contentWidth += (thisWidth+liMargin);
		$(this).css({'width' : thisWidth, 'height' : 520 });
	});
	//
	var offSet = (windowWidth - target.find('ul').find('li').eq(0).find('img').attr('width'))/2;
	var endOffSet = (windowWidth-target.find('ul').find('li').eq(totalLi).find('img').attr('width'))/2;
	contentWidth+=(offSet+endOffSet-liMargin);
	//
	target.find('ul').css({'width' : contentWidth, 'padding-left' : offSet});
	target.animate({scrollLeft: 0}, 0.01);
	//
	var nextImage = '../../images/loader.gif';
	var img = new Image();
	//
	$(img).load(function () {
		loadImage();
	}).attr('src', nextImage);
	//
	target.mouseenter(function(e){
		//startImageScroller();
		target.unbind('mousemove');
		moveVal = (contentWidth - windowWidth)/windowWidth;
		winOffSet = target.offset();
		winOffSetLeft = winOffSet.left; 
		xPos = e.pageX-winOffSetLeft;	
		target.animate({scrollLeft: (xPos*moveVal)}, 200, startImageScroller)
	});
};

function loadImage(){
	var nextImage = imgArray[arrayLoadItem];
	var img = new Image();
	//
	$(img).load(function () {
		target.find('ul').find('li').eq(arrayLoadItem).find('img').show();
		if(arrayLoadItem<totalLi){
			arrayLoadItem++;
			loadImage()
		};
	}).attr('src', nextImage);
};

function startImageScroller(){
	target.mousemove(function(e){	
		xPos = e.pageX-winOffSetLeft;		
		target.scrollLeft(xPos*moveVal);
	});
};


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

		// GALLERY VIEWER //

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


function initGalleryViewer(){
	target= $('#content_scroller_window');
	windowWidth = target.width();
	target.animate({scrollLeft: 0}, 0.01);
	totalLi = target.find('div.column').length;
	//
	contentWidth = 240*totalLi;
	target.find('#scroller_content').css('width',contentWidth);
	//
	target.mouseenter(function(e){
		target.unbind('mousemove');
		moveVal = (contentWidth - windowWidth)/windowWidth;
		winOffSet = target.offset();
		winOffSetLeft = winOffSet.left; 
		xPos = e.pageX-winOffSetLeft;	
		target.animate({scrollLeft: (xPos*moveVal)}, 200, startGalleryScroller)
	});
};

function startGalleryScroller(){
	//
	target.mousemove(function(e){	
		xPos = e.pageX-winOffSetLeft;
		target.scrollLeft(xPos*moveVal);
	});
};

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

		// MULTI IMAGE NAV //

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

(function($){
	
	$.fn.initialiseNews = function (){
		var target = this;
		var imageItem = 0;
		var imgArray = new Array();
		target.css({'width':164});
		//target.css({'margin-left': 0, 'width':178});
		//target.prepend('<a href="#" class="image_prev">1</a>');
		target.append('<a href="#" class="image_next">2</a>');
		//
		target.find('img').each(function(i) {
			imgArray.push($(this).attr('src'));
			if(i==0){
				//$(this).before('<a href="#">');
				//$(this).after('<a href="#">');
				$(this).wrap('<a href="#" class="image_img_next"></a>');

				$(this).css('float', 'left');
			}else{
				$(this).hide();
			}
		});
		//
		function advanceImage(){
			if (imageItem<imgArray.length-1){
				imageItem++;
				target.find('img').eq(0).attr('src', imgArray[imageItem]);
			}else{
				imageItem=0;
				target.find('img').eq(0).attr('src', imgArray[imageItem]);
			}
			if(imageItem<imgArray.length-1){
				target.find('a.image_next').html(imageItem+2);
			}else{
				target.find('a.image_next').html(1);
			}			  
		};
		//
		target.find('a.image_next').bind('click', function(){
			advanceImage();
		});
		target.find('a.image_img_next').bind('click', function(){
			advanceImage();
		});
	};
	
})(jQuery);

function initImageNav(){
	$('div#content').find('div.image_holder').each(function(i) {
		if($(this).find('img').length>1){
			$(this).initialiseNews();
		}
	});
};

//
$(document).ready(function(){	
	if ($('#scroller_content div.column').length>4) {  
		initGalleryViewer();
	};
	if($('div#content').find('div.image_holder').length){
		initImageNav();
	};
	if ($('#folio_viewer').length) {  
		initFolioViewer();
	};
	if($('#contact_form')) {
		initContactForm();	
	};
});

