/*
* Mag Studio - Portfolio - Load AJAX/JSON Content
* @Author: Alexander Gavazov
* @Site: www.creative.bg & www.studio.bg
*/

var PortfolioLoadContent = function(parameters)
{
	this.url = parameters.url;
	this.categories = parameters.categories;
	this.filters = parameters.filters;
	this.viewCatalog = parameters.viewCatalog;
	this.viewThumbs = parameters.viewThumbs;
	this.viewActiveSlider = parameters.viewActiveSlider;
	this.yearSelect = parameters.yearSelect;
	this.pagesStr = parameters.pagesStr;
	this.putContent = parameters.putContent;
	this.show_cat = parameters.show_cat;
	this.show_year = parameters.show_year;
	this.show_filter = parameters.show_filter;
	this.show_method = parameters.show_method;
	this.show_page = parameters.show_page;

	this.pages = '';

	this._viewMethod = parameters.method;
	this._ViewEingine = new VE;
	this._page = 0;
	this._order_by = 'client';
	this._order_direction = 'asc';
	this._timeLoad = '';
	this._preloader = new Element('div', {className: 'ajax_preloader'});

	if(this.show_method) {
		this._viewMethod = this.show_method == 'catalog' ?'thumbs' : 'catalog';
		this.JUSTmoveViewActiveSlider(this.show_method);
	}

	this.setBehaviour();

	if(this.show_cat) {
		this.setBackCategory(this.categories[this.show_cat]);
	}

	if(this.show_page) {
		this.setBackPage(this.pagesStr[this.show_page]);
	}

	if(this.show_year) {
		this.yearSelect.value = this.show_year;
	}

	if(this.show_filter) {
		this.setBackFilter(this.show_filter);
	}
}

PortfolioLoadContent.prototype.setBehaviour = function()
{
	this.viewCatalog.onclick = this.moveViewActiveSlider.bind(this, 'catalog');
	this.viewThumbs.onclick = this.moveViewActiveSlider.bind(this, 'thumbs');
	this.viewActiveSlider.style.left = 0;

	this.categories.each(function(node) {
		node.onclick = function(){return false;} // IE6
		Event.observe(node, 'click', this.setCategory.bind(this, node));
	}.bind(this));

	this.filters.each(function(node) {
		Event.observe(node, 'click', this.loadContent.bind(this));
	}.bind(this));

	this.yearSelect.onchange = this.loadContent.bind(this);

	this.getPages();
}

PortfolioLoadContent.prototype.getPages = function(node)
{
	this.pages = $$(this.pagesStr);
	this.pages.each(function(node) {
		node.onclick = function(){return false;} // IE6
		Event.observe(node, 'click', this.setPage.bind(this, node));

		if(node.className.search('active') != -1)
		{
			this._page = node.rel;
		}
	}.bind(this));
}

PortfolioLoadContent.prototype.setBackCategory = function(node)
{
	this._page = 0;
	this.categories.each(function(node) {
		node.className = '';
	});

	node.className = 'active private';

	return false;
}

PortfolioLoadContent.prototype.setBackPage = function()
{
	$$(this.pagesStr).each(function(node) {
		if(this.show_page == node.rel){
			node.addClassName('active');
		}else{
			node.removeClassName('active');
		}
	}.bind(this));

	return false;
}

PortfolioLoadContent.prototype.setBackFilter = function(node)
{

	$$('.checkbox_list li span').each(function(elem){
		if(elem.id == ('filterCat'+node)){
			elem.parentNode.addClassName('active private');
		}
		else{
			elem.parentNode.removeClassName('active private');
		}
	});
}

PortfolioLoadContent.prototype.setCategory = function(node)
{
	this._page = 0;
	this.categories.each(function(node) {
		node.className = '';
	});

	node.className = 'active private';

	this.loadContent();

	return false;
}

PortfolioLoadContent.prototype.setPage = function(node)
{
	this._page = node.rel;
	this.pages.each(function(node) {
		if(this._page == node.rel && !isNaN(node.rel))
		{
			node.addClassName('active');
		}
		else
		{
			node.removeClassName('active');
		}
	}.bind(this));

	this.loadContent();

	return false;
}

