Tooltip = {};

Tooltip._active = null;


Tooltip.hide = function(){
	if (Tooltip._active)
		Tooltip._active.parentNode.removeChild(Tooltip._active);
};


Tooltip.show = function(node, text){
	var pos = Tooltip.position(node);
	var t = document.createElement("DIV");
	t.innerHTML = '<div class="container">' + text + '</div><div class="start"></div>';
	t.className = "tooltip";
	document.body.appendChild(t);
	t.style.left = (pos.x) + 'px';
	t.style.top = (pos.y - t.clientHeight) + 'px';
	Tooltip._active = t;
};


Tooltip.position = function(node){
	var x = 0;
	var y = 0;
	for (; node != null; node = node.offsetParent){
		x += node.offsetLeft;
		y += node.offsetTop;
	}
	return {x: x, y: y};
};




Wall = {};

Wall._MODE_NONE = 0;
Wall._MODE_PHOTO_OPTIONS = 1;
Wall._MODE_PHOTO_FILE = 2;
Wall._MODE_PHOTO_CAM = 3;
Wall._MODE_VIDEO_OPTIONS = 4;
Wall._MODE_VIDEO_FILE = 5;
Wall._MODE_VIDEO_CAM = 6;
Wall._MODE_AUDIO_OPTIONS = 7;
Wall._MODE_AUDIO_FILE = 8;
Wall._MODE_AUDIO_CAM = 9;


WallConfig = {
	submitTo: '/net/savewall',
	
	photoFlashFile: '/swf/photo.swf',
	photoFlashPhotoButtonSrc: "/images/photobutton.png",
	
	videoFlashFile: '/swf/video.swf',
	videoFlashStartRecordButtonSrc: encodeURIComponent("/images/wall/record3.png"),
	videoFlashStopRecordButtonSrc: encodeURIComponent("/images/wall/pause3.png"),
	videoFlashUploadUrl: encodeURIComponent("rtmp://91.199.230.202/videourl"),
	
	audioFlashFile: '/swf/sound.swf',
	audioFlashStartRecordButtonSrc: encodeURIComponent("/images/wall/record3.png"),
	audioFlashStopRecordButtonSrc: encodeURIComponent("/images/wall/pause3.png"),
	audioFlashMicrophoneButtonSrc: encodeURIComponent("/images/wall/microphone.png"),
	audioFlashUploadUrl: encodeURIComponent("rtmp://91.199.230.202/videourl")
};

Wall.randomSeed = Math.round(Math.random()*100000);

