/**
	Copyright (c) 2008 Victor Stanciu; contact [at] victorstanciu [dot] ro; http://www.victorstanciu.ro/

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

/** 
 * @description		showcase plugin for jquery.js
 * @author		Victor Stanciu; contact [at] victorstanciu [dot] ro; http://www.victorstanciu.ro/ 				
 * @ported to jquery by 	Federico Garc’a
 * @date		03/10/2008
 * @updated	25/06/2010
 * @requires		jquery 1.4.2
*/

function Showcase() {}
Showcase.prototype.init = function(sections, controls, options) {

	//Perform calculations considering 9 elements max
	this.allSections = this.sections = sections.slice(0, 9);

	this.controls 	= controls;
	
	this.options = {
		ratio: 					0.1,
		initialDelay: 			1,
		duration: 				250,			
		size:					this.sections.length,
		onChange:				function(index) {},
		startID:				null
		};
		
	for(key in options) {
		this.options[key] = options[key]
	}
				
	this.running 		= false;
	this.queue			= new Array;
	
	this.computeMetrics();	
	
	//Calculations ready, now get all sections
	this.allSections 	= this.sections = sections

	this.sections = this.allSections.slice(this.currentIndex - this.half, this.currentIndex + this.half + 1);
	
	$(this.allSections).each(jQuery.proxy(function (index, section) {

		$(section).css({
			position: 'absolute',
			zIndex: Math.abs(index - this.sections.length),
			left: '50%',
			top: '50%',
			marginLeft: -Math.round($(section).outerWidth() / 2) + 'px',
			marginTop: -Math.round($(section).outerHeight() / 2) + 'px'
		})
				
		this.allSections[index].initialIndex = index;
				
		var thisSection = section
		$(section).bind('click', {el: section}, jQuery.proxy(function(event) {
			this.allSections[index].disabled = (index != this.currentIndex);
			this.jump(event)
		}, this))
		
		$(section).bind('mouseover', jQuery.proxy(function (event) {
			$(section).css('opacity', 1);
		}))
			
		$(section).bind('mouseout', function() {
			$(section).css('opacity', section.opacity);
		})
		
		this.allSections[index].opacity = 1;		
				
		if(jQuery.inArray(section, this.sections) == -1) {
			$(section).hide()
			this.queue.push(section);
		}	
	
	}, this))
	
	for (i = 0; i <= this.half; i++) {
		this.sections.push(this.sections.shift());
	}
	
	$(this.controls).bind('click', jQuery.proxy(this.click, this));
	
	//Start in specified position
	if(this.options.startID != null) {
		
		this.indexSections();
		var startID = this.options.startID;
		
		var evt = function() {}
		evt.data = {el : $('#'+startID).get(0) }
		evt.stopPropagation = function() {}

		this.jump(evt);
		
	} else {
		this.animate();
	}
	
}

Showcase.prototype.computeMetrics = function () {

	if(this.options.size > 9)
		var size = 9;
	else
		var size = this.options.size;

	this.half 			= this.currentIndex = (this.options.size - 1) / 2;		
	this.ratioStep 		= Math.round(((1 - this.options.ratio) / this.currentIndex) * 100) / 100;
	this.positionStep 	= Math.round(50 / this.half * 100) / 100;		
	this.maxDimensions 	= { width: $(this.sections[0]).outerWidth(), height: $(this.sections[0]).outerHeight() };

}
	
Showcase.prototype.click = function (event) {
	event.stopPropagation();
	
	var element = event.target;

	if (!this.running) {
		eval("this." + element.rel + "()");
	}		
	
	this.animate(element.rel);

}
	
Showcase.prototype.previous = function () {
	if (this.options.size < this.allSections.length) {
		var sectionIn 	= this.queue.shift();
		var sectionOut 	= this.sections.pop();		
	
		this.sections.unshift(sectionIn);
		
		$(sectionOut).fadeOut(this.options.duration)
		this.queue.push(sectionOut);
	} else {
		this.sections.unshift(this.sections.pop());
	}
}

Showcase.prototype.next = function () {		
	if (this.options.size < this.allSections.length) {
		var sectionIn 	= this.queue.shift();
		var sectionOut 	= this.sections.shift();

		this.sections.push(sectionIn);
		
		$(sectionOut).fadeOut(this.options.duration)

		this.queue.push(sectionOut);
	} else {
		this.sections.push(this.sections.shift());
	}
}

Showcase.prototype.jump = function (event) {
	
	event.stopPropagation();

	var found = jQuery.inArray(event.data.el, this.sections);

	if(found == -1)
		return false;

	if (!this.running) {
		var section = this.sections[found];

		var direction = '';
		
		if (section.index < this.half) {
				
			for(var i = 0; i < (this.half - section.index); i++)
				this.previous()
				
			direction = 'previous';
		} else if (section.index == this.half) {

		} else {
			for(var i = 0; i < (section.index - this.half); i++)
				this.next()
				
			direction = 'next';
		}
	}
	
	this.animate(direction);
}

