$(document).ready(function() {

    /* Account status levels */
    applyStatusLevelListeners(document);

    /* Been Here / Listing Status */
    
    $('.status_have_been_here').qtip({
        content: 'You have been to this business',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    
    $('.status_have_not_been_here').qtip({
        content: 'You have not been to this business',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    
    $('.status_wish_list').qtip({
        content: 'This business is on your wish list',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    
    $('.status_favorite_list').qtip({
        content: 'This business is on your list of favorites',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    
    
    $('.question_status_why').qtip({
        content: '<strong>Why should I share this?</strong><br />Sharing which businesses you have been to: <ul><li>enables your friends to see where you might want to go together, and </li><li>helps us personalize your experience on College Life DC</li></ul>',
        style:
        {
            tip: 'bottomRight',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topLeft',
               tooltip: 'bottomRight'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    /* end Listing Status */
    
    /* Other Listing */
    
    /* End other listing */
    
    $('.listing_closed').qtip({
        content: 'Unfortunately, this this listing is no longer in business.  We have chosen to preserve this listing so that our users can continue to share thoughts and experiences.',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'center',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    
    /* Surveys */
    $('.question_survey').qtip({
        content: 'Complete the following survey to tell the College Life DC community what you thought about this business!',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    
    $('.question_survey_results').qtip({
        content: 'These survey question results represent College Life DC users\' thoughts about this business',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    
    /* End surveys */
    
    /* Recommendations */
    $('.question_other_ideas').qtip({
        content: 'Users who have been to this place have also been to the businesses listed below',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    
    /* End Recommendations */
    
    /* Talk */
    $('.question_talk').qtip({
        content: '<strong>What is Talk?</strong><br />Don\'t want to write a whole review?  <strong>Talk</strong> allows you to share a quick, 140-character thought about a business.',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    $('.question_recent_talk').qtip({
        content: 'This represents the most recent <strong>Talk</strong> posts about the businesses on College Life DC',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });
    /* End Talk */
    
    /* Thumbs */
    $('.thumb_count_review').each(function () {
        $(this).qtip({
            content: '<center>' + $(this).find('.thumb_count').text() + ' people found this useful.</center>',
            style:
            {
                tip: 'bottomMiddle',
                name: 'dark',
                border: {
                    width: 3,
                    radius: 5
                }
            },
            position:
            {
                corner:
                {
                   target: 'topMiddle',
                   tooltip: 'bottomMiddle'
                }
            },
            show: 'mouseover',
            hide: {
                fixed: true,
                when: {
                    event:'mouseout'
                }
            }
        });
    });
    
    $('.thumb_review').find('a').click( function(e){
        e.preventDefault();
    });
    
    $('.thumb_remove').find('a').click( function(e){
        e.preventDefault();
    });
    
    $('.thumb_review').each(function () {
        var review_class = this;
        $(this).qtip({
            content: 'Saving...',
            style:
            {
                tip: 'bottomMiddle',
                name: 'dark',
                border: {
                    width: 3,
                    radius: 5
                }
            },
            position:
            {
                corner:
                {
                   target: 'topMiddle',
                   tooltip: 'bottomMiddle'
                }
            },
            show: {
                when: 'click',
                solo: true
            },
            hide: {
                fixed: true,
                delay: 2000,
                when: {
                    event:'mouseout'
                }
            },
            api: {
                onShow: function()
                {
                    var self = this;
                    var this_content = "";
                    var review_id = $(review_class).find('.hidden_review_id').text();
                    
                    $.ajax({
                        type: "GET",
                        url: "/reviews/ajax_thumb_up/"+review_id,
                        success: function(msg) {
                            success = parseInt(msg.substring(0,2));
                            msg = msg.substring(2,msg.length);
                            
                            if (success == 1)
                            {
                                self.updateContent("Cool, glad it was useful.");
                                
                                //get current count, update
                                var current_count = $(review_class).parents('.rating').find('.thumb_count').text();
                                current_count = parseInt(current_count);
                                $(review_class).parents('.rating').find('.thumb_count').text(current_count + 1);
                                
                                //update text
                                $(review_class).parents('.rating').find('.thumb_review').addClass("hidden");
                                
                                //add delete button
                                $(review_class).parents('.rating').find('.thumb_remove').removeClass("hidden"); //("<a href=\"#\">I did not find this useful</a>");

                            } else
                            {
                                self.updateContent("Sorry, couldn't save.  Try again later.");
                            }
                        }
                    }); //ajax
                }
            }
        });
    });
    
    $('.thumb_remove').each(function () {
        var review_class = this;
        $(this).qtip({
            content: 'Saving...',
            style:
            {
                tip: 'bottomMiddle',
                name: 'dark',
                border: {
                    width: 3,
                    radius: 5
                }
            },
            position:
            {
                corner:
                {
                   target: 'topMiddle',
                   tooltip: 'bottomMiddle'
                }
            },
            show: {
                when: 'click',
                solo: true
            },
            hide: {
                fixed: true,
                delay: 2000,
                when: {
                    event:'mouseout'
                }
            },
            api: {
                onShow: function()
                {
                    var self = this;
                    var this_content = "";
                    var review_id = $(review_class).find('.hidden_review_id').text();
                    
                    $.ajax({
                        type: "GET",
                        url: "/reviews/ajax_thumb_remove/"+review_id,
                        success: function(msg) {
                            success = parseInt(msg.substring(0,2));
                            msg = msg.substring(2,msg.length);
                            
                            if (success == 1)
                            {
                                self.updateContent("Oh well, sorry you didn't like it.");
                                
                                //get current count, update
                                var current_count = $(review_class).parents('.rating').find('.thumb_count').text();
                                current_count = parseInt(current_count);
                                $(review_class).parents('.rating').find('.thumb_count').text(current_count - 1);
                                
                                //update text
                                $(review_class).parents('.rating').find('.thumb_remove').addClass("hidden");
                                
                                //add 'add' button
                                $(review_class).parents('.rating').find('.thumb_review').removeClass("hidden"); //html("<a href=\"#\">I found this useful</a>");
                                
                            } else
                            {
                                self.updateContent("Sorry, couldn't save.  Try again later.");
                            }
                        }
                    }); //ajax
                }
            }
        });
    });



    /* End Thumbs */
    
    /* Login prompt */
    loginPromptListeners();
    
});

function addLoginPromptListener(item)
{
        $(item).click(function(e){ e.preventDefault(); });
        /*
        $(item).qtip({
        content: '<div class="qtip_login"><p class="login_title">Log in</p><p><strong>Only registered users can do that.</strong></p><p>Please <strong>log in</strong> below or <a href="/users/register"><strong>sign up</strong></a>!</p>           <form method="post" action="/users/login"><fieldset style="display:none;"><input type="hidden" name="_method" value="POST" /></fieldset><p><label for="UserEmail">Email</label><br /><input name="data[User][email]" type="text" maxlength="255" value="" id="UserEmail" /></p><p><label for="UserPassword">Password</label><br /><input type="password" name="data[User][password]" value="" id="UserPassword" /></p><p>Remember me <input type="hidden" name="data[User][remember_me]" id="UserRememberMe_" value="0" /><input type="checkbox" name="data[User][remember_me]" value="1" id="UserRememberMe" /><label for="UserRememberMe"></label></p><input type="submit" value="Login" /></form> <p align="right"><a href="/users/register"><img src="/img/sign_up.gif" alt="Sign Up" /></a></p></div>',
        style:
        {
            tip: 'bottomMiddle',
            name: 'green',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topMiddle',
               tooltip: 'bottomMiddle'
            }
        },
        show:
        {
            when:'click',
            delay: 0,
            solo: true
        },
        
        hide: {
            fixed: true,
            delay: 2000,
            when: {
                event:'mouseout'
            }
        }
    });
/*
        /* MODAL VERSION */
        $(item).qtip({
            content: {
                title: {
                    text: 'Please Log In!',
                    button: 'Close'
                },
                text: '<div class="qtip_login"><p class="login_title">Log in</p><p><strong>Only registered users can do that.</strong></p><p>Please <strong>log in</strong> below or <a href="/users/register"><strong>sign up</strong></a>!</p>           <form method="post" action="/users/login"><fieldset style="display:none;"><input type="hidden" name="_method" value="POST" /></fieldset><p><label for="UserEmail">Email</label><br /><input name="data[User][email]" type="text" maxlength="255" value="" id="UserEmail" /></p><p><label for="UserPassword">Password</label><br /><input type="password" name="data[User][password]" value="" id="UserPassword" /></p><p>Remember me <input type="hidden" name="data[User][remember_me]" id="UserRememberMe_" value="0" /><input type="checkbox" name="data[User][remember_me]" value="1" id="UserRememberMe" /><label for="UserRememberMe"></label></p><input type="submit" value="Login" /></form> <p align="right"><a href="/users/register"><img src="/img/sign_up.gif" alt="Sign Up" /></a></p></div>'
            },
            position: {
                target: $(document.body),
                corner: 'center'
            },
            show: {
                when: 'click',
                solo: true
            },
            hide: false,
            style: {
                width: {max: 350},
                padding: '14px',
                border: {
                    width: 9,
                    radius: 9,
                    color: '#8AC33E'
                },
                name: 'green'
            },
            api: {
                beforeShow: function()
                {
                    $("#qtip-blanket").fadeIn(this.options.show.effect.length);
                },
                beforeHide: function()
                {
                    $("#qtip-blanket").fadeOut(this.options.hide.effect.length);
                }
            }
        });
        
        $('<div id="qtip-blanket">')
            .css( {
                position: 'absolute',
                top: $(document).scrollTop(),
                left: 0,
                height: $(document).height(),
                width: '100%',
                
                opacity: 0.7,
                backgroundColor: 'black',
                zindex: 5000
            })
            .appendTo(document.body)
            .hide();
        
        
        /* End login prompt */
}

function loginPromptListeners() {
    $(".login_prompt").each(function() {
        addLoginPromptListener(this);
    });
} //loginPromptListeners

//applies status tooltips to all elements below 'item' in doc tree
function applyStatusLevelListeners(item)
{
    $(item).find('.status_level1').qtip({
        content: 'This user is a CLDC <strong>Freshman</strong>',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    $(item).find('.status_level2').qtip({
        content: 'This user is a CLDC <strong>Sophomore</strong>',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    $(item).find('.status_level3').qtip({
        content: 'This user is a CLDC <strong>Junior</strong>',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    $(item).find('.status_level4').qtip({
        content: 'This user is a CLDC <strong>Senior</strong>',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

    $(item).find('.status_level5').qtip({
        content: 'This user is a CLDC <strong>Alumn</strong>',
        style:
        {
            tip: 'bottomLeft',
            name: 'dark',
            border: {
                width: 3,
                radius: 5
            }
        },
        position:
        {
            corner:
            {
               target: 'topRight',
               tooltip: 'bottomLeft'
            }
        },
        show: 'mouseover',
        hide: 'mouseout'
    });

} //applyStatusLevelListeners

