/*
* Mag Studio - Home Slider - Load AJAX/JSON Content
* @Author: Alexander Gavazov
*/

var HomeSliderLoadContent = function(parameters)
{
	this.Slider = parameters.Slider;
	this.container = parameters.container;
	this.links = parameters.links;
	this.loadImgEvent = parameters.loadImgEvent; // Becouse IE6

	this.url_to_file = parameters.url_to_file;

	this._preloader = new Element('div', {className: 'ajax_preloader'});
	this._totalImages = 0;
	this._loadedImages = 0;

	this.setBehaviour();
}

HomeSliderLoadContent.prototype.setBehaviour = function()
{
	this.links.each(function(node) {
		node.onclick = this.loadPage.bind(this, node);
	}.bind(this));

	Element.insert(this.container, {top: this._preloader});
}

HomeSliderLoadContent.prototype.loadPage = function(link)
{
	if(link.className != 'active')
	{
		this.Slider.goTo(-1);
		this.Slider.dontMove = true;

		this.setActive(link);

		this._preloader.innerHTML = 'loading data';

		setTimeout(this.loadJSON.bind(this, link.rel), this.Slider._duration * 1000);
	}

	return false;
}

HomeSliderLoadContent.prototype.loadJSON = function(catId)
{
	new Ajax.Request(this.url_to_file, {
		method: 'get',
		parameters: 'cat_id=' + catId,
		onComplete: function(req) {
			this.json = eval(req.responseText);
			this.writeContent();
		}.bind(this)
	});
}

HomeSliderLoadContent.prototype.writeContent = function()
{
	while(node = this._preloader.next())
	{
		node.remove();
	}

	this._loadedImages = 0;
	this._totalImages = 0;
	this.json.each(function(row, i) {

		var ul = new Element('ul', {'class': 'home last'});

		var detailes_link = new Element('a', { 'class': 'icon-details', href: row.link }).update(lng_project_detailes);
		var li1 = new Element('li', {'class':'first' }).insert({bottom: detailes_link});
		ul.insert({bottom: li1});

		if(row.url){
			var visit_link = new Element('a', { 'class': 'icon-visit', href: row.url, target: '_blank' }).update(lng_visit_online);
			var li3 = new Element('li').insert({bottom: visit_link});
			ul.insert({bottom: li3});
		}

		if(row.case_studie)	{
			var case_studie_link = new Element('a', { 'class': 'icon-case_studie', href: row.case_studie }).update(lng_case_study);
			var li2 = new Element('li').insert({bottom: case_studie_link});
			ul.insert({bottom: li2});
		}

		var summary = new Element('p',{'class': 'desc'}).update(row.summary);
		var date	= new Element('p',{'class': 'date'}).update(row.launch_date);

		var h2_link = new Element('a', { href: row.link}).update(row.title);;
		var h2		= new Element('h2').insert({bottom: h2_link});

		var div_bottom = new Element('div', {'class': 'bottom-bg'});
		div_bottom.insert({bottom: h2});
		div_bottom.insert({bottom: date});
		div_bottom.insert({bottom: summary});
		div_bottom.insert({bottom: ul});

		var border =  new Element('span', {'class': 'border'}).update('&nbsp;');
		border.observe('click', function(){window.location = row.link});

		var link_img		= new Element('img', {src: row.image, onload:this.loadImgEvent});
		var project_link 	= new Element('a', { 'class': 'image-link', href: row.link, rel: row.link }).insert(link_img);

		var project = new Element('div', {className: 'project_listing'}).insert({bottom: project_link});
		project.insert({bottom: border});
		project.insert({bottom: div_bottom});

		Element.insert(this.container, {bottom: project});

		this._totalImages++;
	}.bind(this));
}

HomeSliderLoadContent.prototype.nLoadImage = function()
{
	this._loadedImages++;

	this._preloader.innerHTML = 'loading image ' + parseInt((this._loadedImages * 100) / this._totalImages) + '%';

	if(this._totalImages <= this._loadedImages)
	{
		this.loadImages();
	}
}

HomeSliderLoadContent.prototype.loadImages = function(link)
{
	setTimeout(this.finish.bind(this), 500);
}

HomeSliderLoadContent.prototype.setActive = function(link)
{
	this.links.each(function(node) {
		node.className = '';
	});

	link.className = 'active';
}

HomeSliderLoadContent.prototype.finish = function()
{

	this.Slider.dontMove = false;
	this.Slider.goTo(1);
}
