this.onload = function(){
	AudioRecorder.init();
}
var AudioRecorder={};
AudioRecorder=function(){
	
	var RECORD_ICON = "http://mediacomments.flashvisions.com/images/media-comments-audio-ico.gif";
	var PLAYBACK_ICON = "http://mediacomments.flashvisions.com/images/mediacomment.gif";
	var RECORDER = '<embed src="http://mediacomments.flashvisions.com/swf/AudioRecorder.swf" wmode="opaque" allowScriptAccess="always" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="250" height="150">';
	var WPCOMMENTS = "comment";
	var CLOSE_SYMBOL = "x";
	var RECORD_LABEL = "Add audio comment (beta!)";
	
	var closeSymbolPad = 10;
	var closeButtonColor = "#000000";
	var recordButtonClassName = "insert-mediacomments-button";
	var recorderContainerID = "mediaRecorder";
	var recorderClassName = "mediaCommentsRecorder";
	var recorderTitleBarClass = "mediaCommentsRecorderTitleBar";
	var commentsClass = "mediaComment";
	var commentsContainerClass = "mediacommentContainer";
	
	var $uuid = "media_comment_";
	var $mdb = {};
	
	var $config = {commentBox:WPCOMMENTS,closeSymbolColor:closeButtonColor,closeSymbol:CLOSE_SYMBOL,addCommentLabel:RECORD_LABEL};
	
		function init(){
			createAudioCommentButton();
			createMediaRecorderContainer();
			renderMediaComments();
		}
		function renderMediaComments(){
			var comments = getElementsByClass(commentsClass, null, 'a');
			for (var i = 0; i < comments.length; i++) {
				var comment = comments[i];
			
				var commentContainer = document.createElement('div');
				commentContainer.className = commentsContainerClass;
				comment.parentNode.appendChild(commentContainer);
				
				var playbackicon = document.createElement('img');
				playbackicon.setAttribute('src', PLAYBACK_ICON);
				commentContainer.appendChild(playbackicon);
				commentContainer.appendChild(comment);
				
				var id = $uuid + i;
				$mdb[id] = comment.getAttribute('href');
				comment.setAttribute('href', '#');
				comment.setAttribute('id', id);
				comment.onclick = showPlayer;
			}
		}
		function showPlayer(){
			var comment_container = this.parentNode;
			var commentTrigger = this;
			var id = commentTrigger.getAttribute('id');
			var link = $mdb[id];
			var player = document.createElement('embed');
			player.setAttribute('quality', 'high');
			player.setAttribute('pluginspage', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash');
			player.setAttribute('type', 'application/x-shockwave-flash');
			player.setAttribute('width', '250');
			player.setAttribute('height', '30');
			player.setAttribute('src', link);
			comment_container.parentNode.replaceChild(player, comment_container);
			return false;
		}
		function createAudioCommentButton(){
			var commentBox = document.getElementsByName(WPCOMMENTS)[0];
			var commentsContainer = commentBox.parentNode;
			var addCommentButtonDiv = document.createElement('div');
			var audioicon = document.createElement('img');
			audioicon.setAttribute('src', RECORD_ICON);
			addCommentButtonDiv.appendChild(audioicon);
			var audioCommentButtonLink = document.createElement('a');
			audioCommentButtonLink.setAttribute('href', '#');
			audioCommentButtonLink.className = recordButtonClassName;
			audioCommentButtonLink.innerHTML = RECORD_LABEL;
			addCommentButtonDiv.appendChild(audioCommentButtonLink);			
			commentsContainer.insertBefore(addCommentButtonDiv, commentBox);
			audioCommentButtonLink.onclick = showRecorder;
		}
		function createMediaRecorderContainer(){
			var commentBox = document.getElementsByName(WPCOMMENTS)[0];
			var commentsContainer = commentBox.parentNode;
			var recorderContainer = document.createElement('div');
			recorderContainer.setAttribute('id', recorderContainerID);
			commentsContainer.insertBefore(recorderContainer, commentBox);
			return false;
		}
		function getElementsByClass(searchClass, node, tag){
			var classElements = [];
			if (node == null)node = document;
			if (tag == null)tag = '*';
			var els = node.getElementsByTagName(tag);
			var elsLen = els.length;
			var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
			for (i = 0, j = 0; i < elsLen; i++) {
				if (pattern.test(els[i].className)) {
					classElements[j] = els[i];
					j++;
				}
			}
			return classElements;
		}
		function hideRecorder(){
			var container = document.getElementById(recorderContainerID);
			container.innerHTML = '';
			return false;
		}
		function showRecorder(){
			var container = document.getElementById(recorderContainerID);
			container.className = recorderClassName;
			if (container.innerHTML != '') return false;
			var closeButtonContainer = document.createElement('div');
			closeButtonContainer.className = recorderTitleBarClass;
			var closeButton = document.createElement('a');
			closeButton.style.color = closeButtonColor;
			closeButton.style.paddingRight = closeSymbolPad + 'px';
			closeButton.setAttribute('href', '#');
			closeButton.appendChild(document.createTextNode(CLOSE_SYMBOL));
			closeButton.onclick = hideRecorder;
			closeButtonContainer.appendChild(closeButton);
			container.appendChild(closeButtonContainer);
			var recorderContainer = document.createElement("div");
			recorderContainer.style.width = '100%';
			container.appendChild(recorderContainer);
			
			var recorder = document.createElement('embed');
			recorder.setAttribute('src', 'http://mediacomments.flashvisions.com/swf/AudioRecorder.swf');
			recorder.setAttribute('wmode', 'opaque');
			recorder.setAttribute('allowScriptAccess', 'always');
			recorder.setAttribute('quality', 'high');
			recorder.setAttribute('pluginspage', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash');
			recorder.setAttribute('type', 'application/x-shockwave-flash');
			recorder.setAttribute('width', '250');
			recorder.setAttribute('height', '150');
			
			container.appendChild(recorder);
			
			return false;
		}
		return{
			init:init,
			showRecorder:showRecorder,
			hideRecorder:hideRecorder,
			configuration:$config
		}
}();	


/* Callback from recorder */

function ShowAlert(msg){
	TINY.box.show(msg,0,0,0,0,0);
}

function getHTMLDomain(){
	return document.domain;
}

function onMediaRecorderResetRecording(){
	
}

function onMediaRecorderMicrophoneNotFound(){
	TINY.box.show("No microphone found !" ,0,0,0,0,0);
}

function onMediaRecorderReady(){
	
}

function onMediaRecorderFailed(){
	TINY.box.show('Recorder failed !',0,0,0,0,0);
}

function onMediaRecorderConnectionFailed(){
	TINY.box.show('Server Connection failed !',0,0,0,0,0);
}

function onMediaRecorderConnectionReady(){
	
}

function onMediaRecorderMicrophoneReady(){
	
}

function onMediaRecorderMicrophoneInit(){
	
}

function onMediaRecorderStartRecord(){
	
}

function onMediaRecorderRecording(){
	
}

function onMediaRecorderStopRecord(){
	
}

function onMediaRecorderStopRecord(){
	
}

function onMediaRecorderStartPlayback(){
	
}

function onMediaRecorderStopPlayback(){
	
}

function onMediaRecorderSaveFailed(){
	TINY.box.show("Save failed !" ,0,0,0,0,0);
}


function onMediaRecorderSaveSuccess()
{
	var WPCOMMENTS = "comment";
	var result = eval(arguments[0]);
	var commentBox = document.getElementsByName(WPCOMMENTS)[0];
	commentBox.value =  '[mediacomments media="'+result[0]+'"]';
	AudioRecorder.hideRecorder();
	TINY.box.hide();
}

function onMediaRecorderSaveRecorded()
{
	TINY.box.show("Saving recording !" ,0,0,0,0,0);
}


/* -------------- TINY BOX -----------------*/

var TINY={};

function T$(i){return document.getElementById(i)}

TINY.box=function(){
	var p,m,b,fn,ic,iu,iw,ih,ia,f=0;
	return{
		show:function(c,u,w,h,a,t){
			if(!f){
				p=document.createElement('div'); p.id='tinybox';
				m=document.createElement('div'); m.id='tinymask';
				b=document.createElement('div'); b.id='tinycontent';
				document.body.appendChild(m); document.body.appendChild(p); p.appendChild(b);
				m.onclick=TINY.box.hide; window.onresize=TINY.box.resize; f=1
			}
			if(!a&&!u){
				p.style.width=w?w+'px':'auto'; p.style.height=h?h+'px':'auto';
				p.style.backgroundImage='none'; b.innerHTML=c
			}else{
				b.style.display='none'; p.style.width=p.style.height='100px'
			}
			this.mask();
			ic=c; iu=u; iw=w; ih=h; ia=a; this.alpha(m,1,80,3);
			if(t){setTimeout(function(){TINY.box.hide()},1000*t)}
		},
		fill:function(c,u,w,h,a){
			if(u){
				p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}
				};
				x.open('GET',c,1); x.send(null)
			}else{
				this.psh(c,w,h,a)
			}
		},
		psh:function(c,w,h,a){
			if(a){
				if(!w||!h){
					var x=p.style.width, y=p.style.height; b.innerHTML=c;
					p.style.width=w?w+'px':''; p.style.height=h?h+'px':'';
					b.style.display='';
					w=parseInt(b.offsetWidth); h=parseInt(b.offsetHeight);
					b.style.display='none'; p.style.width=x; p.style.height=y;
				}else{
					b.innerHTML=c
				}
				this.size(p,w,h)
			}else{
				p.style.backgroundImage='none'
			}
		},
		hide:function(){
			TINY.box.alpha(p,-1,0,3)
		},
		resize:function(){
			TINY.box.pos(); TINY.box.mask()
		},
		mask:function(){
			m.style.height=TINY.page.total(1)+'px';
			m.style.width=''; m.style.width=TINY.page.total(0)+'px'
		},
		pos:function(){
			var t=(TINY.page.height()/2)-(p.offsetHeight/2); t=t<10?10:t;
			p.style.top=(t+TINY.page.top())+'px';
			p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px'
		},
		alpha:function(e,d,a){
			clearInterval(e.ai);
			if(d==1){
				e.style.opacity=0; e.style.filter='alpha(opacity=0)';
				e.style.display='block'; this.pos()
			}
			e.ai=setInterval(function(){TINY.box.ta(e,a,d)},20)
		},
		ta:function(e,a,d){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai);
				if(d==-1){
					e.style.display='none';
					e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''
				}else{
					e==m?this.alpha(p,1,100):TINY.box.fill(ic,iu,iw,ih,ia)
				}
			}else{
				var n=Math.ceil((o+((a-o)*.5))); n=n==1?0:n;
				e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		},
		size:function(e,w,h){
			e=typeof e=='object'?e:T$(e); clearInterval(e.si);
			var ow=e.offsetWidth, oh=e.offsetHeight,
			wo=ow-parseInt(e.style.width), ho=oh-parseInt(e.style.height);
			var wd=ow-wo>w?0:1, hd=(oh-ho>h)?0:1;
			e.si=setInterval(function(){TINY.box.ts(e,w,wo,wd,h,ho,hd)},20)
		},
		ts:function(e,w,wo,wd,h,ho,hd){
			var ow=e.offsetWidth-wo, oh=e.offsetHeight-ho;
			if(ow==w&&oh==h){
				clearInterval(e.si); p.style.backgroundImage='none'; b.style.display='block'
			}else{
				if(ow!=w){var n=ow+((w-ow)*.5); e.style.width=wd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				if(oh!=h){var n=oh+((h-oh)*.5); e.style.height=hd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				this.pos()
			}
		}
	}
}();

TINY.page=function(){
	return{
		top:function(){return document.documentElement.scrollTop||document.body.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},
		total:function(d){
			var b=document.body, e=document.documentElement;
			return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
			Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();

