﻿var PhotoTimeDes;
var PhotoTimeAp;

var oculto = false;
var Newruta;
var photo_id;
var photoType;
var photoControlsWait;
var PhotoUser;
var isUserPhotos;
var isPhotoOwner;
//----->

//
redirectPage = "true";

window.onload = function()
{
   searchKey();
      
    waterUser();
    primero = true;      
    
    setSendButton();
    
    $("txtMailComment").onkeypress = function(){ $("btnSendProfile").disabled = false;
            $("btnSendProfile").className = "input_botons";};
            
     checkQuickUploadSession("go");
}

function setSendButton()
{
    var EsperandoAlWindow;
    if($("txtPhotoReview") != null)
    {
        $("txtPhotoReview").onkeypress = function(){$("btnSendPhotoReview").className = "input_botons"; $("btnSendPhotoReview").disabled = false;};
        if(EsperandoAlWindow != null)
        {
            clearTimeout(EsperandoAlWindow);
        }
    }
    else
    {
        EsperandoAlWindow = setTimeout(setSendButton, 500);
    }
}

function setBigPhoto(ruta, photo, type)
{
    checkQuickUploadSession();
    
    Newruta = ruta;
    photo_id = photo;
    photoType = type;
    //---->
    
    Desaparecer();
    
    return false;
}

function Desaparecer()
{
    Aparecer();
}

function Aparecer()
{
    $("firstFoto").src = Newruta;
    setPhotoID();
}
//----->
function getPhotoDescription()
{
    __doPostBack('LinkGetDescription','');
}

function setPhotoID(){
    $Ajax(basepath + "controllers/Photos/Gallery.aspx?m=setPhotoID", {
            cache: false,
            metodo: $metodo.POST,            
            parametros: {"id":photo_id,"type":photoType},
            onfinish:function(p){if(p==1){getPhotoDescription();}else{alert("error")}}
            ,tipoRespuesta:$tipo.JSON});
}

//----->

//Creo los Tips
function showTipAbuse(id)
{
    Tip('Let us know if you believe <br />this content is not appropiate', STICKY, false,FADEIN, 500,CLICKCLOSE, false, SHADOW, true, TITLE, 'Report Abuse', PADDING, 1);
}

function showTipPM(id)
{
   Tip('You´ll be able to send messages', STICKY, false,FADEIN, 500,CLICKCLOSE, false, SHADOW, true, TITLE, 'Coming Soon', PADDING, 1); 
}

function loginTo()
{  
    Effect.Fade('divControlsContainer', { duration: 0.5 });
    showPhotoUpload();    
}

function goSendPhotoReview()
{
    //SendMessage.js
    SendPhotoReview();
}

var PhotoReviewResponseWait;
//----->
function PhotoReviewResponse()
{
    Effect.BlindUp('divPhotoReviewResponse', { duration: 0.5 });
    clearTimeout(PhotoReviewResponseWait);
    $("btnSendPhotoReview").className = "input_botons_off";
    $("btnSendPhotoReview").disabled = true;
}

function showPhotoUpload()
{
    //----->
    if($('divControlsContainer').style.display == "none")
    {
        //----->
        __doPostBack('linkLogin','');        
        Effect.Appear('divControlsContainer', { duration: 2.0 });
        
        $("divRemitenteName").style.display = "none";
        $("divRemitenteMail").style.display = "none";  
        
        $("divPhotoUpload").style.display = "";
    }
    else
    {
        photoControlsWait = setTimeout(showPhotoUpload, 1000);
    }
}

//-----> Detecta el Tipo de Fotos

function searchKey()
{
    PhotoUser = window.location + '';
    PhotoUser = PhotoUser.split('/');
    PhotoUser = PhotoUser[PhotoUser.length - 1];
    PhotoUser = PhotoUser.split('.');
    PhotoUser = PhotoUser[0];
    PhotoUser = PhotoUser.split('-');
    //----->
    if(PhotoUser[1] == "user")
    {
        isUserPhotos = true;
    }
    PhotoUser = PhotoUser[0];
}

//-----------------------Photos Uploader-------------------------------------//

function setPhotoUploader() {
    
	
}   
     