Wall._state = {
		textChanged: false,
		mode: Wall._MODE_NONE,
		initilized: false,
		externalSubmit: null,
		internalSubmit: null,
		selectAttachButtons: null,
		selectAttachButtonsOldValue: null,
		
		appendPhotoBlock: function(){
			var div = Wall._state._generateOptions(WallLocalization.photoDialogTitle, "photo",
					[
               			{name: WallLocalization.uploadPhotoOptionText, descr: WallLocalization.uploadPhotoOptionSnippet, onclick: "Wall.toAttachFile()"},
               			{name: WallLocalization.takePhotoOptionText, descr: WallLocalization.takePhotoOptionSnippet, onclick: "Wall.toAttachNewRecord()"}
               		]);
			Wall._state.options = Wall._state._attachBlock(div);
		},
		
		appendVideoBlock: function(){
			var div = Wall._state._generateOptions(WallLocalization.videoDialogTitle, "video",
					[
               			{name: WallLocalization.uploadVideoOptionText, descr: WallLocalization.uploadVideoOptionSnippet, onclick: "Wall.toAttachFile()"},
               			{name: WallLocalization.takeVideoOptionText, descr: WallLocalization.takeVideoOptionSnippet, onclick: "Wall.toAttachNewRecord()"}
               		]);
			Wall._state.options = Wall._state._attachBlock(div);
		},
		
		appendAudioBlock: function(){
			var div = Wall._state._generateOptions(WallLocalization.audioDialogTitle, "video",
					[
               			{name: WallLocalization.uploadAudioOptionText, descr: WallLocalization.uploadAudioOptionSnippet, onclick: "Wall.toAttachFile()"},
               			{name: WallLocalization.takeAudioOptionText, descr: WallLocalization.takeAudioOptionSnippet, onclick: "Wall.toAttachNewRecord()"}
               		]);
			Wall._state.options = Wall._state._attachBlock(div);
		},
		
		removeOptionsBlock: function(){
			var o = Wall._state.options;
			if (o && o.parentNode)
				o.parentNode.removeChild(o);
		},
		
		removeOptionBlock: function(){
			var o = Wall._state.option;
			if (o && o.parentNode)
				o.parentNode.removeChild(o);
		},
		
		appendExternalSubmit: function(){
			//var input = Wall._state.externalSubmit = document.createElement("DIV");
			//input.innerHTML = '<input type="submit" value="' + WallLocalization.shareButtonText + '" class="enabled"/>';
			//input.className = "share";
			//Wall._state.container.appendChild(input);
			Wall._state.internalSubmit.disabled = true;
			var x = Wall._state.internalSubmit.className;
			if (!x)
				x = "";
			Wall._state.internalSubmit.className = x.replace("disabled", "") + "disabled";
		},
		
		disableExternalSubmit: function(){
			//var i = WUtil.byTag(Wall._state.externalSubmit, "INPUT");
			//i.className = "disabled";
			//i.disabled = true;
			Wall._state.internalSubmit.disabled = true;
			var x = Wall._state.internalSubmit.className;
			if (!x)
				x = "";
			Wall._state.internalSubmit.className = x.replace("disabled", "") + "disabled";
			if (Wall._state.externalSubmit != null)
				Wall._state.externalSubmit.parentNode.removeChild(Wall._state.externalSubmit);
		},
		
		enableExternalSubmit: function(){
			//var i = WUtil.byTag(Wall._state.externalSubmit, "INPUT");
			//i.className = "enabled";
			//i.disabled = false;
			Wall._state.internalSubmit.disabled = false;
			var x = Wall._state.internalSubmit.className;
			if (!x)
				x = "";
			Wall._state.internalSubmit.className = x.replace("disabled", "");
			if (Wall._state.externalSubmit == null){
				var x = Wall._state.externalSubmit = document.createElement("DIV");
				x.innerHTML = '<table><tbody><tr><td><a href="#" onclick="Wall._state._showAsAttach(); return false;">' +
					 WallLocalization.attachFile + '</a></td></tr></tbody></table>';
				WUtil.byClassSubstr(Wall._state.option, "choosen-option").appendChild(x);
			}
		},
		
		_showAsAttach: function(){
			var p = document.getElementById("wall_popup");
			Wall._state.container.appendChild(p);
			p.style.display = 'none';
			Wall._state.selectAttachButtonsOldValue = Wall._state.selectAttachButtons.innerHTML;
			Wall._state.selectAttachButtons.innerHTML = WallLocalization.fileAttached +
				'<a href="#" onclick="Wall._state._removeAttach(); return false;">' + WallLocalization.removeAttachedFile + '</a>';
		},
		
		_removeAttach: function(){
			var p = document.getElementById("wall_popup");
			p.parentNode.removeChild(p);
			Wall._state.selectAttachButtons.innerHTML = Wall._state.selectAttachButtonsOldValue;
			Wall.toDefault();
		},
		
		removeExternalSubmit: function(){
			//var s = Wall._state.externalSubmit;
			//s.parentNode.removeChild(s);
			//Wall._state.externalSubmit = null;
			Wall._state.internalSubmit.disabled = false;
			var x = Wall._state.internalSubmit.className;
			if (!x)
				x = "";
			Wall._state.internalSubmit.className = x.replace("disabled", "");
		},
		
		hideAttachButtonsContainer: function(){
			//Wall._state.attachButtonsContainer.style.display = 'none';
		},
		
		showAttachButtonsContainer: function(){
			Wall._state.attachButtonsContainer.style.display = '';
		},
		
		showAttachImageButton: function(){
			Wall._state._showAttachButton(WallLocalization.attachPhotoDialogTitle, WallLocalization.attachPhotoText, "photo", "photofile", "Wall.onAttachFile();");
		},
		showAttachVideoButton: function(){
			Wall._state._showAttachButton(WallLocalization.attachVideoDialogTitle, WallLocalization.attachVideoText, "video", "videofile", "Wall.onAttachFile();");
		},
		showAttachAudioButton: function(){
			Wall._state._showAttachButton(WallLocalization.attachAudioDialogTitle, WallLocalization.attachAudioText, "audio", "audiofile", "Wall.onAttachFile();");
		},
		
		_showAttachButton: function(header, text, clazz, name, onchange){
			var div = Wall._state._generateOption(header, clazz,
						text + "<br/>" + 
						'<input type="file" name="' + name + '" onchange="' + onchange + '"/>'
					);
			Wall._state.option = Wall._state._attachBlock(div);
		},
		
		
		showPhotoCamera: function(){
			Wall._state._startFlashLoad(WallLocalization.takePhotoDialogTitle, WallConfig.photoFlashFile,
					"onload=Wall.onFlashLoad&" + 
					"button_src=" + WallConfig.photoFlashPhotoButtonSrc + "&" +
					"font=Arial&font_color=0x0000FF&" +
					"onphoto=Wall.onFlashStopRecord");
		},
		
		showVideoCamera: function(){
			Wall._state._startFlashLoad(WallLocalization.takeVideoDialogTitle, WallConfig.videoFlashFile,
					"onload=Wall.onFlashLoad&" + 
					"record_src=" + WallConfig.videoFlashStartRecordButtonSrc + "&" +
					"pause_src=" + WallConfig.videoFlashStopRecordButtonSrc + "&" +
					"upload_to=" + WallConfig.videoFlashUploadUrl + "&" +
					"user_id=" + WallUserConfig.userId + "&" +
					"random_seed=" + Wall.randomSeed + "&" +
					"onstoprecord=Wall.onFlashStopRecord&" +
					"onstartrecord=Wall.onFlashStartRecord");
		},
		
		showAudioCamera: function(){
			Wall._state._startFlashLoad(WallLocalization.takeAudioDialogTitle, WallConfig.audioFlashFile,
					"onload=Wall.onFlashLoad&" + 
					"record_src=" + WallConfig.audioFlashStartRecordButtonSrc + "&" +
					"pause_src=" + WallConfig.audioFlashStopRecordButtonSrc + "&" +
					"microphone_src=" + WallConfig.audioFlashMicrophoneButtonSrc + "&" +
					"upload_to=" + WallConfig.audioFlashUploadUrl + "&" +
					"user_id=" + WallUserConfig.userId + "&" +
					"random_seed=" + Wall.randomSeed + "&" +
					"onstoprecord=Wall.onFlashStopRecord&" +
					"onstartrecord=Wall.onFlashStartRecord");
		},
		
		removeLoad: function(){
			var n = WUtil.byClassSubstr(Wall._state.option, "load");
			if (n)
				n.className = n.className.replace("load", "");
		},
		
		_startFlashLoad: function(flashTitle, flashName, flashArgs){
			var div = Wall._state._generateOption(flashTitle, "photo",
					'<embed id="main" width="350" height="300" allowfullscreen="true" wmode="window" scale="noscale" ' +
					'quality="high" bgcolor="#ffffff" name="main" style="" ' +
					'src="' + flashName + '?' + flashArgs + '" type="application/x-shockwave-flash" FlashVars="' + flashArgs + '"/>',
				true);
			Wall._state.option = Wall._state._attachBlock(div);
		},
		
		flashExec: function(func, url, callback){
			var embed = WUtil.byTag(Wall._state.option, "EMBED");
			var f;
			url += ((url.indexOf('?') >= 0) ? "&" : "?") + WUtil.serializeForm(Wall._state.container);
			eval("embed." + func + "('" + url + "', '" + callback + "')");
		},
		
		_attachBlock: function(div){
			var x = document.getElementById("wall_popup");
			if (x){
				for (; !x.className || x.className.indexOf("p-out") < 0; x = x.parentNode);
				x.parentNode.removeChild(x);
			}
				
			var d1 = document.createElement("DIV");
			d1.id = "wall_popup";
			d1.appendChild(div);
			document.body.appendChild(d1);
			Popup.open("wall_popup", {width: 450, onClose: "Wall.toDefault(); return false;"});
			return d1;//document.getElementById("wall_popup_content").firstChild;
		},

		_generateOption: function(title, className, innerHTML, load){
			var div = document.createElement("DIV");
			div.innerHTML = 
				'<table><tbody>' +
					//'<tr><td colspan="' + 1 + '" class="hl">' +
					//	'<span class="close" onclick="Wall.toDefault();"></span>' +
					//	'<div class="titlecnt">' +
					//		'<span class="' + className + '"></span>' +
					//		'<span class="title">' + title + '</span>' +
					//	'</div>' +
					//'</td></tr>' +
					'<tr><td><div class="choosen-option' + (load ? ' load' : '') + '">' +
						innerHTML +
					'</div></td></tr>' +
				'</tbody></table>';
			div.className = "wall-options";
			return div;
		},
		
		_generateOptions: function(title, className, options){
			var div = document.createElement("DIV");
			var html =
				'<table><tbody>';// +
					//'<tr><td colspan="' + (2*options.length-1) + '" class="hl">' +
					//	'<span class="close" onclick="Wall.toDefault();"></span>' +
					//	'<div class="titlecnt">' +
					//		'<span class="' + className + '"></span>' +
					//		'<span class="title">' + title + '</span>' +
					//	'</div>' +
					//'</td></tr>' +
					//'<tr>';
			for (var i=0; i<options.length; i++){
				var o = options[i];
				html +=
					'<td class="option" style="width: ' + (100/options.length) + '%" onclick="' + o.onclick + '; return false;">' +
						'<div class="name">' + o.name + '</div>' +
						'<div class="description">' + o.descr + '</div>' +
					'</td>';
				if (i != options.length-1)
					html += '<td class="vl"></td>';
			}
			html += '</tr></tbody></table>';
			div.className = "wall-options";
			div.innerHTML = html;
			return div;
		}
	};




