g_mediaRecorded=false;
Hictu = {};
//Navigation Functions in nav var
Hictu.nav = {
	cookieName : 'hictuApplicationTab',
	selectedMenuId : "",
	lastLayerOn : "textmode",
	changeTab : function(tabId){
		if(tabId=='menuVideo')
			document.getElementById('sendArea').style.display='none';
		else
			document.getElementById('sendArea').style.display='block';
			/*MOD S*/
		document.getElementById('menuVideoLink').className = 'menuOff';	
		document.getElementById('menuAudioLink').className = 'menuOff';	
		document.getElementById('menuTextLink').className = 'menuOff';	
		document.getElementById(tabId+'Link').className = 'menuOn';
		/*if (tabId != this.selectedMenuId){
		
			document.getElementById(tabId+'Link').className = 'menuOn';
			try{
				document.getElementById(this.selectedMenuId+'Link').className = 'menuOff';
			} catch (E){}*/
			this.selectedMenuId = tabId;	
			this.goToSection(this.selectedMenuId);
		/*}*/
	},
	goToSection : function(sectionId){
									switch (sectionId){
							 			case "menuVideo":
							 							this.goToLayer('videowizard');
							 							break;
							 			case "menuAudio":
							 							this.goToLayer('audiomode');
							 							break;
							 			case "menuText":
							 							this.goToLayer('textmode');
							 							break;
							 		}
	},
	goToLayer : function(layerOnId){
								document.getElementById(this.lastLayerOn).style.display = 'none';
						 		document.getElementById(layerOnId).style.display = 'block';
						 		this.lastLayerOn = layerOnId;
						 		if(layerOnId != 'videowizard')
						 			document.getElementById('sendArea').style.display='block';
						 		
	},
	regCookie : function (){
						//alert(Hictu.nav.cookieName+" "+Hictu.nav.lastLayerOn);
						var today = new Date();
 						var expire = new Date();
 						nDays=365;
 						expire.setTime(today.getTime() + 3600000*24*nDays);
 						document.cookie = Hictu.nav.cookieName+"="+escape(Hictu.nav.lastLayerOn)+ ";expires="+expire.toGMTString();
 						
	},
	readCookie : function (){
						var nameEQ = this.cookieName + "=";
						var ca = document.cookie.split(';');
						for(var i=0;i < ca.length;i++) {
							var c = ca[i];
							while (c.charAt(0)==' ') c = c.substring(1,c.length);
							if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
						}
						return null;
	},
	init : function(){
		//controlla cookie
		cookieVal = this.readCookie();
		if (cookieVal == null) cookieVal = "videwizard";
		switch(cookieVal){
			case "videomode":
			case "youtubemode":
			case "googlemode":
				this.changeTab('menuVideo');
				this.goToLayer(cookieVal);					
				break;
			case "videowizard":
				this.changeTab('menuVideo');
				break;
			case "audiomode":
				this.changeTab('menuAudio');
				break;
			case "textmode":
				this.changeTab('menuText');
				break;
		}
		
		var body = document.getElementsByTagName('body')[0];
		YAHOO.util.Event.addListener(body, "unload", this.regCookie);  
	}
};

// :( variabili globali da usarsi per il send su twitter che avviene dopo 
// la ricezione della risposta al primo ajax, per cui l'oggetto hictu.gui non è più istanziato :(
var gTwitterChecked=false;
var gFacebookChecked=false;

