var IntRotator = function(options){
	this.options = options;
	this.$rotator = options.$container;
};

IntRotator.prototype.init = function(){
	var images = [];
	
	this.$rotator.find('img').each(function(){
		images.push(this);
	})
	.hide();
	
	this.images = images;
	this.process();
};

IntRotator.prototype.process = function(){
	for(i in this.images){
		this.boxes.push(this.createBox(this.images[i]));
	}
	
	for(i in this.boxes){
		this.$rotator.append(this.boxes[i].show());
	}
};

IntRotator.prototype.generateView = function(){
	this.boxes[0].addClass('int-rotator-box-main');
	this.boxes[1].addClass('int-rotator-box-right');
	this.boxes[this.boxes.length-1].addClass('int-rotator-box-left');
};

IntRotator.prototype.show = function(){
	
	IntRotator.click = false;
	
	this.boxes[0].css({
		width: (this.boxes[0].data('dim').width) + 'px',
		height: this.boxes[0].data('dim').height + 'px',
		zIndex: 3600,
		opacity: 1		
	});
	
	this.boxes[this.boxes.length-1].css({
		top: (parseInt(this.boxes[this.boxes.length-1].css('top')) + this.boxes[this.boxes.length-1].data('dim').height * 0.05) + 'px',
		width: (this.boxes[this.boxes.length-1].data('dim').width - this.boxes[this.boxes.length-1].data('dim').width * 0.1) + 'px',
		zIndex: 3500,
		opacity: 0.4,
		height: (this.boxes[this.boxes.length-1].data('dim').height - this.boxes[this.boxes.length-1].data('dim').height * 0.1) + 'px'
	});
	
	this.boxes[1].css({
		top: (parseInt(this.boxes[1].css('top')) + this.boxes[1].data('dim').height * 0.05) + 'px',
		width: (this.boxes[1].data('dim').width - this.boxes[1].data('dim').width * 0.1) + 'px',
		zIndex: 3500,
		opacity: 0.4,
		height: (this.boxes[1].data('dim').height - this.boxes[1].data('dim').height * 0.1) + 'px'
	});
	
	this.boxes[0].animateWidth(true);
	this.boxes[this.boxes.length-1].animateWidth(false, {
		left: (this.boxes[this.boxes.length-1].data('pos').left - this.boxes[this.boxes.length-1].data('dim').width*0.5) + 'px'
	});
	this.boxes[1].animateWidth(false,{
		left: (this.boxes[1].data('pos').left + this.boxes[1].data('dim').width*0.5) + 'px'
	});
	
	IntRotator.enableClick(this.boxes[0]);
};

IntRotator.prototype.createBox = function(img){
	var $b = $('<div />');
	var $img = $(img);
	var pos = this.$rotator.offset();	
	
	$b.addClass('int-rotator-box');
	$b.width($img.width());
	$b.height($img.height());	
	

	if(this.$rotator.find('.int-rotator-fake').length == 0){
		var $fake = $('<div />');	
		$fake.addClass('int-rotator-fake');
		$fake.width($b.width());
		$fake.height($b.height());
		
		this.$rotator.append($fake);
	}
		
	var left = (pos.left + (this.$rotator.outerWidth())/2 - $img.width()/2) + 'px';
	var top = (pos.top + (this.$rotator.outerHeight())/2 - $img.height()/2) + 'px';
	
	$b.css({
		overflow: 'hidden',
		background: 'center center url("' + img.src + '") no-repeat',
		display: 'none',
		position: 'absolute',
		left: left,
		top: top
	});
	
	$b.data('dim', {width: $img.width(), height: $img.height()});
	$b.data('pos', {left: parseInt(left), top: parseInt(top)});
	
	
	return $b;
};

IntRotator.prototype.addLinks = function(){
	for(var i = 0; i < this.boxes.length; i++){
		this.boxes[i].bind('click.introtator', {context: this, $box: this.boxes[i]}, IntRotator.rotate);
	}
};

$.fn.leftSlide = function(animate_height){
	css = {			
			top: (this.data('pos').top + this.data('dim').height * 0.05) + 'px',			
			zIndex: 3500
		};
	
	anim = {
		left: (this.data('pos').left - this.data('dim').width*0.5) + 'px',
		width: (this.data('dim').width - this.data('dim').width * 0.1) + 'px'
	};
	
	if(!animate_height){
		css.height = (this.data('dim').height - this.data('dim').height * 0.1) + 'px';		
	}
	else{
		anim.height = (this.data('dim').height - this.data('dim').height * 0.1) + 'px';
		css.top = (this.data('pos').top) + 'px';
		anim.top = (this.data('pos').top + this.data('dim').height * 0.05) + 'px';
	}
	
	
	this.css(css).animateWidth(false, anim);
};

