﻿function close_notif(notif_id) {
   var opt = {
	  method: 'post',
	  postBody: 'notif_id=' + notif_id,
	  onSuccess: function(t) {
		 //$('loading').hide();
		 //$('logued').show();
		 var response = t.responseText.split(':');
		 if (response[0] == "OK"){
			 
		 }
	  },
	  onLoading: function() {
		 //$('logued').hide();
		 //$('loading').show();
	  },
	  on404: function(t) {
		 alert('Error 404: location "' + t.statusText + '" was not found.');
	  },
	  onFailure: function(t) {
		 alert('Error ' + t.status + ' -- ' + t.statusText);
	  }
   }
   new Ajax.Request('/ajax_close_notification.php', opt);
}


function insertThis(ubb){
    comp = navigator.userAgent.toLowerCase();
    moz = ((comp.indexOf("msie") == -1) && (comp.indexOf('mozilla')!=-1) && (comp.indexOf("opera") == -1));

    if(moz)
    {
        message_field = document.getElementById("txtcoment");
        message_field.value += ubb;
    }

    if (document.all.txtcoment.createTextRange)
    {
        document.all.txtcoment.focus();
        document.selection.createRange().duplicate().text = ubb;
    }
    else
    {
        document.all.txtcoment.value += ubb;
    }
}

function bajar_texto(rid)
{
  current=(document.getElementById(rid).style.display == 'none') ? 'block' : 'none';
   
  if (current == 'none')
	  new Effect.Fade(rid, {duration:0.4, from:1.0, to:0.0});
  else
	  new Effect.Appear(rid);
}

var wdwLoad = function() {
}



function photoVote(rate) {
	
   var opt = {
      method: 'post',
	  postBody: 'uid=' + $F('uid') + '&eid=' + $F('eid') + '&rate=' + rate,
	  onSuccess: function(t) {
 	     //$('loading').hide();
         //$('logued').show();
	     var response = t.responseText.split(':');
         //alert(response[1]);
		 window.location.reload();
	  },
	  onLoading: function() {
	     //$('logued').hide();
		 //$('loading').show();
	  },
	  on404: function(t) {
	  	 alert('Error 404: location "' + t.statusText + '" was not found.');
	  },
	  onFailure: function(t) {
	     alert('Error ' + t.status + ' -- ' + t.statusText);
	  }
   }
   new Ajax.Request('ajax_photo_vote.php', opt);
}

function comment() {
   if ($F('c_comment').length > 0)
   {
	   if ( $F("sid") != "" )
	   {		
		   var opt = {
			  method: 'post',
			  postBody: 'uid=' + $F('sid') + '&eid=' + $('eid').value + '&comment=' + encodeURI( $F('c_comment')),
			  onSuccess: function(t) {
				 //$('loading').hide();
				 //$('logued').show();
				 var response = t.responseText.split(':');
				 if (response[0] == "OK"){
					addComment(response[1],$('sid').value,$('slg').value,"",$('c_comment').value,$('sav').value);
					 document.getElementById('c_comment').value = "";
		
					alert("Tu comentario ha sido agregado");				
				 }
				 else if(response[0] == "ER")
					alert(response[1]);
			  },
			  onLoading: function() {
				 //$('logued').hide();
				 //$('loading').show();
			  },
			  on404: function(t) {
				 alert('Error 404: location "' + t.statusText + '" was not found.');
			  },
			  onFailure: function(t) {
				 alert('Error ' + t.status + ' -- ' + t.statusText);
			  }
		   }
		   new Ajax.Request('/ajax_photo_comment.php', opt);
	   }
	   else
	   	 alert('Debes introducir tu nombre antes de enviar el comentario.');
   }
   else
   	 alert('No se pueden enviar comentarios vacios.');
}

function inapropiate(id) {
   var name = '';
   if ( $('reason') ) reason = $F('reason');
   if ( $('explanation') ) explanation= $F('explanation');   
   
   if (explanation != "")
   {   
	   var opt = {
		  method: 'post',
		  postBody: 'pid=' + id + '&reason=' + reason + '&explanation=' + explanation,
		  onSuccess: function(t) {
			 //$('loading').hide();
			 //$('logued').show();
			 var response = t.responseText.split(':');
			 alert(response[1]);
		  },
		  onLoading: function() {
			 //$('logued').hide();
			 //$('loading').show();
		  },
		  on404: function(t) {
			 alert('Error 404: location "' + t.statusText + '" was not found.');
		  },
		  onFailure: function(t) {
			 alert('Error ' + t.status + ' -- ' + t.statusText);
		  }
	   }
	   new Ajax.Request('ajax_photo_inapropiate.php', opt);
	   overlay();
   }
   else
   {
		alert("Debes ingresar la razón por la que crees que esta foto es inapropiada.");   
   }
}