var gMessageText='';
// General Function
Hictu.gui = {
	countdownfrom : gConfPostTimeout,
	currentsecond : 4,
	gConfMaxPostChars : gConfMaxPostChars,
	gLoginSendButton : "Send",
	gPostType:0,
	showTwitterAlert : function(text, obj){
		if(obj.checked && text != undefined && text != '')	
			alert(text);
	},
	checkTextArea : function (modename){
									
										this.clearResponseSpans();
										stringa = document.getElementById('textareamessage').value;
										if(stringa.length == 0){
											document.getElementById('sendbutton').disabled = true;
										}
										else{
											document.getElementById('sendbutton').disabled = false;
										}
										sottr = this.gConfMaxPostChars - stringa.length;
										if(sottr <= 0) sottr = 0; 
										document.getElementById('countchars').innerHTML = "["+sottr+"]";
										if (stringa.length > this.gConfMaxPostChars){
								   			document.getElementById('textareamessage').value = stringa.substr(0, this.gConfMaxPostChars);
										}
	},
	countredirect : function (modename){
										document.getElementById('sendbutton').disabled = true;
										if (this.currentsecond!=1){
											this.currentsecond-=1;
											//if (modename == 'youtube')  document.getElementById('youtubeLink').disabled = true;
											document.getElementById('textareamessage').disabled = true;
											document.getElementById('sendbutton').value = this.gLoginSendButton +'('+this.currentsecond+')';
											setTimeout("Hictu.gui.countredirect('"+modename+"')",1000);
										}
										else{
											 /*if (modename == 'youtube'){
											 	document.getElementById('youtubeLink').disabled = false;
											 	document.getElementById('youtubeLink').value = "";
											 }*/
											 document.getElementById('countchars').innerHTML = "["+this.gConfMaxPostChars+"]";
											 document.getElementById('textareamessage').disabled = false;
											 document.getElementById('textareamessage').value = "";
											 document.getElementById('sendbutton').value = this.gLoginSendButton;
											 this.currentsecond = this.countdownfrom;
											 Hictu.gui.sendPost();
										}
	},
	setPostData : function (postType,fileHash){
		this.postType=postType;
		this.fileHash='';
		if(fileHash != undefined)this.fileHash=fileHash;
		gTwitterChecked = document.getElementById('tw_twitter').checked?1:0;
		gFacebookChecked = document.getElementById('tw_facebook').checked?1:0;
		
		this.textPart = document.getElementById('textareamessage').value;
		this.textPart = this.textPart.replace(/\&/g, '%26');
		this.textPart = this.textPart.replace(/\+/g, '%2B');
		gMessageText = this.textPart;
		widget = document.getElementById('nickAppend');
		blog = document.getElementById('blogAppend');
		this.nickAppend='';
		this.blogAppend='';
		if(widget){
			this.nickAppend=widget.value;
			this.blogAppend=blog.value;
		}

	},
	sendPost : function (postType,filehash){
							var mood = document.getElementById('mood_id');
							var mood_id='';
							if(mood){
								mood_id=mood.value;
							}
							this.showFeedbackHictuSpan('<img src="images/ajax-loader-mini.gif" alt="loading..." />');
							
							//this.textPart = escape(this.textPart);
							//this.textPart = encodeURI(this.textPart);
							var pars = "post_from="+this.postType+"&text="+this.textPart+"&fileName="+this.fileHash+"&nickAppend="+this.nickAppend+'&blogAppend='+this.blogAppend+'&mood='+mood_id;
							YAHOO.util.Connect.asyncRequest('POST', g_postUrl, Hictu.gui.sendPostCallback, pars);  
							//posta su twitt
											
	},
    
    sendGenericPost : function(){
//    	alert(Hictu.nav.selectedMenuId+"  "+Hictu.nav.lastLayerOn);
    	switch(Hictu.nav.lastLayerOn)
    	{
    		case 'youtubemode':
    			ret=Hictu.youtube.sendYoutubePost();
    			break;
    		case 'googlemode':
    			ret=Hictu.google.sendGooglePost();
    			break;
    		case 'videomode':
    			ret=Hictu.video.sendVideoPost();
    			break;
    		case 'textmode':
    			ret=Hictu.text.sendTextPost();
    			break;
    		case 'audiomode':
    			ret=Hictu.audio.sendAudioPost();
    			break;

    	}

    	if(ret)
    		Hictu.gui.countredirect();
    		
    },
    
    clearResponseSpans : function()
    {
    	this.showFeedbackHictuSpan('');
    	this.showFeedbackTwitterSpan('');
		this.showFeedbackFacebookSpan('');
    }
    ,
    
    showFeedbackHictuSpan : function(hictu){
		document.getElementById("hictu_span").innerHTML=hictu;	
    },

    showFeedbackTwitterSpan : function(twitter){

		document.getElementById("tw_span").innerHTML=twitter;	
    	
    },
	showFeedbackFacebookSpan : function(facebook){

		document.getElementById("fb_span").innerHTML=facebook;	
    	
    },

    	
	sendPostCallback : {
		success:function(o){
			var jsonList = o.responseText;
			var parsedJSON = jsonList.parseJSON();
			if(parsedJSON.hictu_response)
			{
			//visualilzza però il filtro corrente!!!
				printMessageDiv(1);
	
				Hictu.gui.showFeedbackHictuSpan('');
				
				if(parsedJSON.hictu_response == 'OK'){
					Hictu.gui.showFeedbackHictuSpan('<a href="topic.php?mid='+parsedJSON.hictu_message+'" style="font-size:9px">Topic</a>');
					
					if(gTwitterChecked==1)
					{
						pars = "mid="+parsedJSON.hictu_message+"&twitter=1&text="+gMessageText+"&twitterTo="+g_twitterTo;
						Hictu.gui.showFeedbackTwitterSpan('<img src="images/ajax-loader-mini.gif" alt="loading..." />');
						YAHOO.util.Connect.asyncRequest('POST', g_postTwitterUrl, Hictu.gui.sendPostCallback, pars);
					}
					
					if(gFacebookChecked==1)
					{
						pars = "mid="+parsedJSON.hictu_message+"&facebook=1&text="+gMessageText;
						Hictu.gui.showFeedbackFacebookSpan('<img src="images/ajax-loader-mini.gif" alt="loading..." />');
						YAHOO.util.Connect.asyncRequest('POST', g_postFacebookUrl, Hictu.gui.sendPostCallback, pars);
					}
				
				}
			}
	
			if(parsedJSON.twitter_response){
				Hictu.gui.showFeedbackTwitterSpan('');
				if(parsedJSON.twitter_response.response=='OK')
						Hictu.gui.showFeedbackTwitterSpan('<a target="_blank" href="http://twitter.com/'+tw_userid_link+'" style="font-size:9px">Sent</a>');
			}
			
			if(parsedJSON.facebook_response){
				Hictu.gui.showFeedbackFacebookSpan('');
				if(parsedJSON.facebook_response.response=='OK')
						Hictu.gui.showFeedbackFacebookSpan('<a target="_blank" href="http://facebook.com" style="font-size:9px">Sent</a>');
			}
			
		},
		failure:function(o){
			Hictu.gui.showFeedbackHictuSpan('error');
    	}
	}
};
// End General Function
// YouTube and GVideo Functionalities
Hictu.youtube = {
	sendYoutubePost : function (){
								 			postType = 6; //'youtube';
								 			ylink = document.getElementById('youtubeLink').value;
								 			
											var youtubeHash = false; 
											
											if(this.extractYoutubeLink(ylink)!= false)
												youtubeHash = this.extractYoutubeLink(ylink);
											else 
												if(this.extractGoogleLink(ylink)!= false){
													postType = 7; //'google';
													youtubeHash = this.extractGoogleLink(ylink);
												}
											/*
											else	
												youtubeHash = false;
								 			*/
											
											if (youtubeHash == false){
								 				this.checkYoutubeLink();
								 				return false;
								 			}
											else{
												
												//alert("type: "+postType+" hash: "+youtubeHash+" twitter: "+twitt+" text: "+comment);
												//Hictu.gui.sendPost(postType,youtubeHash);
												Hictu.gui.setPostData(postType,youtubeHash);
												return true;
								 			}
 	},
 	checkYoutubeLink : function (){
										 		ylink = document.getElementById('youtubeLink');
										 		if (this.extractYoutubeLink(ylink.value)=== false && this.extractGoogleLink(ylink.value)=== false) {
										 			error = 'Bad Url';
										 			document.getElementById('youtubeError').innerHTML = error;
										 			ylink.style.border = '1px solid #FF2A2A';
										 			return false;
										 		}
										 		return true;
	},
	/*
	checkGoogleLink : function (){
										 		ylink = document.getElementById('googleLink');
										 		if (this.extractGoogleLink(ylink.value)=== false) {
										 			error = 'Bad Url';
										 			document.getElementById('googleError').innerHTML = error;
										 			ylink.style.border = '1px solid #FF2A2A';
										 			return false;
										 		}
										 		return true;
	},
	*/
	resetYoutubeLink : function (){
									 		document.getElementById('youtubeError').innerHTML = '';
									 		document.getElementById('youtubeLink').style.border = '1px solid #7F9DB9';
 	},
 	extractYoutubeLink : function (ylink){
													if ((ylink.indexOf("youtube") == -1) && (ylink.indexOf("watch?v=") == -1)) return false;
													var offsetA = ylink.indexOf("?");
													var offsetB = ylink.indexOf("&");
													if (offsetB == -1) offsetB = ylink.length;
													yfileid = ylink.substring(offsetA+3,offsetB);
													return yfileid;
	},
	extractGoogleLink : function (ylink){
													if ((ylink.indexOf("google") == -1) && (ylink.indexOf("docid=") == -1)) return false;
													var offsetA = ylink.indexOf("docid=");
													var offsetB = ylink.indexOf("&");
													if (offsetB == -1) offsetB = ylink.length;
													yfileid = ylink.substring(offsetA+6,offsetB);
													return yfileid;
	}
};
//End Youtube Func