$.fn.rightSlide = function(animate_height){
	css = {			
			top: (this.data('pos').top + this.data('dim').height * 0.05) + 'px',			
			zIndex: 3500
		};
	
	anim = {
		left: (this.data('pos').left + this.data('dim').width*0.5) + 'px',
		width: (this.data('dim').width - this.data('dim').width * 0.1) + 'px'
	};
	
	if(!animate_height){
		css.height = (this.data('dim').height - this.data('dim').height * 0.1) + 'px';		
	}
	else{
		anim.height = (this.data('dim').height - this.data('dim').height * 0.1) + 'px';
		css.top = (this.data('pos').top) + 'px';
		anim.top = (this.data('pos').top + this.data('dim').height * 0.05) + 'px';
	}
	
	
	this.css(css).animateWidth(false, anim);
};

$.fn.centerSlide = function(hide){	
	if(hide){
		this.css({
			height: (this.data('dim').height - this.data('dim').height * 0.1) + 'px',
			width: (this.data('dim').width - this.data('dim').width * 0.1) + 'px',
			zIndex: 3500,
			opacity: 0.1
		})
		.animateWidth(false,{
			left: (this.data('pos').left) + 'px'
		}, function(ob){
			ob.hide();
		});
	}
	else{
		this.css({
			zIndex: 3600
		})
		.animateWidth(true,{		
			left: this.data('pos').left + 'px',
			top: this.data('pos').top + 'px'
		});
		
	}
};

IntRotator.rotate = function(ev){
	ev.stopPropagation();

	if(!IntRotator.click){
		return false;
	}
	

	
	IntRotator.click = false;
	c = ev.data.context;
	$box = ev.data.$box; 
	cl = $box.attr('class');

	$('.int-rotator-box-right').removeClass('int-rotator-box-right');
	$('.int-rotator-box-left').removeClass('int-rotator-box-left');
	$('.int-rotator-box-main').removeClass('int-rotator-box-main');
	
	if(cl.indexOf('int-rotator-box-left') != -1){
		last = c.boxes[c.boxes.length-1];
		// 1 1 2 1
		for(var i = c.boxes.length-1; i > 0 ; i--){
			c.boxes[i] = c.boxes[i-1];
		}
				
		c.boxes[0] = last;
		
		c.boxes[c.boxes.length-1].leftSlide();		
		c.boxes[0].centerSlide();		
		c.boxes[1].rightSlide(true);		
		
		if(c.boxes.length > 3)
			c.boxes[2].centerSlide(true);		
	}
	else if(cl.indexOf('int-rotator-box-right') != -1){			
		last = c.boxes[0];
		// 1 1 2 1
		for(var i = 0; i < c.boxes.length - 1; i++){
			c.boxes[i] = c.boxes[i+1];
		}
		

		
		c.boxes[c.boxes.length-1] = last;
				
		//c.boxes[0] = last;
	
		
		c.boxes[c.boxes.length-1].leftSlide(true);	
		 
		c.boxes[0].centerSlide();		
		c.boxes[1].rightSlide();		
		
		if(c.boxes.length > 3)
			c.boxes[c.boxes.length-2].centerSlide(true);		
	}
	
	c.boxes[c.boxes.length-1].addClass('int-rotator-box-left');
	c.boxes[1].addClass('int-rotator-box-right');
	
	c.boxes[0].addClass('int-rotator-box-main');
	
	IntRotator.enableClick($box);
};

IntRotator.enableClick = function($box){
	function enableClick(){
		if(jQuery.queue($box, "fx" ).length){
			setTimeout(enableClick, 50);
			
			return false;
		}
		
		IntRotator.click = true;
	}
	
	setTimeout(enableClick, 50);
}


IntRotator.click = true;
IntRotator.prototype.options = null;
IntRotator.prototype.images = [];
IntRotator.prototype.boxes = [];
IntRotator.prototype.$rotator = null;


$.fn.animateWidth = function(dir, add_anim, callback){
  anim = {};
	
	if(!add_anim){
		add_anim = {};
	}
	
	if(dir){		
		anim = {
				zIndex: 3600,				
				height: (this.data('dim').height) + 'px',
				width: (this.data('dim').width) + 'px',
				opacity: 1			
			};
	}
	else{
		anim = {
			zIndex: 3500,						
			opacity: 0.4
		};
	}
	
	if(this.is(':hidden'))
		this.show();

	$.extend(anim, add_anim);
	
	var ob = this;
	jQuery.queue(this, "fx", function(){
		jQuery.dequeue(this);
	});
	
	this.animate(anim, { 
		queue: true, 
		duration: 900, 
		complete: function(){
			jQuery.dequeue(ob, 'fx');

			if(callback){
				callback(ob);
			}
		}
	});
	
	return this;
};

$.fn.IntRotator = function(opt){
	if(!opt){
		opt = {};
	}
	
	if(!opt.$container)
		$.extend(opt, {$container: this});
	
	var r = new IntRotator(opt);
	r.init();
	r.generateView();
	r.show();
	r.addLinks();
};
