
$(document).ready(function(){
    $('.thumbsup, .thumbsdown').click(function(){
        var aid = $(this).closest('section').attr('id');
        var action = $(this).attr('class');
        var qid = $('.question').attr('id');

        // Break up aid and qid to get the real ids
        var temp = aid.split('-');
        var answer_id = temp[1];
        var temp2 = qid.split('-');
        var question_id = temp2[1];

        // Post this data to our script
        $.post('/answer/vote', { question_id: question_id, answers_id: answer_id, action:action});

        // Update the voting option to show how they voted
        var decision;
        if(action == 'thumbsup'){
            decision = 'Agree';
        } else if (action == 'thumbsdown') {
            decision = 'Disagree';
        }
        $(this).parent('span').html('You ' + decision + ' With This');

        // This looks rather confusing. We are finding the 'agree' or disagree number
        // and incrementing by 1 or decreasing by 1
        var target_update = '#' + decision + '-' + answer_id;
        $( target_update ).text( parseInt( $(target_update).text() ) + 1);

        return false;
    })

    $('#follow').click(function(){
        
        var qid = $('.question').attr('id');
        var temp = qid.split('-');
        var question_id = temp[1];

        $(this).toggleClass('button-green button-gray');
        $(this).children('span').toggleClass('add accept');

        var cur_status = $(this).children('strong').text();
        var cur_val = parseInt($('h3.agree').text());

        if(cur_status == 'Follow'){
            $.post('/questions/follow', {table_id: question_id});
            $(this).children('strong').text('Following');
            $('h3.agree').text( cur_val + 1);


            return false;
        } else {
            $.post('/questions/unfollow', {table_id: question_id});
            $('h3.agree').text( cur_val - 1 );
            $(this).children('strong').text('Follow');
            return false;
        }
    })

    $('#gologin').click(function(){
        window.location('/users/login');
    })

    $('.addWebsite').click(function(){
        // Check to see how many are already listed
        var number = $('input[name="website[]"]').length;
        if(number < 5){
            $('#first-user-website').clone().appendTo('#additional-websites');
            $('#additional-websites input[name="website[]"]:last, #additional-websites input[name="anchor[]"]:last').val('');
            
        } else {
            $('#overlimit').appendTo('body');
            $('#overlimit').overlay({

            effect:'apple',

            // some mask tweaks suitable for modal dialogs
            mask: {
                color: '#412A30',
                loadSpeed: 200,
                opacity: 0.75
            },

            closeOnClick:false,

            load: true
            })
        }
        return false;
    })

    $('#your-questions').load('/questions/user/');

    $('#your-answers').load('/users/answers');

    $('#questions-following').load('/users/following');

    $('#your-comments').load('/users/comments');

    $('.comment').click(function(){
        $(this).closest('p').siblings('form').toggle();
        return false;
    })

    $('.edit-question').click(function(){
        var qid = $(this).attr('rel');
        if(qid == null){
            return false;
        } else {
            $(this).closest('p').prev('div').load('/questions/edit/' + qid);
        }
        return false
    })
    $('.edit-answer').click(function(){
        var qid = $(this).attr('rel');
        if(qid == null){
            return false;
        } else {
            $(this).closest('p').prev('div').load('/answer/edit/' + qid);
        }
        return false
    })

    $('a.flag').click(function(){
        var action = $(this).attr('href');
        target = $(this);
        $.get(action, function(data){
            if(data == 'login required'){
                alert( 'You Need to Login First' );
            } else {
                $(target).replaceWith('This has been flagged');
            }
        });

        return false;
    })

    $('a.delete').click(function(){
        var answer = confirm('Are you sure you want to delete this?');
        if(answer){
            var action = $(this).attr('href');
            target = $(this);
            $.get(action)
            $(this).closest('div').fadeOut('slow');
            return false;
        }
        return false;
    })

    $('a.delete-question').click(function(){
        var answer = confirm('Are you sure you want to delete this?');
        if(answer){
            var action = $(this).attr('href');
            target = $(this);
            $.get(action)
            window.location = '/questions/';
        }
        return false;
    })

})
