var inlineeditor = (function() {	
	if (window['inlineeditor']) {
		return true;
	}
	
	var $ = window['$'] || window['jQuery'];	
	
	// about switch for editable
	var editable_pattern = /_inline_editable=true/ig
	var is_editable = function() {
		var cookie = document.cookie;
		if (editable_pattern.test(cookie)) {
			return true;
		} else {
			return false;
		}
	};
	if (!is_editable()) {
		return true;
	}
	
	// about cookie
	var privlige_pattern = /kk_account_privilege="([^"]*)PAGE_EDITOR([^"]*)"/ig
	var has_privlige = function() {
		var cookie = document.cookie;
		if (privlige_pattern.test(cookie)) {
			return true;
		} else {
			return false;
		}
	}
	
	// about dialog
	var dialog_html = [
		'<div class="inlineedit-dialog">',
			'<div class="template-path"></div>',
			'<div class="inlineedit"></div>',
			'<div class="op"><input type="button" value="保存" class="save" /> <input type="button" value="关闭" class="close" /></div>',
		'</div>'
	].join('');
	var textarea_html = '<textarea cols="133" rows="25">${HTML}</textarea>';
	var open_dialog = function(tplpath, html) {
		var e = $('.inlineedit-dialog');
		if (!e.length) {
			$(document.body).append(dialog_html);
			e = $('.inlineedit-dialog');
			e.find('input.save').click(function() {
				var html = e.find('textarea').val();
				var tplpath = e.find('.template-path').html();
				save_html({tplpath : tplpath, data : html});
			});
			e.find('input.close').click(function() {
				close_dialog();
			});
		}
		
		e.find('.template-path').html(tplpath);
		e.find('.inlineedit').html(textarea_html.replace('${HTML}', html));
		var x = 10;
		var y = parseInt(document.documentElement.scrollTop) + 10;
		e.css('left', x + 'px');
		e.css('top', y + 'px');
		e.show();
	}
	var close_dialog = function() {
		$('.inlineedit-dialog').hide();
	}
	
	// about ajax
	var get_html = function(params) {
		$.get('/webadmin/inlineeditmanager/get_html/', params, function(r) {
			if (r.result) {
				var tplpath = r.tplpath;
				var html = r.data;
				open_dialog(tplpath, html);
			}
		}, 'json');
	}
	
	var save_html = function(params) {
		$.post('/webadmin/inlineeditmanager/save_html/', params, function(r) {
			if (r.result) {
				alert('保存成功！');
			} else {
				alert('保存失败！');
			}
		}, 'json');
	}
	
	var cal_offset = function(e) {
		var offset = e.offset();
		/*
		while (e.parent().css('position') == 'relative') {
			e = e.parent();
			var n_offset = e.offset();
			offset.x -= n_offset.x;
			offset.y -= n_offset.y;
		}
		*/
		return offset;
	}

	// about init function
	var init = function() {
		var body = $(document.body);
		$('.editable_inline_wrap__').each(function() {
			var wrap = $(this);
			var inline = wrap.prev();
			if (inline.length) {
				var w = inline.width();
				var h = inline.height();
				var offset = cal_offset(inline);
				wrap.appendTo(body);
				wrap.width(w - 4);
				wrap.height(h - 4);
				wrap.css('left', offset.left + 'px');
				wrap.css('top', offset.top + 'px');
						
				inline.hover(function() {
					wrap.show();
				}, function() {
					wrap.hide();
				});
				
				wrap.hover(function() {
					wrap.show();
				}, function() {
					wrap.hide();
				});
								
				wrap.find('.edit').click(function() {
					var tplpath = wrap.attr('template_path');
					get_html({tplpath : tplpath});
				});				
			}
		});
		$('.editable_inline_wrap__').appendTo(body);
	};
	
	
	// add css and jquery if not exist
	var _onload = window.onload;
	window.onload = function() {
		if (_onload) {
			_onload();
		}
		
		if (has_privlige()) {
			var header = document.getElementsByTagName('head')[0];
			
			var link = document.createElement('link');
			link.type = 'text/css';
			link.rel = 'stylesheet';
			link.href = CSS_MEDIA_PATH + '/inlineedit/style.css';
			header.appendChild(link);
			
			if (!$) {
				var script = document.createElement('script');
				script.type = 'text/javascript';
				script.src = JS_MEDIA_PATH + '/lib/jquery.js';
				var loaded = false;
				var onreadystatechange = function() {
					if (loaded) {
						return;
					}
					if (this.readyState) {
						if (/loaded|complete/.test(this.readyState)) {
							$ = window['jQuery'];
							init();
							loaded = true;
						}
					} else {
						$ = window['jQuery'];
						init();
						loaded = true;
					}
					
				}
				script.onreadystatechange = onreadystatechange;
				script.onload = onreadystatechange;
				header.appendChild(script);
			} else {
				init();
			}
		}
	}
	
	return true;
})();

