var qq_template = {
	title: '请输入你的QQ号码',
	el: [{
		name: 'feed',
		label: 'QQ',
		type: 'text'
	}, {
		name: 'type',
		type: 'hidden',
		value: 'qq'
	}],
	url: 'http://www.facefeed.cn/index.php/feed/insert_source',
	tips: '您QZone里的更新将会被扫描到我们的系统当中',
	iconCls: 'qq'
};

var sina_template = {
	title: '请输入你在新浪的用户名',
	el: [{
		name: 'feed',
		label: '用户名',
		type: 'text'
	}, {
		name: 'type',
		type: 'hidden',
		value: 'sina'
	}],
	url: 'http://www.facefeed.cn/index.php/feed/insert_source',
	tips: '您新浪博客里的更新将会被扫描到我们的系统当中',
	iconCls: 'sina'
};

var template_array = [qq_template, sina_template];

function FeedWindow(config) {
	this.formEl = $('#' + config.formId);
	this.iconContainerEl = $('#' + config.iconContainerId);
	this.titleEl = $('#' + config.titleElId);
	
	this.templateArray = config.templateArray;
	
	this.bind();
	
	FeedWindow.instance = this;
}

FeedWindow.prototype = {
	activeConfig: null,
	bind: function() {
		var len = this.templateArray.length;
		
		for (var i = 0; i < len; i++) {
			this._bindSingle(this.templateArray[i]);
		}
	},
	_bindSingle: function(feedSourceConfig) {
		var a = $('<a href="#" class="feed-icon"></a>');
		a.addClass(feedSourceConfig.iconCls);
		
		this.iconContainerEl.append(a);
		
		var formEl = this.formEl;
		var titleEl = this.titleEl;
		var feedWindow = this;
		
		a.click(function() {
			formEl.find('*').remove();
			titleEl.text(feedSourceConfig.title);
			
			for (var i = 0; i < feedSourceConfig.el.length; i++) {
				var field = feedSourceConfig.el[i];
				
				switch (field.type) {
					case 'text':
					var div = $('<div class="form-field"></div>');
					if (field.label != '') {
						var label = $('<label>' + field.label + '</label>');
						if (field.name != '') {
							label.attr('for', field.name);
						}
						
						div.append(label);
					}
					
					var text = $('<input type="text" size="45"></input>');
					if (field.name != '') {
						text.attr('name', field.name);
					}
					if (field.value != '') {
						text.val(field.value);
					}
					
					div.append(text);
					
					formEl.append(div);
					break;
					case 'hidden':
					var hidden = $('<input type="hidden"></input>');
					if (field.name != '') {
						hidden.attr('name', field.name);
					}
					if (field.value != '') {
						hidden.val(field.value);
					}
					
					formEl.append(hidden);
					break;
				}
			}
			
			if (feedSourceConfig.tips != '') {
				var tips = $('<div class="hint">' + feedSourceConfig.tips + '</div>')
				
				formEl.append(tips);
			}
			
			feedWindow.activeConfig = feedSourceConfig;
			
			//$('#split').click();
			return false;
		});
	},
	submit: function() {
		if (this.activeConfig == null) {
			alert('请选择右侧您要添加的更新的来源');
			return false;
		}
		
		var input_array = $('input', this.formEl);
		
		var data = {};
		
		for (var i = 0; i < input_array.length; i++) {
			var field = $(input_array[i]);
			
			var name = field.attr('name');
			var value = field.val();
			
			data[name] = value;
		}
		
		$.ajax({
				type: 'post',
				url: this.activeConfig.url,
				data: data,
				success: function(data, testStatus) {
					var result = JSON.parse(data);
					
					if (result.status == 'ok') {
						alert(result.message);
					} else {
						alert(result.message);
					}
				},
				error: function(data, testStatus) { alert('oops...'); },
				complete: function(data, testStatus) { }
		});
	}
};

$(function() {
	new FeedWindow({
		formId: 'add-feed-form',
		iconContainerId: 'feed-icon-panel',
		titleElId: 'form-title',
		templateArray: template_array
	});
});
