var Posts = function(appUrl, controllerUrl) {
	this.controllerUrl = controllerUrl;
	this.appUrl = appUrl;
	this.postsTarget = '#posts';
	this.compatibilityMode = false;	
	var $this = this;
		
	if ("onhashchange" in window) {		
		jQuery(window).bind('hashchange', function() {		
			$this.init();
		});
	} else {		
		this.compatibilityMode = true;
	}
	
	this.init();
};

Posts.prototype.init = function() {	
	var $this = this;
	var controllerPos = window.location.href.indexOf($this.controllerUrl);	
	if (controllerPos > -1) {		
		var sharpPos = window.location.href.indexOf('#');
		var href = window.location.href.substring(sharpPos + 1);
		var uri = href.substr(href.indexOf('?') + 1);
		var uriParsed = uri.split('&');
		var params = {};
		for(var i = 0; i < uriParsed.length; i++) {
			var tmp = uriParsed[i].split('=');
			if (tmp[0]) {				
				params[tmp[0]] = tmp[1];
			}
		}
		
		if (params.a) {
			switch(params.a) {
			case 'byid':				
				this.getById(params.id);
				break;
			case 'all':				
				this.getAll();
				break;
			}
		}		
	}
};

Posts.prototype.registrationForm = function(c, title) {
	var $this = this;
	var form = '<a name="registration-form"></a><h3>Регистрация на "' + title + '"</h3><ul> \
					<li><label for="name">*ФИО:</label><input class="text" type="text" id="name" name="name" value=""></li> \
					<li><label for="phone">*Телефон:</label><input class="text" type="text" id="phone" name="phone" value=""></li> \
					<li><label for="email">Email:</label><input class="text" type="text" id="email" name="email" value=""></li> \
					<li><label for="other">Дополнительная информация:</label><textarea name="other" id="other"></textarea></li>' +					
					'<li><input id="event-register" type="submit" value="Ок" name="submit"></li> \
				</ul>';
	c.html(form);
	jQuery('#event-register').bind('click', function() {
		var uri = '?name=' + jQuery('#name').val() + '&phone=' + jQuery('#phone').val() + '&email=' + jQuery('#email').val() + '&other=' + jQuery('#other').val() + '&title=' + title + '&callback=?'; 
		jQuery.ajax({
			url: $this.appUrl + '/app/posts/registration-cp1251.php' + uri,			
			type: 'POST',					
			dataType: 'JSONP',			
			success: function(data) {
				if (data.error) {					
					if (jQuery('#registration-error').length <= 0)
						jQuery('#registration-form').append('<div id="registration-error">' + data.message + '</div>');					
				} else
					jQuery('#registration-form').html('<div id="registration-success">' + data.message + '</div>');					
			}
		});
	});
};

Posts.prototype.getPreviews = function(target) {	
	var $this = this;
	jQuery.getJSON($this.appUrl + '/app/posts?t=news&a=previews&callback=?', function(data) {		
		var posts = data.data;
		jQuery.each(posts, function(i) {			
			var div = jQuery('<div/>').css('margin-bottom', '5px').css('margin-top', '5px');
			div.append('<div class="title">' + posts[i].title + '</div>')
			   .append('<div class="text">' + posts[i].text + '</div>')
			   .append('<div class="created-at">' + posts[i].created_at + '</div>');			
			jQuery(target).append(div);
		});		
		var link = jQuery('<a/>').attr('href', $this.controllerUrl + '#/app/posts/?t=news&a=all').html(data.all_posts);		
		jQuery(target).append(link);	
						
		jQuery(target + ' a.title').bind('click', function(e) {			
			var href = jQuery(this).attr('href');			
			window.location = $this.controllerUrl + '#' + href.substr(1);			
		});		
	});
};

Posts.prototype.getAll = function() {	
	var link = '/app/posts/?t=news&a=all';
	this.getPosts(link);	
};

Posts.prototype.getById = function(id) {
	var link = '/app/posts/?t=news&a=byid&id=' + id;
	this.getPosts(link);
};

Posts.prototype.getPosts = function(link, callback) {
	var $this = this;
	jQuery.getJSON($this.appUrl + link + '&callback=?', function(data) {		
		var html = '<div id="news-menu"></div>';		
		jQuery.each(data.data, function(i) {			
			html += '<div class="post">' + 
					'<div class="title">' + data.data[i].title + '</div>' + 
					'<div class="text">' + data.data[i].image + ' ' + data.data[i].text + '</div>' +
					'<div style="clear:both;" class="created_at">' + data.data[i].created_at + '</div></div>';					
		});		 
		jQuery($this.postsTarget).html(html);
		
		if (data.data.length == 1 && data.data[0].registration == 1) {
			jQuery($this.postsTarget + ' div.post').append('<div id="registration-form"></div>');
			jQuery($this.postsTarget + ' div.text').prepend('<div id="registration-button"><a href="#registration-form">Зарегистрироваться на "' + data.data[0].title + '"</a></div>');
			$this.registrationForm(jQuery('#registration-form'), data.data[0].title);
		}
		
		if ($this.compatibilityMode) {
			jQuery('div.post div.title a.title').bind('click', function() {
				 window.location.href = $this.controllerUrl + jQuery(this).attr('href');
			});			
		}
		
		var allLink = jQuery('<a/>').attr('href', '#/app/posts/?t=news&a=all').html(data.all_posts);		
				
		if ($this.compatibilityMode) {			
			allLink.bind('click', function() {
				window.location.href = $this.controllerUrl + jQuery(this).attr('href');
			});			
		}
		
		jQuery('#news-menu').prepend(allLink);		
	});
};

