﻿/**Inicialitzacions**/
window.onload = function() {
    preBubbles();
}

function showDefaultText(object, isfocus, defaultmessage){
    if(object.value == ''){
        if(isfocus == false){
            object.value= defaultmessage;
            object.style.color='Gray';
        }
    }else if(object.style.color=='Gray' || object.style.color=='gray'){
        object.value=''; 
        object.style.color='#000';
    }
}

function inicialitza() {
    if (document.getElementById('lIdea') != null) {
        if (ideaI == 0) {
            ideaI = maxIdeas;
        } else {
            ideaI--;
        }
        dameIdea();
    }
}

function imageLoaded(elementsLoadedCount, totalImagesCount) {
}

function allLoaded() {
    if (waitBubble) {
        waitBubble = false;
        bubbles();
    }
}

function preBubbles() {
    if (document.getElementById('i1') != null) {
        $('#i1').attr('src', 'images/profiles/' + damePicture());
        $('#b1').text(dameSplashIdea());

        $("#p1").find('img').batchImageLoad({
            loadingCompleteCallback: imageLoaded,
            imageLoadedCallback: allLoaded
        });
    }
}

function bubbles() {
    $('#i2').attr('src', 'images/profiles/' + damePicture());
    $('#b2').text(dameSplashIdea());
    
    $('#p1').fadeIn(1000).delay(6000).fadeOut(1000, function() {
        $('#p1').hide();
        bubbles2();
    });
        // Animation complete.
        //    });
}

function bubbles2() {
    $('#i3').attr('src', 'images/profiles/' + damePicture());
    $('#b3').text(dameSplashIdea());
    $('#p2').fadeIn(1000).delay(6000).fadeOut(1000, function() {
        $('#p2').hide();
        bubbles3();
    });
        // Animation complete.
        //    });
}

function bubbles3() {
    $('#i1').attr('src', 'images/profiles/' + damePicture());
    $('#b1').text(dameSplashIdea());

    $('#p3').fadeIn(1000).delay(6000).fadeOut(1000, function() {
            $('#p3').hide();
            bubbles();
    });
        // Animation complete.
        //    });
}

/***FUNCIO PER LIMITAR EL NOMBRE DE CARÀCTERS DUN TEXTBOX MULTILINE SENSE COUNTER***/
function ismaxlength(obj) {
    var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
    obj.value=obj.value.substring(0,mlength)
}

/**FUNCIONS PER LIMITAR EL NOMBRE DE CARÀCTERS D'UN TEXTBOX AMB COUNTER***/
maxL = 140;
var bName = navigator.appName;
function taLimit(taObj) {
    //if (taObj.value.length == maxL) return false;
    return true;
}

function taCount80(taObj, Cnt) {
    objCnt = createObject(Cnt);
    objVal = taObj.value;
    //if (objVal.length > maxL) objVal = objVal.substring(0, maxL);
    objCnt.style.color = '';
    if (objVal.length > 80) objCnt.style.color = 'Red';
    if (objCnt) {
        if (bName == "Netscape") {
            objCnt.textContent = 80 - objVal.length;
        }
        else { objCnt.innerText = 80 - objVal.length; }
    }
    return true;
}

function taCount(taObj, Cnt) {
    objCnt = createObject(Cnt);
    objVal = taObj.value;
    //if (objVal.length > maxL) objVal = objVal.substring(0, maxL);
    objCnt.style.color = '';
    if (objVal.length > maxL) objCnt.style.color = 'Red';
    if (objCnt) {
        if (bName == "Netscape") {
            objCnt.textContent = maxL - objVal.length;
        }
        else { objCnt.innerText = maxL - objVal.length; }
    }
    return true;
}

function createObject(objId) {
    if (document.getElementById) return document.getElementById(objId);
    else if (document.layers) return eval("document." + objId);
    else if (document.all) return eval("document.all." + objId);
    else return eval("document." + objId);
}
/************/

/***Funcions per deshabilitar un link**/
function returnFalse() {
    return false;
}

function enableLinkButton(clientID) {
    document.getElementById(clientID).disabled = "";
    document.getElementById(clientID).onclick = "";
}

