SharePanelManager=function(){
	this._shrPanels = {};
}
SharePanelManager.prototype={
userEmail: '',
userName: '',
userKey: '',
initUser: function(userName,userEmail,userKey){
	this.userEmail=userEmail;
	this.userName=userName;
	this.userKey=userKey;
},
initPanel: function(panelId,holderId,shareType,shareId,linkUrl,linkTitle){
	if(this._shrPanels[panelId]==null){
		var _shrPanel = new SharePanel();
		_shrPanel.init(panelId,holderId,shareType,shareId,linkUrl,linkTitle,this.userName,this.userEmail,this.userKey);
		this._shrPanels[panelId]=_shrPanel;
	}
	return false;
},
togglePanel: function(panelId,type){
	if(this._shrPanels[panelId]){
		this._shrPanels[panelId].toggle(type);
	}
	return false;
},
closePanel: function(panelId){
	if(this._shrPanels[panelId]){
		this._shrPanels[panelId].closePanel();
	}
	return false;
},
sendMail: function(panelId){
	if(this._shrPanels[panelId]){
		this._shrPanels[panelId].sendMail();
	}
	return false;
},
publish: function(panelId,type){
	if(this._shrPanels[panelId]){
		this._shrPanels[panelId].publish(type);
	}
	return false;
},
previewMiniAlbum: function(panelId,flag){
	if(this._shrPanels[panelId]){
		this._shrPanels[panelId].previewMiniAlbum(flag);
	}
	return false;
},
doSendMailResponse: function(raw,idObj){
	var panelId=idObj.id;
	if(_shrPanelManager._shrPanels[panelId]){
		_shrPanelManager._shrPanels[panelId].doSendMailResponse(raw);
	}
	return false;
}
}