//function SaveAllPhotos()
//{  
//    //---------------------------------------------->>
//    var searchTitleRegExp = new RegExp("([0-9]+__title)", "i");
//    
//    var PhotoTitleHelpArr = new Array();
//    PhotoTitleHelpArr = $$('input');
//    var PhotoTitleString = "";
//    var tester = false;
//    
//        PhotoTitleHelpArr.each(function(thisID) { 
//	    
//                if(thisID.id != null)
//                {
//                    if(searchTitleRegExp.test(thisID.id))
//                    {                        
//                        thisID.value = deleteInvalidTags(thisID.value);
//                        if(CheckPalabras(thisID.value) == false)
//                        {
//                            return false;
//                        }
//                        else if(thisID.value == MarcaDelTitulo)
//                        {
//                            thisID.value = "";
//                        }
//                        PhotoTitleString += escape(thisID.value);
//                        PhotoTitleString += "#";                  
//                    }
//                }
//	    
//            });
//	    
//        PhotoTitleString = PhotoTitleString.substring(0, PhotoTitleString.length - 1);	
//    
//    //---------------------------------------------->>
//     var searchDescRegExp = new RegExp("([0-9]+__desc)", "i");
//     var PhotoDescHelpArr = new Array();
//     PhotoDescHelpArr = $$('textarea');
//     var PhotoDescString = "";
//    
//        PhotoDescHelpArr.each(function(thisID) { 
//	    
//                if(thisID.id != null)
//                {
//                    if(searchDescRegExp.test(thisID.id))
//                    {
//                        thisID.value = deleteInvalidTags(thisID.value);
//                        if(CheckPalabras(thisID.value) == false)
//                        {
//                            return false;
//                        }
//                        PhotoDescString += escape(thisID.value);
//                        PhotoDescString += "#";		                        
//                    }
//                }
//	    
//            });
//    
//        PhotoDescString = PhotoDescString.substring(0, PhotoDescString.length - 1);	    
//    		    $Ajax(basepath+"controllers/PhotoUpload/PhotoUploadController.aspx?m=saveAll", {
//    cache: false, 
//    metodo: $metodo.POST,
//    parametros: {"photoTitleList":PhotoTitleString,"photoDescList":PhotoDescString},
//    onfinish: function(html) {
//             if(html == "1")
//             {             
//                total_files = 0;
//                open_div(1);
//             }       
//        }
//    });
//    return false;
//}     

//function open_div(num)
//{
//    switch(num)
//    {
//        case 1:
//        Effect.BlindUp('thumbnails', { duration: 1.0 });
//        //----->>Limpio en contenedor de los thumbnails
//        clearThumbnails();
//        //----->>
//        setResponseError('Photo uploaded. It will be evaluated and subsequently published by the moderator.');         
//        $('divFileProgressContainer').innerHTML = '';
//        setPhotoUploader();
//        $("imgSignIn").style.display = "none";      
//        $("btnBrowse").disabled = false;     
//        $("btnBrowse").className = "input_botons";
//        break;               
//    }
//}      

function goSelectPhotos(id)
{
    if(!checkQuickUploadSession())
    {
        return false;
    }
    else
    {
	    $('divUpdateResponse').style.display = "none";
    }
}

//function clearThumbnails()
//{
//    var timeThumb;
//    if($('thumbnails').style.display == "none")
//    {
//        $('thumbnails').innerHTML = ''; 
//        
//        if(timeThumb != null)clearTimeout(timeThumb);
//    }
//    else
//    {
//        timeThumb = setTimeout(clearThumbnails, 500);
//    }
//}

function setResponseError(text)
{
    if(text == null || text == "")
    {
        Effect.Appear('divUpdateResponse', { duration: 1.5 });
    }
    else
    {
        $('divUpdateResponse').innerHTML = text;
        Effect.Appear('divUpdateResponse', { duration: 1.5 });
    }    
}