/*
// Google Functionalities
Hictu.google = {
	sendGooglePost : function (){
								 			postType = 7; //'google';
								 			ylink = document.getElementById('googleLink').value;
								 			var hash = this.extractGoogleLink(ylink);
								 			if (hash == false){
								 				this.checkGoogleLink();
								 				return false;
								 			}
											else{
												
												//alert("type: "+postType+" hash: "+youtubeHash+" twitter: "+twitt+" text: "+comment);
												//Hictu.gui.sendPost(postType,hash);
												Hictu.gui.setPostData(postType,hash);
												return true;
								 			}
 	},
 	checkGoogleLink : function (){
										 		ylink = document.getElementById('googleLink');
										 		if (this.extractGoogleLink(ylink.value)=== false) {
										 			error = 'Bad Url';
										 			document.getElementById('googleError').innerHTML = error;
										 			ylink.style.border = '1px solid #FF2A2A';
										 			return false;
										 		}
										 		return true;
	},
	resetGoogleLink : function (){
									 		document.getElementById('googleError').innerHTML = '';
									 		document.getElementById('googleLink').style.border = '1px solid #7F9DB9';
 	},
 	extractGoogleLink : function (ylink){
													if ((ylink.indexOf("google") == -1) && (ylink.indexOf("docid=") == -1)) return false;
													var offsetA = ylink.indexOf("docid=");
													var offsetB = ylink.indexOf("&");
													if (offsetB == -1) offsetB = ylink.length;
													yfileid = ylink.substring(offsetA+6,offsetB);
													return yfileid;
	}
};
*/
//End Youtube Func