function disableLinkButton(clientID, text, cssclass) {
    
    if (clientID != "" && document.getElementById(clientID) != null) {
        document.getElementById(clientID).innerHTML = text;
        document.getElementById(clientID).className = cssclass;
        document.getElementById(clientID).disabled = "disabled";
        document.getElementById(clientID).onclick = returnFalse;
    }
}

function disableLinks(clientID) {

    if (clientID != "" && document.getElementById(clientID) != null) {
        //document.getElementById(clientID).innerText = text;
        //document.getElementById(clientID).className = cssclass;
        //document.getElementById(clientID).disabled = "disabled";
        document.getElementById(clientID).href = "#"
        document.getElementById(clientID).onclick = returnFalse;
    }
}
/*************/

function objectIdea(description, urlname, url,id,source, promotedEventID) {
    this.description = description;
    this.urlname = urlname;
    this.url = url;
    this.id = id;
    this.source = source;
    this.promotedEventID = promotedEventID;
}

function deleteIdea() {
        document.getElementById('pUrl').value = '-1';
        document.getElementById('pUrlname').value = '-1';
        document.getElementById('pDescription').value = '-1';
        document.getElementById('pSource').value = '-1';
        document.getElementById('pId').value = '-1';
        document.getElementById('pPromotedEventID').value = '-1';
        document.getElementById('xProposal').value = '';
        document.getElementById('publishDiv').style.display = 'none';
        return taCount(document.getElementById('xProposal'), 'myCounter');
}

function copyIdea() {
    if (arrayIdeas[ideaI].url != "-1") {
        document.getElementById('publishLink').href = arrayIdeas[ideaI].url;
        document.getElementById('publishLink').innerHTML = arrayIdeas[ideaI].urlname;
        document.getElementById('pUrl').value = arrayIdeas[ideaI].url;
        document.getElementById('pUrlname').value = arrayIdeas[ideaI].urlname;
        document.getElementById('publishDiv').style.display = '';
    } else {
        document.getElementById('pUrl').value = arrayIdeas[ideaI].url;
        document.getElementById('pUrlname').value = arrayIdeas[ideaI].urlname;
        document.getElementById('publishDiv').style.display = 'none';
    }
    document.getElementById('pDescription').value = arrayIdeas[ideaI].description;
    document.getElementById('xProposal').value = arrayIdeas[ideaI].description;
    document.getElementById('pSource').value = arrayIdeas[ideaI].source;
    document.getElementById('pId').value = arrayIdeas[ideaI].id;
    document.getElementById('pPromotedEventID').value = arrayIdeas[ideaI].promotedEventID;
    return taCount(document.getElementById('xProposal'), 'myCounter');
}
/***FUNCIO QUE RETORNA IDEES DE PLANS***/
function dameIdea() {
    if (ideaI < maxIdeas) {
        ideaI++;
        document.getElementById('lIdea').innerHTML = arrayIdeas[ideaI].description;
        if (arrayIdeas[ideaI].url != '-1') {
            document.getElementById('linkIdea').href = arrayIdeas[ideaI].url;
            document.getElementById('linkIdea').innerHTML = arrayIdeas[ideaI].urlname;
        }else{
            document.getElementById('linkIdea').innerHTML = '';
        }
    } else {
        ideaI = 0;
        document.getElementById('lIdea').innerHTML = arrayIdeas[ideaI].description;
        if (arrayIdeas[ideaI].url != '-1') {
            document.getElementById('linkIdea').href = arrayIdeas[ideaI].url;
            document.getElementById('linkIdea').innerHTML = arrayIdeas[ideaI].urlname;
        } else {
            document.getElementById('linkIdea').innerHTML = '';
        }
    }
    document.getElementById('linkCopy').innerHTML = 'Copiar idea ' + (ideaI + 1) + '/' + (maxIdeas + 1);
}

