﻿

function ReloadCurrentPage(){
    //FIRST GET CURRENT PAGE ....

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/home/CurrentPageInSession_Ajax",
        dataType: "json",
        cache: false,
        success: function ReloadCurrent(result) {
            //THEN RELOAD CURRENT PAGE

            var curPage = result.curPage;

            if (curPage == 'home') {
                navigateToHome();
            }
            else if (curPage == 'blog') {
                navigateToBlog();
            }
            else if (curPage == 'speakUp') {
                navigateToSpeakUp();
            }
            else if (curPage == 'jhGallery') {
                navigateToJHGallery();
            }
            else if (curPage == 'fanGallery') {
                navigateToFanGallery();
            }
            else if (curPage == 'contact') {
                navigateToContact();
            }
            else if (curPage == 'videos') {
                navigateToVideos();
            }
            else if (curPage == 'tour') {
                navigateToTour();
            }
            else if (curPage == 'discography') {
                navigateToDiscography();
            }
        }
    });
}

/* AJAX AJAX AJAX */
/*
var url = '/home/AjaxReturnTest/2';
$.ajax({
    url: url,
    success: function (data) {
        $('#ajaxContainer').html(data);
    },
    type: 'POST',
    dataType: 'html'
});
*/
/* AJAX AJAX AJAX */

function LoginIn_Ajax(userName, userEmail) {

    var url = 'http://' + window.location.hostname + '/home/LoginIn_Ajax';
    $.ajax({
        url: url,
        type: 'POST',
        data: 'userName=' + userName + '&userEmail=' + userEmail,
        dataType: 'json',
        cache: false,
        success: function (Result) {
            if (Result.success == true) {
                ShowHideAdminPanels();
                ReloadCurrentPage(); //This is the new show hide admin panels ...
                hideError(); //this is not an error .. it the loggin popup
            } else {
                alert(Result.message);
            }
        },
        error: function () {
            alert('Sorry something went wrong :(');
        }
    });
}



function ShowLoginFields() {

    /* animate in login fields */
    $('#hideSignInDiv').show('slow');
    $('#bannerSignIn').css('border', 'none');

    /* AFTER SHOWING LOGIN FIELDS ADD BTN functionality */
    $('#loginBtn').click(function (e) {
        e.preventDefault();

        var userName = $('#user').val();
        var userEmail = $('#email').val();

        if ((userName != '') && (userEmail != '')) {
            LoginIn_Ajax(userName, userEmail);
        } else {
            alert('please enter username and email');
        }
    });
}






$(document).ready(function () {

    $('#loginBtn').click(function (e) {
        e.preventDefault();

        $('#loginBtn').unbind('click');
        ShowLoginFields();
    });

    /* LOGIN IN POPUP */
    $('.loginBtnPopUp').click(function (e) {
        e.preventDefault();

        var userName = $('#userNamePopUp').val();
        var userEmail = $('#userEmailPopUp').val();

        if (((userName != '') && (userEmail != '')) || ((userName != 'UserName') && (userEmail != 'Email'))) {
            LoginIn_Ajax(userName, userEmail);
        } else {
            alert('please enter username and email');
        }
    });

    $('#logOutUserBtn').click(function (e) {
        e.preventDefault();

        $.ajax({
            type: "POST",
            url: "http://" + window.location.hostname + "/home/LoginOut_Ajax",
            dataType: "json",
            cache: false,
            success: function (result) {
                if (result.loggedOut == true) {
                    ShowHideAdminPanels();
                    blogCommentWrapID = ""; //reset the global blog textwrapId var back to null
                    ReloadCurrentPage();
                }
            },
            error: function () {
                displayError('ajaxError');
            }
        });
    });
});

/* //////////////////////////////////////////////////////////////////////////////////////////////////// */
/* ///////////////////////// FACEBOOK LOGIN FUNCTIONS ///////////////////////////////////////////////// */
/* ///////////////////////// FACEBOOK LOGIN FUNCTIONS ///////////////////////////////////////////////// */
/* ///////////////////////// FACEBOOK LOGIN FUNCTIONS ///////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////////////////////////////////////////////// */

/* ///////////////////////////////////////////////////////////////////////// */
/* LOGIN WITH FACEBOOK FUNCTIONALITY                                          */
/* ///////////////////////////////////////////////////////////////////////// */
//
function CreateFacebookAccountAjax(userName, avatarURL, email, homeTown) {

    $.ajax({
        url: "http://" + window.location.hostname + "/home/CreateFacebookAccountAjax",
        type: "POST",
        data: 'facebookUserName=' + encodeURIComponent(userName) + '&facebookProfilePic=' + encodeURIComponent(avatarURL) + '&facebookEmail=' + encodeURIComponent(email) + '&facebookHomeTown=' + encodeURIComponent(homeTown),
        dataType: "json",
        cache: false,
        success: function (result) {
            if (result.success == true) {

                //show success message
                $('.loginPopUpWrapper').css('background-image', 'url(http://www.johnnyhollow.com/imagesProduction/generalSprite.jpg)'); //loginBGSuccess.jpg
                $('.loginPopUpWrapper').css('background-position', '0 -656px');

                $('.initialLoginOptions').css('display', 'none');

                //refresh page after login
                ShowHideAdminPanels();
                ReloadCurrentPage();
            } else {
                displayError('ajaxError');
            }
        },
        error: function () {
            displayError('ajaxError');
        }
    });
}

