/**
 * Object view class
 *
 * Shows objects, divided into pages and can be used to filter objects
 */
var ObjectView = new Class({
	/**
	 * Contains the number of items that should be shown per page
	 */
	perpage: 5,
	/**
	 * Contains all the objects which can be viewed
	 */
	objects: null,
	/**
	 * Contains the objects that should be viewed
	 */
	viewobjects: null,
	/**
	 * Contains the holder element in which all the new nodes are created
	 */
	holder: null,
	/**
	 * Contains the category code, which is used by css etc..
	 */
	code: '',
	/**
	 * Sets the given objects object to the internal objects object
	 */
	initialize: function(Objects, Holder, Code)
	{
		if ($type(Objects) !== 'array' || !$(Holder)) return false;
		this.objects = Objects;
		this.viewobjects = Objects;
		this.holder = $(Holder);
		this.code = Code;
		this.createPage();
	},
	/**
	 * Changes the page to the requested
	 */
	changePage: function(p)
	{
		if (p.toInt() >= 0) {
			var instance = this;
			this.holder.set('tween', {
				onComplete: function()
				{
					instance.createPage((p * instance.perpage.toInt()));
					instance.holder.fade(1);
				}
			}).fade(0);
		}
	},
	/**
	 * Creates the views for n objects and appends them to the holder
	 */
	createPage: function(start)
	{
		if (!$chk(start)) start = 0;
		this.holder.empty();
		if (this.viewobjects.length > 0) {
			if (this.viewobjects.length > this.perpage.toInt()) {
				this.createPagination().inject(this.holder);
				$('p' + (start / this.perpage.toInt())).addClass('active');
			}
			this.perpage.toInt().times(function(j)
			{
				var i = (start + j).toInt();
				if ($chk(this.viewobjects[i]) && $type(this.viewobjects[i]) === 'object') {
					this.createView(this.viewobjects[i]).inject(this.holder);
				}
			}, this);
			if (this.viewobjects.length > this.perpage.toInt()) {
				this.createPagination(true).inject(this.holder);
				$('bp' + (start / this.perpage.toInt())).addClass('active');
			}
		} else {
			new Element('p').set('text', 'Er zijn geen objecten gevonden voor deze categorie, probeer het later nog eens.').inject(this.holder);	
		}
	},
	/**
	 * Creates the html for the given object
	 */
	createView: function(Object)
	{
		return new Element('a').set('href', window.baseurl + 'object/' + Object.objectid + '/' + Object.friendlyaddress + '-' + Object.friendlycity).addClass('objectlink').adopt(
			new Element('div').addClass('bspace').addClass('fixedheight').addClass('objects').addClass(this.code).adopt(
				new Element('div').addClass('column').adopt(
					new Element('div').addClass('image').setStyle('background-image', 'url(' + window.mediaurl + 'o' + Object.objectmediaid + '/t)').setStyle('background-repeat', 'no-repeat').adopt(
						new Element('span').set('text', Object.address + ', ' + Object.city)
					)
				),
				new Element('div').addClass('column').adopt(
					new Element('div').addClass('image').setStyle('background-image', 'url(' + window.mediaurl + 'o' + Object.secondobjectmediaid + '/t)').setStyle('background-repeat', 'no-repeat').adopt(
						new Element('span').set('text', Object.address + ', ' + Object.city)
					)
				),
				new Element('div').addClass('column').addClass('lastcolumn').adopt(
					new Element('div').addClass('image').setStyle('background-image', 'url(' + window.mediaurl + 'o' + Object.thirdobjectmediaid + '/t)').setStyle('background-repeat', 'no-repeat').adopt(
						new Element('span').set('text', Object.address + ', ' + Object.city)
					)
				),
				new Element('div').addClass('clearer').adopt(new Element('span')),
				new Element('h3').set('text', Object.address + ', ' + Object.city).setStyle('margin-top', (Browser.Engine.trident ? '-10px' : '0')),
				new Element('p').set('text', Object.price.replace('_E_', '€') + ' ' + Object.pricepostfix)
			)
		);
	},
	/**
	 * Creates the pagination if needed
	 */
	createPagination: function(bottom)
	{
		if (!$chk(bottom)) bottom = false;
		var instance = this;
		var pages = Math.ceil(this.objects.length / this.perpage.toInt());
		var holder =  new Element('div').addClass((bottom ? 'b' : '') + 'icons');
		var list = new Element('ul').setStyle('margin', '0 0 0 0');
		pages.times(function(p)
		{
			list.adopt(new Element('li').set('id', (bottom ? 'b' : '') + 'p' + p).adopt(
				new Element('a').set('href', 'javascript:void(0);').set('text', (p + 1)).addEvent('click', function(){window.scrollTo(0, 0);instance.changePage(p);})
			));
		});
		return holder.adopt(list, new Element('div').addClass('clearer').adopt(new Element('span')));
	},
	/**
	 * Filters the objects according to the slider values
	 *
	 * 1. Get all current slider values
	 * 2. Create object array with only those objects that fit the slider values
	 * 3. Redraw our object view
	 */
	filterObjects: function(stepValue, contentSlider, cubeSlider, roomSlider, categoryID)
	{
		if (stepValue.toInt() > 0) {
			var contentClauseMin = false, cubeClauseMin = false, roomClauseMin;
			if (contentSlider.step.toInt() > 0 && contentSlider.step.toInt() <= 100) {
				if (contentSlider.step.toInt() < 17) {
					contentClauseMin = false;
				} else if (contentSlider.step.toInt() > 16 && contentSlider.step.toInt() < 35) {
					contentClauseMin = (categoryID.toInt() === 1 ? 150 : 2000);
				} else if (contentSlider.step.toInt() > 34 && contentSlider.step.toInt() < 52) {
					contentClauseMin = (categoryID.toInt() === 1 ? 300 : 5000);
				} else if (contentSlider.step.toInt() > 51 && contentSlider.step.toInt() < 69) {
					contentClauseMin = (categoryID.toInt() === 1 ? 500 : 10000);
				} else if (contentSlider.step.toInt() > 68 && contentSlider.step.toInt() < 87) {
					contentClauseMin = (categoryID.toInt() === 1 ? 750 : 30000);
				} else if (contentSlider.step.toInt() > 86) {
					contentClauseMin = (categoryID.toInt() === 1 ? 1000 : 50000);
				}
			}
			if (cubeSlider.step.toInt() > 0 && cubeSlider.step.toInt() <= 100) {
				if (cubeSlider.step.toInt() < 17) {
					cubeClauseMin = false;
				} else if (cubeSlider.step.toInt() > 16 && cubeSlider.step.toInt() < 35) {
					cubeClauseMin = (categoryID.toInt() === 1 ? 200 : 400);
				} else if (cubeSlider.step.toInt() > 34 && cubeSlider.step.toInt() < 52) {
					cubeClauseMin = (categoryID.toInt() === 1 ? 350 : 600);
				} else if (cubeSlider.step.toInt() > 51 && cubeSlider.step.toInt() < 69) {
					cubeClauseMin = (categoryID.toInt() === 1 ? 500 : 800);
				} else if (cubeSlider.step.toInt() > 68 && cubeSlider.step.toInt() < 87) {
					cubeClauseMin = (categoryID.toInt() === 1 ? 650 : 1000);
				} else if (cubeSlider.step.toInt() > 86) {
					cubeClauseMin = (categoryID.toInt() === 1 ? 800 : 1500);
				}
			}
			if (roomSlider.step.toInt() > 0 && roomSlider.step.toInt() <= 100) {
				if (roomSlider.step.toInt() < 17) {
					roomClauseMin = false;
				} else if (roomSlider.step.toInt() > 16 && roomSlider.step.toInt() < 35) {
					roomClauseMin = (categoryID.toInt() === 1 ? 1 : 5);
				} else if (roomSlider.step.toInt() > 34 && roomSlider.step.toInt() < 52) {
					roomClauseMin = (categoryID.toInt() === 1 ? 2 : 6);
				} else if (roomSlider.step.toInt() > 51 && roomSlider.step.toInt() < 69) {
					roomClauseMin = (categoryID.toInt() === 1 ? 3 : 7);
				} else if (roomSlider.step.toInt() > 68 && roomSlider.step.toInt() < 87) {
					roomClauseMin = (categoryID.toInt() === 1 ? 4 : 8);
				} else if (roomSlider.step.toInt() > 86) {
					roomClauseMin = (categoryID.toInt() === 1 ? 5 : 10);
				}
			}
			var tmpobjects = [];
			this.objects.each(function(object, i)
			{
				var addObject = false;
				if ($chk(object.content) && contentClauseMin !== false && object.content > contentClauseMin) addObject = true;
				if ($chk(object.cube) && cubeClauseMin !== false && object.cube > cubeClauseMin) addObject = true;
				if ($chk(object.rooms) && roomClauseMin !== false && object.rooms > roomClauseMin) addObject = true;
				if (contentClauseMin === false && cubeClauseMin === false && roomClauseMin === false) addObject = true;
				if (addObject) tmpobjects[tmpobjects.length] = object;
			});
			this.viewobjects = tmpobjects;
		} else {
			this.viewobjects = this.objects;
		}
		this.createPage();
	},
	/**
	 * Filters the objects according to the slider values for Agricultural/Greenhouse objects
	 */
	filterObjectsAgri: function(stepValue, priceSlider)
	{
		if (stepValue.toInt() > 0) {
			var priceClauseMin = false;
			if (priceSlider.step.toInt() > 0 && priceSlider.step.toInt() <= 100) {
				if (priceSlider.step.toInt() < 17) {
					priceClauseMin = false;
				} else if (priceSlider.step.toInt() > 16 && priceSlider.step.toInt() < 35) {
					priceClauseMin = 100000;
				} else if (priceSlider.step.toInt() > 34 && priceSlider.step.toInt() < 52) {
					priceClauseMin = 200000;
				} else if (priceSlider.step.toInt() > 51 && priceSlider.step.toInt() < 69) {
					priceClauseMin = 500000;
				} else if (priceSlider.step.toInt() > 68) {
					priceClauseMin = 1000000;
				}
			}
			var tmpobjects = [];
			this.objects.each(function(object, i)
			{
				var addObject = false;
				if ($chk(object.rawprice) && priceClauseMin !== false && object.rawprice > priceClauseMin) addObject = true;
				if (priceClauseMin === false) addObject = true;
				if (addObject) tmpobjects[tmpobjects.length] = object;
			});
			this.viewobjects = tmpobjects;
		} else {
			this.viewobjects = this.objects;
		}
		this.createPage();
	}
});