PortfolioLoadContent.prototype.moveViewActiveSlider = function(type)
{
	if(this._viewMethod == type)
	{
		return;
	}

	var start = parseInt(this.viewActiveSlider.style.left);
	var end = type == 'catalog' ? 0 : 33;
	var _this = this;

	this._ViewEingine.stop();
	this._ViewEingine.init(start, end, Easing.expoOut, .7);
	this._ViewEingine.onChange = function(position)
	{
		_this.viewActiveSlider.style.left = position + 'px';
	}
	this._ViewEingine.start();

	this._viewMethod = type;

	this.loadContent();
}

PortfolioLoadContent.prototype.JUSTmoveViewActiveSlider = function(type)
{
	if(this._viewMethod == type)
	{
		return;
	}

	var start = parseInt(this.viewActiveSlider.style.left);
	var end = type == 'catalog' ? 0 : 33;
	var _this = this;

	this._ViewEingine.stop();
	this._ViewEingine.init(start, end, Easing.expoOut, .7);
	this._ViewEingine.onChange = function(position)
	{
		_this.viewActiveSlider.style.left = position + 'px';
	}
	this._ViewEingine.start();

	this._viewMethod = type;

}

PortfolioLoadContent.prototype.loadContent = function()
{
	this.createParameters();

	this.putContent.style.height = this.putContent.getHeight() + 'px';

//	Effect.fade('projects-listing', {afterFinish: function(){alert('BOOOM!');
//		this.putContent.appear();
//	}});
	Effect.Fade('projects-listing', {
		afterFinish: function() {
			this._preloader.update('Loading data');
			this.putContent.update(this._preloader);
			this.putContent.appear({duration: 0.5});
		}.bind(this),
		duration: 0.5
	});

	// Ajax
	new Ajax.Request(this.url, {
		method: 'post',
		parameters: this.parameters,
		onComplete: function(req) {
			Effect.Fade('projects-listing', {
				afterFinish: function() {
					this._timeLoad = setTimeout(this.updateContent.bind(this, req.responseText), 500);
				}.bind(this),
				duration: 0.5
			});
			
		}.bind(this)
	});
}

PortfolioLoadContent.prototype.createParameters = function()
{
	this.parameters = '';

	// Method
	this.parameters += 'method=' + this._viewMethod;

	// Categories
	this.categories.each(function(node) {
		if(node.className.search('active') != -1)
		{
			var id = node.rel;
			this.parameters += '&cat=' + id ;
		}
	}.bind(this));

	// Filter
	this.filters.each(function(node) {
		if(node.parentNode.className.search('active') != -1)
		{
			var id = node.id.replace('filterCat', '');
			this.parameters += '&filter[' + id + ']=' + id + '';
		}
	}.bind(this));

	// Year
	this.parameters += '&year=' + this.yearSelect.value;

	// Page
	this.parameters += '&page=' + this._page;

	// order
	this.parameters += '&order=' + this._order_by;

	// order direction
	this.parameters += '&order_direction=' + this._order_direction;

}

PortfolioLoadContent.prototype.updateContent = function(code)
{
	this.putContent.update(code);
	this.putContent.style.height = 'auto';
	this.putContent.appear({duration: 0.5});
	this.getPages();
	$$('.border').each(function(elem){
		elem.observe('click', function(){
			var parent = elem.up();
			window.location = parent.down('a').rel;
		});
	});
	if($('order_by_client')){
		$('order_by_client').observe('click', function(){
			if(this._order_by == 'client'){
				this.toggleOrderDirection();
			}else{
				this._order_by = 'client';
			}
			this.loadContent();
		}.bind(this));
	}
	if($('order_by_date')){
		$('order_by_date').observe('click', function(){
			if(this._order_by == 'date'){
				this.toggleOrderDirection();
			}else{
				this._order_by = 'date';
			}
			this.loadContent();
		}.bind(this));
	}
	$('page_tools_bottom').setStyle({bottom: '0'});
}

PortfolioLoadContent.prototype.toggleOrderDirection = function()
{
	if(this._order_direction == 'asc'){
		this._order_direction = 'desc';
	}else{
		this._order_direction = 'asc';
	}
}
