﻿$(document).ready(function () {
    // all elements with the tooltip class will register the hover event to display their tooltip
    $('.tooltip').hover(
        function () {
            this.tip = this.title;
            $(this).append('<div class="votosTooltip"><img src="../../Content/Images/tooltip_arrow.png" alt="tooltip arrow" /><span>' + this.tip + '</span></div>');
            this.title = "";
            var position = $(this).position();
            var tooltip = $(this).children('.votosTooltip')[0];
            $(tooltip).css({ left: $(this).width() + position.left + 25, top: position.top + 10, 'z-index': 999 });
            $(tooltip).fadeIn(100);
        },
        function () {
            var tooltip = $(this).children('.votosTooltip')[0];
            $(tooltip).fadeOut(100, function () {
                $(tooltip).remove(); //$(this).children('.votosTooltip').remove();
            });
            this.title = this.tip;
        }
    );
});

//jQuery(function ($) {
//    //Browsers Content area
//    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
//    $(window).scroll(function (e) {
//        //scroll position
//        pageoffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
//        newPagePosition = pageoffset;
//        nextPage = '/account/createdpolls';

//        //let’s call the next page after the page is scrolled 1/4 of total content area and be sure it is moved downwards
//        if ((windowHeight / 4) <= newPagePosition) {
//            //wait for 500 milliseconds to confirm the user scroll action
//            setTimeout(function () {
//                //checking if scrolled to the same position
//                if (newPagePosition != oldPagePosition) {
//                    $.ajax({
//                        type: 'GET',
//                        url: nextPage,
//                        data: params,
//                        success: function (response) {
//                            //Appends the response of nextPage to ul.items
//                            $('ul#mylist').append(response);
//                        }
//                    });
//                    oldPagePosition = newPagePosition;
//                }
//            },
//            500);
//        }
//    });
//});


//Set selected nav link
function setSelectedNavLink(navLink) {
    $('#mavmaintop').children('li').removeClass('on');
    $('#' + navLink).addClass('on');
}

//*
//* Sharing tools that are global to Votos
//*
function fbShare(pollGuid) {
    FB.init({
        appId: '312186388682',
        status: true, // check login status
        cookie: true // enable cookies to allow the server to access the session
    });

    FB.getLoginStatus(function (response) {
        if (response.session) {
            // logged in and connected user, someone you know
            $.ajax({
                url: '/poll/getjsonpoll/' + pollGuid,
                dataType: 'json',
                success: function (data) {
                    doFBShare(data);
                }
            });
        } else {
            FB.login(function (response) {
                if (response.session) {
                    // user successfully logged in
                    $.ajax({
                        url: '/poll/getjsonpoll/' + pollGuid,
                        dataType: 'json',
                        success: function (data) {
                            doFBShare(data);
                        }
                    });
                } else {
                    // user cancelled login
                }
            });
        }
    });
}

function doFBShare(poll) {
    FB.ui({
        method: 'stream.publish',
        message: ' has just created a poll on Votos.com.  Click on the link below to vote on it!',
        attachment: {
            name: poll.question,
            image: 'http://www.votos.com/Content/images/results_thumbnail.png',
            description: 'This is a poll about ' + poll.description,
            href: 'http://www.votos.com/poll/' + poll.instanceUrl
            },
        action_links: [
            { text: 'Vote on this poll', href: 'http://www.votos.com/poll/' + poll.pollId }
        ],
        user_message_prompt: 'Share your new poll with your friends and find out what they think!'
    },
    function (response) {
        if (response && response.post_id) {
            //post was published so let's close the dialog
            $.modal.close();
        } else {
            //there was a problem or user cancelled             
        }
    });
}

function twitterShare(pollGuid) {
    twttr.anywhere(function (T) {
        if (!T.isConnected()) {
            T.bind('authComplete', function (e, user) {
                $.ajax({
                    url: '/poll/getjsonpoll/' + pollGuid,
                    dataType: 'json',
                    success: function (data) {
                        $.ajax({
                            url: '/google/shortenurl',
                            data: { url: 'http://www.votos.com/poll/' + data.instanceUrl },
                            dataType: 'json',
                            success: function (result) {
                                data.url = result.data.id;
                                doTweet(data);
                            }
                        });
                    }
                });
            });
            T.signIn();
        }
        else {
            $.ajax({
                url: '/poll/getjsonpoll/' + pollGuid,
                dataType: 'json',
                success: function (data) {
                    $.ajax({
                        url: '/google/shortenurl',
                        data: { url: 'http://www.votos.com/poll/' + data.instanceUrl },
                        dataType: 'json',
                        success: function (result) {
                            data.url = result.data.id;
                            doTweet(data);
                        }
                    });
                }
            });
        }
    });
}

function doTweet(poll) {
    $('#shareDialog').modal({
        modal: true,
        minWidth: 410,
        minHeight: 280,
        autoResize: true,
        overlayClose: false,
        overlayId: 'lightBoxBackground',
        containerId: 'modalDialogContainer',
        closeHTML: '<div class="modalDialogClose"><a class="simplemodal-close" href="#"><span>Close&nbsp;&nbsp;X</span></a></div>',
        onClose: function () {
            $.modal.close();
        }
    });

    twttr.anywhere(function (T) {
        T("#tweet-box").tweetBox({
            height: 90,
            width: 375,
            label: 'Want to know what your followers think?  Tweet your poll and find out!',
            defaultContent: poll.question + ' ' + poll.url + ' via @ItsVotos',
            onTweet: function () {
                $.modal.close();
            }
        });
    });
}

function emailShare() {
    $('.button').button();

    $('#emailDialog').modal({
        modal: true,
        minWidth: 500,
        minHeight: 400,
        autoResize: true,
        overlayClose: false,
        overlayId: 'lightBoxBackground',
        containerId: 'modalDialogContainer',
        closeHTML: '<div class="modalDialogClose"><a class="simplemodal-close" href="#"><span>Close&nbsp;&nbsp;X</span></a></div>',
        onClose: function () {
            $.modal.close();
        }
    });
}

function doEmailSend() {
    $('#sendEmailButton').val('Sending...');
    $('#sendingSpinner').fadeIn('300', function () {
        $.ajax({
            url: '/Poll/Email',
            type: 'POST',
            data: {
                to: $('#toField').val(),
                from: $('#fromField').html(),
                subject: $('#subjectField').val(),
                message: $('#messageField').val()
            },
            dataType: 'json',
            success: function (result) {
                if (result.success) {
                    $('#sendingSpinner').fadeOut('200', function () {
                        $('#sendingSpinner').attr('src', '../../Content/Images/checkmark.png');
                        $('#sendEmailButton').val('Sent');
                        $('#sendingSpinner').fadeIn('200', function () {
                            setTimeout(function () { $.modal.close(); }, 1000);
                        });
                    });
                }
                else {
                    alert('There was a problem sending your email.');
                }
            },
            error: function (xhr, text, error) {
                alert('There was a problem');
                $.modal.close();
            }
        });
    });
}
