// SINGLE PAGE - second page for projects and artists 

//set vars 
numberTinyThumbs = 0;
currentRow = 1;
totalRows	= 0;
currentImage = 0;
firstImage = 0;

artistName = "name";
artistNameLink = "name";
mailLink = 0;

// TINY THUMB CALLERBRATION 

// Pads the tiny thumbs so that they are always central
function sortTinyThumbs()
{
		// default padding for 8 thumbs
		defaultPadding = "25px";		
		// are we on the last row - if not just set padding to default
		if(currentRow == totalRows){
			// get number of thumbs of this last row
			numThumbs = numberTinyThumbs - ((totalRows-1) * 8) -1;
			// work out padding
			padding = (402 - (50*numThumbs))/2;
		}else{
			padding = defaultPadding;
		}
		// get width of tiny thumb holder
		$(document).find('.tiny-thumb-holder').css("margin-left", padding);				
}

// resize tuny thumbs so they fit properly
function resizeTinyThumbs()
{
		// count thumbs
		numImages = $(document).find('.tiny-thumb img').size();
		// work through all images and resize
		for (var i=0; i < numImages; i++) {
			// get thumb height and width
			th = $(document).find('#tt'+i).attr('height');
			tw = $(document).find('#tt'+i).attr('width');
			ratio = th / tw;
			// if height is greater than width adjust width to 48px
			if(th>tw){
				//$(document).find('#tt'+i).css('width', '48px');
				// adjust height to ration
				newHeight = Math.ceil(48 * ratio);
				pxHeight = newHeight+"px";
				$(document).find('#tt'+i).css('height', pxHeight).css('width', '48px');
				// set pad top to centre
				newPad = (48 - newHeight)/2;
				newPad = Math.ceil(newPad)+"px";
				$(document).find('#tt'+i).css('top',newPad);
			} else { // if width if greater then adjust height
				//$(document).find('#tt'+i).css('height', '48px');
				// adjust width to ration
				newWidth =  Math.ceil(48 / ratio);
				pxWidth = newWidth+"px";
				$(document).find('#tt'+i).css('width', pxWidth).css('height', '48px');
				// set pad left to centre
				newPad = (48 - newWidth)/2;
				newPad = Math.ceil(newPad)+"px";
				$(document).find('#tt'+i).css('left',newPad);
			}
			$(document).find('#tt'+i).fadeIn(400);
		}				
}

// TINY THUMB PRESSED 
function tinyThumbPressed(obj)
{
	var srcPath = $(obj).find('img').attr('thumbID');
	fadeToNewImage(srcPath);
}

// Nav Prev Next pressed - navigates to next or previous image
function nextPrevImage(dir)
{
	var d = dir; // DIRECTION
	
	// grab current image CountID
	var curCount = $(".tt"+currentImage).attr('countID');

	if (d=='n') {	var newCount = parseFloat(curCount) + 1; }
	else if (d=='p'){ var newCount = parseFloat(curCount) - 1; }
	
	// get id of next image
	var imageID =  $("#tt"+newCount).attr('thumbID');

	if(imageID){
		fadeToNewImage(imageID);
	}
	
}

// hide or show buttons

function hideShowPrevNext()
{
	// grab current image CountID
	var curCount = $(".tt"+currentImage).attr('countID');
	nCount = parseFloat(curCount) + 1;
  pCount = parseFloat(curCount) - 1;
	// get id of next image
	var nImageID =  $("#tt"+nCount).attr('thumbID');
	var pImageID =  $("#tt"+pCount).attr('thumbID');
	
	if(!nImageID){
		$('.next-image').fadeOut(200);
	}else{
		$('.next-image').fadeIn(200);
	}
	if(!pImageID){
		$('.prev-image').fadeOut(200);
	}else{
		$('.prev-image').fadeIn(200);
	}

}

// Nav buttons pressed
function nextSmallPressed()
{
		if(currentRow != totalRows){ 
			topPos = $(document).find('.tiny-thumb-holder').css("top");
			newPos = parseFloat(topPos) - 50;
			$(document).find('.tiny-thumb-holder').css('top',	newPos);
			currentRow++;
			sortSmallButtons()
		 	sortTinyThumbs()
		}
}

function prevSmallPressed()
{
		if(currentRow != 1){ 
			topPos = $(document).find('.tiny-thumb-holder').css("top");
			newPos = parseFloat(topPos) + 50;
			$(document).find('.tiny-thumb-holder').css('top',	newPos);
			currentRow--;
			sortSmallButtons()
		 	sortTinyThumbs()
		}
}


// bridge to fade first image
function hideAll(){
	fadeFirstImage();
}

