﻿/*GENERAL VARS*/
var txtLinkMore = "לצפיה בהודעות נוספות";
var txtLinkEnd = "חזרה לאתר";
var loggedUser;
var userClicks = 0;
var interestCounter = 0;
var helloTitle;
var shortWebSiteTextsArray;
var shortUserTxt;

/*ONLOAD GENERAL*/
$(function () {

    //Get cookie value
    var cookieVals = $("#cookieVal").val();
    cookieValsArray = cookieVals.split(",");
    // (cookieValsArray[2] == 'F')

    //Texts from cookies for all website


    var textsToShow;


    //Guest - 0
    // shortWebSiteTextGuest = "אירוע פתיחת שנה מיוחד - שישי אקשן בעקבות השבטים האבודים ב-1/10. ההרשמה בעיצומה!";

    //RolePlaying - 1
    shortWebSiteTextRolePlaying = "השנה מתקיימים חוגי מבוכים ודרקונים בתל אביב, רמת אביב, רמת גן וגבעתיים. ההרשמה בעיצומה!";

    //Science - 2
    shortWebSiteTextScience = "חוג מדען צעיר מתקיים השנה בבית ספר רמת אביב ג. ההרשמה בעיצומה!";

    //Birthday - 3
    shortWebSiteTextBirthday = "אירוע פתיחת שנה מיוחד - שישי אקשן בעקבות השבטים האבודים ב-1/10. ההרשמה בעיצומה!";

    //Young - 4 
    shortWebSiteTextYoung = "חוגי יצירה וסטיילינג אישי מתקיימים השנה בבית הספר רמת אביב ג'. ההרשמה בעיצומה!";




    //get Guest text
    shortWebSiteTextGuest = $('#GuestWebsiteMsgM').val();
    linkWebsiteTxt = $('#GuestWebsiteMsgLink').val();


    //Texts Array
    //  var myTextsForCookie = new Array(shortWebSiteTextGuest, shortWebSiteTextRolePlaying, shortWebSiteTextScience, shortWebSiteTextBirthday, shortWebSiteTextYoung);



    //Check login TRUE(ok)/FALSE(failed)/NULL(guest)
    if (cookieVals == 'NULL') { //we have a guest!
        helloTitle = "שלום אורח!";
        shortUserTxt = shortWebSiteTextGuest;
        linkWebsiteTxt = $('#GuestWebsiteMsgLink').val();
        $('#helloUserWebSite').text(helloTitle + " " + shortUserTxt);
        $('#helloUserWebSite').attr('href', linkWebsiteTxt);
        $('#registerUser').css('visibility', 'visible');
        $('#loginUser').css('visibility', 'visible');

    }

    //check if login failed
    if (cookieVals == 'FALSE') {

        showPopup('#loginForm');
        $('#loginError').css('display', 'block');
        helloTitle = "שלום אורח!";
        shortUserTxt = shortWebSiteTextGuest;
        linkWebsiteTxt = $('#GuestWebsiteMsgLink').val();
        $('#helloUserWebSite').text(helloTitle + " " + shortUserTxt);
        $('#helloUserWebSite').attr('href', linkWebsiteTxt);
        $('#loginUser').css('visibility', 'visible');


    }

    //good login - show popups by interest   
    if (cookieValsArray[0] == 'TRUE') {


        //create string for txts
        var textsToShowValsString = "";


        //get username
        loggedUser = cookieValsArray[1];

        //Hello FirstName,
        helloTitle = "שלום " + loggedUser + ",";
        $('#helloUserWebSite').text(helloTitle);


        $('#loginUser').css('visibility', 'visible');
        $('#loginUser').text('אינך ' + loggedUser + '?' + ' התחברות בשם אחר');
        $('#logoutUser').css('visibility', 'visible');


        var found = "false";
        var i = 0;

        //find short text
        for (i = 0; i < cookieValsArray.length; i++) {
            if (cookieValsArray[i + 4] == 'RolePlaying') {
                //    found = 'true';
                //    shortUserTxt = shortWebSiteTextRolePlaying;
                textsToShowValsString = textsToShowValsString + ",RolePlaying";

            }
            if (cookieValsArray[i + 4] == 'Science') {
                //found = 'true';
                //    shortUserTxt = shortWebSiteTextScience;
                textsToShowValsString = textsToShowValsString + ",Science";
            }
            if (cookieValsArray[i + 4] == 'Birthday') {
                //  found = 'true';
                //  shortUserTxt = shortWebSiteTextBirthday;
                textsToShowValsString = textsToShowValsString + ",Birthday";
            }
            if (cookieValsArray[i + 4] == 'Young') {
                //  found = 'true';
                //  shortUserTxt = shortWebSiteTextYoung;
                textsToShowValsString = textsToShowValsString + ",Young";
            }
        }

        //alert(textsToShowValsString);

        //Get random number to choose text
        //     alert('textsToShowValsString: ' + textsToShowValsString);
        textsToShowArray = textsToShowValsString.split(","); //Array of relevent numbers

        var max = textsToShowArray.length; //how many options?
        max = max - 1;
      //  alert('size: ' + max);

        var randomnumber = Math.floor(Math.random() * max); //choose random field   
        if (randomnumber == 0) randomnumber = 1;

        //alert('randomnumber: ' + randomnumber);

        //   alert('randomnumber ' + randomnumber);
        //var myRandomChoice = textsToShowArray[randomnumber]; //selected id
        //   alert('myRandomChoice ' + myRandomChoice);

        //get random field from array
        var chosenField = textsToShowArray[randomnumber];
        //alert('textsToShowArray[randomnumber] ' + textsToShowArray[randomnumber]);

        //get Txt according to choice:
        //RolePlaying
        if (chosenField == 'RolePlaying') {

            if (cookieValsArray[2] == 'F') {
                shortUserTxt = $('#RolePlayingWebsiteMsgF').val();
                linkWebsiteTxt = $('#RolePlayingWebsiteMsgLink').val();
            }
            else {
                shortUserTxt = $('#RolePlayingWebsiteMsgM').val();
                linkWebsiteTxt = $('#RolePlayingWebsiteMsgLink').val();
            }
        }

        //Science
        if (chosenField == 'Science') {

            if (cookieValsArray[2] == 'F') {
                shortUserTxt = $('#ScienceWebsiteMsgF').val();
                linkWebsiteTxt = $('#ScienceWebsiteMsgLink').val();
            }
            else {
                shortUserTxt = $('#ScienceWebsiteMsgM').val();
                linkWebsiteTxt = $('#ScienceWebsiteMsgLink').val();
            }
        }


        //Birthdays
        if (chosenField == 'Birthday') {

            if (cookieValsArray[2] == 'F') {
                shortUserTxt = $('#BirthdayWebsiteMsgF').val();
                linkWebsiteTxt = $('#BirthdayWebsiteMsgLink').val();
            }
            else {
                shortUserTxt = $('#BirthdayWebsiteMsgM').val();
                linkWebsiteTxt = $('#BirthdayWebsiteMsgLink').val();
            }
        }


        //Young
        if (chosenField == 'Young') {

            if (cookieValsArray[2] == 'F') {
                shortUserTxt = $('#YoungWebsiteMsgF').val();
                linkWebsiteTxt = $('#YoungWebsiteMsgLink').val();
            }
            else {
                shortUserTxt = $('#YoungWebsiteMsgM').val();
                linkWebsiteTxt = $('#YoungWebsiteMsgLink').val();
            }
        }



        //shortUserTxt = myTextsForCookie[myRandomChoice]; //get text for ID
        //  alert('shortUserTxt ' + shortUserTxt);



        //}

        //alert(shortUserTxt);
        //create text to show
        $('#helloUserWebSite').text(helloTitle + " " + shortUserTxt);
        $('#helloUserWebSite').attr('href', linkWebsiteTxt);

        //How many interests? - remove loginValue,FirstName, Gender,Blank
        interestCounter = cookieValsArray.length - 4;

        /*
        //what do we do onlick?
        $('#moreMsgsLink').click(function () {

        //count user clicks
        userClicks = userClicks + 1;

        //do we have more msgs for user?
        if ($('#moreMsgsLink').text() == txtLinkEnd) {
        hidePopup('#interestPopup');
        }
        else {

        //create popup
        showPopup('#interestPopup');
        //say hello
        $('#interestPopupTitle').text(helloTitle);
        //get text
        getInterestTxt(userClicks);
        //are there more msgs?
        if (userClicks + 1 == interestCounter) {
        $('#moreMsgsLink').text(txtLinkEnd);
        }
        else {
        $('#moreMsgsLink').text(txtLinkMore);
        }

        }




        }); //end click function*/



        //Check if we are in the main page
        var href = jQuery(location).attr('href');

        //create popup for mainpage
        if (href == "http://stavarts.com/" || href == "http://www.stavarts.com/") {

            //       showPopup('#interestPopup');

            //Hello + First Name
            $('#interestPopupTitle').text(helloTitle);

            //get number of interests

            //alert('interestCounter:' + interestCounter);
            //Get texts
            for (j = 0; j < interestCounter; j++) {
                getInterestTxt(j);
            }

            /* //show right link text
            if (interestCounter > 1) {
            $('#moreMsgsLink').text(txtLinkMore);
            }
            else {
            $('#moreMsgsLink').text(txtLinkEnd);
            }*/

        } //end main page popup



    } //end good login





});



