
// Function to turn red stars while mouse overs

function OverStars(VideoId, HowManyStarsOvered, Over, HowManyStarsFilled, RandomId){
	//HowManyStarsOvered++;
	HowManyStarsFilled--;
	if (Over){
		for(i = 0; i <= HowManyStarsOvered; i++){
			MM_swapImage('Video' + VideoId + '-Star' + i + '-'+ RandomId, '','/images/elmnt/star_full_red.gif',1);
		}
	} else {
		for(i = 0; i <= HowManyStarsOvered; i++){
			if(i <= HowManyStarsFilled){
				MM_swapImage('Video' + VideoId + '-Star' + i + '-'+ RandomId, '','/images/elmnt/star_full_grey.gif',1);
			} else {
				MM_swapImage('Video' + VideoId + '-Star' + i + '-'+ RandomId, '','/images/elmnt/star_empty_grey.gif',1);
			}
		}
	}
}




// Ban user, called from /admin/users

function banUser(UserId){ // Use prototype
	var elemId = 'BtnBanUser-'+UserId;
	var Blocked = ($(elemId).src.indexOf('unlock') == -1 ? 0: 1);
	new Ajax.Request('/users/banUser/userId:' + UserId + '/canComment:' + Blocked + '/',{onSuccess: function(response){eval(response.responseText)}});
}

// Inserts or replaces vote
function VoteVideo(VideoId, Rating, RandomId){ /* jquery updated */
	$.ajax({
		  url: '/videos/vote/'+VideoId+'/'+Rating+'/'+RandomId+'/'
		, dataType : 'html'
		, success : function(data, textStatus, XMLHttpRequest){
			//$(this).html(data);
		}
	});
}

// Reports comment abuse
function ReportCommentAbuse(CommentId){ /* jquery updated */
	$.ajax({
		  url: '/comments/reportAbuse/'+CommentId+'/'
		, dataType : 'html'
		, success : function(data, textStatus, XMLHttpRequest){
			//$(this).html(data);
		}
	});
}


function htmlEntities(texto){
    //by Micox - elmicoxcodes.blogspot.com - www.ievolutionweb.com
    var i,carac,letra,novo='';
    for(i=0;i<texto.length;i++){
        carac = texto[i].charCodeAt(0);
        if( (carac > 47 && carac < 58) || (carac > 62 && carac < 127) ){
            //se for numero ou letra normal
            novo += texto[i];
        }else{
            novo += "&#" + carac + ";";
        }
    }
    return novo;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else
		cntfield.value = maxlimit - field.value.length;
}


function closeQuickBanner(){ /* jquery updated */
	$('#DivQuickBanner').html('');
	$('#DivQuickBanner').hide();
	$('#Container').show();
}

 

function ajaxLink(Url, divToUpdate){ /* jquery updated */
	$.ajax({
		  url: Url
		, context :  $('#' +  divToUpdate )
		, dataType : 'html'
		, beforeSend: function(xhr) {
			xhr.setRequestHeader("X-Update",divToUpdate);
		}
		, success : function(data, textStatus, XMLHttpRequest){
			$(this).html(data);
		}
	});
}


 function fBlogout(logOutUrl){
	 if(logOutUrl.length == 0){ logOutUrl = '/';}
	 try {
		FB.Connect.logoutAndRedirect(logOutUrl+'/users/logout/');
		return false;
	 } catch(e){
		return true;
	 }
 }

 function logOut(logOutUrl){
    try{
        FB.Connect.ifUserConnected(function(){
            FB.Connect.logoutAndRedirect(logOutUrl+'/users/logout/');
        }, logOutUrl+'/users/logout/');
    }catch(e){
        location.href = logOutUrl+'/users/logout';
    }
	return false;
}
function autocompleteForm(connected){
  if(connected == 'notConnected')
      return false;
    FB.api('/me', function(response) {
      var fb_uid = response.id;
      var profile_url = 'http://www.facebook.com/profile.php?id=' + response.id;
      var first_name = response.first_name;
      var last_name = response.last_name;
      var gender = response.gender;
      var email = response.email;
      var birthday = response.birthday.split("/");
      var month = birthday[0];
      var day = birthday[1];
      var year = birthday[2];
      var profile_image = 'http://graph.facebook.com/' + response.id + '/picture';
       $('#UserRegisterForm input, #UserRegisterForm select').each(function (){
      switch(this.name){
	  default:
	  break;
	  case 'data[User][password]':
	  case 'data[User][password_confirmation]':
	  break;

      case 'data[User][first_name]': $(this).val(first_name); break;
      case 'data[User][last_name]': $(this).val(last_name); break;
      case 'data[User][username]':$(this).val(first_name.toLower() + '.' + last_name.toLower()); break;
      case 'data[User][email]': $(this).val(email); break;
      case 'data[ExternalUser][external_id]': $(this).val(fb_uid); break;
      case 'data[User][birthdate][year]':$(this).val(year);break;
      case 'data[User][birthdate][month]':$(this).val(month);break;
      case 'data[User][birthdate][day]': $(this).val(day);break;

      case 'data[User][siteNewsletter]':
      case 'data[User][offNewsletter]':
      case 'data[User][partnerNewsletter]':
      break;
      case 'data[ExternalUser][external_id]': $(this).val(fb_uid); break;
      case 'data[User][title]':
	    if(gender == 'male')
		$(this).val("Mr.");
	    else $(this).val("Mme.");
          break;
	}
      });
  },{locale: 'en_US'});
  return true;
}

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