Wall._init = function(){
	if (!Wall._state.initilized){
		var c = Wall._state.container = document.getElementById("wall");
		Wall._state.textarea = WUtil.byTag(c, "TEXTAREA");
		Wall._state.attachButtonsContainer = document.getElementById("wall-attach");
		Wall._state.initilized = true;
		Wall._state.internalSubmit = document.getElementById("wall-attach-submit");
		//var form = Wall._state.internalSubmit;
		//for (; form.tagName != 'FORM'; form = form.parentNode);
		//Wall._state.internalSubmit.onclick = form.onsubmit;
		Wall._state.selectAttachButtons = document.getElementById("wall-attach-value");
	}
};


Wall._toOptions = function(showOptionsFunction, resMode){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_NONE){
		Wall._state.hideAttachButtonsContainer();
		showOptionsFunction();
		Wall._state.appendExternalSubmit();
		Wall._state.disableExternalSubmit();
		Wall._state.mode = resMode;
	};
}

Wall.toAddPhoto = function(){
	Wall._toOptions(Wall._state.appendPhotoBlock, Wall._MODE_PHOTO_OPTIONS);
};

Wall.toAddVideo = function(){
	Wall._toOptions(Wall._state.appendVideoBlock, Wall._MODE_VIDEO_OPTIONS);
};