function delayOpenCommentField() {
    $(blogCommentWrapID).css('display', 'block');
    $(blogCommentWrapID).animate({
        'height': '150'
    }, 1000);
}

function loginWithFacebook_BASIC_PERMISSIONS(blogCommentWrapID) {

    FB.ui({ method: "permissions.request" }, function () {

        FB.api('/me', function (fbUserObject) {
            if (!fbUserObject || fbUserObject.error) {
                alert("Sorry a facebook error occurred.");
            } else {

                //request picture then CREATE ACCOUNT with name / email / hometown
                FB.api('/me?fields=picture&type=square', function (fbPicObject) {
                    CreateFacebookAccountAjax(fbUserObject.name, fbPicObject.picture, 'empty', 'empty');
                });

                if (blogCommentWrapID != '') {
                    $('.mp_error_close').click(function () {

                        setTimeout('delayOpenCommentField()', 1000); //wait for success popup to fade before opening ..

                        $('.mp_error_close').unbind('click'); //remove the click event
                    });
                }
            }
        });
    });
}

function loginWithFacebook(blogCommentWrapID) {

    var userName;

    FB.login(function (response) {
        if (response.session) {
            if (response.perms) {
                //User granted all the permissions .. login as per normal and store 'hometown' and 'email' in DB

                FB.api('/me', function(fbUserObject) {
                    if (!fbUserObject || fbUserObject.error) {
                        alert(!fbUserObject || fbUserObject.error);
                    } else {

                        //THERE IS A 'ME' OBJECT, see if they have there hometown/email set ..
                        if ((fbUserObject.hometown) && (fbUserObject.email)) {

                            //request picture then CREATE ACCOUNT with name / email / hometown
                            FB.api('/me?fields=picture&type=square', function (fbPicObject) {
                                CreateFacebookAccountAjax(fbUserObject.name, fbPicObject.picture, fbUserObject.email, fbUserObject.hometown.name);
                            });
                        } else {
                            FB.api('/me', function (fbUserObject) {
                                    
                                //request picture then CREATE ACCOUNT with name / email / hometown
                                FB.api('/me?fields=picture&type=square', function (fbPicObject) {
                                    CreateFacebookAccountAjax(fbUserObject.name, fbPicObject.picture, 'empty', 'empty');
                                });
                            });
                        }

                        if (blogCommentWrapID != '') {
                            $('.mp_error_close').click(function () {
                                setTimeout('delayOpenCommentField()', 1000); //wait for success popup to fade before opening ..
                                $('.mp_error_close').unbind('click'); //remove the click event
                            });
                        }
                        }
                    });
          
            } else { // user refused email permissions .. try for less .. & don't try store email etc .. 
                loginWithFacebook_BASIC_PERMISSIONS(blogCommentWrapID);
            }
        } else { // something wentwrong .. try for less .. & don't try store email etc .. 
            loginWithFacebook_BASIC_PERMISSIONS(blogCommentWrapID);
        }
    }, { perms: 'email,user_hometown' });              //user_hometown

    /////////////////////////////////////////// END NEW LOGIN WITH FACEBOOK
}

function loginWithOutFacebook() {
    $('.loginQuestionWrap').fadeIn('slow');
    $('.initialLoginOptions').fadeOut('fast');
    $('.loginPopUpWrapper').css('background-image', 'url(http://www.johnnyhollow.com/imagesProduction/generalSprite.jpg)'); //loginBGfirstTimeQuestion.jpg
    $('.loginPopUpWrapper').css('background-position', '0 -436px');
}

/* /////////// CREATE ACCOUNT HAS BEEN HERE QUESTION /////////////////// */

function hasUserBeenHere(x) {
    if (x == false) {
        $('.accountCreate').show();
        $('.accountLogin').hide();
        $('.loginQuestionWrap').hide('fast');

        $('.mp_error_wrap').css('height', '550px');
        $('.loginPopUpWrapper').css('background-image', "url('http://www.johnnyhollow.com/imagesProduction/loginCreateAcountBG.jpg')");
        $('.loginPopUpWrapper').css('background-position', '0px 0px');

        $('.loginPopUpWrapper').css('height', '116px');
        $('.mp_error_wrap').CenterIt();
    } else {
        $('.accountCreate').hide();
        $('.accountLogin').show();
        $('.loginQuestionWrap').hide('fast');

        $('.loginPopUpWrapper').css('height', '0px');
        $('.mp_error_wrap').CenterIt();
    }
}