function checkQuickUploadSession(go_true)
{
     //----->
    $Ajax(basepath+"Controllers/Login/login.aspx?method=checkSession", {
        cache: false,
        onfinish:function(session){
            if(session=="False"){         
                $("divPhotoUpload").style.display = "none";
                if( $("divPhotoReviewsSend") != null)
                $("divPhotoReviewsSend").style.display = "none";
                
                $("divRemitenteName").style.display = "block";
                $("divRemitenteMail").style.display = "block";
   
                return false;
            }
            else
            {
                $("divRemitenteName").style.display = "none";
                $("divRemitenteMail").style.display = "none";   
                $("divPhotoUpload").style.display = "block";                          
            }
            
        },
        tipoRespuesta:$tipo.TEXT});
    
    return true;        
}

//Envia foto a mis amigos
var waitingProfile;

function sendMyPhoto()
{
    var tester_rem = true;
    var tester_rem2 = true;
    //Primero chequeo los datos del remitente
    if($("divRemitenteName").style.display != "none")
    {
        if($F("txtRemitenteName") == "" || $F("txtRemitenteName") == " " || $F("txtRemitenteName").length < 3)
        {
            $("remitenteNameError").innerHTML = "*";
            tester_rem2 &= false;
        }
        else
        {
            $("remitenteNameError").innerHTML = "";
            tester_rem2 &= true;
        }
        
        if(!tester_rem2)
        {
            return false;
        }    
        
        if($F("txtRemitenteMail") == "" || $F("txtRemitenteMail") == " ")
        {
            $("remitenteMailError").innerHTML = "*";
            tester_rem &= false;
        }    
        else if(!checkEmail($F("txtRemitenteMail")))
        {
            $("remitenteMailError").innerHTML = "*";
            tester_rem &= false;
        }
        else
        {
            $("remitenteMailError").innerHTML = "";
            tester_rem &= true;       
        }
        
        if(!tester_rem)
        {
            return false;
        }
    }
    
    var myMessage;
    var myMailsList="";
    var mailsArr = new Array(); 
    var tester = true;
    //------------------------>>    
    for(i=1;i<=5;i++)
    {
        if($("txtFriendMail_0" + i).value != "" && $("txtFriendMail_0" + i).value != " ")
        {
            mailsArr.push("txtFriendMail_0" + i);                   
        }
    }
    
    if(mailsArr.length < 1)
    {
        goResponseSendProfile("Please enter recipient’s email");
        return false;
    }
    
    //------------------------>>    
    for(i=0;i<mailsArr.length;i++)
    {
        if(!checkEmail($(mailsArr[i]).value))
        {        
            $(mailsArr[i] + '_err').innerHTML = '*';
            tester &= false;
        }
        else
        {
            myMailsList += $(mailsArr[i]).value + "#";
            $(mailsArr[i] + '_err').innerHTML = '';            
            tester &= true;
        }
    }    
    myMailsList += '#';
    myMailsList = myMailsList.replace('##','');
    
    if(!tester)
    {
        return false;
    }
    //------------------------>>   
    
    if(CheckPalabras($F("txtMailComment")))
    {
        myMessage = escape(deleteInvalidTags($F("txtMailComment")));
    }
    else
    {
        return false;
    }
    
    $("btnSendProfile").disabled = true;
    $("btnSendProfile").className = "input_botons_off";
    
    clearMailsErrors();
    
    $Ajax(basepath + "controllers/Photos/Gallery.aspx?m=sendMyPhoto", {
        cache: false, 
        avisoCargando:"divCargando",
        metodo: $metodo.POST,            
        parametros: {"comment":myMessage,"myMailsList":myMailsList,"from_name":escape($F("txtRemitenteName")),"from_mail":escape($F("txtRemitenteMail"))},
        onfinish:function(respuesta){
            if(respuesta == "1"){goResponseSendProfile("Your message has been sent");
            hide('divCargando');
            clearMailsComment(mailsArr);
            }else{goResponseSendProfile("Please try again later");
            hide('divCargando');
            $("btnSendProfile").disabled = false;
            $("btnSendProfile").className = "input_botons";
            }        
        },
        tipoRespuesta:$tipo.TEXT});
    
}

function goResponseSendProfile(texto)
{
    $("responseSendPhoto").innerHTML = texto;
    Effect.Appear('responseSendPhoto', { duration: 0.8 });
    waitingProfile = setTimeout(goResponseClose, 3000);
}

