$(document).ready(function() {
    fwix.comments.bindKeyPressEvents();
});


/* ARTICLE  
/ *******************************************************************************************/
fwix.article = {
    /*** fwix.article.focusOnComments ***/
    focusOnComments: function(){
        $(document).ready(function(){
            $('#comments textarea').focus();
            $.scrollTo('#comments textarea');
        });
    },

    /*** fwix.article.commentSubmit ***/
    commentSubmit: function(){
        var comments = $('#comments'),
            comments_form = comments.find('#comment_form'),
    	    body = comments_form.find('textarea'),
    	    name = comments_form.find('[name="name"]'),
    	    email = comments_form.find('[name="email"]'),
    	    submit = comments_form.find('.submit'),
            storyid = $('#story').attr('storyid');

        if(name.length > 0  && email.length > 0) 
            fwix.account.saveAnnon(name.val(),email.val());

        fwix.loadingShow();
        $.ajax({
            type: "POST",
            url: fwix.site_url+'/ajax/comment_submit.php', 
            data: "body="+body.val()+
                    "&name="+name.val()+
                    "&email="+email.val()+
                    "&geoid="+fwix.geo_id+
                    "&storyid="+storyid,
            dataType: "json",
            success: function(data){
                fwix.loadingHide();
                if(data.success == '1') {
                    comments.find('ul li').removeClass('last');
                    fwix.article.commentBuild(comments.find('ul')[0], data.comment)
                    fwix.trackers.comment();
                    if(fwix.test)
                        fwix.trackers.articlePageComment(fwix.test);
                }else{
                    fwix.formResponse(submit, 'error', data.error);
                }
            }
        });
    },

    /*** fwix.article.commentBuild ***/
    commentBuild: function(ref, data){
       comment = document.createElement('li');
       ref.appendChild(comment);
       comment.className = 'comment last';
       comment.setAttribute('comment_id', data.cid);

        if(data.is_owner){
            delete_button = document.createElement('a');
            comment.appendChild(delete_button);
            delete_button.className = 'delete';
            delete_button.href = 'javascript:fwix.comments.deleteComment(\''+data.cid+'\');';
            delete_button.innerHTML = 'Delete Comment';
        }

        if(data.user.short_name != ''){
            avatar = document.createElement('a');
            avatar.href = fwix.site_url+"/user/"+data.user.short_name;
            if(!data.user.public)
                avatar.setAttribute('rel', 'nofollow');

            _name = document.createElement('a');
            _name.href = fwix.site_url+"/user/"+data.user.short_name;
            if(!data.user.public)
                _name.setAttribute('rel', 'nofollow');
            _name.innerHTML = data.user.name;
        }else{
            avatar = document.createElement('span');
            thumbnail = fwix.images_url+'/default_4.gif';
            _name = document.createTextNode(data.user.name);
        }
        comment.appendChild(avatar);
        avatar.className = 'avatar';
        avatar_img = document.createElement('img');
        avatar.appendChild(avatar_img);
        avatar_img.src = data.user.thumbnail;
        username = document.createElement('b');
        comment.appendChild(username);
        username.className = 'username';
        username.appendChild(_name);

        comment.appendChild(document.createTextNode(' '));

        time_ago = document.createElement('span');
        comment.appendChild(time_ago);
        time_ago.className = 'time_ago';
        time_ago.innerHTML = fwix.timeAgo(data.print_time);

        br = document.createElement('br');
        comment.appendChild(br);

        _body = document.createElement('span');
        comment.appendChild(_body);
        _body.className = 'body';
        _body.innerHTML = data.body;

        clear = document.createElement('div');
        clear.className = 'clear';
        comment.appendChild(clear);
    },

    /*** fwix.article.report ***/
    report: function(storyid, report_type){
        $('.report_options').hide();

        $.ajax({
            type: 'GET',
            url: fwix.site_url+'/ajax/report_story.php',
            data: 'storyid='+storyid+
                  '&geoid='+fwix.geo_id+
                  '&uid='+fwix.user_id+
                  '&report='+report_type,
            dataType: 'json',
            success: function(data){
                if(data.error)
                    fwix.formResponse($('#story small'), 'error', '<br/>'+data.error);
                else
                    fwix.formResponse($('#story small'), 'success', '<br/>Thanks for helping us improve our reporting');
            }
        });
    }
};


/* ARTICLE  
/ *******************************************************************************************/
fwix.articlePromo = {
    /*** fwix.articlePromo.displayNewQuestion ***/
    answer: function(storyid, questionid, answerid){
        promo = $('#welcome .wrap');

        // update traker data
        fwix.survey.updateTracker(questionid, storyid);

        $.ajax({
            type: "GET",
            url: fwix.site_url+'/ajax/question_answer.php',
            data: 'storyid='+storyid+
                  '&geoid='+fwix.geo_id+
                  '&questionid='+questionid+
                  '&answerid='+answerid,
            dataType: 'json',
            success: function(data){
                promo.find('.slider *').fadeOut(200);
                setTimeout(function(){
                    promo.find('.slider').remove();
                    if(data.survey_completed)
                        fwix.articlePromo.completeSurvey(promo[0]);
                    else
                        fwix.articlePromo.displayNewQuestion(promo[0], storyid, data.survey);
                }, 100);
            }
        });
    },

    /*** fwix.articlePromo.displayNewQuestion ***/
    displayNewQuestion: function(ref, storyid, data){
        question = data.question;

        slider = document.createElement('div');
        slider.style.display = 'none';
        ref.insertBefore(slider, ref.lastChild);
        slider.className = 'slider';

        h4 = document.createElement('h4');
        slider.appendChild(h4);
        h4.innerHTML = question.body;

        msg = document.createElement('p');
        slider.appendChild(msg);
        msg.innerHTML = 'Compare yourself to friends and neighbors.';

        for(var i in data.answers){
            answer = data.answers[i];
            button = document.createElement('a');
            slider.appendChild(button);
            button.setAttribute('answer', answer.answerid);
            button.href = 'javascript:fwix.articlePromo.answer(\''+storyid+'\','+question.questionid+','+answer.answerid+');';
            button.className = 'button alt';
            if(data.user_answer == answer.answerid)
                button.className += ' selected';
            button_text = document.createElement('span');
            button.appendChild(button_text);
            button_text.innerHTML = answer.short;
        }
        $(slider).fadeIn(500);
    },

    /*** fwix.articlePromo.completeSurvey ***/
    completeSurvey: function(ref){
        slider = document.createElement('div');
        slider.style.display = 'none';
        ref.insertBefore(slider, ref.lastChild);
        slider.className = 'slider';

        h4 = document.createElement('h4');
        slider.appendChild(h4);
        h4.innerHTML = 'Join Fwix to find out how your community responded!';

        msg = document.createElement('p');
        slider.appendChild(msg);
        msg.innerHTML = 'Should there be some more copy here?';

        button = document.createElement('a');
        slider.appendChild(button);
        slider.href = fwix.site_url+'/register.php';
        button.className = 'button alt';
        button_text = document.createElement('span');
        button.appendChild(button_text);
        button_text.innerHTML = 'Register Now!';

        $(slider).fadeIn(500);
    }
}
