/*
 * Detail Plugin
 * Author: Emanuele Tortolone
 * http://www.fillstudio.com
*/

function Detail(element, opts)
{
	this.defaults = {
			url:'',
			closeBtn:'',
			prevBtn:'',
			nextBtn:'',
			marginBottom:''
	};
	
	this.options = $.extend(this.defaults, opts);
	
	this.instance = $(element);
	this.loading = null;
	this.content = null;
	this.data = null;
	this.closeBtn = null;
	this.prevBtn = null;
	this.nextBtn = null;
	
	this.marginBottom = this.options['marginBottom'] != '' ? this.options['marginBottom'] : '0px';
};

Detail.prototype = {
		init : function()
		{
			this.createElements();
			this.setDefaultStyle();
		},
		
		createElements : function()
		{
			//	LOADING
			this.instance.append("<div class='detail_loading'></div>");	
			this.loading = $('.detail_loading', this.instance);
			
			//	CONTENT
			this.instance.append("<div class='content'></div>");	
			this.content = $('.content', this.instance);
		},
		
		hideLoading : function()
		{
			var $this = this;
			$(this.loading).animate({'height':'0px'}, 1000, 'easeInOutQuart',function()
			{
				$this.loading.html('');
				$this.loading.remove();
			});
		},
		
		setDefaultStyle : function()
		{
			var $this = this;
			this.instance.css({
				'position': 'relative',
				'overflow':'hidden'
			});
			
			$(this.loading).html('<span>Loading...</span>');
			var finalLoadingHeight = $(this.loading).css('height');
			$(this.loading).css(
			{
				'height':'0px'
			});
			
			//	LOADING ANIMATION / LOAD CONTENT
			$(this.loading).animate({'height':finalLoadingHeight}, 1000, 'easeInOutQuart', function()
			{
				$this.loadContent();
			});
		},
		
		loadContent : function()
		{
			var $this = this;
			this.instance.trigger('LOAD');
			$.ajax({
				cache:false,
				url:$this.options['url']+'/true',
				success:function(data)
				{
					$this.data = data
					$this.loadCompleteHandler();
				}
			});
		},
		
		loadCompleteHandler : function ()
		{
			this.instance.trigger('LOAD_COMPLETE');
			
			this.initContent();
			this.hideLoading();
		},
		
		initContent : function()
		{
			var height = '40px';
			this.content.append(this.data);
			this.instance.css({'height':height});
			
			//	BTN LISTENERS
			this.closeBtn = this.options['closeBtn'] != '' ? $(this.options['closeBtn'], this.instance) : null;
			if(this.closeBtn != null)
			{
				this.closeBtn.bind('click', {instance:this}, this.close);
			}
			
			this.nextBtn = this.options['nextBtn'] != '' ? $('a.'+this.options['nextBtn'], this.instance) : null;
			if(this.nextBtn)
			{
				this.nextBtn.bind('click', {instance:this}, this.changeProject);
			}
			
			this.prevBtn = this.options['prevBtn'] != '' ? $('a.'+this.options['prevBtn'], this.instance) : null;
			if(this.prevBtn)
			{
				this.prevBtn.bind('click', {instance:this}, this.changeProject);
			}
			
			$this = this;
			_toolbarArray.push({toolbar: $('.toolbar', $this.instance),parent:this.instance});
				
			$(window).bind('scroll',function()
					{
						var toolcount = _toolbarArray.length;
						for(var i=0;i<toolcount;i++)
						{
							var newPos = ($(document).scrollTop() - _toolbarArray[i].parent.position().top)+10;
							if(newPos < 15){newPos = 15};
							if(newPos > _toolbarArray[i].parent.height()-70){newPos = _toolbarArray[i].parent.height()-70};
							_toolbarArray[i].toolbar.stop().animate({'top' : newPos},100);
						}
			});
			
			//	OPEN DETAIL
			var detailHeight = this.content.height();
			this.instance.delay(100).animate({'height': detailHeight, 'margin-bottom':this.marginBottom}, 1000, 'easeInOutQuart');
			
		},
		changeProject : function(e)
		{
			var $this = e.data.instance ? e.data.instance : this;
			var url = ($(e.currentTarget).attr('href'));
			var ind = url.lastIndexOf('/');
			var code = url.substring(ind+1);
			_next_project = $('#'+code);
			_next_project.trigger('mouseenter');
			$('.open_detail',_next_project).trigger('click');
			$.scrollTo(_next_project.position().top - _marginTop, 500);
			
			/*
			var projectDetailList = _next_project.has('h3 > a');
			if(projectDetailList.length > 0)
			{
				for (var i=0;i<_openedDetailArray.length;i++){
					if(_next_project.has(_openedDetailArray[i]).length>0)
					{
						
					}
				}
				$('.open_detail',_next_project).trigger('click');
			}
			else
			{
				//$this.close({data:{}});
			}
			*/
			return false;
		},
		close :function(event)
		{
			var $this = event.data.instance ? event.data.instance : this;
			$(event.target).unbind('click', $this.close);
			$this.instance.animate({'height': '0px', 'margin-bottom':'0px'}, 1000, 'easeInOutQuart', function()
			{
				$this.content.css({'display':'none'});
				
				if($this.closeBtn != null)
				{
					$this.closeBtn.bind('click', {instance:$this}, $this.close);
				}
				$this.instance.trigger('CLOSE');
			});
			$this.instance.trigger('CLOSE_SELECTED');
		}
};

$.fn.detail = function(options) 
{
	if(this.length) 
	{
		this.each(function() 
		{
			var new_detail = new Detail(this, options);
			new_detail.init();
		});
	}
};