/*POPUP FUNCTIONS*/
function showPopup(id) {
    $('#mypopupbg').css('display', 'block');
    $(id).css('display', 'block');
}
function hidePopup(id) {
    $('#mypopupbg').css('display', 'none');
    $(id).css('display', 'none');
}

/*LOGIN USER POPUP*/
function validLogin() {

    var user = $('#username').val();
    var pass = $('#password').val();

    if (user == "" || user == " " || user == null || pass == "" || pass == " " || pass == null) {
        alert('חובה להזין שם משתמש וסיסמא!');
        return false;
    }
    else {

        return true;
    }

}

/*INTEREST POPUP*/
function getInterestTxt(i)
{

  //  $('#interestPopupImg1').attr('style', 'float:left;margin:0 10px 10px 0');
  //  $('#interestPopupImg2').attr('style', 'float:right;margin:0 0 10px 30px');

  //  $('#interestTxt').css('display', 'block');
 //   var txtInterestVal;


    //  alert(cookieValsArray[2]);

     //Get txt by interest & gender
    if (cookieValsArray[i + 4] == 'RolePlaying') {
        
        //get text       
        if (cookieValsArray[2] == 'F')
            txtInterestVal = $('#RolePlayingPopupMsgF').val();
        else
            txtInterestVal = $('#RolePlayingPopupMsgM').val();
        $('#interestTxtRolePlaying').text(txtInterestVal);

        //get link
        RolePlayingUrl = $('#RolePlayingPopupLink').val();
        $('#interestPopupLinkRolePlaying').attr('href', RolePlayingUrl);
        
        //get image
        RolePlayingUrl = $('#RolePlayingPopupImg').val();
        $('#interestPopupImgRolePlaying').attr('src', RolePlayingUrl);

        //display popup
        $('#interestPopup').css('display', 'block');
        $('#interestRolePlaying').css('display', 'block');
        $('#mypopupbg').css('display', 'block');

         // moreInfoUrl = "http://stavarts.com/?page_id=1364";
         // $('#interestPopupImg1').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/dnd3.jpg');
         // $('#interestPopupImg2').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/dnd1.jpg');
         // $('#moreInfoLink').attr('href', moreInfoUrl);
          
      }

      if (cookieValsArray[i + 4] == 'Birthday') {
      /*
         // txtInterestVal = $('#BirthdayTxt').val();
          //moreInfoUrl = "http://stavarts.com/?page_id=7";
          //$('#interestTxt').text(txtInterestVal);
          //$('#interestPopupImg1').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/bday3.jpg');
          $('#interestPopupImg2').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/bday1.jpg');
          $('#moreInfoLink').attr('href', moreInfoUrl);*/


          //get text       
          if (cookieValsArray[2] == 'F')
              txtInterestVal = $('#BirthdayPopupMsgF').val();
          else
              txtInterestVal = $('#BirthdayPopupMsgM').val();
          $('#interestTxtBirthday').text(txtInterestVal);

          //get link
          BirthdayUrl = $('#BirthdayPopupLink').val();
          $('#interestPopupLinkBirthday').attr('href', BirthdayUrl);

          //get image
          BirthdayUrl = $('#BirthdayPopupImg').val();
          $('#interestPopupImgBirthday').attr('src', BirthdayUrl);

          //display popup
          $('#interestPopup').css('display', 'block');
          $('#interestBirthday').css('display', 'block');
          $('#mypopupbg').css('display', 'block');



       }

      if (cookieValsArray[i + 4] == 'Science') {
      /*
          txtInterestVal = $('#ScienceTxt').val();
          $('#interestTxt').text(txtInterestVal);
          moreInfoUrl = "http://stavarts.com/?page_id=1217";
          $('#interestPopupImg1').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/science2.jpg');
          $('#interestPopupImg2').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/science1.jpg');
          $('#moreInfoLink').attr('href', moreInfoUrl);

          */


          //get text       
          if (cookieValsArray[2] == 'F')
              txtInterestVal = $('#SciencePopupMsgF').val();
          else
              txtInterestVal = $('#SciencePopupMsgM').val();
          $('#interestTxtScience').text(txtInterestVal);

          //get link
          ScienceUrl = $('#SciencePopupLink').val();
          $('#interestPopupLinkScience').attr('href', ScienceUrl);

          //get image
          ScienceUrl = $('#SciencePopupImg').val();
          $('#interestPopupImgScience').attr('src', ScienceUrl);

          //display popup
          $('#interestPopup').css('display', 'block');
          $('#interestScience').css('display', 'block');
          $('#mypopupbg').css('display', 'block');


   

       }

       if (cookieValsArray[i + 4] == 'Young') {

       /*    txtInterestVal = $('#YoungTxt').val();
           $('#interestTxt').text(txtInterestVal);
           moreInfoUrl = "http://stavarts.com/?page_id=1204";
           $('#interestPopupImg1').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/sipur2.jpg');
           $('#interestPopupImg2').attr('src', 'http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/images/sipur1.jpg');
           $('#moreInfoLink').attr('href', moreInfoUrl);
           */

           //get text       
           if (cookieValsArray[2] == 'F')
               txtInterestVal = $('#YoungPopupMsgF').val();
           else
               txtInterestVal = $('#YoungPopupMsgM').val();
           $('#interestTxtYoung').text(txtInterestVal);

           //get link
           YoungUrl = $('#YoungPopupLink').val();
           $('#interestPopupLinkYoung').attr('href', YoungUrl);

           //get image
           YoungUrl = $('#YoungPopupImg').val();
           $('#interestPopupImgYoung').attr('src', YoungUrl);

           //display popup
           $('#interestPopup').css('display', 'block');
           $('#interestYoung').css('display', 'block');
           $('#mypopupbg').css('display', 'block');
           

        }
     //   alert('txtInterestVal:' + txtInterestVal);
    }
    /*REGISTRATION POPUP*/
    function checkUsername()
    {
        var userChoice = $('#usernameInsert').val();
        $('#usernameInsertHelper').val(userChoice);
        $('#insertWebUserForm').submit();

    }
    function goToRegistration() {
        window.location = "http://stavarts.com/NewWebSite/wp-content/themes/Blue3colRTL/insertUser/insertWebUser.php";
    }
