// Measure the dimensions of all images in the current jQuery array of elements:
// Optional: Specify deep=true to also measure dimensions of all images within the array of elements.
// Required: Provide a callback to receive the measurements. (This is necessary because images are loaded asynchronously)
// The callback will be triggered for every IMG in the jQuery array that has a "src" attribute.
// The callback is passed 2 params:
// - The first param is an object of {width,height,fileSize}.
// - The second param is the temporary IMG element that was used for measuring. (It gets destroyed after callback)
// Inside the callback you can use "this" to refer to the IMG element that was in the original jQuery array. 
// Note that fileSize can only be measured in IE. This value will be 0 in browsers that doe not support IMG.fileSize.
jQuery.fn.imgSize = function(deep,callback){

 callback = callback || deep;
 var $images = this.filter("IMG[src]");
 if(deep) $images.add( this.find("IMG[src]") );

 $images.each(function(){

  var origImg = this;
  var url = $(origImg).attr("src");

  $("<img>").load(function(){
   var $dummy = $(this), size = { width:$dummy.width(), height:$dummy.height(), fileSize:0 };
   try{ size.fileSize = parseInt($dummy.attr("fileSize")) || 0 }catch(e){};
   jQuery.isFunction(callback) && callback.apply( origImg, [size,this] );
   $dummy.remove();
  })
  // To be on the safe side, apply inline styles to prevent any css styles affecting our measurements:
  // (We use a try-catch workaround for IE7 because it raises errors when we try to set maxWidth/maxHeight)
  .css({ display:"none", width:"auto", height:"auto", minWidth:"auto", minHeight:"auto" })
  .each(function(){ try{ $(this).css({ maxWidth:"auto", maxHeight:"auto" }) }catch(e){}; })
  .addClass("imgSize-temp-img")
  .appendTo(document.body)    // The width/height would be zero if img is not added to DOM.
  .attr({ src:url });

 });

 return this;
}











/////////////







function ranRange(lowerlimit,upperlimit)
{

var rand_no = Math.floor((upperlimit-(lowerlimit-1))*Math.random()) + lowerlimit;

return rand_no;
}