Wall.toAddAudio = function(){
	Wall._toOptions(Wall._state.appendAudioBlock, Wall._MODE_AUDIO_OPTIONS);
};

Wall.toDefault = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_PHOTO_OPTIONS || Wall._state.mode == Wall._MODE_VIDEO_OPTIONS || Wall._state.mode == Wall._MODE_AUDIO_OPTIONS){
		Wall._state.showAttachButtonsContainer();
		Wall._state.removeOptionsBlock();
		Wall._state.removeExternalSubmit();
		Wall._state.mode = Wall._MODE_NONE;
	}
	else if (Wall._state.mode == Wall._MODE_PHOTO_FILE || Wall._state.mode == Wall._MODE_PHOTO_CAM ||
			 Wall._state.mode == Wall._MODE_VIDEO_FILE || Wall._state.mode == Wall._MODE_VIDEO_CAM ||
			 Wall._state.mode == Wall._MODE_AUDIO_FILE || Wall._state.mode == Wall._MODE_AUDIO_CAM){
		Wall._state.showAttachButtonsContainer();
		Wall._state.removeOptionBlock();
		Wall._state.removeExternalSubmit();
		Wall._state.mode = Wall._MODE_NONE;
	}
	
};

Wall.onAttachFile = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_PHOTO_FILE || Wall._state.mode == Wall._MODE_VIDEO_FILE || Wall._state.mode == Wall._MODE_AUDIO_FILE){
		Wall._state.enableExternalSubmit();
		Wall._state._showAsAttach();
	}
};