function getYouTubeVideos(str)
{
	var res = Array();
	var youTubeRegExp;
 	youTubeRegExp  = "<object width=\"([0-9]*)\" height=\"([0-9]*)\">";												
	//																			3			4			5					6				7             8  9                                                                    
	youTubeRegExp += "<param name=\"movie\" value=\"http://www.youtube.com/v/([^&]*)&hl=([^&\"]*)&fs=([^&\"]*)&color1=([^&\"]*)&color2=([^&\"]*)(&border=(1|0)){0,1}\"></param><param name=\"allowFullScreen\" value=\"true\"></param>";
	//															10				11			12				13				14			15	  16																 					17					 18				
	youTubeRegExp += "<embed src=\"http://www.youtube.com/v/([^&\"]*)&hl=([^&\"]*)&fs=([^&\"]*)?&color1=([^&\"]*)&color2=([^&\"]*)(&border=(1|0)){0,1}\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"([0-9]*)\" height=\"([0-9]*)\"></embed>";	
	youTubeRegExp += "</object>";
	var reg = RegExp(youTubeRegExp);
	var match_video;
	var i = 0;
	while( reg_result = reg.exec(str) )
	{
		res[i] = reg_result[0];
		str = str.replace(reg_result[0]);
		i++;
	}
	return res;
}

function getYouTubeVideos2(str)
{
	var videoStr;
	var res = Array();
	var youTubeRegExp;
 	youTubeRegExp  = "\\[yt\\](.*)\\[/yt\\]";
	var reg = RegExp(youTubeRegExp);
	var match_video;
	var i = 0;
	while( reg_result = reg.exec(str) )
	{
		//res[i] = reg_result[0];
		videoStr  = "<object width=\"480\" height=\"385\"\">";
		videoStr += "<param name=\"movie\" value=\"http://www.youtube.com/v/" + reg_result[1] + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param>";
		videoStr += "<embed src=\"http://www.youtube.com/v/" + reg_result[1] + "\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"480\" height=\"385\"></embed>";	
		videoStr += "</object>";
	
		var tmp = Array();
		tmp["code"] = videoStr;
		tmp["original"] = reg_result[0];	
		res[i] = tmp;
		str = str.replace(reg_result[0]);
		i++;	
	}
	return res;
}


function getImgs(str)
{
	var res = Array();
	var imgRegExp;
 	imgRegExp  = "\\[img\\](.*)\\[/img\\]";

	var reg = RegExp(imgRegExp);
	var match_video;
	var i = 0;
	while( reg_result = reg.exec(str) )
	{
		var tmp = Array();
		tmp["code"] = "<img src='" + reg_result[1] + "'>";
		tmp["original"] = reg_result[0];
		res[i] = tmp;
		str = str.replace(reg_result[0]);
		i++;
	}
	return res;
}

function parseEmoticons(comment){      	   
	comment = comment.replace(':D', "<img src=\"images/smilies/icon_biggrin.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':S', "<img src=\"images/smilies/icon_confused.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':H:', "<img src=\"images/smilies/icon_cool.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':-(', "<img src=\"images/smilies/icon_cry.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':O', "<img src=\"images/smilies/icon_eek.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':evil:', "<img src=\"images/smilies/icon_evil.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':-D', "<img src=\"images/smilies/icon_lol.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace('8-(', "<img src=\"images/smilies/icon_mad.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(';D', "<img src=\"images/smilies/icon_mrgreen.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':|', "<img src=\"images/smilies/icon_neutral.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':-)', "<img src=\"images/smilies/icon_razz.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':$', "<img src=\"images/smilies/icon_redface.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace('8-)', "<img src=\"images/smilies/icon_rolleyes.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':(', "<img src=\"images/smilies/icon_sad.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':smile:', "<img src=\"images/smilies/icon_smile.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace('8-D', "<img src=\"images/smilies/icon_surprised.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':evil2:', "<img src=\"images/smilies/icon_twisted.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(';)', "<img src=\"images/smilies/icon_wink.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':-&gt;:', "<img src=\"images/smilies/icon_arrow.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':!:', "<img src=\"images/smilies/icon_exclaim.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':idea:', "<img src=\"images/smilies/icon_idea.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':?:', "<img src=\"images/smilies/icon_question.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':drool:', "<img src=\"images/smilies/drool.gif\" border=\"0\" height=\"16\" width=\"15\" />");
	comment = comment.replace(':porfa:', "<img src=\"images/smilies/pray.gif\" border=\"0\" height=\"16\" width=\"19\" />");
	comment = comment.replace(':ohjeje:', "<img src=\"images/smilies/shifty.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':nonono:', "<img src=\"images/smilies/snooty.gif\" border=\"0\" height=\"15\" width=\"16\" />");
	comment = comment.replace(':angel:', "<img src=\"images/smilies/angel.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':sisisi:', "<img src=\"images/smilies/dance.gif\" border=\"0\" height=\"16\" width=\"27\" />");
	comment = comment.replace(':ouch:', "<img src=\"images/smilies/doh.gif\" border=\"0\" height=\"16\" width=\"22\" />");
	comment = comment.replace(':eh?:', "<img src=\"images/smilies/eh.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':stop:', "<img src=\"images/smilies/hand.gif\" border=\"0\" height=\"16\" width=\"17\" />");
	comment = comment.replace(':pinocho:', "<img src=\"images/smilies/liar.gif\" border=\"0\" height=\"15\" width=\"20\" />");
	comment = comment.replace(':shhh:', "<img src=\"images/smilies/shhh.gif\" border=\"0\" height=\"16\" width=\"15\" />");
	comment = comment.replace(':sick:', "<img src=\"images/smilies/sick.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':silencio:', "<img src=\"images/smilies/silenced.gif\" border=\"0\" height=\"15\" width=\"15\" />");
	comment = comment.replace(':think:', "<img src=\"images/smilies/think.gif\" border=\"0\" height=\"16\" width=\"17\" />");
	comment = comment.replace(':wall:', "<img src=\"images/smilies/wall.gif\" border=\"0\" height=\"15\" width=\"25\" />");
	comment = comment.replace(':aplauso:', "<img src=\"images/smilies/clap.gif\" border=\"0\" height=\"16\" width=\"19\" />");
	comment = comment.replace(':nanana:', "<img src=\"images/smilies/naughty.gif\" border=\"0\" height=\"16\" width=\"20\" />");
	comment = comment.replace(':lalala:', "<img src=\"images/smilies/whistle.gif\" border=\"0\" height=\"16\" width=\"22\" />");
	comment = comment.replace(':boohoo:', "<img src=\"images/smilies/boohoo.gif\" border=\"0\" height=\"16\" width=\"39\" />");
    return comment;	
}