function goResponseClose()
{    
    Effect.Fade('responseSendPhoto', { duration: 0.8 });
    if(waitingProfile != null)clearTimeout(waitingProfile);
}

function clearMailsComment(arr)
{
    var currArr = new Array();
    currArr = arr;
    for(i=0;i<currArr.length;i++)
    {
        $(currArr[i]).value = "";
    }
    
    $("txtRemitenteName").value = "";
    $("txtRemitenteMail").value = "";
    $("txtMailComment").value = "";
    $("charsLeft_txtMailComment").value = "";
    
}

function clearMailsErrors()
{
    for(i=1;i<=5;i++)
    {
       $("txtFriendMail_0" + i + "_err").innerHTML = "";                   
    }
}

//function addImage(src) {
//    
//    
//    var myArr = src.split("=");
//    var IDsrc = myArr[myArr.length - 1];
//    
//    //alert(IDsrc);

//    var newAllContainer = document.createElement("div");
//	newAllContainer.id = "allContainer";
//    $("thumbnails").appendChild(newAllContainer);
//    
//    var newImgContainer = document.createElement("div");
//    //newImgContainer.className = "myNewPhoto";
//    newAllContainer.appendChild(newImgContainer);
//    
//    var newContainer = document.createElement("div");
//    newContainer.setAttribute("style","width:250px;");
//    newAllContainer.appendChild(newContainer);
//    
//   	var newImg = document.createElement("img");
//	newImg.style.margin = "5px";
//	newImg.id = IDsrc;
//	
//	var newImgTitle = document.createElement("input");
//	newImgTitle.type = "text";
//	newImgTitle.style.width = "200px";
//	newImgTitle.id = IDsrc + "__title";
//	newImgTitle.className = "input_forms";
//	newImgTitle.setAttribute("maxlength","100");		
//	
//	var newImgDesc = document.createElement("textarea");
//	//newImgDesc.setAttribute("onkeyup", "checkForLength(this.id,'chars" + IDsrc + "',5000);");
//	newImgDesc.style.width = "200px";
//	newImgDesc.style.height = "50px";
//	newImgDesc.id = IDsrc + "__desc";
//	newImgDesc.className = "input_forms";
//	
//	
//	//-------------------------------------------->>
//	
//	var newCharsLeft = document.createElement("div");
//	newCharsLeft.id = "chars" + IDsrc; //
//	
//	//-------------------------------------------->>
//	
//	var newImgTitleText = document.createElement("div");
//	newImgTitleText.setAttribute("style", "text-align:left;padding:2px 20px;");
//	newImgTitleText.innerHTML = "Title";
//	
//	var newImgDescText = document.createElement("div");
//	newImgDescText.setAttribute("style", "text-align:left; padding:2px 20px;");
//	newImgDescText.innerHTML = "Description";

//	newImgContainer.appendChild(newImg);
//	
//	newContainer.appendChild(newImgTitleText);
//	newContainer.appendChild(newImgTitle);
//	newContainer.appendChild(newImgDescText);
//	newContainer.appendChild(newImgDesc);	
//	newContainer.appendChild(newCharsLeft);
//	
//	$(newImgDesc.id).onkeyup = function(){checkForLength(this.id,"chars" + IDsrc,5000)};
//	
//	//------->>Coloco el Watermark al titulo
//	watermark(newImgTitle.id, MarcaDelTitulo, 'input_marcagua');
//	
//	if (newImg.filters) {
//		try {
//			newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
//		} catch (e) {
//			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
//			newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
//		}
//	} else {
//		newImg.style.opacity = 0;
//	}

//	newImg.onload = function () {
//		fadeIn(newImg, 0);
//	};
//	
//	total_files ++;
//	//Chequeo la cantidad de archivos subidos 
//	//si supera al max. deshabilito el boton
//	if(total_files >= total_files_constant)
//	{
//	    $("btnBrowse").disabled = true;
//	    $("btnBrowse").className = "input_botons_off";
//	}
//	//------------------->>
//	newImg.src = src;
//}


//------------------------------------------------------------------------------------>>