/*
Name:       ImageStack
Version:    1.0.0
Author:     Brad Davis

Licence:    ImageStack may not be copied.
*/



/* ImageStack constructor */
function ImageStack ()
{
	/* Closure for this */
	var thisObject = this;
	var stack;
	this.stackSize = 5;
	this.maxHeight = 0;
	this.numImgs =  0; 
	this.imgPointer = 0;
	this.rotation = Array();


	
	this.showTop = function()
	{
		this.stack.parent().children('input').attr('disabled','disabled');
		this.stack.parent().children('input').show();

		this.stack.children("div").each(function(i){
			if((i - thisObject.imgPointer) < thisObject.stackSize && i>= thisObject.imgPointer)
			{
				if($(this).height()>$(this).parent().height())
				{
					$(this).parent().css('height', $(this).height()+90);
				}
				if(thisObject.rotation[i]!=true)
				{
					var r  = Math.floor(((Math.random()*20)-10)/2);
					$(this).css({
						'-moz-transform'    :'rotate('+r+'deg)',
						'-o-transform' :'rotate(30deg)'
					});		

					// Taken out because it looks terrible.	'-webkit-transform' :'rotate('+r+'deg)',
					thisObject.rotation[i]=true;
					
					var t  = Math.floor(Math.random()*20);
					var l  = Math.floor(Math.random()*20);
					$(this).css('top',(t+10)+'px');
					$(this).css('left',(l+15)+'px');
					if($(this).height()>$(this).width())
					{
						var r  = Math.floor(Math.random()*($(this).parent().width()-($(this).width()+30)));
						if(r<20)
						{
							r = 20;
						}
						$(this).css('left',r);
					}
				}
				$(this).show();
				$(this).css('visibility','visible');
				if(!$.browser.msie)
				{
					if(thisObject.stackSize - (i - thisObject.imgPointer) == 1)
					{
						$(this).css('opacity','0');
						$(this).animate({opacity:'1'},500);
					}
					else 
					{
						$(this).css('opacity','1');
					}
				}
			}
			else
			{
				$(this).css('visibility','hidden');
			}
		});
		this.stack.parent().children('span').html('Image '+(thisObject.imgPointer+1)+' of '+thisObject.numImgs);
		this.stack.parent().children('input').attr('disabled','');
	};

	this.nextImage = function()
	{
		
		this.imgPointer = this.imgPointer + 1;
		if(this.imgPointer > this.numImgs-1)
		{
			this.imgPointer = 0;
		} 
		this.showTop();
	};

	this.prevImage = function()
	{
		this.imgPointer = this.imgPointer - 1;
		if(this.imgPointer < 0)
		{
			this.imgPointer = this.numImgs-1;
		}
		this.showTop();
	};

	/* Initiate ImageStack */
	this.init = function (options)
	{
		thisObject.stack = options['stack'];
		thisObject.numImgs =  this.stack.children(".stack-img-wrapper").size(); 
		thisObject.maxWidth = this.stack.width();

		this.stack.css('position','relative');
		//Initialize and display
		this.stack.children(".stack-img-wrapper").each(function(i) {
			$(this).css('z-index',thisObject.numImgs-i);
			$(this).children('img').bind("load", function () { 
				var ref = thisObject;
				if($(this).height()>$(this).width())
				{
					$(this).height(360);
				}
				if($(this).width()>(ref.maxWidth-80))
				{
					$(this).width(ref.maxWidth-80);
				}
				$(this).parent().width($(this).width());
				$(this).parent().height($(this).height());
				$(this).parent().borderImage('url("/wp-content/themes/foodiefresh/images/ff/photo-border.png") 20 20 20 20');
				if(i==0)	
				{
					ref.showTop();
				}
			});
			$(this).children("img").each(function(){
				if(this.complete) $(this).trigger("load");
			});
		});
		
		this.stack.parent().children("input").each(function() {
			if($(this).attr('class')=='image-button-next')
			{
				$(this).bind('click',function(){
					thisObject.nextImage();
				});
			}
			if($(this).attr('class')=='image-button-prev')
			{
				$(this).bind('click',function(){
					thisObject.prevImage();
				});
			}
		});
	};
}

//Prepare the image stack
$(document).ready(function(){

	if ($.browser.msie) {
		$("#wrapper").css('border-width', '0px');
	}

	//The following is copyright Foodiefresh.com
	$("img[class*='wp-image']").each(function (i) {
		$(this).bind('load', function() {
			if($(this).attr('cornered')=='true')
			{
				return false;
			}
			$(this).attr('cornered','true');
			$(this).css('border-top','1px solid #CCC');
			$(this).css('border-left','1px solid #CCC');
			$(this).css('border-bottom','1px solid #999');
			$(this).css('border-right','1px solid #999');

			var w = $(this).width();
			var h = $(this).height();
			var tag = "<div class='blogImageBorder' style='position:relative;margin:8px auto 11px auto;padding:0px;width:"+w+"px; display:block; height: "+h+"px;'/>";
			var cl = $(this).attr('class');
			$(this).removeClass();
			$(this).parent().wrap(tag);

			var check = $(this).parent().parent().parent();
			if(check.hasClass('wp-caption'))
			{
				var borderWidth = w+40;
				check.css('width', borderWidth+'px');
			}

			
			$(this).parent().parent().addClass(cl);
			var borderDivRef = $(this).parent().parent();
			borderDivRef.css('position', 'relative');
			var random = Math.floor(Math.random()*4+1);

			borderDivRef.append("<div class='tc tc"+random+"' style='width: "+(w+20)+"px; height: "+(h+20)+"px;'></div>");
			borderDivRef.append("<div class='bc bc"+random+"' style='width: "+(w+20)+"px; height: "+(h+20)+"px;'></div>");
			
		});

		if(this.complete) $(this).trigger("load");
	});
});

$(document).ready(function(){
	$(".imagestack").each(function(){
		var instance = new ImageStack();
		instance.init({stack : $(this)});
	});
});