/***FUNCIO QUE RETORNA IDEES DE PLANS***/
function dameIdeaAnterior() {
    if (ideaI == 0) {
        ideaI = maxIdeas;
        document.getElementById('lIdea').innerHTML = arrayIdeas[ideaI].description;
        if (arrayIdeas[ideaI].url != '-1') {
            document.getElementById('linkIdea').href = arrayIdeas[ideaI].url;
            document.getElementById('linkIdea').innerHTML = arrayIdeas[ideaI].urlname;
        } else {
            document.getElementById('linkIdea').innerHTML = '';
        }
    } else {
        ideaI--;
        document.getElementById('lIdea').innerHTML = arrayIdeas[ideaI].description;
        if (arrayIdeas[ideaI].url != '-1') {
            document.getElementById('linkIdea').href = arrayIdeas[ideaI].url;
            document.getElementById('linkIdea').innerHTML = arrayIdeas[ideaI].urlname;
        } else {
            document.getElementById('linkIdea').innerHTML = '';
        }
    }
    document.getElementById('linkCopy').innerHTML = 'Copiar idea ' + (ideaI + 1) + '/' + (maxIdeas + 1);
}
/*---FUNCIO QUE RETORNA IDEES DE PLANS---*/


/**Funcio que retorna lindex de la foto i el text del plan**/
var maxSplashIdeas = 20;
var ideaSplashI = Math.floor(Math.random() * (maxSplashIdeas));
var ideaSplash;
var waitBubble = true;

var arraySplashIdeas = new Array(maxSplashIdeas);
arraySplashIdeas[0] = 'Quiero pasar un fin de semana en un sitio tranquilo';
arraySplashIdeas[1] = 'Quiero que alguien me enseñe las cosas típicas del centro de la ciudad';
arraySplashIdeas[2] = '¡A mi y a una amiga nos gustaría salir de fiesta con alguien!';
arraySplashIdeas[3] = '¿Quien se apunta a tomarse unas copas?';
arraySplashIdeas[4] = 'Quiero ir al cine a ver una peli';
arraySplashIdeas[5] = 'Quiero que alguien me cuente su historia';
arraySplashIdeas[6] = 'Quiero conocer alguien que me haga reír';
arraySplashIdeas[7] = '¿Quien se apunta a dar un paseo con nuestros animales de compañía?';
arraySplashIdeas[8] = '¡Quiero tener un fin de semana de aventura!';
arraySplashIdeas[9] = 'Quiero a alguien para que me aconseje a la hora de comprar ropa';
arraySplashIdeas[10] = 'Quiero ir al teatro';
arraySplashIdeas[11] = 'Quiero ir a un concierto pero nadie me acompaña';
arraySplashIdeas[12] = 'Quiero dar un paseo por la playa';
arraySplashIdeas[13] = 'Quiero hacer un viaje exótico con alguien';
arraySplashIdeas[14] = 'Quiero pasar el domingo por la tarde paseando por la playa';
arraySplashIdeas[15] = 'Quiero hacer una ruta en bici con alguien';
arraySplashIdeas[16] = 'Quiero ir a un concierto de Jazz';
arraySplashIdeas[17] = 'Quiero cantarle una canción a alguien';
arraySplashIdeas[18] = 'Quiero compartir una taza de chocolate caliente con churros';
arraySplashIdeas[19] = 'Quiero conocer a una chica interesante';

function dameSplashIdea() {
    if (ideaSplashI < maxSplashIdeas) {
        ideaSplash = arraySplashIdeas[ideaSplashI];
        ideaSplashI++;
        return ideaSplash;
    } else {
        ideaSplash = arraySplashIdeas[0];
        ideaSplashI = 1;
        return ideaSplash;
    }
}

function damePicture() {
    if (ideaSplashI % 2) {
        return 'chico'+ideaSplashI+'.jpg';
    } else {
        if (ideaSplashI < maxSplashIdeas) {
            return 'chica' + ideaSplashI + '.jpg';
        } else {
            return 'chica' + 0 + '.jpg';
        }
    }
}


/*** FUNCIONS INFORMATION BAR ****/
function showStatus(cssclass, image, message, fadeIn, timeout, fadeOut, invitePoints, showShareLinks) {
    //Append the info bar div
    append(cssclass, invitePoints, showShareLinks);

    //Shows the message
    $('#imageBar').attr('src', 'images/' + image);
    $('#textBar').html(message);
    $('#infoBar').fadeIn(fadeIn, function() {
        if (timeout > 0) {
            $('#infoBar').fadeOut(fadeOut, function() {
                $('#infoBar').hide();
            });
        }
    });
}
function hideStatus(fadeOut) {
    $('#infoBar').fadeOut(fadeOut);
}