Wall.onFlashLoad = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_PHOTO_CAM){
		Wall._state.removeLoad();
	}
};

Wall.onFlashStopRecord = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_VIDEO_CAM || Wall._state.mode == Wall._MODE_AUDIO_CAM || Wall._state.mode == Wall._MODE_PHOTO_CAM){
		Wall._state.enableExternalSubmit();
	}
};

Wall.onFlashStartRecord = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_VIDEO_CAM || Wall._state.mode == Wall._MODE_AUDIO_CAM){
		Wall._state.disableExternalSubmit();
	}
};

Wall.toAttachFile = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_VIDEO_OPTIONS){
		Wall._state.showAttachVideoButton();
		Wall._state.removeOptionsBlock();
		Wall._state.mode = Wall._MODE_VIDEO_FILE;
	}
	else if (Wall._state.mode == Wall._MODE_AUDIO_OPTIONS){
		Wall._state.showAttachAudioButton();
		Wall._state.removeOptionsBlock();
		Wall._state.mode = Wall._MODE_AUDIO_FILE;
	}
	else if (Wall._state.mode == Wall._MODE_PHOTO_OPTIONS){
		Wall._state.showAttachImageButton();
		Wall._state.removeOptionsBlock();
		Wall._state.mode = Wall._MODE_PHOTO_FILE;
	}
};

Wall.toAttachNewRecord = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_VIDEO_OPTIONS){
		Wall._state.showVideoCamera();
		Wall._state.removeOptionsBlock();
		Wall._state.mode = Wall._MODE_VIDEO_CAM;
	}
	else if (Wall._state.mode == Wall._MODE_PHOTO_OPTIONS){
		Wall._state.showPhotoCamera();
		Wall._state.removeOptionsBlock();
		Wall._state.mode = Wall._MODE_PHOTO_CAM;
	}
	else if (Wall._state.mode == Wall._MODE_AUDIO_OPTIONS){
		Wall._state.showAudioCamera();
		Wall._state.removeOptionsBlock();
		Wall._state.mode = Wall._MODE_AUDIO_CAM;
	}
};



