var ContentManager = new Class({
	Implements: [Events, Options],

	options: {
		container: null
	},

	initialize: function(options) {
		this.setOptions(options);

		this.actions		= null;

		this.scroller		= null;
		this.container		= $('content');
		this.module_id		= 0;
		this.module			= '';
		this.footer_menu	= [
			{'title':'services', 'desc':'our suggestions'},
			{'title':'team', 'desc':'who we are'},
			{'title':'technologies', 'desc':'what and why'},
			{'title':'success stories', 'desc':'and customers'}
		];
		this.getData();
	},

	build: function() {
		var id_prefix = this.module.alias;
		if (this.module_id == 0) {
			new Element('div', {'id':id_prefix + '-top'}).setHTML('<img src="images/logo.png" class="fright" style="position:relative;top:10px;" />').inject(this.container);
			new Element('div', {'id':id_prefix + '-content'}).setHTML('<div id="drive-line"><div class="fright mr20 lh2">drive your web right...</div></div>').inject(this.container);
			this.footer = new Element('div', {'id':id_prefix + '-footer'}).inject(this.container);
			for(var i=0; i<this.footer_menu.length; i++) {
				mr = '';
				if (i != 3) mr = 'mr5';
				new Element('div', {'class':'button '+mr+' fleft cp', 'events':{'click':this.menuClick.bindWithEvent(this, this.footer_menu[i].title)}}).grab(
					new Element('div', {'class':'b font20 mb25'}).setHTML(this.footer_menu[i].title)
				).grab(
					new Element('div', {'class':'fright font16'}).setHTML(this.footer_menu[i].desc)
				).store('mid', this.footer_menu[i].title).inject(this.footer);
			}
		} else {
			this.top = new Element('div', {'id':id_prefix+'-top'}
			).grab(
				new Element('div', {'class':'fleft'}
				).grab(
					new Element('div', {'id':'main-title'}).setHTML(this.module.title)
				).grab(
					new Element('div', {'id':'main-description'}).setHTML(this.module.desc)
				)
			).grab(
				new Element('img', {'src':'images/logo_min.png', 'class':'fright mr10 mt15'})
			).grab(
				new Element('div', {'class':'project-title'}).setHTML('Zolotoy vostok')
			).grab(
				new Element('div', {'class':'clear'})
			).inject(this.container);
			
			this.content = new Element('div', {'id':id_prefix+'-content'}
			).grab(
				new Element('div')
			).inject(this.container);
			
			this.footer = new Element('div', {'id':id_prefix+'-footer'}).inject(this.container);
		}
		this.footer.grab(new Element('div', {'class':'clear'}));
	},
	
	menuClick: function(ev, mid) {
		$(this.module.alias+'-top').destroy();
		$(this.module.alias+'-content').destroy();
		$(this.module.alias+'-footer').destroy();
		this.module_id = 1;
		this.getData();
	},
	
	onpageClick:function(ev) {
		this.getData({'on_page':ev.target.get('value')});	
	},
	
	pagingClick:function(ev) {
		this.getData({'page':ev.target.retrieve('page_n')});
	},
	
	getData: function() {
		var params = {'module_id':this.module_id};
		if (arguments.length > 0) {
			params.paging = arguments[0];
		}
		new Request.JSON({
			url: base_url + 'index/getContent.json',
			onComplete: (function(res) {
				this.module = res.module;
				this.build();
			}).bind(this)
		}).post(params);
	},

	clear: function() {
		this.content.empty();
	},

	createCheckBox: function() {
		$$('input.checkBox').each(function (el) {
			new Element('div', {
				'id': 'wrap_' + el.getProperty('id'),
				'class': 'checkBox',
				'events': {
					'click': function(evt) {
						var trgt = evt.target;
						if (trgt.getProperty('id') != 'wrap_check_all') { 
							if (!trgt.hasClass('checkedBox')) {
								this.selectCheckBox(trgt);
							} else {
								this.deselectCheckBox(trgt);
							}
						} else {
							if (!trgt.hasClass('checkedBox')) {
								$$('div.checkBox').each(this.selectCheckBox);
							} else {
								$$('div.checkBox').each(this.deselectCheckBox);
							}
						}
					}.bind(this)
				}
			}).wraps(el);
		}.bind(this));
	},

	selectCheckBox: function(chk) {
		if (!chk.hasClass('checkedBox')) {
			chk.addClass('checkedBox');
			chk.getFirst('input').setProperty('value', '1');
		}
	},

	deselectCheckBox: function(chk) {
		if (chk.hasClass('checkedBox')) {
			chk.removeClass('checkedBox');
			chk.getFirst('input').setProperty('value', '0');
		}
	}
});

//console.log('ContentManager.js loaded');