(function($){

	$.fn.initPortfolio = function(options) {
		
		
		
// ============================================================ //

					// VARIABLES
					
// ============================================================ //
		
		var imageTotal= $('div#portfolio_thumbs_scroll a').length;
		var imageTotalPol= $('div#portfolio_polaroid a').length;
			
		var imageLoad= 0;
		var imageLoadPol= 0;
			
		var imageSelect= 0;
		var imageSelectPol= 0;
			
		var scrollWidth= 10;
		var scrollWinWidth= $('div#portfolio_thumbs').width()-40;
		
		var polVisible= false;
		var scrollActive= false;
//
// ============================================================ //

				// SWITCH MAIN IMAGE
				
// ============================================================ //
		
		switchImage = function(target){ // Change status of thumbnail and change SRC of main image
			//
			var imagePath = target.attr('href');
			target.addClass('active').children('img').fadeTo(200, 0.9999).parent().parent().find('a').not(target).removeClass('active').children('img').fadeTo(400, 0.1);
			// find position in list of clicked item for next/prev buttons
			if(polVisible){
				imageSelectPol = $('div#portfolio_polaroid a').index(target);
			}else{
				imageSelect = $('div#portfolio_thumbs_scroll a').index(target);
			};
			//
			$('img#portfolio_main_img').fadeTo(300, 0, function() {
				$('img#portfolio_main_img').attr('src', imagePath);
				window.setTimeout(function() {
					setImageButtPos(target);
				}, 400);
			});
			//
		};
		//
		setImageButtPos = function(target){//position main image left and right arrows according to width of image
			
			var thisWidth = target.children('img').attr('width');
			//
			if (thisWidth>90){
				$('div#portfolio_main').css('width', 500);
				$('a#portfolio_main_prev').css('width', 249).css('background-position', '0 0');
				$('a#portfolio_main_next').css('width', 250).css('background-position', '113px 0');
			}else{
				$('div#portfolio_main').css('width', 274);	
				$('a#portfolio_main_prev').css('width', 137).css('background-position', '0 0');
				$('a#portfolio_main_next').css('width', 137).css('background-position', '0 0');
			};
			// fade image back in
			$('img#portfolio_main_img').fadeTo(300, 0.9999);
		};
		//
		advanceImage = function(direction){// Load next or previous image
			//
			if(polVisible){		
				imageSelectPol = imageSelectPol+direction;
				if(imageSelectPol<0){
					imageSelectPol= imageTotalPol-1;
				};
				if(imageSelectPol>imageTotalPol-1){
					imageSelectPol= 0;
				};
				switchImage($('div#portfolio_polaroid a').eq(imageSelectPol));
			}else{
				imageSelect = imageSelect+direction;
				if(imageSelect<0){
					imageSelect= imageTotal-1;
				};
				if(imageSelect>imageTotal-1){
					imageSelect= 0;
				};
				switchImage($('div#portfolio_thumbs_scroll a').eq(imageSelect));
				scrollToSelected(imageSelect);
			};
		};
//
// ============================================================ //

					// SCROLLING FUNCTIONS
					
// ============================================================ //
//
		findScrollWidth = function(){
			$('div#portfolio_thumbs_scroll a').each(function(i){
				scrollWidth = scrollWidth + $(this).children('img').attr('width') + 9;
			});
			//
			if(scrollWidth>scrollWinWidth){
				initScroll();
			};
			//
		};

		initScroll = function(){// Turn on thumb scroll
			
			//
			scrollActive = true;
			//
			var scrollRatio = (scrollWidth-876);
			$('div#portfolio_thumbs_scroll').css('width', scrollWidth).css('text-align', 'left');
			// realign thumbs
			$('div#portfolio_thumbs_scroll a:first').children('img').css('margin-left', 10);
			//
			$('div#portfolio_thumbs').mousemove(function(e){	
				var x = e.pageX - this.offsetLeft;			
				//
				var scrollPos = Math.round((x/700)*scrollRatio)-50;
				$('div#portfolio_thumbs').scrollLeft(scrollPos);
				
			}).scrollLeft(0);	
		};
		
		scrollToSelected = function(whichThumb){// Scroll thumbs to selected image
			if(scrollActive){
				var scrollPos = $('div#portfolio_thumbs_scroll a').eq(whichThumb).position();
				$('div#portfolio_thumbs').animate({scrollLeft: scrollPos.left}, 600);
			};
		};
//
// ============================================================ //

					// IMAGE LOADING FUNCTIONS
					
// ============================================================ //
//
		loadImage = function(target, whichTotal, whichLoad, loadMainPics){
			// load each main image in sequence and add events to thumbnails
			var nextImage = $(target).eq(whichLoad).data('imgPathHold');
			$(target).eq(whichLoad).removeData('imgPathHold');
			//
			var img = new Image();
			//
			$(img).load(function () {
				$(this).hide();
				if(whichLoad==0){var thisFade = 0.9999;}else{var thisFade = 0.1;};
				//
				$(target).eq(whichLoad).each(function() {
					$(this).hover(function() {
						if($(this).attr('class') != 'active') {
							$(this).children('img').fadeTo(100, 0.9999);	
						};
					}, function() {
						if($(this).attr('class') != 'active') {
							$(this).children('img').fadeTo(200, 0.1);
						};
					});
					// replace href
					$(this).attr({href: nextImage});
					// set thumbnail click
					var whichIndex = whichLoad;
					$(this).bind('click', function(){
						switchImage($(this));
						//switchImage(whichIndex, false);
						return false;
					});
					//
				}).children('img').fadeTo(200, thisFade, function(){		
					if(whichLoad==0){
						if(loadMainPics){
							findScrollWidth();
						};
					};
					//
					if(whichLoad<(whichTotal-1)){// if its not last image in row
						whichLoad ++;
						loadImage(target, imageTotal, whichLoad, loadMainPics);
					}else{
						// Load polaroids
						if(loadMainPics){// if its not main row start off polaroid row	
							//findScrollWidth();
							//
							if(imageTotalPol){// If there are polaroid images on page
								$("div#portfolio_options_a a").show();
								whichLoad=0;
								loadImage('div#portfolio_polaroid a', imageTotal, whichLoad, false);
							};
						};
					};
				});	
			}).attr('src', nextImage);
		};
//
// ============================================================ //

					// SWITCH POLAROID
					
// ============================================================ //
//	
	
		switchPol = function(target){// switch between main portfolio images and polaroids
			var tempTarget
			//
			if(! $(target).hasClass('selected') ) {
				$('div#portfolio_thumbs').toggle();
				$('div#portfolio_polaroid').toggle();
			};
			//
			$(target).addClass('selected').parent().find('a').not($(target)).removeClass('selected');
			//
			if($('div#portfolio_polaroid').is(':visible')){
				polVisible=true;
				tempTarget = $('div#portfolio_polaroid a').eq(imageSelectPol);
			}else{
				polVisible=false;
				tempTarget = $('div#portfolio_thumbs_scroll a').eq(imageSelect);
			};
			//
			var imagePath = tempTarget.attr('href');
			//
			$('img#portfolio_main_img').attr('src', imagePath);
			setImageButtPos(tempTarget);
		};

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

					// INITIALISE PAGE
					
// ============================================================ //
//

		removeHref = function(target){
			var imgPath = target.attr('href');
			target.unbind('click').removeAttr('href').data('imgPathHold', imgPath).children('img').fadeTo(0.01, 0.0001).css('display', 'inline');
		};
		//
		initialise = function(){
			// for each image link, remove/store href and hide image 
			$('div#portfolio_thumbs_scroll a').each(function(i){
				removeHref($(this));
			});
			//
			// Hide measurements
			$('div#portfolio_measurements').fadeTo(0.01, 0.0001).css('display', 'block');
			//
			// Hide polaroids
			$("div#portfolio_polaroid").hide();
			//
			if(imageTotalPol){// If there are polaroid images on page
				//
				$('div#portfolio_polaroid a').each(function(i){
					removeHref($(this));
				});
				//
				$("div#portfolio_options_a a").click(function () {
					switchPol(this);
					return false;
				});
			};
			//
			// set large image arrow buttons and hide
			$('div#portfolio_main').append('<a href="#" id="portfolio_main_prev"><\/a><a href="#" id="portfolio_main_next"><\/a>');
			$('div#portfolio_main a').fadeTo(0.01, 0.0001).css('display','block');
			//
			// set large arrow rollovers
			$('div#portfolio_main a').hover(function() {
				$(this).fadeTo(200, 0.6);		
				$('div#portfolio_measurements').stop().fadeTo(200, 0.9999);	
			}, function() {
				$(this).fadeTo(300, 0.0001);
				$('div#portfolio_measurements').stop().fadeTo(300, 0.0001);
			});
			//
			// set large arrow links
			$('a#portfolio_main_prev').bind('click', function(){
				advanceImage(-1);
				return false;
			});
			//
			$('a#portfolio_main_next').bind('click', function(){
				advanceImage(1);
				return false;
			});
			//
			// Start loading each thumbnail image
			loadImage('div#portfolio_thumbs_scroll a', imageTotal, imageLoad, true);
		};	
		initialise();
	
	};// end $.fn.initPortfolio
	

})(jQuery);  
		  
$(document).ready(function(){						   
	$('body').initPortfolio();
});