// FADE TO FIRST IMAGE
function fadeFirstImage()
{				
			
		  var	newImage = firstImage; // NEW IMAGE
		
			currentImage = firstImage;				// set current image
			hideShowPrevNext();
			isSold(firstImage);								// sort sold sticker
			infoClose();											// close info panel
			hideImageInfo(newImage); // DISPLAY INFO
			
			// get image width 			
			var iWidth = 	$(document).find('#'+newImage).attr("width");
			var iWidth = parseFloat(iWidth);
			// SET LEFT PADDING
			var p = (770 - iWidth) / 2;
			// adjust info to center
			//var iPad = (iWidth - 390) / 2;
			
			// SHOW FIRST
			// pad info to center
			//$(document).find('#info-box').css('left',iPad);
			// hide all images
			$(document).find('#gallery-single img').hide();
			// set gallery single to correct size
			$(document).find('#gallery-single').css('left',p).css("width", iWidth).css('height',"420px").css('border','10px solid #f2f2f2');
			// fade all in
			$(document).find('#'+ newImage).delay(300).fadeIn('slow');
}


// FADE TO NEW IMAGE
function fadeToNewImage(srcPath)
{			
		  var	newImage = srcPath;	// NEW IMAGE
		
		if( currentImage != srcPath){
			currentImage = srcPath;						// set current image
			hideShowPrevNext();
			infoClose();											// close info panel
			isSold(srcPath);									// change sold 
			hideImageInfo(newImage); // DISPLAY INFO
			
			// get image width 			
			var iWidth = 	$(document).find('#'+newImage).attr("width");
			var iWidth = parseFloat(iWidth);
			// work out left padding
			var p = (770 - iWidth) / 2;
			// adjust info to center
			//var iPad = (iWidth - 390) / 2;

			// pad info to center
			//$(document).find('#info-box').css('left',iPad);
			// fade out image
			$(document).find('#gallery-single img').fadeOut('300');
			// animate holder to new width
			$(document).find('#gallery-single').delay(300).animate({'width':iWidth, 'left': p}, {duration:200}, "jswing" );
			// set padding of new image
			$(document).find('#'+ newImage).css("left", '0px');
			// fade new image in 
			$(document).find('#'+ newImage).delay(700).fadeIn('300');
		}
}

// sold - hide or show sold sticker
function isSold(src){
	s = $(document).find('#'+ src).attr('sold');
	if(s == 1){
		$(document).find('.sold').fadeIn('200');	
	}else{
		$(document).find('.sold').fadeOut('200');	
	}	
}

// resize image - sorts out top padding of image
function resizeImage()
{
	// count all images
	numImages = $(document).find('#gallery-single img').size();
	// work through all images and pad top
	for (var i=0; i < numImages; i++) { padTop(i); }
}

function padTop(imgNo)
{
	var iHeight = $(document).find('.imgNo'+imgNo).attr("height"); // work out height
	iHeight = parseFloat(iHeight);
	var pt = (420 - iHeight) / 2;		// work out new padding	
	// SET THE TOP PADDING IF HEIGHT IS LESS THAN 420
	if(iHeight < 440){
		$(document).find('.imgNo'+imgNo).css('top', pt);
	}
}

// IMAGE INFO
function hideImageInfo(imageId){
	// hides all and shows specified
	//$(document).find('#image-info-box').fadeOut(600);
	//$(document).find('.image-info-'+imageId).delay(700).fadeIn(700);
	$(document).find('#image-info-box').hide();
	$(document).find('.image-info-'+imageId).show();
	
}

// INFO 
// Info pressed 
function infoPressed(obj)
{
	$(document).find('#info-box').animate({'opacity':.8}, {duration:200}, "jswing" );	
//	$(document).find('#info-'+ currentImage).animate({'opacity':.8}, {duration:400}, "jswing" );	
}
// show Info
function showInfo(srcPath)
{			
		  var	newImage = srcPath;
			// FADE OUT
			$(document).find('#gallery-single img').fadeOut('300');
			$(document).find('#'+ newImage).fadeIn('300');
}
// close info
function infoClose()
{			
	$(document).find('#info-box').animate({'opacity':.0}, {duration:0}, "jswing" );	
}

// sort out small nav buttons 
function sortSmallButtons()
{
		if(currentRow == totalRows){ 
			$(document).find('.next-small').stop(false,true).animate({'opacity':0}, {duration:200}, "jswing" );	
		}else{
			$(document).find('.next-small').stop(false,true).animate({'opacity':1}, {duration:200}, "jswing" );	
		}
		if (currentRow == 1) {
			$(document).find('.prev-small').stop(false,true).animate({'opacity':0}, {duration:200}, "jswing" );	
		}else{
			$(document).find('.prev-small').stop(false,true).animate({'opacity':1}, {duration:200}, "jswing" );	
		}
}

// MAIL 
function	mailOut(w){
		// get current image title
		var imageTitle = $(document).find('#'+ currentImage).attr('imgtitle');
		var ml = $(document).find('#'+ currentImage).attr('mail');
		if(w=='a'){ var link = 'http://morgandaviesart.com/artists/'+artistNameLink+'/'+ml;}
		if(w=='p'){ var link = 'http://morgandaviesart.com/projects/'+artistNameLink+'/'+ml;}
		var email = "info@morgandaviesart.com"; 
		var subject = "Enquiry about "+artistName+' - '+imageTitle; 
		var body_message = "Link to artwork = "+link;
		var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
	  window.open(mailto_link,'emailWindow'); 
}