Showcase.prototype.runEffects = function () {
	
	this.stackSections();

	this.running = true;

	$(this.effects).each(jQuery.proxy(function(i, effect) {
	
		$(effect.section).animate(effect.style, { 
			duration: this.options.duration,
			complete: jQuery.proxy(function() { this.running = false}, this),
			queue: false
		})
	
	}, this))

	this.options.onChange(this.currentIndex)
}
	
Showcase.prototype.stackSections = function () {

	$(this.sections).each(function (k, section) {		
		$(section).css({zIndex: section.stackIndex});
	});
}
	
Showcase.prototype.indexSections = function () {	
	$(this.sections).each(jQuery.proxy(function (index, section) {
		this.sections[index].index 			= index;
		this.sections[index].modifier 		= Math.abs(Math.abs((section.index - (this.sections.length - 1) / 2)) - this.half);
		
		this.sections[index].ratio 			= Math.round(((section.modifier * this.ratioStep) + this.options.ratio) * 100) / 100;			
					
		this.sections[index].width 			= Math.min(Math.round(this.maxDimensions.width * section.ratio), this.maxDimensions.width);
		this.sections[index].height 			= Math.min(Math.round(this.maxDimensions.height * section.ratio), this.maxDimensions.height);		
		
		this.sections[index].positionIndex 	= (section.index - (this.sections.length - 1) / 2);
		this.sections[index].stackIndex 		= Math.abs(Math.abs((section.index - (this.sections.length - 1) / 2)) - this.half) + 1;
		
		this.sections[index].left 			= section.top = Math.round((this.half + section.positionIndex) * this.positionStep);
		this.sections[index].opacity			= Math.min(section.ratio, 1);			
	}, this));
}	
	
function ShowcaseHorizontal(sections, controls, options) {
	this.init(sections, controls, options)
}
	
ShowcaseHorizontal.prototype = new Showcase;
ShowcaseHorizontal.prototype.animate = function (direction) {
	this.indexSections();
	
	this.effects = new Array();		
	$(this.sections).each(jQuery.proxy(function (i, section) {
		var style = {
			left: 		section.left + '%', 
			top:		'50%',
			marginTop:	-Math.abs(section.height / 2) + 'px',
			width: 		section.width + 'px', 
			height: 	section.height + 'px',
			opacity: section.opacity
			};
		
		if (section.left == 0) {
			style.marginLeft = '0px';
			} else if (section.left == 50) {
				style.marginLeft 		= -Math.round(section.width / 2) + 'px';					
				} else if (section.left == 100) {
					style.marginLeft 	= -section.width + 'px';
					} else {
						style.marginLeft = -Math.round(section.width / 2) + 'px';
						}
		
		this.effects.push({section: section, style: style});		
	}, this));

	this.currentIndex = this.sections[Math.round(this.half)].initialIndex;			
	
	this.runEffects();
}

function ShowcaseVertical(sections, controls, options) {
	this.init(sections, controls, options)
}
	
ShowcaseVertical.prototype = new Showcase;
ShowcaseVertical.prototype.animate = function (direction) {
	this.indexSections();
	
	this.effects = new Array();		
	$(this.sections).each(jQuery.proxy(function (i, section) {
		var style = {
			top: 				section.top + '%', 
			left:				'50%',
			marginLeft:			-Math.abs(section.width / 2) + 'px',				
			width: 				section.width + 'px', 
			height: 			section.height + 'px',
			opacity: section.opacity				
			};
		
		if (section.top == 0) {
			style.marginTop = '0px';
			} else if (section.top == 50) {
				style.marginTop = -Math.round(section.height / 2) + 'px';					
				} else if (section.top 	== 100) {
					style.marginTop = -section.height + 'px';
					} else {
						style.marginTop = -Math.round(section.height / 2) + 'px';
						}
		
		this.effects.push({section: section, style: style});
	}, this));

	this.currentIndex = this.sections[this.half].initialIndex;			
	
	this.runEffects();

}

function ShowcaseDiagonal(sections, controls, options) {
	this.init(sections, controls, options)
}

ShowcaseDiagonal.prototype = new Showcase;	
ShowcaseDiagonal.prototype.animate = function (direction) {
	this.indexSections();
	
	this.effects = new Array();		
	$(this.sections).each(jQuery.proxy(function (i, section) {
		var style = {
			left: 		section.left + '%', 
			top: 		section.top + '%',				
			width: 		section.width + 'px', 
			height: 	section.height + 'px',
			opacity: section.opacity
			};
		
		if (section.left == 0) {
			style.marginLeft = '0px';
			} else if (section.left == 50) {
				style.marginLeft 		= -Math.round(section.width / 2) + 'px';					
				} else if (section.left == 100) {
					style.marginLeft 	= -section.width + 'px';
					} else {
						style.marginLeft = -Math.round(section.width / 2) + 'px';
						}
						
		if (section.top == 0) {
			style.marginTop = '0px';
			} else if (section.top == 50) {
				style.marginTop = -Math.round(section.height / 2) + 'px';					
				} else if (section.top 	== 100) {
					style.marginTop = -section.height + 'px';
					} else {
						style.marginTop = -Math.round(section.height / 2) + 'px';
						}							
		
		this.effects.push({section: section, style: style});		
	}, this));

	this.currentIndex = this.sections[this.half].initialIndex;			
	
	this.runEffects();

}