function nltobr(string)
{
	
	var regExpNL = new RegExp("(\r\n)|\r|\n", "g");
	return string.replace(regExpNL, "<br />");
}

function addComment(id,sid,login,name,comment,avatar) {
   name   = login.length == 0 ? name : login;
   avatar = avatar.length == 0 ? '/images/nofoto.gif' : avatar;

   //comment = parseEmoticons(comment)	

	var html_comment =  "<div class=\"comment\" id=\"comment_"+id+"\">\n";
		html_comment += "		<div class=\"left\">\n";
		html_comment += "			<img src=\""+avatar+"\" align=\"absmiddle\" align=\"absmiddle\" class=\"avatar\" width=\"48\" height=\"48\"/> \n";
		html_comment += "				</div>\n";
		html_comment += "				<div class=\"right\">\n";
		html_comment += "					<div class=\"author\">\n";
		html_comment += "							 <a href=\"/"+login+"\">"+name+"</a>\n";
		html_comment += "					</div>\n";

	   
	   //var videos = getYouTubeVideos(comment);
	   var videos = getYouTubeVideos2(comment);
   	   var imgs = getImgs(comment);
	   comment = comment.escapeHTML();
	   
	   for (var i = 0; i < videos.length ; i++)
	   {		   	   
		   //comment = comment.replace(videos[i].escapeHTML(), videos[i]);
		   comment = comment.replace(videos[i]["original"].escapeHTML(), videos[i]["code"]);		   
	   }   
	   for (var i = 0; i < imgs.length ; i++)
	   {		   	   
		   comment = comment.replace(imgs[i]["original"].escapeHTML(), imgs[i]["code"]);
	   }
	   
	   html_comment += "					<div class=\"text\">\n";	   
	   
       html_comment+= parseEmoticons(nltobr(comment))+"\n";

	   html_comment += "					</div>\n";
	   html_comment += "					<div class=\"date\">\n";
	   html_comment += "						Hace 1 segundo\n";
	   html_comment += "					</div>\n";
	   html_comment += "					<div class=\"options\">\n";
	   html_comment += "						<a href=\"\" onclick=\"if(confirm('Esta seguro de que desea eliminar este comentario?')) delPhotoComment('"+ id  +"'); return false;\" >Borrar</a>\n";
	   html_comment += "					</div>\n";
	   html_comment += "				</div>\n";
	   html_comment += "			</div>\n";
			
   new Insertion.Bottom('comments', html_comment);
}

function aboutUser(user_login)
{
   Popup.open({url:'/sobre/' + user_login});
}

function friendMail(photo_id)
{
   Popup.open({url:'/enviar-amigo/' + photo_id, width:335, height:185});
}

function command(op)
{
	document.getElementById('op').value  = op;
	$("form_command").submit();
	return false;
}

function showLogin()
{
	//var posicion = findPos(obj);
	$('login_cont').show();	
}

function hideLogin()
{
	$('login_cont').hide();	
}

Event.observe(window,"load",wdwLoad);