SharePanel=function(){
	this.mailErrors={};
}
SharePanel.prototype={
mail_aj: null,
ctype: '',
panelId: '',
holder: null,
shareType: '',
shareId: '',
linkUrl: '',
baseLinkTitle: '',
linkTitle: '',
userName: '',
userEmail: '',
working: false,
k: '',
exportUrl: 'http://'+document.location.hostname+'/_share/',
init: function(panelId,holderId,shareType,shareId,linkUrl,baseLinkTitle,userName,userEmail,k){
	this.panelId=panelId;
	this.holder=gel(holderId);
	this.shareType=shareType;
	this.shareId=shareId;
	this.linkUrl=linkUrl;
	this.baseLinkTitle=baseLinkTitle;
	this.linkTitle='musicMe - '+baseLinkTitle;
	this.userName=userName;
	this.userEmail=userEmail;
	this.k=k;
},
display: function(type){
	var pnlhtml='';
	this.preview = null;
	if(type==this.ctype){
		this.closePanel();
	}
	if(type=='email'){
		pnlhtml=this.getContentEmail();
		if(typeof(pageTracker)!="undefined"){pageTracker._trackPageview('/clic/share/email');}
	}
	else if(type=='share'){
		pnlhtml=this.getContentShare();
		if(typeof(pageTracker)!="undefined"){pageTracker._trackPageview('/clic/share/publish');}
	}
	else if(type=='emailsent'){
		pnlhtml=this.getContentEmailSent();
	}
	else{
		return false;
	}
	this.ctype=type;
	this.holder.innerHTML=pnlhtml;
	this.showPanel();
	return false;
},
getContentEmail: function(){
	var h='';
	h+=this.openContent();
	var mailTitle='';
	var sndClass='';
	var titleClass='';
	if(this.shareType=='album'){
		mailTitle='Envoyer cet album par e-mail';
	}
	else{
		mailTitle='Envoyer cette vid&eacute;o par e-mail';
	}
	if(this.shareType=='videoh'){
		sndClass=' txlarge';
		titleClass='sharetitlelrg';
	}
	else{
		titleClass='sharetitle';
	}
	h+='<table class="sharetitlebar"><tr><td class="'+titleClass+'">'+mailTitle+'</td><td class="shareclose"><a href=# onclick="return _shrPanelManager.closePanel(\''+this.panelId+'\')">Fermer</a></td></tr></table>';
	h+='<table class="shareform"><tr><td class="shrlbl">Votre nom :</td><td class="shrfld"><textarea class="txarone'+sndClass+'" name="shrname" id="shrname'+this.panelId+'">'+this.userName+'</textarea><div class="shareerr" id="err_shrname'+this.panelId+'"></div></td></tr><tr><td class="shrlbl">Votre adresse e-mail :</td><td class="shrfld"><textarea class="txarone'+sndClass+'" name="shremail" id="shremail'+this.panelId+'">'+this.userEmail+'</textarea><div class="shareerr" id="err_shremail'+this.panelId+'"></div></td></tr><tr><td class="shrlbl">Destinataires :<br><span class="sharehelp">(s&eacute;parez les adresses par des virgules)</span></td><td class="shrfld"><textarea name="shrdest" id="shrdest'+this.panelId+'" class="'+sndClass+'"></textarea><div class="shareerr" id="err_shrdest'+this.panelId+'"></div></td></tr><tr><td class="shrlbl">Votre message :<br><span class="sharehelp">(optionnel)</span></td><td class="shrfld"><textarea name="shrmsg" id="shrmsg'+this.panelId+'" class="'+sndClass+'"></textarea><div class="shareerr" id="err_shrglobal'+this.panelId+'"></div></td></tr>';
	//h+='<tr><td class="shrbtns" colspan=2><a href="#" onclick="return _shrPanel.previewMail()"><img src="http://graphics2.musicme.com/pict/v5/btn_preview_g.gif" border=0 hspace=4></a><a href="#" onclick="return _shrPanel.sendMail()"><img src="http://graphics2.musicme.com/pict/v5/btn_send.gif" border=0 hspace=4></a></td></tr></table>';
	h+='<tr><td class="shrbtns" colspan=2><a href="#" onclick="return _shrPanelManager.sendMail(\''+this.panelId+'\')"><img src="http://graphics2.musicme.com/pict/v5/btn_pop_send.gif" border=0 hspace=4></a></td></tr></table>';
	h+=this.closeContent();
	return h;
},
getContentShare: function(){
	var miniAlbum='<object width="304" height="200"><param name="movie" value="http://www.musicme.com/_share/minialbum.swf?cb='+this.shareId+'"></param><param name="wmode" value="transparent"></param><embed src="http://www.musicme.com/_share/minialbum.swf?cb='+this.shareId+'" type="application/x-shockwave-flash" width="304" height="200" wmode="transparent"></embed></object><div style="width:280px;position:relative;z-index:1573;top:0px;left:12px;margin-top:-200px;overflow:hidden;"><a href="'+this.linkUrl+'"><img src="http://graphics2.musicme.com/pict/spacer.gif" border="0" width="280" height="180"></a><br /><b><a href="'+this.linkUrl+'">'+this.baseLinkTitle+'</a></b><br />album propos&eacute; par <a href="http://www.musicme.com/">musicMe</a></div>';
	var vidPlayer='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="496" height="441" id="musicmevideo'+this.shareId+'"><param name="movie" value="http://www.musicme.com/_share/vplayer.swf?cb='+this.shareId+'"></param><param name="allowScriptAccess" value="always"><embed src="http://www.musicme.com/_share/vplayer.swf?cb='+this.shareId+'" type="application/x-shockwave-flash" width="496" height="441" allowScriptAccess="always"></embed></object><br /><b><a href="'+this.linkUrl+'">'+this.baseLinkTitle+'</a></b><br />vid&eacute;o propos&eacute;e par <a href="http://www.musicme.com/">musicMe</a>';
	var profileTitle='',codeTitle='';
	var sndClass='';
	var titleClass='';
	if(this.shareType=='album'){
		profileTitle='Publier cet album sur mon profil';
		//codeTitle='Partager cet album sur un blog';
		codeTitle='Partager cet album';
		resTitle='cet album';
	}
	else{
		profileTitle='Publier cette vid&eacute;o sur mon profil';
		//codeTitle='Partager cette vid&eacute;o sur un blog';
		codeTitle='Partager cette vid&eacute;o';
		resTitle='cette vid&eacute;o';
	}
	if(this.shareType=='videoh'){
		sndClass=' txlarge';
		titleClass='sharetitlelrg';
	}
	else{
		titleClass='sharetitle';
	}
	var h=''; 
	h+=this.openContent();
	// profile
	//h+='<table class="sharetitlebar"><tr><td class="'+titleClass+'">'+profileTitle+'</td><td class="shareclose"><a href=# onclick="return _shrPanelManager.closePanel(\''+this.panelId+'\')">Fermer</a></td></tr></table><div class="shareprofile"><table><tr><td class="sharefb"><a href=# onclick="return _shrPanelManager.publish(\''+this.panelId+'\',\'facebook\')" title="'+profileTitle+' Facebook"><img alt="'+profileTitle+' Facebook" src="http://graphics2.musicme.com/pict/v5/vir_fb.gif" border=0 align="absmiddle"> Facebook</a></td><td class="sharemyspace"><a href=# onclick="return _shrPanelManager.publish(\''+this.panelId+'\',\'myspace\')" title="'+profileTitle+' MySpace"><img alt="'+profileTitle+' MySpace" src="http://graphics2.musicme.com/pict/v5/vir_myspace_w.gif" border=0 align="absmiddle"> MySpace</a></td></tr></table></div>';
	// code
	h+='<table class="sharetitlebar"><tr><td class="'+titleClass+'">'+codeTitle+'</td><td class="shareclose"><a href=# onclick="return _shrPanelManager.closePanel(\''+this.panelId+'\')">Fermer</a></td></tr></table><table class="shareform" style="margin-top:8px"><tr><td colspan=2 style="height:24px">Copiez-collez le code ci-dessous sur votre blog ou site perso :</td></tr>';
	if(this.shareType=='album'){
		h+='<tr><td class="shrlbl">Mini-album :</td><td class="shrfld"><textarea wrap="off" class="txarone txargrey" name="shrcode" style="width:288px" readonly="readonly" onclick="this.select()">'+miniAlbum+'</textarea><a href="#" onclick="return _shrPanelManager.previewMiniAlbum(\''+this.panelId+'\',true)" onmouseover="return _shrPanelManager.previewMiniAlbum(\''+this.panelId+'\',true)" onmouseout="return _shrPanelManager.previewMiniAlbum(\''+this.panelId+'\',false)"><img src="http://graphics2.musicme.com/pict/v5/btn_pop_preview.gif" border=0 hspace=16></a></td></tr>';
	}
	h+='<tr><td class="shrlbl">Permalien :</td><td class="shrfld"><textarea wrap="off" class="txarone txargrey'+sndClass+'" name="shrlink" readonly="readonly" onclick="this.select()">'+this.linkUrl+'</textarea></td></tr>';
	if(this.shareType=='videom'||this.shareType=='videoh'){
		h+='<tr><td class="shrlbl">Mini-lecteur :</td><td class="shrfld"><textarea wrap="off" class="txarone txargrey'+sndClass+'" name="shrcode" readonly="readonly" onclick="this.select()">'+vidPlayer+'</textarea></td></tr>';
	}
	h+='<tr><td colspan="2" style="padding-top:24px">ou partagez directement '+resTitle+' sur <a href=# onclick="return _shrPanelManager.publish(\''+this.panelId+'\',\'facebook\')" title="Partager sur Facebook"><img alt="Partager sur Facebook" src="http://graphics2.musicme.com/pict/v5/share_logo_facebook.gif" border=0 align="absmiddle"> <b>Facebook</b></a> | <a href=# onclick="return _shrPanelManager.publish(\''+this.panelId+'\',\'myspace\')" title="Partager sur MySpace"><img alt="Partager sur MySpace" src="http://graphics2.musicme.com/pict/v5/share_logo_myspace.gif" border=0 align="absmiddle"> <b>MySpace</b></a> | <a href=# onclick="return _shrPanelManager.publish(\''+this.panelId+'\',\'twitter\')" title="Partager sur Twitter"><img alt="Partager sur Twitter" src="http://graphics2.musicme.com/pict/v5/share_logo_twitter.gif" border=0 align="absmiddle"> <b>Twitter</b></a> | <a href=# onclick="return _shrPanelManager.publish(\''+this.panelId+'\',\'delicous\')" title="Partager sur Delicious"><img alt="Partager sur Delicious" src="http://graphics2.musicme.com/pict/v5/share_logo_delicious.gif" border=0 align="absmiddle"> <b>Delicious</b></a></td></tr>';
	h+='</table>';
	h+=this.closeContent();
	return h;
},
getContentEmailSent: function(){
	var mailTitle='';
	if(this.shareType=='album'){
		mailTitle='Envoyer cet album par e-mail';
	}
	else{
		mailTitle='Envoyer cette vid&eacute;o par e-mail';
	}
	var h='';
	h+=this.openContent();
	h+='<table class="sharetitlebar"><tr><td class="sharetitle">'+mailTitle+'</td><td class="shareclose"><a href=# onclick="return _shrPanelManager.closePanel(\''+this.panelId+'\')">Fermer</a></td></tr></table><table class="shareform"><tr><td>Votre message a &eacute;t&eacute; envoy&eacute;, merci !</td></tr></table>';
	h+=this.closeContent();
	return h;
},
previewMail: function(){
	
},
sendMail: function(){
	if(this.working){
		return false;
	}
	this.mailErrors={};
	var is_error=0;
	var sender='';
	var sname='';
	var dest='';
	var msg='';
	if(nm=gel('shrname'+this.panelId)){
		if(nm.value==''){
			this.mailErrors['shrname']=1;
			is_error=1;
			this.displayError('shrname','Saisissez votre nom');
		}
		else{
			sname=nm.value;
			this.clearError('shrname');
		}
	}
	if(em=gel('shremail'+this.panelId)){
		if(em.value==''){
			this.mailErrors['shremail']=1;
			is_error=1;
			this.displayError('shremail','Saisissez votre adresse e-mail');
		}
		else if(em.value.indexOf("@")==-1||em.value.indexOf(".")==-1){
			this.mailErrors['shremail']=2;
			is_error=1;
			this.displayError('shremail','Adresse e-mail incorrecte, v&eacute;rifiez votre saisie');
		}
		else{
			sender=em.value;
			this.clearError('shremail');
		}
	}
	if(em=gel('shrdest'+this.panelId)){
		var dest_error=0;
		var ems=em.value.split(",");
		for(var i in ems){
			if(ems[i]==''){
				this.mailErrors['shrdest']=1;
				dest_error=1;
				this.displayError('shrdest','Saisissez une ou plusieurs adresses e-mail d\'envoi');
			}
			else if(ems[i].indexOf("@")==-1||ems[i].indexOf(".")==-1){
				this.mailErrors['shrdest']=2;
				dest_error=1;
				this.displayError('shrdest','Adresse(s) e-mail incorrecte(s), v&eacute;rifiez votre saisie');
			}
		}
		if(dest_error==1){
			is_error=1;
		}
		else{
			dest=em.value;
			this.clearError('shrdest');
		}
	}
	this.clearError('shrglobal');
	if(tx=gel('shrmsg'+this.panelId)){
		msg=tx.value;
	}
	if(is_error==0){
		this.toggleDisabledForm(true);
		this.doSendMail(sender,dest,sname,msg);
	}
	return false;
},
displayError: function(errType,errMsg){
	if(o=gel('err_'+errType+this.panelId)){
		o.innerHTML=errMsg;
		o.style.display='block';
	}
	if(o=gel(errType+this.panelId)){
		o.style.border='1px solid #ff0000';
	}
},
clearError: function(errType){
	if(o=gel('err_'+errType+this.panelId)){
		o.style.display='none';
	}
	if(o=gel(errType+this.panelId)){
		o.style.border='1px solid #9494a1';
	}
},
doSendMail: function(sender,dest,sname,msg){
	this.mail_aj=new ajax();
	this.mail_aj.method='POST';
	this.mail_aj.setResultHandler('_shrPanelManager.doSendMailResponse',{id:this.panelId});
	this.mail_aj.params='t='+this.shareType+'&i='+this.shareId+'&k='+this.k+'&s='+encodeURIComponent(sender)+'&n='+encodeURIComponent(sname)+'&r='+encodeURIComponent(dest)+'&m='+encodeURIComponent(msg);
	this.mail_aj.call('sendmail.ajax.php?rnd='+Math.random());
},
doSendMailResponse: function(raw){
	var ref=this;
	ref.working=false;
	ref.toggleDisabledForm(false);
	if(raw=='ok'){
		ref.display('emailsent');
	}
	else if(raw.substr(0,6)=='error='){
		var errs=raw.substr(6).split(',');
		for(var i in errs){
			if(errs[i]=='sender'){
				ref.displayError('shremail','Adresse e-mail incorrecte, v&eacute;rifiez votre saisie');
			}
			else if(errs[i]=='recipient'){
				ref.displayError('shrdest','Adresse(s) e-mail incorrecte(s), v&eacute;rifiez votre saisie');
			}
			else{
				ref.displayError('shrglobal','Une erreur s\'est produite, veuillez r&eacute;essayer ult&eacute;rieurement');
			}
		}
	}
	else{
		ref.displayError('shrglobal','Une erreur s\'est produite, veuillez r&eacute;essayer ult&eacute;rieurement');
	}
},
toggleDisabledForm: function(flag){
	var fields=['shrname','shremail','shrdest','shrmsg'];
	for(var i in fields){
		if(o=gel(fields[i]+this.panelId)){
			o.disabled=flag;
		}
	}
},
previewMiniAlbum: function(flag){
	if(flag){
		if(this.preview==null){
			var miniAlbum='<object width="304" height="200"><param name="movie" value="/_share/minialbum.swf?cb='+this.shareId+'"></param><param name="wmode" value="transparent"></param><embed src="/_share/minialbum.swf?cb='+this.shareId+'" type="application/x-shockwave-flash" width="304" height="200" wmode="transparent"></embed></object>';
			this.preview = document.createElement("div");
			this.preview.setAttribute("id","sharepreview");
			var previewObj = this.holder.appendChild(this.preview);
			if(o=gel('sharepreview')){
				var pos = getPos(this.holder);
				o.style.left=(parseInt(pos.x)+322)+'px';
				o.style.top=(parseInt(pos.y)-108)+'px';
				//o.innerHTML='<iframe src="/_share/xalbum.php?id='+this.shareId+'" frameborder="0" border="0" width="282" height="182" scrolling="no" style="border:0;width:282px;height:182px"></iframe>';
				o.innerHTML=miniAlbum;
			}
		}
		else{
			if(o=gel('sharepreview')){
				o.style.display='block';
			}
		}
	}
	else{
		if(o=gel('sharepreview')){
			o.style.display='none';
		}
	}
	return false;
},
publish: function(ptype){
	var u="";
	/*
	switch(ptype){
		case 'myspace':
			u="http://www.myspace.com/Modules/PostTo/Pages/?u="+encodeURIComponent(this.linkUrl)+"&t="+encodeURIComponent(this.linkTitle);
			break;
		case "facebook":
			u="http://www.facebook.com/sharer.php?u="+encodeURIComponent(this.linkUrl)+"&t="+encodeURIComponent(this.linkTitle);
			break;
	}
	if(u!=""){
		window.open(u,"share","scrollbars=1,toolbar=0,status=0,width=820,height=490");
	}*/
	u="/_share/link.php?target="+ptype+"&ref="+this.panelId+"&type="+this.shareType;
	window.open(u);
	return false;
},
openContent: function(){
	var cs='';
	var h='';
	var midClass='';
	if(this.shareType=='videoh'){
		cs='vidshareh';
		midClass='2';
	}
	else{
		if(this.shareType=='album'){cs='albshare';}
		else{cs='vidshare';}
		midClass='';
	}
	h='<div class="'+cs+'"><div class="tlbgm'+midClass+'"><div class="tlbgb'+midClass+'"><div class="tlbgt'+midClass+'"><div class="sharecnt">';
	return h;
},
closeContent: function(){
	var h='';
	h='</div></div></div></div></div>';
	return h;
},
showPanel: function(){
	if(this.holder){
		this.holder.style.display='block';
	}
	return false;
},
closePanel: function(){
	if(this.holder){
		this.holder.style.display='none';
	}
	this.ctype='';
	this.preview = null;
	return false;
},
toggle: function(type){
	if(type==this.ctype) this.closePanel();
	else this.display(type);
	return false;
}
}

var _shrPanelManager = new SharePanelManager();