//Video Functions
Hictu.video = {
	sendVideoPost : function (){
		var flashObject = document.getElementById("MessageRecorder");
		fName = flashObject.getFileName();
		if (fName.length && g_mediaRecorded==true){ 
			fType = flashObject.getFileType();
			postType = 3;
			if (fType == 'audio') postType = 2;
			
			//alert("type: "+postType+" hash: "+fName+" twitter: "+twitt+" text: "+comment);		
			//Hictu.gui.sendPost(postType,fName);
			Hictu.gui.setPostData(postType,fName);
			if (flashObject && flashObject.postSent) flashObject.postSent();
			g_mediaRecorded=false;
			return true;
		}
		else {
			alert('Warning: video not recorded');
			return false;
		}		
	}
};
//End Video Func
//Audio Functions
Hictu.audio = {
	sendAudioPost : function (){
		var flashObject = document.getElementById("MessageAudioRecorder");
		fName = flashObject.getFileName();
		if (fName.length  && g_mediaRecorded==true){ 
			postType = 2;
			fType = 'audio'
			
			//alert("type: "+postType+" hash: "+fName+" twitter: "+twitt+" text: "+comment);		
			//Hictu.gui.sendPost(postType,fName);
			Hictu.gui.setPostData(postType,fName);
			if (flashObject && flashObject.postSent) flashObject.postSent();
			g_mediaRecorded=false;
			return true;
		}		
		else {
			alert('Warning: audio not recorded');
			return false;
		}
	}
};
//End Audio Func
//Text Functions
Hictu.text = {
	sendTextPost : function (){
		postType = 1;
		
		//alert("type: "+postType+" twitter: "+twitt+" text: "+comment);		
		//Hictu.gui.sendPost(postType);
		Hictu.gui.setPostData(postType);
			
		return true;
	}
};
//End Text Func

function setMediaRecorded(value)
{
//	alert('setVideoRecorded: '+value);
	g_mediaRecorded = value;	
}