﻿window.addEvent('domready', function() {
    $$('.ArticleOneClipListLinkOff').each(function(element) {
        element.addEvent('mouseenter', function() {
            element.set('styles', { 'color': '#ffffff', 'background-color': '#67a1ce' });
        });
        element.addEvent('mouseleave', function() {
            element.set('styles', { 'color': '#535353', 'background-color': '#dfdfdf' });
        });
    });
});

function changeClass(element) {
    $(element).set('tween', { duration: 5000 });
    $(element).tween('background-color', '#517d9a', '#dfdfdf');
}

//The following function is to make sure that image widths are no
//bigger than 300px. This is to avoid layout issues that arise
//when two images are next to each other and their total widths are
//greater than the width available.
function adjustWidths() {
    $$('.ArticleImage').each(function(element) {
        var img = new Image();
        img.src = element.get('src');
        //alert(img.width);
        //if (img.complete) {
        img.onload = function() {
            if (img.width > 300) {
                element.set('width', '300px');
            }
        }
        //}
    });
}

function loadComments(aid, rp, ps, rand) {
    /*alert(document.getElementById('ArticleCommentsContainer').text);*/
    var req = new Request.HTML({
        method: 'get',
        url: 'Comments.aspx',
        data: { 'aid': aid, 'rp': rp, 'ps': ps, 'rand': rand },
        onRequest: function() { document.getElementById('ArticleCommentsContainer').innerHTML = '<img src=\"Images/commentsLoader.gif\" alt=\"loading comments...\" />'; },
        update: $('ArticleCommentsContainer'),
        onComplete: function(response) { /*alert('Response: ' + response); $('ArticleCommentsContainer').setStyle('background', '#fffea1');*/ }
    }).send();
}

function submitComment(aid, divId) {
    //validate
    if ($('ArticleAuthorTxt').get('value') == '') {
        document.getElementById('ArticleAuthorError').innerHTML = 'Please provide a display name for yourself';
        document.getElementById('ArticleAuthorTxt').focus()
        return false;
    }
    if ($('ArticleCommentTxt').get('value') == '') {
        alert('You did not provide a comment');
        return false;
    }
    var body = $('ArticleCommentTxt').get('value');
    var author = $('ArticleAuthorTxt').get('value');
    author = encodeURIComponent(author);
    body = encodeURIComponent(body);

    //submit
    var req = new Request.HTML({
        method: 'get',
        url: 'SubmitComment.aspx',
        data: { 'aid': aid, 'a': author, 'b': body, 'rand': Math.random() },
        /*onRequest: function() { alert('Request made. Please wait...'); },*/
        /*update: $('ArticleCommentsContainer'),*/
        onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
            var response;
            responseElements.each(function(element) {
                if (element.get('id') == 'responseDiv') {
                    $(divId).set('text', element.get('text'));
                    $(divId).setStyle('background', '#fffea1');
                }
            });
        }
    }).send();
    return true;
}

function clearAuthorError() {
    document.getElementById('ArticleAuthorError').innerHTML = '';
}