jQuery(document).ready(function(){
	
	
	
	
////
// wait
//
$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };	
	




//
//
////
// animate home link
//
//var hbang = -ranRange(10,128);
//$('.nav .home a').wait(ranRange(10,10000)).animate({
//	left: hbang+'px'
//	}, ranRange(5000,9000), function(){
//	$(this).wait(ranRange(10,7000)).animate({
//		left: '-128px'
//		}, ranRange(5000,9000), function(){
		// end animation
//		});
//	});
//	
////
// animate about link
//
//var abang = -ranRange(10,124);
//
//
//$('.nav .about a').wait(ranRange(10,10000)).animate({
//	left: abang+'px'
//	}, ranRange(5000,9000), function(){
//	$(this).wait(ranRange(10,7000)).animate({
//		left: '-124px'
//		}, ranRange(5000,9000), function(){
		// end animation
//		});
//	});
//	
////
// animate portfolio link
//
//var pbang = -ranRange(10,108);
//
//
//$('.nav .portfolio a').wait(ranRange(10,10000)).animate({
//	left: pbang+'px'
//	}, ranRange(5000,9000), function(){
//	$(this).wait(ranRange(10,7000)).animate({
//		left: '-108px'
//		}, ranRange(5000,9000), function(){
		// end animation
//		});
//	});
//
////
// animate resources link
//
//var rbang = -ranRange(10,101);
//
//$('.nav .resources a').wait(ranRange(10,10000)).animate({
//	left: rbang+'px'
//	}, ranRange(5000,9000), function(){
//	$(this).wait(ranRange(10,7000)).animate({
//		left: '-101px'
//		}, ranRange(5000,9000), function(){
		// end animation
//		});
//	});
//
////
// animate contact link
//
//var cbang = -ranRange(10,114);
//
//$('.nav .contact a').wait(ranRange(10,10000)).animate({
//	left: cbang+'px'
//	}, ranRange(5000,9000), function(){
//	$(this).wait(ranRange(10,7000)).animate({
//		left: '-114px'
//		}, ranRange(5000,9000), function(){
		// end animation
//		});
//	});
//
//
//
//
//







////
////
//
// animate each nav link 
//
$('.nav a').each(function() {
	$(this).wait(ranRange(10,10000)).animate({
		left: (-ranRange(10,101))+'px'
		}, ranRange(5000,9000), function(){
		$(this).wait(ranRange(10,7000)).animate({
			left: -ranRange(10,101)
			}, ranRange(5000,9000), function(){
			// end animation
			});
		});
	});
	

////
// get file name from full src string
//
function newSrc(srcStr, big)
{
	var sbit = srcStr.split('/');
	var sName = sbit[sbit.length-1].split('-t');
	var sbName = sbit[sbit.length-1].split('-');
	if(big===true)
	{
		return sbit[0]+'/'+sbit[1]+'/'+sbit[2]+'/'+sbName[0]+'-1.jpg';
	}
	else if(big=='thumb')
	{
		return sbit[0]+'/'+sbit[1]+'/'+sbit[2]+'/'+sName[0]+'-t';
	}
	else if(big=='thumbindex')
	{
		var index = sName[1].split('.jpg');
		return index[0]
	}
	else
	{
		return sbit[0]+'/'+sbit[1]+'/'+sbit[2]+'/'+sName[0]+'-'+sName[1];
	}
}

function arrayThumbChange()
{
	
	
	
}


	
////
// onclick change img attr src
//	
$('#batch img').click(function() {

	$('#feat').attr('src', 'pixels/gap.png');
	
	
	$('#feat').attr('src', newSrc($(this).attr('src'),false));
	
	$('#feat').imgSize(function(size){
				 //alert(size.width + "x" + size.height + "px " + size.fileSize + " bytes");
				
//	if($(this).attr('alt')=='431x581')
	if(size.width < 600)
	{


		$('#feat').attr({
			width:431,
			height:581
			})
			.addClass('portrait');

	}
	else
	{
		
		$('#feat').attr({
			width:648,
			height:431
			})
			.removeClass('portrait');
	}

	});
	
	
	
				


	
	tpos = $(this).parent().children().index(this)+1
	
	tindex = newSrc($(this).attr('src'),'thumbindex');
	
	//console.log('tindex '+tindex+' tpos '+tpos);
	
	tstr = newSrc($(this).attr('src'),'thumb');
	
	i = 1;
	
	$(this).parent().children().each(function(index) {
//		if(index < tpos)
//		{
//			$(this).attr('src',tstr+i+'.jpg');
//			i++
//		}
//		else if (index >= tpos)
//		{
//			$(this).attr('src',tstr+(i+1)+'.jpg');
//			i++
//		}
		//console.log('index '+index);
		
		
		if(index+2 == tindex)
		{
			//console.log($(this).prev().attr('alt'));
			//$(this).attr('alt',$(this).prev().attr('alt'))
			$(this).attr('src',tstr+i+'.jpg');
			console.log('a index '+index+' change '+tindex+' i '+i );
			i++;
			
		}
		else if(index < tindex-1)
		{
			//$(this).attr('alt',$(this).prev().attr('alt'))
			$(this).attr('src',tstr+(i)+'.jpg');
			console.log('b index '+index+' change '+tindex+' i '+i );
			i++;
		}
		else
		{
			$(this).attr('src',tstr+(i+1)+'.jpg');
			console.log('c index '+index+' change '+tindex+' i '+i );
			i++;
		}
	});
	
	
	//$(this).attr('src', newSrc($(this).attr('src'),'thumb')+tpos+'.jpg');
	
	//console.log($(this).parent().children().index(this));
	});


//$('#feat').click(function() {
//
//	var sPath = newSrc($(this).attr('src'),true);
//	
//	if($(this).attr('alt')=='431x581')
//	{
//	$(this).attr('src', 'pixels/gap.png');
//	$(this).attr({
//		width:431,
//		height:581
//		})
//		.addClass('portrait');
//	$(this).attr('src', sPath);
//	}
//	else
//	{
//	$(this).attr('src', 'pixels/gap.png');
//	$(this).attr({
//		width:648,
//		height:431
//		})
//		.removeClass('portrait');
//	$(this).attr('src', sPath);
//	}
//});
	

});



