$().click(function (event) {
	if ($("#loginFormDiv").css('display') != "none") {
		if (event.target.id != 'connexionButton') {
			var inLoginForm=false;
			var node=event.target;
			while(node != null && node != 'undefined') {
				if (node.id == 'loginFormDiv')
					inLoginForm=true;
				node = node.parentNode;
			}
			if(!inLoginForm) {
				switchTopConnexionDiv();
			}
		}
	}
});

function empty(input, text) {
	if ($(input).val() == text) {
		$(input).val('');
	}
}

var afterVotingText;
function videoVote(videoId) {
	jQuery.get(ROOT_DIR+'vote.php?video='+videoId, function(content) {
		answer=content.substring(0, content.indexOf(':'));
		infos=content.substring(content.indexOf(':')+1);
		if (answer=='OK') {
			if (!afterVotingText) {
				afterVotingText='Thank you for voting!';
			}
			$('.voteLinkSpan').html(afterVotingText);
			$('.voteNumVotesSpan').html(infos);
		} else {
			alert('Error voting : '+infos);
		}
	});
}

function watch(user, status) {
	jQuery.get(ROOT_DIR+'keepEye.php?user='+user+'&status='+status, function(content) {
		answer=content.substring(0, content.indexOf(':'));
		infos=content.substring(content.indexOf(':')+1);
		if (answer=='OK') {
			var watching=new Array();
			infos=infos.split(';');
			for (i=0; i<infos.length; i++) {
				infos[i]=infos[i].split('=');
				watching[infos[i][0]]=infos[i][1].split(',');
			}
			$('.watchLinkSpan').html('<center>Done.</center>');
			$('.unwatchLinkSpan').html('<center>Done.</center>');
			$('.watchNumWatchedMeSpan').html(watching['me'][0]);
			$('.watchNumSpectatorsMeSpan').html(watching['me'][1]);
			$('.watchNumWatchedSpan').html(watching['user'][0]);
			$('.watchNumSpectatorsSpan').html(watching['user'][1]);
		} else {
			alert('Error watching : '+infos);
		}
	});
}

function shareByMail(video, email) {
	jQuery.get(ROOT_DIR+'shareByMail.php?video='+video+'&email='+email, function(content) {
		answer=content.substring(0, content.indexOf(':'));
		infos=content.substring(content.indexOf(':')+1);
		if (answer=='OK') {
			$('#sendEmail').val('');
			_gaq.push(['_trackPageview', 'sharedByMail']);
			alert('Your email has been sent. Thank you!');
		} else {
			alert('An error occured when sending your email :'+"\n"+infos);
		}
	})
}

function deleteVideo(videoId) {
	if (confirm('Are you sure your want to delete this video?')==true) {
		jQuery.get(ROOT_DIR+'deleteVideo.php?video='+videoId, function(content) {
			answer=content.substring(0, content.indexOf(':'));
			infos=content.substring(content.indexOf(':')+1);
			if (answer=='OK') {
				window.location.href=ROOT_DIR;
			} else {
				alert('Error deleting : '+infos);
			}
		});
	}
}

function fblogin() {
		if (typeof(FB) != "undefined") {
		FB.login(function(response) {
			if (response.session) {
				window.location.reload();
			} else {
			}
		}, {perms:"email,user_birthday"});
	}
}

function switchTopConnexionDiv() {
	if ($("#loginFormDiv").css('display') == "none") {
		$("#loginFormDiv").show();
		$("#connexion p a").addClass('selected');
		$("#emailInput").focus();
	} else {
		$("#loginFormDiv").hide();
		$("#connexion p a").removeClass('selected');
	}
}

/*************************************************************************
	Cookies management functions
*************************************************************************/
function writeCookie(nom, valeur) {
	var argv=writeCookie.arguments;
	var argc=writeCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}
function readCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) {
			var endstr=document.cookie.indexOf (";", j);
			if (endstr==-1) 
				endstr=document.cookie.length;
			return unescape(document.cookie.substring(j, endstr));
		}
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) 
			break;
   }
   return null;
}
function deleteCookie(nom) {
   date=new Date;
   date.setFullYear(date.getFullYear()-1);
   writeCookie(nom,null,date);
}

function dologforayear() {
   var f1=new getObj("password").obj.value;
   var f2=new getObj("login").obj.value;
   date=new Date;
   date.setFullYear(date.getFullYear()+1);
   writeCookie("kizoapwd", f1, date);
   writeCookie("kizoalog", f2, date);
}