Wall.onSubmit = function(){
	Wall._init();
	if (Wall._state.mode == Wall._MODE_NONE){
		document.getElementsByName("walltype")[0].value = "text";
		if (!Wall._state.textChanged){
			alert(WallLocalization.noText);
			return false;
		}
		return true;
	}
	else if (Wall._state.mode == Wall._MODE_PHOTO_FILE){
		if (!Wall._state.textChanged)
			document.getElementsByName("message")[0].value = "";
		document.getElementsByName("walltype")[0].value = "image";
		Wall._state.disableExternalSubmit();
		return true;
	}
	else if (Wall._state.mode == Wall._MODE_PHOTO_CAM){
		if (!Wall._state.textChanged)
			document.getElementsByName("message")[0].value = "";
		document.getElementsByName("walltype")[0].value = "photo";
		Wall._state.disableExternalSubmit();
		Wall._state.flashExec("uploadPhoto", WallConfig.submitTo, "Wall.afterFlashSubmit");
		return false;
	}
	else if (Wall._state.mode == Wall._MODE_VIDEO_FILE){
		if (!Wall._state.textChanged)
			document.getElementsByName("message")[0].value = "";
		document.getElementsByName("walltype")[0].value = "videofile";
		Wall._state.disableExternalSubmit();
		return true;
	}
	else if (Wall._state.mode == Wall._MODE_AUDIO_FILE){
		if (!Wall._state.textChanged)
			document.getElementsByName("message")[0].value = "";
		document.getElementsByName("walltype")[0].value = "audiofile";
		Wall._state.disableExternalSubmit();
		return true;
	}
	else if (Wall._state.mode == Wall._MODE_VIDEO_CAM){
		if (!Wall._state.textChanged)
			document.getElementsByName("message")[0].value = "";
		document.getElementsByName("walltype")[0].value = "videocam";
		document.getElementsByName("file_id")[0].value = Wall.randomSeed;
		Wall._state.disableExternalSubmit();
		return true;
	}
	else if (Wall._state.mode == Wall._MODE_AUDIO_CAM){
		if (!Wall._state.textChanged)
			document.getElementsByName("message")[0].value = "";
		document.getElementsByName("walltype")[0].value = "audiocam";
		document.getElementsByName("file_id")[0].value = Wall.randomSeed;
		Wall._state.disableExternalSubmit();
		return true;
	}
	else {
		document.getElementsByName("walltype")[0].value = "text";
	}
	return false;
};

Wall.afterFlashSubmit = function(){
	window.location = window.location;
};

Wall.onTextareaKeyDown = function(textarea){
	textarea.value = '';
	textarea.onclick = null;
	Wall._state.textChanged = true;
}









WUtil = {};

WUtil._find = function (node, f){
	if (node == null)
		return null;
	if (f(node))
		return node;
	for (var i = 0; i<node.childNodes.length; i++){
		var x = WUtil._find(node.childNodes[i], f);
		if (x)
			return x;
	}
	return null;
};

WUtil.byTag = function(node, tagName){
	return WUtil._find(node, function(n){ return n.tagName == tagName; });
};

WUtil.byClassSubstr = function(node, clazz){
	return WUtil._find(node, function(n){ var c = n.className; return c != null && c.indexOf(clazz) >= 0; });
};

WUtil.serializeForm = function(node) {
	var form = node;
	for (; form.tagName != 'FORM'; form = form.parentNode);
	  
	var res = "";
	for (var i = 0; i < form.elements.length; i++) {
		var el = form.elements[i];
		if (el.tagName == 'INPUT' && el.type == 'radio') {
			if (el.checked) {
				if (res.length > 0)
					res += '&';
				res += encodeURIComponent(el.name) + '=';
				res += encodeURIComponent(el.value);
			}
	    }
		else {
			if (res.length > 0)
				res += '&';
			res += encodeURIComponent(el.name) + '=';
			if (el.tagName == 'INPUT' && el.type == 'checkbox')
				res += el.checked;
			else
				res += encodeURIComponent(el.value);
		}
	}
	return res;
};
