window.__AVATAR_LOADER = {

	avatarImgId : 'avatarko_img',
	swfId : 'avatarko_swf',
	swfContainerId : 'avatarko_container_swf',
	swfSource : '/a/bmid/swf/avatarko.swf?v1.2',
	expressInstall : '/a/common/js/swfobject_v2.1/expressInstall.swf',
	uploadUrl : '/avatar/upload/bmid/?DEBUG_MODE=0',
	mirrorUrl : '/avatar/echo/?DEBUG_MODE=0',
	tmpUrlPath : '/avatar/tmp/path/',
	delUrlPath : '/avatar/blank/bmid/',
	flashVersion : '9.0.124.0',
	width : 525,
	height : 569,
	blankAvatarkoImg : null,
	BM_IMG_SERVER : null,
	BM_JS_DOMAIN : '',

	init : function() {

		this.trace('init');

		this.swfSource = this.BM_IMG_SERVER + this.swfSource;
		this.expressInstall = this.BM_IMG_SERVER + this.expressInstall;

		this.uploadUrl =  this.BM_JS_DOMAIN + this.uploadUrl;
		this.mirrorUrl =  this.BM_JS_DOMAIN + this.mirrorUrl;
		this.tmpUrlPath = this.BM_JS_DOMAIN + this.tmpUrlPath;
		this.delUrlPath = this.BM_JS_DOMAIN + this.delUrlPath;

		var func_select = function(event){
			event = new Event(event);
			window.__AVATAR_LOADER.show();
			event.stop();
		};

		var func_delete = function(event){
			event = new Event(event);
			window.__AVATAR_LOADER.remove();
			event.stop();
		};

		$('avatar_select_1').addEvent('click', func_select);
		$('avatar_select_2').addEvent('click', func_select);
		$('avatar_delete').addEvent('click', func_delete);

		this.hide();
	},

	getSwf : function() {

	},

	show : function() {

		this.trace('show')

		var flashvars = {
			cancelFunc: 'AvatarCancelButtonFunc',
			saveFunc: 'AvatarSaveButtonFunc',
			mirrorUrl: this.mirrorUrl,
			uploadUrl: this.uploadUrl
		};

		var params = {
			allowScriptAccess: 'always',
			wmode: 'transparent'
		};

		var attributes = {
			name: this.swfId
		};

		swfobject.embedSWF(
			this.swfSource,
			this.swfId,
			this.width, this.height,
			this.flashVersion,
			this.expressInstall,
			flashvars,
			params,
			attributes
		);

		this._position();
	},

	_position : function() {

		this.trace('_position');

		if(window.showHideFlash) window.showHideFlash(false); // hide flash

		// hide all select-box to fix IE bug see http://www.codeproject.com/KB/HTML/dropdown_div.aspx
		if(Browser.Engine.trident) { // ie
			document.getElements('select.hide_from_popup').each(function(select){
				select.setStyle('visibility', 'hidden');
			});
		}

		// proceed escape-key press
		document.addEvent('keyup', function(event){
			event = new Event(event);
			//Event.Keys.esc = 27;
			if (event.key == 'esc') {
				window.__AVATAR_LOADER.hide();
				event.stop();
				document.removeEvent(event);
			}
		});

		// set style for swf object
		if(Browser.Engine.gecko) { // ff
			//$(this.swfContainerId).getElements('object').setStyle('outline-width', '0px');
			$(this.swfId).setStyle('outline-width', '0px');
		}

		// set container styles
		$(this.swfContainerId).setStyles({
			'position': 'absolute',
			'z-index': '101',
			'display': 'block'
		});

		// overlay back-ground
		$('avatar_overlay_bg').setStyles({
			'width' : window.getScrollWidth()+'px',
			'height': window.getScrollHeight()+'px',
			'display': 'block'
		});


		// position
		var xy_dim = {
			x:this.width,
			y:this.height
		};

		if(!xy_dim.y) {
			xy_dim.y = (window.getHeight().toInt()/2).toInt();
		}

		var X = window.getScrollLeft() + Math.round((window.getWidth() - xy_dim.x)/2);
		var Y = window.getScrollTop() + Math.round((window.getHeight() - xy_dim.y)/2);

		$(this.swfContainerId).setStyle('left', X + 'px');
		$(this.swfContainerId).setStyle('top', Y + 'px');

	},

	hide : function() {

		this.trace('hide')

		if(window.showHideFlash) window.showHideFlash(true); // show flash

		if(Browser.Engine.trident) { // ie
			document.getElements('select.hide_from_popup').each(function(select){
				select.setStyle('visibility', 'visible');
			});
		}

		// set style for swf object
		$(this.swfContainerId).setStyle('display', 'none');

		// hide overlay bg
		$('avatar_overlay_bg').setStyle('display', 'none');
	},

	save : function() {

		this.trace('save');
		var result = this.update(this.tmpUrlPath);

		$('avatarko_change').setStyle('display', 'block');
		$('avatarko_load').setStyle('display', 'none');

		return result;
	},

	remove : function() {

		this.trace('remove');
		var result = this.update(this.delUrlPath);

		$('avatarko_change').setStyle('display', 'none');
		$('avatarko_load').setStyle('display', 'block');

		return result;
	},

	update : function(jsurl) {

		var this_closure = this;

		this_closure.trace('update');

		var json_options = {

			url : jsurl,
			onRequest : function() { },
			onComplete : function(json_response) {
				if(json_response && json_response.path) {
					$(this_closure.avatarImgId).set('src', json_response.path);
				}
				else {
					$(this_closure.avatarImgId).set('src', this_closure.blankAvatarkoImg);
				}
				this_closure.hide();
			}
			
		};
		new Request.JSON(json_options).get({
			'DEBUG_MODE': 0,
			'rand': Math.random()
		});

		return false;
	},

	cancel : function() {
		this.trace('cancel');
		this.hide();
	},

	trace : function(what) {
		//if(window.console) {
		//	window.console.log(what);
		//}
	}

};

//window.__AVATAR_LOADER.init();

function AvatarCancelButtonFunc() {
	window.__AVATAR_LOADER.cancel();
}

function AvatarSaveButtonFunc() {
	window.__AVATAR_LOADER.save();
}