function hideStatus2(fadeOut) {
    $('#infobar2').fadeOut(fadeOut);
    //document.getElementById('infobar2').style.display = 'none';
}

function append(cssclass, invitePoints, showShareLinks) {
    if ($('#infoBar').html() != null) {
        $("#infoBar").remove();
    }

    var shareButtons = '';
    if(showShareLinks == "true"){
        shareButtons = '<br /><br />' +
        'Comparte uolala! en <a href="http://www.facebook.com/share.php?u=http://www.uolala.com" onclick="return fbs_click()" target="_blank"><img src="images/fbIcon.png" class="icona" alt="" /> Facebook</a>&nbsp' +
        ' o ' +
        '&nbsp<a href="#" onclick="twitter_click()"><img src="images/tweetIcon.png" class="icona" alt="Share on Twitter2"> Twitter</a> y por cada registro ganarás ' + invitePoints + ' puntos!';
    }
    
    $('#content').prepend(
    '<div id="infoBar" class="' + cssclass + '" style="display:none">' +
        //'<center>' +
        '<div style="display:inline; float:left; margin-left:0px; width: 580px;">' +
            '<img id="imageBar" src="images/closeWhite.png" style="vertical-align:-2px;"/>&nbsp' +
            '<span id="textBar">uolala!</span> ' +
            shareButtons +
        '</div>' +
        '<div style="display:inline; float: right; margin-right:0px;">' +
            '<a href="#" onclick="hideStatus(500);" style="margin:0px; "><img id="imageBar2" src="images/closeWhite.png" style="vertical-align:-4px;"/></a>' +
        '</div>' +
        //'</center>' +
    '</div>'
    );
}

function CheckBoxListSelect(cbControl, state) {
    var chkBoxList = document.getElementById(cbControl);
    var chkBoxCount = chkBoxList.getElementsByTagName("input");
    for (var i = 0; i < chkBoxCount.length; i++) {
        chkBoxCount[i].checked = state;
    }
    return false;
}

/***FACEBOOK AND TWITTER SHARE LINKS***/
function fbs_click() {
    u = document.getElementById('xInvitationLink').value; //"http://www.uolala.com/default.aspx?invitationCode=cf55a84";
    t = "Link de invitación a uolala! La web para conocer gente nueva mediante propuestas de ocio";
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), '_blank', 'toolbar=0,status=0,width=626,height=436');
    return false;
}
function twitter_click() {
    window.open('http://twitter.com/home?status=Conoce gente nueva a partir de propuestas ocio con @uolala ' + document.getElementById('xInvitationLink').value);
}

/***Not used***/
function publish() {
    var message = 'Testing uolala stream publish';
    var attachment = {
        'name': 'Join uolala and...',
        'href': ' http://www.uolala.com',
        'caption': '{*actor*} rated the lolcat 5 stars',
        'description': 'a funny looking cat',
        'properties': {
            'category': { 'text': 'humor', 'href': 'http://www.uolala.com/terms.aspx' },
            'ratings': '5 stars'
        },
        'media': [{ 'type': 'image', 'src': 'http://uolala.com/images/logoGran.png', 'href': 'http://uolala.com/images/logoGran.png'}]
    };
    var action_links = [{ 'text': 'Recaption this', 'href': 'http://uolala.com/images/about.aspx'}];

    FB.Connect.get_status().waitUntilReady(function(status) {
        FB.Connect.streamPublish(message, attachment, action_links);
    });
}

function ask() {
    alert('hola');
    // this checks current status of permission and if not granted it will display an ajax box for user to give permission.
    facebook_prompt_permission('email', function(accepted) {
        alert(accepted);
        if (accepted) {
            // do something here. I send the status that the user has written to FB via an AJAX request.
            alert('status acceptat');
        } else {
            alert('status NO acceptat');
        }
    });
}

function facebook_prompt_permission(permission, callbackFunc) {
    //check is user already granted for this permission or not
    FB.Facebook.apiClient.users_hasAppPermission(permission,
             function(result) {
                 alert(result);
                 // prompt offline permission
                 if (result == 0) {
                     // render the permission dialog
                     FB.Connect.showPermissionDialog(permission, callbackFunc);
                 } else {
                     // permission already granted.
                     callbackFunc(true);
                 }
             });
}   

